RSpec 3 syntax
[arvados.git] / spec / google / api_client_spec.rb
index 40e8d7ed25f67c1357a3b6bbdee3fa29fc560211..6cd4c8ee4d90e86dd7eb42ebdff5831a98ae0c22 100644 (file)
@@ -17,26 +17,25 @@ require 'spec_helper'
 require 'faraday'
 require 'signet/oauth_1/client'
 require 'google/api_client'
-require 'google/api_client/version'
 
 shared_examples_for 'configurable user agent' do
   include ConnectionHelpers
   
   it 'should allow the user agent to be modified' do
     client.user_agent = 'Custom User Agent/1.2.3'
-    client.user_agent.should == 'Custom User Agent/1.2.3'
+    expect(client.user_agent).to eq('Custom User Agent/1.2.3')
   end
 
   it 'should allow the user agent to be set to nil' do
     client.user_agent = nil
-    client.user_agent.should == nil
+    expect(client.user_agent).to eq(nil)
   end
 
   it 'should not allow the user agent to be used with bogus values' do
-    (lambda do
+    expect(lambda do
       client.user_agent = 42
       client.execute(:uri=>'https://www.google.com/')
-    end).should raise_error(TypeError)
+    end).to raise_error(TypeError)
   end
 
   it 'should transmit a User-Agent header when sending requests' do
@@ -45,8 +44,8 @@ shared_examples_for 'configurable user agent' do
     conn = stub_connection do |stub|
       stub.get('/') do |env|
         headers = env[:request_headers]
-        headers.should have_key('User-Agent')
-        headers['User-Agent'].should == client.user_agent
+        expect(headers).to have_key('User-Agent')
+        expect(headers['User-Agent']).to eq(client.user_agent)
         [200, {}, ['']]
       end
     end
@@ -55,17 +54,17 @@ shared_examples_for 'configurable user agent' do
   end
 end
 
-describe Google::APIClient do
+RSpec.describe Google::APIClient do
   include ConnectionHelpers
 
   let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') }
 
   it 'should make its version number available' do
-    Google::APIClient::VERSION::STRING.should be_instance_of(String)
+    expect(Google::APIClient::VERSION::STRING).to be_instance_of(String)
   end
 
   it 'should default to OAuth 2' do
-    Signet::OAuth2::Client.should === client.authorization
+    expect(Signet::OAuth2::Client).to be === client.authorization
   end
 
   describe 'configure for no authentication' do
@@ -83,15 +82,17 @@ describe Google::APIClient do
     end
 
     it 'should use the default OAuth1 client configuration' do
-      client.authorization.temporary_credential_uri.to_s.should ==
+      expect(client.authorization.temporary_credential_uri.to_s).to eq(
         'https://www.google.com/accounts/OAuthGetRequestToken'
-      client.authorization.authorization_uri.to_s.should include(
+      )
+      expect(client.authorization.authorization_uri.to_s).to include(
         'https://www.google.com/accounts/OAuthAuthorizeToken'
       )
-      client.authorization.token_credential_uri.to_s.should ==
+      expect(client.authorization.token_credential_uri.to_s).to eq(
         'https://www.google.com/accounts/OAuthGetAccessToken'
-      client.authorization.client_credential_key.should == 'anonymous'
-      client.authorization.client_credential_secret.should == 'anonymous'
+      )
+      expect(client.authorization.client_credential_key).to eq('anonymous')
+      expect(client.authorization.client_credential_secret).to eq('anonymous')
     end
 
     it_should_behave_like 'configurable user agent'
@@ -113,7 +114,7 @@ describe Google::APIClient do
       client.authorization = :oauth_2
       @connection = stub_connection do |stub|
         stub.post('/prediction/v1.2/training?data=12345') do |env|
-          env[:request_headers]['Authorization'].should == 'Bearer 12345'
+          expect(env[:request_headers]['Authorization']).to eq('Bearer 12345')
           [200, {}, '{}']
         end
       end
@@ -168,7 +169,7 @@ describe Google::APIClient do
      end
   end  
 
-  describe 'when retiries enabled' do
+  describe 'when retries enabled' do
     before do
       client.retries = 2
     end
@@ -189,7 +190,7 @@ describe Google::APIClient do
       end
 
       client.execute(  
-        :uri => 'https://www.gogole.com/foo',
+        :uri => 'https://www.google.com/foo',
         :connection => @connection
       )
     end
@@ -208,11 +209,45 @@ describe Google::APIClient do
       end
 
       client.execute(  
-        :uri => 'https://www.gogole.com/foo',
+        :uri => 'https://www.google.com/foo',
         :connection => @connection
       )
     end
 
+
+    it 'should not attempt multiple token refreshes' do
+      client.authorization.access_token = '12345'
+      expect(client.authorization).to receive(:fetch_access_token!).once
+
+      @connection = stub_connection do |stub|
+        stub.get('/foo') do |env|
+          [401, {}, '{}']
+        end
+      end
+
+      client.execute(  
+        :uri => 'https://www.google.com/foo',
+        :connection => @connection
+      )
+    end
+
+    it 'should not retry on client errors' do
+      count = 0
+      @connection = stub_connection do |stub|
+        stub.get('/foo') do |env|
+          expect(count).to eq(0)
+          count += 1
+          [403, {}, '{}']
+        end
+      end
+
+      client.execute(  
+        :uri => 'https://www.google.com/foo',
+        :connection => @connection,
+        :authenticated => false
+      )
+    end
+
     it 'should retry on 500 errors' do
       client.authorization = nil
 
@@ -225,10 +260,10 @@ describe Google::APIClient do
         end
       end
 
-      client.execute(  
-        :uri => 'https://www.gogole.com/foo',
+      expect(client.execute(  
+        :uri => 'https://www.google.com/foo',
         :connection => @connection
-      ).status.should == 200
+      ).status).to eq(200)
 
     end
 
@@ -242,11 +277,11 @@ describe Google::APIClient do
         end
       end
 
-      client.execute(  
-        :uri => 'https://www.gogole.com/foo',
+      expect(client.execute(  
+        :uri => 'https://www.google.com/foo',
         :connection => @connection
-      ).status.should == 500
-      count.should == 3
+      ).status).to eq(500)
+      expect(count).to eq(3)
     end
 
   end