test/createsend_test.rb in createsend-2.5.1 vs test/createsend_test.rb in createsend-3.0.0

- old
+ new

@@ -1,133 +1,274 @@ require File.dirname(__FILE__) + '/helper' class CreateSendTest < Test::Unit::TestCase - context "when an api caller is authenticated" do + + context "when an api caller requires createsend" do setup do - @api_key = '123123123123123123123' - @base_uri = 'https://api.createsend.com/api/v3' - CreateSend.api_key @api_key - @cs = CreateSend::CreateSend.new + @access_token = "h9898wu98u9dqjoijnwld" + @refresh_token = "tGzv3JOkF0XG5Qx2TlKWIA" + @api_key = "hiuhqiw78hiqhwdwdqwdqw2s2e2" end - - should "include the CreateSend module VERSION constant as part of the user agent when making a call" do - # This test is done to ensure that the version from HTTParty isn't included instead - assert CreateSend::CreateSend.headers["User-Agent"] == "createsend-ruby-#{CreateSend::VERSION}" - stub_get(@api_key, "clients.json", "clients.json") - clients = @cs.clients - clients.size.should == 2 + + should "authenticate using an oauth access token and refresh token" do + auth = { + :access_token => @access_token, + :refresh_token => @refresh_token + } + cs = CreateSend::CreateSend.new auth + cs.auth_details.should == auth end - - should "get api key" do - uri = URI.parse(@base_uri) + + should "authenticate using an api key" do + auth = {:api_key => @api_key} + cs = CreateSend::CreateSend.new auth + cs.auth_details.should == auth + end + + should "get the authorization url without state included" do + client_id = 8998879 + redirect_uri = 'http://example.com/auth' + scope = 'ViewReports,CreateCampaigns,SendCampaigns' + url = CreateSend::CreateSend.authorize_url(client_id, redirect_uri, scope) + url.should == "https://api.createsend.com/oauth?client_id=8998879&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&scope=ViewReports%2CCreateCampaigns%2CSendCampaigns" + end + + should "get the authorization url with state included" do + client_id = 8998879 + redirect_uri = 'http://example.com/auth' + scope = 'ViewReports,CreateCampaigns,SendCampaigns' + state = 89879287 + url = CreateSend::CreateSend.authorize_url(client_id, redirect_uri, scope, state) + url.should == "https://api.createsend.com/oauth?client_id=8998879&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&scope=ViewReports%2CCreateCampaigns%2CSendCampaigns&state=89879287" + end + + should "exchange an OAuth token for an access token, 'expires in' value, and refresh token" do + client_id = 8998879 + client_secret = 'iou0q9wud0q9wd0q9wid0q9iwd0q9wid0q9wdqwd' + redirect_uri = 'http://example.com/auth' + code = 'jdiwouo8uowi9o9o' + options = { + :body => fixture_file("oauth_exchange_token.json"), + :content_type => "application/json; charset=utf-8" } + FakeWeb.register_uri(:post, "https://api.createsend.com/oauth/token", options) + access_token, expires_in, refresh_token = CreateSend::CreateSend.exchange_token( + client_id, client_secret, redirect_uri, code) + + FakeWeb.last_request.body.should == "grant_type=authorization_code&client_id=8998879&client_secret=iou0q9wud0q9wd0q9wid0q9iwd0q9wid0q9wdqwd&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&code=jdiwouo8uowi9o9o" + access_token.should == "SlAV32hkKG" + expires_in.should == 1209600 + refresh_token.should == "tGzv3JOkF0XG5Qx2TlKWIA" + end + + should "raise an error when an attempt to exchange an OAuth token for an access token fails" do + client_id = 8998879 + client_secret = 'iou0q9wud0q9wd0q9wid0q9iwd0q9wid0q9wdqwd' + redirect_uri = 'http://example.com/auth' + code = 'invalidcode' + options = { + :body => fixture_file("oauth_exchange_token_error.json"), + :content_type => "application/json; charset=utf-8" } + FakeWeb.register_uri(:post, "https://api.createsend.com/oauth/token", options) + lambda { access_token, expires_in, refresh_token = CreateSend::CreateSend.exchange_token( + client_id, client_secret, redirect_uri, code) }.should raise_error( + Exception, 'Error exchanging code for access token: invalid_grant - Specified code was invalid or expired') + FakeWeb.last_request.body.should == "grant_type=authorization_code&client_id=8998879&client_secret=iou0q9wud0q9wd0q9wid0q9iwd0q9wid0q9wdqwd&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&code=invalidcode" + end + + should "get a person's api key" do + base_uri = "https://api.createsend.com/api/v3" + uri = URI.parse(base_uri) site_url = "http://iamadesigner.createsend.com/" username = "myusername" password = "mypassword" + cs = CreateSend::CreateSend.new stub_get(nil, "https://#{username}:#{password}@#{uri.host}#{uri.path}/apikey.json?SiteUrl=#{CGI.escape(site_url)}", "apikey.json") - apikey = @cs.apikey(site_url, username, password).ApiKey + apikey = cs.apikey(site_url, username, password).ApiKey apikey.should == "981298u298ue98u219e8u2e98u2" + cs.auth_details.should == {:api_key => apikey} end + end + + context "when an api caller is authenticated using oauth" do + setup do + @access_token = "h9898wu98u9dqjoijnwld" + @refresh_token = "tGzv3JOkF0XG5Qx2TlKWIA" + @auth = { + :access_token => @access_token, + :refresh_token => @refresh_token + } + end + + should "refresh the current access token" do + options = { + :body => fixture_file("refresh_oauth_token.json"), + :content_type => "application/json; charset=utf-8" } + FakeWeb.register_uri(:post, "https://api.createsend.com/oauth/token", options) + cs = CreateSend::CreateSend.new @auth + new_access_token, new_expires_in, new_refresh_token = cs.refresh_token + + FakeWeb.last_request.body.should == "grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA" + new_access_token.should == "SlAV32hkKG2e12e" + new_expires_in.should == 1209600 + new_refresh_token.should == "tGzv3JOkF0XG5Qx2TlKWIA" + cs.auth_details.should == { + :access_token => new_access_token, + :refresh_token => new_refresh_token + } + end + + should "raise an error when an attempt to refresh the access token is made but refresh token is nil" do + cs = CreateSend::CreateSend.new :access_token => 'any token', :refresh_token => nil + lambda { new_access_token, new_refresh_token = cs.refresh_token }.should raise_error( + Exception, '@auth_details[:refresh_token] does not contain a refresh token.') + end + + should "raise an error when an attempt to refresh the access token is made but no there was no refresh token passed in" do + cs = CreateSend::CreateSend.new :access_token => 'any token' + lambda { new_access_token, new_refresh_token = cs.refresh_token }.should raise_error( + Exception, '@auth_details[:refresh_token] does not contain a refresh token.') + end + + should "raise an error when an attempt to refresh the access token is made but no there was no auth hash passed in" do + cs = CreateSend::CreateSend.new + lambda { new_access_token, new_refresh_token = cs.refresh_token }.should raise_error( + Exception, '@auth_details[:refresh_token] does not contain a refresh token.') + end + end + + multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do + setup do + @cs = CreateSend::CreateSend.new @auth + end + + should "include the CreateSend module VERSION constant as part of the user agent when making a call" do + # This test is done to ensure that the version from HTTParty isn't included instead + assert CreateSend::CreateSend.headers["User-Agent"] == "createsend-ruby-#{CreateSend::VERSION}" + stub_get(@auth, "clients.json", "clients.json") + clients = @cs.clients + clients.size.should == 2 + end + should "get all clients" do - stub_get(@api_key, "clients.json", "clients.json") + stub_get(@auth, "clients.json", "clients.json") clients = @cs.clients clients.size.should == 2 clients.first.ClientID.should == '4a397ccaaa55eb4e6aa1221e1e2d7122' clients.first.Name.should == 'Client One' end should "get billing details" do - stub_get(@api_key, "billingdetails.json", "billingdetails.json") + stub_get(@auth, "billingdetails.json", "billingdetails.json") bd = @cs.billing_details bd.Credits.should == 3021 end should "get all countries" do - stub_get(@api_key, "countries.json", "countries.json") + stub_get(@auth, "countries.json", "countries.json") countries = @cs.countries countries.size.should == 245 assert countries.include? "Australia" end should "get system date" do - stub_get(@api_key, "systemdate.json", "systemdate.json") + stub_get(@auth, "systemdate.json", "systemdate.json") systemdate = @cs.systemdate.SystemDate systemdate.should == "2010-10-15 09:27:00" end should "get all timezones" do - stub_get(@api_key, "timezones.json", "timezones.json") + stub_get(@auth, "timezones.json", "timezones.json") timezones = @cs.timezones timezones.size.should == 97 assert timezones.include? "(GMT+12:00) Fiji" end should "get all administrators" do - stub_get(@api_key, "admins.json", "administrators.json") + stub_get(@auth, "admins.json", "administrators.json") administrators = @cs.administrators administrators.size.should == 2 administrators.first.EmailAddress.should == "admin1@blackhole.com" administrators.first.Name.should == 'Admin One' administrators.first.Status.should == 'Active' end - + should "set primary contact" do email = 'admin@blackhole.com' - stub_put(@api_key, "primarycontact.json?email=#{CGI.escape(email)}", 'admin_set_primary_contact.json') + stub_put(@auth, "primarycontact.json?email=#{CGI.escape(email)}", 'admin_set_primary_contact.json') result = @cs.set_primary_contact email result.EmailAddress.should == email end - + should "get primary contact" do - stub_get(@api_key, "primarycontact.json", 'admin_get_primary_contact.json') + stub_get(@auth, "primarycontact.json", 'admin_get_primary_contact.json') result = @cs.get_primary_contact result.EmailAddress.should == 'admin@blackhole.com' end - + end context "when the CreateSend API responds with an error" do - setup do - @api_key = '123123123123123123123' - @base_uri = 'https://api.createsend.com/api/v3' - CreateSend.api_key @api_key - @cs = CreateSend::CreateSend.new - @template = CreateSend::Template.new('98y2e98y289dh89h938389') - end - - { ["400", "Bad Request"] => CreateSend::BadRequest, - ["401", "Unauthorized"] => CreateSend::Unauthorized, - ["404", "Not Found"] => CreateSend::NotFound, - ["500", "Server Error"] => CreateSend::ServerError - }.each do |status, exception| - context "#{status.first}, a get" do - should "raise a #{exception.name} error" do - stub_get(@api_key, "countries.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) - lambda { c = @cs.countries }.should raise_error(exception) - end + + multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do + setup do + @cs = CreateSend::CreateSend.new @auth + @template = CreateSend::Template.new @auth, '98y2e98y289dh89h938389' end - context "#{status.first}, a post" do - should "raise a #{exception.name} error" do - stub_post(@api_key, "clients.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) - lambda { CreateSend::Client.create "Client Company Name", - "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia" }.should raise_error(exception) + { ["400", "Bad Request"] => CreateSend::BadRequest, + ["401", "Unauthorized"] => CreateSend::Unauthorized, + ["404", "Not Found"] => CreateSend::NotFound, + ["500", "Server Error"] => CreateSend::ServerError + }.each do |status, exception| + context "#{status.first}, a get" do + should "raise a #{exception.name} error" do + stub_get(@auth, "countries.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) + lambda { c = @cs.countries }.should raise_error(exception) + end end - end - context "#{status.first}, a put" do - should "raise a #{exception.name} error" do - stub_put(@api_key, "templates/#{@template.template_id}.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) - lambda { @template.update "Template One Updated", "http://templates.org/index.html", - "http://templates.org/files.zip" }.should raise_error(exception) + context "#{status.first}, a post" do + should "raise a #{exception.name} error" do + stub_post(@auth, "clients.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) + lambda { CreateSend::Client.create @auth, "Client Company Name", + "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia" }.should raise_error(exception) + end end - end - context "#{status.first}, a delete" do - should "raise a #{exception.name} error" do - stub_delete(@api_key, "templates/#{@template.template_id}.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) - lambda { @template.delete }.should raise_error(exception) + context "#{status.first}, a put" do + should "raise a #{exception.name} error" do + stub_put(@auth, "templates/#{@template.template_id}.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) + lambda { @template.update "Template One Updated", "http://templates.org/index.html", + "http://templates.org/files.zip" }.should raise_error(exception) + end end + + context "#{status.first}, a delete" do + should "raise a #{exception.name} error" do + stub_delete(@auth, "templates/#{@template.template_id}.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) + lambda { @template.delete }.should raise_error(exception) + end + end end end + + context "when authenticated using oauth and the access token has expired" do + setup do + @access_token = '98y98u98u98ue212' + @refresh_token = 'kj9wud09wi0qi0w' + @auth = { + :access_token => @access_token, + :refresh_token => @refresh_token + } + @cs = CreateSend::CreateSend.new @auth + end + + should "raise a CreateSend::ExpiredOAuthToken error" do + stub_get(@auth, "countries.json", "expired_oauth_token_api_error.json", ["401", "Unauthorized"]) + lambda { c = @cs.countries }.should raise_error(CreateSend::ExpiredOAuthToken) + end + end + end + end