spec/acceptance/rest/auth_spec.rb in ably-1.0.5 vs spec/acceptance/rest/auth_spec.rb in ably-1.0.6

- old
+ new

@@ -470,11 +470,11 @@ let!(:token_details) do auth.request_token(token_params, auth_callback: auth_callback) end let(:auth_callback) do - Proc.new do |token_params_arg| + lambda do |token_params_arg| @block_called = true @block_params = token_params_arg { 'token' => token, 'keyName' => 'J_0Tlg.NxCRig', @@ -484,11 +484,11 @@ 'capability'=> capability_str } end end - it 'calls the Proc when authenticating to obtain the request token' do + it 'calls the lambda when authenticating to obtain the request token' do expect(@block_called).to eql(true) expect(@block_params).to include(token_params) end it 'uses the token request returned from the callback when requesting a new token' do @@ -515,11 +515,11 @@ context 'that returns a TokenDetails object' do let(:client_id) { random_str } let!(:token_details) do - auth.request_token({}, auth_callback: Proc.new do |block_options| + auth.request_token({}, auth_callback: lambda do |token_params| auth.create_token_request(client_id: client_id) end) end it 'uses the token request returned from the callback when requesting a new token' do @@ -531,11 +531,11 @@ context 'that returns a Token string' do let(:second_client) { Ably::Rest::Client.new(key: api_key, environment: environment, protocol: protocol) } let(:token) { second_client.auth.request_token.token } let!(:token_details) do - auth.request_token({}, auth_callback: Proc.new do |block_options| + auth.request_token({}, auth_callback: lambda do |token_params| token end) end it 'uses the token request returned from the callback when requesting a new token' do @@ -687,11 +687,11 @@ end end context 'AuthOptions argument' do let(:token_ttl) { 2 } - let(:auth_callback) { Proc.new do + let(:auth_callback) { lambda do |token_params| auth.create_token_request(ttl: token_ttl) end } let(:default_auth_options) { { auth_callback: auth_callback } } before do @@ -715,11 +715,11 @@ expect(auth.options[:auth_callback]).to be_nil expect(auth.options[:auth_method]).to eql(:post) end it 'updates Auth#options attribute with an immutable hash' do - auth.authorize(nil, auth_callback: Proc.new { '1231232.12321:12321312' }) + auth.authorize(nil, auth_callback: lambda { |token_params| '1231232.12321:12321312' }) expect { auth.options['key_name'] = 'new_name' }.to raise_error RuntimeError, /can't modify frozen.*Hash/ end it 'uses AuthOptions#query_time for this request and will not query_time for subsequent requests (#RSA10g)' do expect(client).to receive(:time).once.and_call_original @@ -761,65 +761,65 @@ expect(auth.options[:authUrl]).to be_nil auth.authorize({}, authUrl: 'http://foo.com') expect(auth.options[:authUrl]).to eql('http://foo.com') end - context 'with a Proc for the :auth_callback option' do + context 'with a lambda for the :auth_callback option' do let(:client_id) { random_str } let!(:token) do - auth.authorize({}, auth_callback: Proc.new do + auth.authorize({}, auth_callback: lambda do |token_params| @block_called ||= 0 @block_called += 1 auth.create_token_request(client_id: client_id) end) end - it 'calls the Proc' do + it 'calls the lambda' do expect(@block_called).to eql(1) end it 'uses the token request returned from the callback when requesting a new token' do expect(token.client_id).to eql(client_id) end context 'for every subsequent #request_token' do - context 'without a :auth_callback Proc' do + context 'without a :auth_callback lambda' do it 'calls the originally provided block' do auth.request_token expect(@block_called).to eql(2) end end context 'with a provided block' do - it 'does not call the originally provided Proc and calls the new #request_token :auth_callback Proc' do - auth.request_token({}, auth_callback: Proc.new { @request_block_called = true; auth.create_token_request }) + it 'does not call the originally provided lambda and calls the new #request_token :auth_callback lambda' do + auth.request_token({}, auth_callback: lambda { |token_params| @request_block_called = true; auth.create_token_request }) expect(@block_called).to eql(1) expect(@request_block_called).to eql(true) end end end end context 'with an explicit token string that expires' do - context 'and a Proc for the :auth_callback option to provide a means to renew the token' do + context 'and a lambda for the :auth_callback option to provide a means to renew the token' do before do # Ensure a soon to expire token is not treated as expired stub_const 'Ably::Models::TokenDetails::TOKEN_EXPIRY_BUFFER', 0 old_token_defaults = Ably::Auth::TOKEN_DEFAULTS stub_const 'Ably::Auth::TOKEN_DEFAULTS', old_token_defaults.merge(renew_token_buffer: 0) @block_called = 0 end let(:token_client) { Ably::Rest::Client.new(default_options.merge(key: api_key, default_token_params: { ttl: 3 })) } let(:client_options) { - default_options.merge(token: token_client.auth.request_token.token, auth_callback: Proc.new do + default_options.merge(token: token_client.auth.request_token.token, auth_callback: lambda do |token_params| @block_called += 1 token_client.auth.create_token_request end) } - it 'calls the Proc once the token has expired and the new token is used' do + it 'calls the lambda once the token has expired and the new token is used' do client.stats expect(@block_called).to eql(0) sleep 3.5 expect { client.stats }.to change { client.auth.current_token_details } expect(@block_called).to eql(1) @@ -827,11 +827,11 @@ end end context 'with an explicit ClientOptions client_id' do let(:client_id) { random_str } - let(:client_options) { default_options.merge(auth_callback: Proc.new { auth_token_object }, client_id: client_id) } + let(:client_options) { default_options.merge(auth_callback: lambda { |token_params| auth_token_object }, client_id: client_id) } let(:auth_client) { Ably::Rest::Client.new(default_options.merge(key: api_key, client_id: 'invalid')) } context 'and an incompatible client_id in a TokenDetails object passed to the auth callback' do let(:auth_token_object) { auth_client.auth.request_token } @@ -869,11 +869,11 @@ it 'returns a TokenRequest object' do expect(subject).to be_a(Ably::Models::TokenRequest) end it 'returns a TokenRequest that can be passed to a client that can use it for authentication without an API key' do - auth_callback = Proc.new { subject } + auth_callback = proc { |token_params| subject } client_without_api_key = Ably::Rest::Client.new(default_options.merge(auth_callback: auth_callback)) expect(client_without_api_key.auth).to be_using_token_auth expect { client_without_api_key.auth.authorize }.to_not raise_error end @@ -930,11 +930,11 @@ it 'overrides the default' do expect(subject.capability).to eql(capability) end it 'uses these capabilities when Ably issues an actual token' do - auth_callback = Proc.new { subject } + auth_callback = lambda { |token_params| subject } client_without_api_key = Ably::Rest::Client.new(default_options.merge(auth_callback: auth_callback)) client_without_api_key.auth.authorize expect(client_without_api_key.auth.current_token_details.capability).to eql(capability) end end @@ -1039,11 +1039,11 @@ hmac_ordered = auth.create_token_request(token_attributes_ordered).mac expect(hmac).to eql(hmac_ordered) end it 'is valid when used for authentication' do - auth_callback = Proc.new do + auth_callback = lambda do |callback| auth.create_token_request(token_attributes) end client = Ably::Rest::Client.new(auth_callback: auth_callback, environment: environment, protocol: protocol) client.auth.authorize end @@ -1322,39 +1322,12 @@ specify '#using_basic_auth? is true' do expect(auth).to be_using_basic_auth end end - context 'deprecated #authorise' do + context 'deprecated #authorise', :prevent_log_stubbing do let(:client_options) { default_options.merge(key: api_key, logger: custom_logger_object, use_token_auth: true) } - let(:custom_logger) do - Class.new do - def initialize - @messages = [] - end - - [:fatal, :error, :warn, :info, :debug].each do |severity| - define_method severity do |message, &block| - message_val = [message] - message_val << block.call if block - - @messages << [severity, message_val.compact.join(' ')] - end - end - - def logs - @messages - end - - def level - 1 - end - - def level=(new_level) - end - end - end - let(:custom_logger_object) { custom_logger.new } + let(:custom_logger_object) { TestLogger.new } it 'logs a deprecation warning (#RSA10l)' do client.auth.authorise expect(custom_logger_object.logs.find { |severity, message| message.match(/authorise.*deprecated/i)} ).to_not be_nil end