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

- old
+ new

@@ -71,11 +71,37 @@ 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 "refresh an access token given a refresh token" do + refresh_token = 'tGzv3JOkF0XG5Qx2TlKWIA' + 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) + new_access_token, new_expires_in, new_refresh_token = CreateSend::CreateSend.refresh_access_token refresh_token + FakeWeb.last_request.body.should == "grant_type=refresh_token&refresh_token=#{refresh_token}" + new_access_token.should == "SlAV32hkKG2e12e" + new_expires_in.should == 1209600 + new_refresh_token.should == "tGzv3JOkF0XG5Qx2TlKWIA" + end + + should "raise an error when an attempt to refresh an access token fails" do + refresh_token = 'invalidrefreshtoken' + options = { + :body => fixture_file("oauth_refresh_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.refresh_access_token( + refresh_token) }.should raise_error( + Exception, 'Error refreshing access token: invalid_grant - Specified refresh_token was invalid or expired') + FakeWeb.last_request.body.should == "grant_type=refresh_token&refresh_token=#{refresh_token}" + 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" @@ -132,10 +158,40 @@ 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 + + should "raise an error when an attempt to refresh the access token is made but the refresh token is invalid" do + refresh_token = 'invalidrefreshtoken' + cs = CreateSend::CreateSend.new :access_token => 'any token', :refresh_token => refresh_token + options = { + :body => fixture_file("oauth_refresh_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 = cs.refresh_token }.should raise_error( + Exception, 'Error refreshing access token: invalid_grant - Specified refresh_token was invalid or expired') + end + + should "raise a CreateSend::InvalidOAuthToken error when an access token is invalid" do + cs = CreateSend::CreateSend.new @auth + stub_get(@auth, "countries.json", "invalid_oauth_token_api_error.json", ["401", "Unauthorized"]) + lambda { c = cs.countries }.should raise_error(CreateSend::InvalidOAuthToken) + end + + should "raise a CreateSend::ExpiredOAuthToken error when an access token is expired" do + cs = CreateSend::CreateSend.new @auth + stub_get(@auth, "countries.json", "expired_oauth_token_api_error.json", ["401", "Unauthorized"]) + lambda { c = cs.countries }.should raise_error(CreateSend::ExpiredOAuthToken) + end + + should "raise a CreateSend::RevokedOAuthToken error when an access token is revoked" do + cs = CreateSend::CreateSend.new @auth + stub_get(@auth, "countries.json", "revoked_oauth_token_api_error.json", ["401", "Unauthorized"]) + lambda { c = cs.countries }.should raise_error(CreateSend::RevokedOAuthToken) + end + end multiple_contexts "authenticated_using_oauth_context", "authenticated_using_api_key_context" do setup do @cs = CreateSend::CreateSend.new @auth @@ -216,10 +272,11 @@ end { ["400", "Bad Request"] => CreateSend::BadRequest, ["401", "Unauthorized"] => CreateSend::Unauthorized, ["404", "Not Found"] => CreateSend::NotFound, + ["418", "I'm a teapot"] => CreateSend::ClientError, ["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) @@ -227,11 +284,11 @@ end end 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) + 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 @@ -247,26 +304,9 @@ 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