spec/acceptance/rest/auth_spec.rb in ably-0.7.0 vs spec/acceptance/rest/auth_spec.rb in ably-0.7.1

- old
+ new

@@ -3,21 +3,23 @@ describe Ably::Auth do include Ably::Modules::Conversions def hmac_for(token_request, secret) - text = token_request.values_at( + ruby_named_token_request = Ably::Models::IdiomaticRubyWrapper.new(token_request) + + text = [ :id, :ttl, :capability, :client_id, :timestamp, :nonce - ).map { |t| "#{t}\n" }.join("") + ].map { |key| "#{ruby_named_token_request[key]}\n" }.join("") encode64( - Digest::HMAC.digest(text, key_secret, Digest::SHA256) + OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, secret, text) ) end vary_by_protocol do let(:client) do @@ -36,11 +38,11 @@ body = if protocol == :msgpack MessagePack.unpack(request.body) else JSON.parse(request.body) end - body[key.to_s].to_s == val.to_s + body[convert_to_mixed_case(key)].to_s == val.to_s end def serialize(object, protocol) if protocol == :msgpack MessagePack.pack(token_response) @@ -48,11 +50,11 @@ JSON.dump(token_response) end end it 'has immutable options' do - expect { auth.options['key_id'] = 'new_id' }.to raise_error RuntimeError, /can't modify frozen Hash/ + expect { auth.options['key_id'] = 'new_id' }.to raise_error RuntimeError, /can't modify frozen.*Hash/ end describe '#request_token' do let(:ttl) { 30 * 60 } let(:capability) { { :foo => ['publish'] } } @@ -68,11 +70,11 @@ expect(actual_token.issued_at).to be_within(2).of(Time.now) expect(actual_token.expires_at).to be_within(2).of(Time.now + ttl) end %w(client_id capability nonce timestamp ttl).each do |option| - context "option :#{option}", :webmock do + context "with option :#{option}", :webmock do let(:random) { random_int_str } let(:options) { { option.to_sym => random } } let(:token_response) { { access_token: {} } } let!(:request_token_stub) do @@ -86,11 +88,11 @@ ) end before { auth.request_token options } - it 'overrides default' do + it 'overrides default and uses camelCase notation for all attributes' do expect(request_token_stub).to have_been_requested end end end @@ -132,11 +134,11 @@ end context 'without :query_time option' do let(:options) { { query_time: false } } - it 'queries the server for the time' do + it 'does not query the server for the time' do expect(client).to_not receive(:time) auth.request_token(options) end end @@ -354,50 +356,51 @@ let(:capability) { { :foo => ["publish"] } } let(:options) { Hash.new } subject { auth.create_token_request(options) } it 'uses the key ID from the client' do - expect(subject[:id]).to eql(key_id) + expect(subject['id']).to eql(key_id) end it 'uses the default TTL' do - expect(subject[:ttl]).to eql(Ably::Models::Token::DEFAULTS[:ttl]) + expect(subject['ttl']).to eql(Ably::Models::Token::DEFAULTS[:ttl]) end it 'uses the default capability' do - expect(subject[:capability]).to eql(Ably::Models::Token::DEFAULTS[:capability].to_json) + expect(subject['capability']).to eql(Ably::Models::Token::DEFAULTS[:capability].to_json) end context 'the nonce' do it 'is unique for every request' do - unique_nonces = 100.times.map { auth.create_token_request[:nonce] } + unique_nonces = 100.times.map { auth.create_token_request['nonce'] } expect(unique_nonces.uniq.length).to eql(100) end it 'is at least 16 characters' do - expect(subject[:nonce].length).to be >= 16 + expect(subject['nonce'].length).to be >= 16 end end %w(ttl capability nonce timestamp client_id).each do |attribute| context "with option :#{attribute}" do let(:option_value) { random_int_str(1_000_000_000) } before do options[attribute.to_sym] = option_value end it "overrides default" do - expect(subject[attribute.to_sym].to_s).to eql(option_value.to_s) + expect(subject[convert_to_mixed_case(attribute)].to_s).to eql(option_value.to_s) end end end context 'with additional invalid attributes' do let(:options) { { nonce: 'valid', is_not_used_by_token_request: 'invalid' } } specify 'are ignored' do expect(subject.keys).to_not include(:is_not_used_by_token_request) - expect(subject.keys).to include(:nonce) - expect(subject[:nonce]).to eql('valid') + expect(subject.keys).to_not include(convert_to_mixed_case(:is_not_used_by_token_request)) + expect(subject.keys).to include('nonce') + expect(subject['nonce']).to eql('valid') end end context 'when required fields are missing' do let(:client) { Ably::Rest::Client.new(auth_url: 'http://example.com', protocol: protocol) } @@ -415,20 +418,20 @@ let(:time) { Time.now - 30 } let(:options) { { query_time: true } } it 'queries the server for the timestamp' do expect(client).to receive(:time).and_return(time) - expect(subject[:timestamp]).to eql(time.to_i) + expect(subject['timestamp']).to eql(time.to_i) end end context 'with :timestamp option' do let(:token_request_time) { Time.now + 5 } let(:options) { { timestamp: token_request_time } } it 'uses the provided timestamp in the token request' do - expect(subject[:timestamp]).to eql(token_request_time.to_i) + expect(subject['timestamp']).to eql(token_request_time.to_i) end end context 'signing' do let(:options) do @@ -442,10 +445,10 @@ } end it 'generates a valid HMAC' do hmac = hmac_for(options, key_secret) - expect(subject[:mac]).to eql(hmac) + expect(subject['mac']).to eql(hmac) end end end context 'using token authentication' do