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

- old
+ new

@@ -3,103 +3,108 @@ describe Ably::Rest::Presence do include Ably::Modules::Conversions vary_by_protocol do - let(:default_options) { { api_key: api_key, environment: environment, protocol: protocol } } + let(:default_options) { { key: api_key, environment: environment, protocol: protocol } } let(:client_options) { default_options } let(:client) do Ably::Rest::Client.new(client_options) end let(:fixtures) do TestApp::APP_SPEC['channels'].first['presence'].map do |fixture| IdiomaticRubyWrapper(fixture, stop_at: [:data]) end end + let(:non_encoded_fixtures) { fixtures.reject { |fixture| fixture['encoding'] } } + # Encrypted fixtures need encryption details or an error will be raised + let(:cipher_details) { TestApp::APP_SPEC_CIPHER } + let(:algorithm) { cipher_details.fetch('algorithm').upcase } + let(:mode) { cipher_details.fetch('mode').upcase } + let(:key_length) { cipher_details.fetch('keylength') } + let(:secret_key) { Base64.decode64(cipher_details.fetch('key')) } + let(:iv) { Base64.decode64(cipher_details.fetch('iv')) } + + let(:cipher_options) { { key: secret_key, algorithm: algorithm, mode: mode, key_length: key_length, iv: iv } } + let(:fixtures_channel) { client.channel('persisted:presence_fixtures', encrypted: true, cipher_params: cipher_options, iv: iv) } + context 'tested against presence fixture data set up in test app' do + before(:context) do + # When this test is run as a part of a test suite, the presence data injected in the test app may have expired + reload_test_app + end + describe '#get' do - before(:context) do - # When this test is run as a part of a test suite, the presence data injected in the test app may have expired - reload_test_app - end + let(:presence_page) { fixtures_channel.presence.get } - let(:channel) { client.channel('persisted:presence_fixtures') } - let(:presence) { channel.presence.get } - it 'returns current members on the channel with their action set to :present' do - expect(presence.size).to eql(4) + expect(presence_page.items.size).to eql(fixtures.count) - fixtures.each do |fixture| - presence_message = presence.find { |client| client.client_id == fixture[:client_id] } + non_encoded_fixtures.each do |fixture| + presence_message = presence_page.items.find { |client| client.client_id == fixture[:client_id] } expect(presence_message.data).to eq(fixture[:data]) expect(presence_message.action).to eq(:present) end end context 'with :limit option' do - let(:page_size) { 2 } - let(:presence) { channel.presence.get(limit: page_size) } + let(:page_size) { 3 } + let(:presence_page) { fixtures_channel.presence.get(limit: page_size) } it 'returns a paged response limiting number of members per page' do - expect(presence.size).to eql(2) - next_page = presence.next_page - expect(next_page.size).to eql(2) - expect(next_page).to be_last_page + expect(presence_page.items.size).to eql(page_size) + next_page = presence_page.next + expect(next_page.items.size).to eql(page_size) + expect(next_page).to be_last end end end describe '#history' do - before(:context) do - # When this test is run as a part of a test suite, the presence data injected in the test app may have expired - reload_test_app - end + let(:history_page) { fixtures_channel.presence.history } - let(:channel) { client.channel('persisted:presence_fixtures') } - let(:presence_history) { channel.presence.history } - it 'returns recent presence activity' do - expect(presence_history.size).to eql(4) + expect(history_page.items.size).to eql(fixtures.count) - fixtures.each do |fixture| - presence_message = presence_history.find { |client| client.client_id == fixture['clientId'] } + non_encoded_fixtures.each do |fixture| + presence_message = history_page.items.find { |client| client.client_id == fixture['clientId'] } expect(presence_message.data).to eq(fixture[:data]) end end context 'with options' do - let(:page_size) { 2 } + let(:page_size) { 3 } context 'direction: :forwards' do - let(:presence_history) { channel.presence.history(direction: :forwards) } - let(:paged_history_forward) { channel.presence.history(limit: page_size, direction: :forwards) } + let(:history_page) { fixtures_channel.presence.history(direction: :forwards) } + let(:paged_history_forward) { fixtures_channel.presence.history(limit: page_size, direction: :forwards) } it 'returns recent presence activity forwards with most recent history last' do expect(paged_history_forward).to be_a(Ably::Models::PaginatedResource) - expect(paged_history_forward.size).to eql(2) + expect(paged_history_forward.items.size).to eql(page_size) - next_page = paged_history_forward.next_page + next_page = paged_history_forward.next - expect(paged_history_forward.first.id).to eql(presence_history.first.id) - expect(next_page.first.id).to eql(presence_history[page_size].id) + expect(paged_history_forward.items.first.id).to eql(history_page.items.first.id) + expect(next_page.items.first.id).to eql(history_page.items[page_size].id) end end context 'direction: :backwards' do - let(:presence_history) { channel.presence.history(direction: :backwards) } - let(:paged_history_backward) { channel.presence.history(limit: page_size, direction: :backwards) } + let(:history_page) { fixtures_channel.presence.history(direction: :backwards) } + let(:paged_history_backward) { fixtures_channel.presence.history(limit: page_size, direction: :backwards) } it 'returns recent presence activity backwards with most recent history first' do expect(paged_history_backward).to be_a(Ably::Models::PaginatedResource) - expect(paged_history_backward.size).to eql(2) + expect(paged_history_backward.items.size).to eql(page_size) - next_page = paged_history_backward.next_page + next_page = paged_history_backward.next - expect(paged_history_backward.first.id).to eql(presence_history.first.id) - expect(next_page.first.id).to eql(presence_history[page_size].id) + expect(paged_history_backward.items.first.id).to eql(history_page.items.first.id) + expect(next_page.items.first.id).to eql(history_page.items[page_size].id) end end end end end @@ -115,17 +120,24 @@ client_end_point.user = user client_end_point.password = secret end end let(:client) do - Ably::Rest::Client.new(api_key: "#{user}:#{secret}") + Ably::Rest::Client.new(key: "#{user}:#{secret}") end + let(:default_options) do + { + direction: :backwards, + limit: 100 + } + end [:start, :end].each do |option| describe ":#{option}", :webmock do let!(:history_stub) { - stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence/history?#{option}=#{milliseconds}"). + query_params = default_options.merge(option => milliseconds).map { |k, v| "#{k}=#{v}" }.join('&') + stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence/history?#{query_params}"). to_return(:body => '{}', :headers => { 'Content-Type' => 'application/json' }) } before do presence.history(options) @@ -152,21 +164,46 @@ end end end end - describe 'decoding', :webmock do + describe 'decoding' do + context 'with encoded fixture data' do + let(:decoded_client_id) { 'client_decoded' } + let(:encoded_client_id) { 'client_encoded' } + + def message(client_id, messages) + messages.items.find { |message| message.client_id == client_id } + end + + describe '#history' do + let(:history) { fixtures_channel.presence.history } + it 'decodes encoded and encryped presence fixture data automatically' do + expect(message(decoded_client_id, history).data).to eql(message(encoded_client_id, history).data) + end + end + + describe '#get' do + let(:present) { fixtures_channel.presence.get } + it 'decodes encoded and encryped presence fixture data automatically' do + expect(message(decoded_client_id, present).data).to eql(message(encoded_client_id, present).data) + end + end + end + end + + describe 'decoding permutations using mocked #history', :webmock do let(:user) { 'appid.keyuid' } let(:secret) { random_str(8) } let(:endpoint) do client.endpoint.tap do |client_end_point| client_end_point.user = user client_end_point.password = secret end end let(:client) do - Ably::Rest::Client.new(client_options.merge(api_key: "#{user}:#{secret}")) + Ably::Rest::Client.new(client_options.merge(key: "#{user}:#{secret}")) end let(:data) { random_str(32) } let(:channel_name) { "persisted:#{random_str(4)}" } let(:cipher_options) { { key: random_str(32), algorithm: 'aes', mode: 'cbc', key_length: 256 } } @@ -193,39 +230,39 @@ end end context '#get' do let!(:get_stub) { - stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence"). + stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence?limit=100"). to_return(:body => serialized_encoded_message, :headers => { 'Content-Type' => content_type }) } after do expect(get_stub).to have_been_requested end it 'automaticaly decodes presence messages' do - present = presence.get - expect(present.first.encoding).to be_nil - expect(present.first.data).to eql(data) + present_page = presence.get + expect(present_page.items.first.encoding).to be_nil + expect(present_page.items.first.data).to eql(data) end end context '#history' do let!(:history_stub) { - stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence/history"). + stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence/history?direction=backwards&limit=100"). to_return(:body => serialized_encoded_message, :headers => { 'Content-Type' => content_type }) } after do expect(history_stub).to have_been_requested end it 'automaticaly decodes presence messages' do - history = presence.history - expect(history.first.encoding).to be_nil - expect(history.first.data).to eql(data) + history_page = presence.history + expect(history_page.items.first.encoding).to be_nil + expect(history_page.items.first.data).to eql(data) end end end context 'invalid data' do @@ -240,14 +277,14 @@ end context '#get' do let(:client_options) { default_options.merge(log_level: :fatal) } let!(:get_stub) { - stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence"). + stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence?limit=100"). to_return(:body => serialized_encoded_message_with_invalid_encoding, :headers => { 'Content-Type' => content_type }) } - let(:presence_message) { presence.get.first } + let(:presence_message) { presence.get.items.first } after do expect(get_stub).to have_been_requested end @@ -264,13 +301,13 @@ end context '#history' do let(:client_options) { default_options.merge(log_level: :fatal) } let!(:history_stub) { - stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence/history"). + stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence/history?direction=backwards&limit=100"). to_return(:body => serialized_encoded_message_with_invalid_encoding, :headers => { 'Content-Type' => content_type }) } - let(:presence_message) { presence.history.first } + let(:presence_message) { presence.history.items.first } after do expect(history_stub).to have_been_requested end