lib/submodules/ably-ruby/spec/acceptance/rest/client_spec.rb in ably-rest-0.7.5 vs lib/submodules/ably-ruby/spec/acceptance/rest/client_spec.rb in ably-rest-0.8.1

- old
+ new

@@ -10,18 +10,18 @@ connection_retry = Ably::Rest::Client::CONNECTION_RETRY context '#initialize' do let(:client_id) { random_str } - let(:token_request) { client.auth.create_token_request(key_id: key_id, key_secret: key_secret, client_id: client_id) } + let(:token_request) { client.auth.create_token_request(key_name: key_name, key_secret: key_secret, client_id: client_id) } - context 'with an auth block' do - let(:client) { Ably::Rest::Client.new(client_options) { token_request } } + context 'with a :auth_callback Proc' do + let(:client) { Ably::Rest::Client.new(client_options.merge(auth_callback: Proc.new { token_request })) } - it 'calls the block to get a new token' do - expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token } - expect(client.auth.current_token.client_id).to eql(client_id) + it 'calls the auth Proc to get a new token' do + expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token_details } + expect(client.auth.current_token_details.client_id).to eql(client_id) end end context 'with an auth URL' do let(:client_options) { default_options.merge(auth_url: token_request_url, auth_method: :get) } @@ -30,58 +30,61 @@ before do allow(client.auth).to receive(:token_request_from_auth_url).with(token_request_url, :auth_method => :get).and_return(token_request) end it 'sends an HTTP request to the provided URL to get a new token' do - expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token } - expect(client.auth.current_token.client_id).to eql(client_id) + expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token_details } + expect(client.auth.current_token_details.client_id).to eql(client_id) end end end context 'using tokens' do let(:client) do - Ably::Rest::Client.new(client_options) do + Ably::Rest::Client.new(client_options.merge(auth_callback: Proc.new do @request_index ||= 0 @request_index += 1 - send("token_request_#{@request_index}") - end + send("token_request_#{@request_index > 2 ? 'next' : @request_index}") + end)) end let(:token_request_1) { client.auth.create_token_request(token_request_options.merge(client_id: random_str)) } let(:token_request_2) { client.auth.create_token_request(token_request_options.merge(client_id: random_str)) } + # If token expires against whilst runnig tests in a slower CI environment then use this token + let(:token_request_next) { client.auth.create_token_request(token_request_options.merge(client_id: random_str)) } + context 'when expired' do - let(:token_request_options) { { key_id: key_id, key_secret: key_secret, ttl: Ably::Models::Token::TOKEN_EXPIRY_BUFFER } } + let(:token_request_options) { { key_name: key_name, key_secret: key_secret, ttl: Ably::Models::TokenDetails::TOKEN_EXPIRY_BUFFER } } it 'creates a new token automatically when the old token expires' do - expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token } - expect(client.auth.current_token.client_id).to eql(token_request_1['clientId']) + expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token_details } + expect(client.auth.current_token_details.client_id).to eql(token_request_1.client_id) sleep 1 - expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token } - expect(client.auth.current_token.client_id).to eql(token_request_2['clientId']) + expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token_details } + expect(client.auth.current_token_details.client_id).to eql(token_request_2.client_id) end end context 'when token has not expired' do - let(:token_request_options) { { key_id: key_id, key_secret: key_secret, ttl: 3600 } } + let(:token_request_options) { { key_name: key_name, key_secret: key_secret, ttl: 3600 } } it 'reuses the existing token for every request' do - expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token } - expect(client.auth.current_token.client_id).to eql(token_request_1['clientId']) + expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token_details } + expect(client.auth.current_token_details.client_id).to eql(token_request_1.client_id) sleep 1 - expect { client.channel('channel_name').publish('event', 'message') }.to_not change { client.auth.current_token } - expect(client.auth.current_token.client_id).to eql(token_request_1['clientId']) + expect { client.channel('channel_name').publish('event', 'message') }.to_not change { client.auth.current_token_details } + expect(client.auth.current_token_details.client_id).to eql(token_request_1.client_id) end end end context 'connection transport' do - let(:client_options) { default_options.merge(api_key: api_key) } + let(:client_options) { default_options.merge(key: api_key) } context 'for default host' do it "is configured to timeout connection opening in #{connection_retry.fetch(:single_request_open_timeout)} seconds" do expect(client.connection.options.open_timeout).to eql(connection_retry.fetch(:single_request_open_timeout)) end @@ -105,11 +108,11 @@ context 'fallback hosts', :webmock do let(:path) { '/channels/test/publish' } let(:publish_block) { proc { client.channel('test').publish('event', 'data') } } context 'configured' do - let(:client_options) { default_options.merge(api_key: api_key) } + let(:client_options) { default_options.merge(key: api_key) } it 'should make connection attempts to A.ably-realtime.com, B.ably-realtime.com, C.ably-realtime.com, D.ably-realtime.com, E.ably-realtime.com' do hosts = [] 5.times do hosts << client.fallback_connection.host @@ -117,11 +120,11 @@ expect(hosts).to match_array(%w(A.ably-realtime.com B.ably-realtime.com C.ably-realtime.com D.ably-realtime.com E.ably-realtime.com)) end end context 'when environment is NOT production' do - let(:client_options) { default_options.merge(environment: 'sandbox', api_key: api_key) } + let(:client_options) { default_options.merge(environment: 'sandbox', key: api_key) } let!(:default_host_request_stub) do stub_request(:post, "https://#{api_key}@#{environment}-#{Ably::Rest::Client::DOMAIN}#{path}").to_return do raise Faraday::TimeoutError.new('timeout error message') end end @@ -133,11 +136,11 @@ context 'when environment is production' do let(:custom_hosts) { %w(A.ably-realtime.com B.ably-realtime.com) } let(:max_attempts) { 2 } let(:cumulative_timeout) { 0.5 } - let(:client_options) { default_options.merge(environment: nil, api_key: api_key) } + let(:client_options) { default_options.merge(environment: nil, key: api_key) } before do stub_const 'Ably::FALLBACK_HOSTS', custom_hosts stub_const 'Ably::Rest::Client::CONNECTION_RETRY', { single_request_open_timeout: 4, @@ -207,11 +210,11 @@ end end context 'with a custom host' do let(:custom_host) { 'host.does.not.exist' } - let(:client_options) { default_options.merge(api_key: api_key, rest_host: custom_host) } + let(:client_options) { default_options.merge(key: api_key, rest_host: custom_host) } let(:capability) { { :foo => ["publish"] } } context 'that does not exist' do it 'fails immediately and raises a Faraday Error' do expect { client.channel('test').publish('event', 'data') }.to raise_error Ably::Exceptions::ConnectionError @@ -240,10 +243,10 @@ end end end context 'that times out', :webmock do - let(:path) { '/keys/app_id.key_id/requestToken' } + let(:path) { '/keys/app_id.key_name/requestToken' } let!(:custom_host_request_stub) do stub_request(:post, "https://#{custom_host}#{path}").to_return do raise Faraday::TimeoutError.new('timeout error message') end end