require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')
class TestClient < Test::Unit::TestCase
context "client module" do
should "create a new eroi client" do
credentials = fixture(:test)
client = EROI.new(credentials[:user_token], credentials[:api_password])
assert_equal EROI::Client, client.class
end
end
context "using the eroi client" do
setup do
credentials = fixture(:test)
@client = EROI.new(credentials[:user_token], credentials[:api_password])
FakeWeb.register_uri(
:post, EROI::Request::Post::API_URL,
:body => successful_post_response)
FakeWeb.register_uri(
:get, /#{EROI::Request::Get::API_URL}*/,
:body => successful_get_response)
end
context "when finding a contact" do
should "respond with a success" do
response = @client.contact('longbob@longbob.com', :mailing_lists => 'TestList')
assert_equal true, response.success?
assert_equal 'longbob@longbob.com', response.contact['Email']
end
end
context "when adding a contact" do
should "respond with a success" do
response = @client.add_contact(
:email => 'longbob@longbob.com',
:firstname => 'Longbob',
:lastname => 'Longson',
:mailing_lists => 'TestList')
assert_equal true, response.success?
assert_equal 1, response.number_of_records
end
end
context "when changing a contact's email" do
should "respond with a success" do
response = @client.change_contact_email(
'longbob@longbob.com', 'longbob@boblong.com')
assert_equal true, response.success?
assert_equal 1, response.number_of_records
end
end
context "when updating a contact" do
should "respond with a success" do
response = @client.update_contact(
:email => 'longbob@longbob.com',
:firstname => 'Longbob',
:lastname => 'Longson',
:mailing_lists => 'TestList')
assert_equal true, response.success?
assert_equal 1, response.number_of_records
end
end
context "when removing a contact" do
should "respond with a success" do
response = @client.remove_contact('longbob@longbob.com')
assert_equal true, response.success?
assert_equal 1, response.number_of_records
end
end
context "when there is an error" do
setup do
FakeWeb.register_uri(
:get, /#{EROI::Request::Get::API_URL}*/,
:body => unsuccessful_get_response(1))
end
should "respond with a failure" do
response, fields = @client.contact('longbob@longbob.com')
assert_equal false, response.success?
end
end
end
def successful_post_response
<<-EOF
Yes
OK
MailingListName_someEditionName
1
1526
1
1
0
Complete
EOF
end
def successful_get_response
<<-EOF
523
longbob@longbob.com
Joe
Somebody
Some Company
some data here
We'll put more data here
And we'll put more notes here
20030913143010
5
3
1
2
Sent
EOF
end
def unsuccessful_get_response(code = 1)
"Unable to authorize supplied username and password"
end
end