spec/acceptance/rest/presence_spec.rb in ably-0.8.2 vs spec/acceptance/rest/presence_spec.rb in ably-0.8.3

- old
+ new

@@ -37,14 +37,16 @@ describe '#get' do let(:presence_page) { fixtures_channel.presence.get } it 'returns current members on the channel with their action set to :present' do + expect(presence_page).to be_a(Ably::Models::PaginatedResult) expect(presence_page.items.size).to eql(fixtures.count) non_encoded_fixtures.each do |fixture| presence_message = presence_page.items.find { |client| client.client_id == fixture[:client_id] } + expect(presence_message).to be_a(Ably::Models::PresenceMessage) expect(presence_message.data).to eq(fixture[:data]) expect(presence_message.action).to eq(:present) end end @@ -52,31 +54,91 @@ 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_page.items.size).to eql(page_size) - # TODO: To be enabled once Realtime Presence issue #164 is resolved - # expect(presence_page).to be_first next_page = presence_page.next expect(next_page.items.size).to eql(page_size) expect(next_page).to be_last end end + + context 'default :limit', webmock: true do + let(:query_options) do + { + limit: 100 + } + end + let(:endpoint) do + client.endpoint.tap do |client_end_point| + client_end_point.user = key_name + client_end_point.password = key_secret + end + end + let!(:get_stub) { + query_params = query_options.map { |k, v| "#{k}=#{v}" }.join('&') + stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence?#{query_params}"). + to_return(:body => '{}', :headers => { 'Content-Type' => 'application/json' }) + } + let(:channel_name) { random_str } + let(:channel) { client.channels.get(channel_name) } + + before do + channel.presence.get + end + + it 'defaults to a limit of 100' do + expect(get_stub).to have_been_requested + end + end + + context 'with :client_id option' do + let(:client_id) { non_encoded_fixtures.first[:client_id] } + let(:presence_page) { fixtures_channel.presence.get(client_id: client_id) } + + it 'returns a list members filtered by the provided client ID' do + pending 'not implemented in the REST API yet' # TODO realtime/issues/243 + expect(presence_page.items.count).to eql(1) + expect(presence_page.items.first.client_id).to eql(client_id) + end + end + + context 'with :connection_id option' do + let(:connection_id) { fixtures_channel.presence.get.first.connection_id } + let(:presence_page) { fixtures_channel.presence.get(connection_id: connection_id) } + + it 'returns a list members filtered by the provided connection ID' do + pending 'not implemented in the REST API yet' # TODO realtime/issues/243 + expect(presence_page.items.count).to eql(1) + expect(presence_page.items.first.connetion_id).to eql(connetion_id) + end + end end describe '#history' do let(:history_page) { fixtures_channel.presence.history } it 'returns recent presence activity' do + expect(history_page).to be_a(Ably::Models::PaginatedResult) expect(history_page.items.size).to eql(fixtures.count) non_encoded_fixtures.each do |fixture| presence_message = history_page.items.find { |client| client.client_id == fixture['clientId'] } + expect(presence_message).to be_a(Ably::Models::PresenceMessage) expect(presence_message.data).to eq(fixture[:data]) end end + context 'default behaviour' do + let(:default_page) { fixtures_channel.presence.history } + let(:backwards_page) { fixtures_channel.presence.history(direction: :backwards) } + + it 'uses backwards direction' do + expect(default_page.items).to eq(backwards_page.items) + end + end + context 'with options' do let(:page_size) { 3 } context 'direction: :forwards' do let(:history_page) { fixtures_channel.presence.history(direction: :forwards) } @@ -110,11 +172,11 @@ end end end describe '#history' do - context 'with time range options' do + context 'with options' do let(:channel_name) { "persisted:#{random_str(4)}" } let(:presence) { client.channel(channel_name).presence } let(:user) { 'appid.keyuid' } let(:secret) { random_str(8) } let(:endpoint) do @@ -124,45 +186,94 @@ end end let(:client) do Ably::Rest::Client.new(key: "#{user}:#{secret}") end - let(:default_options) do + let(:history_options) do { direction: :backwards, limit: 100 } end - [:start, :end].each do |option| - describe ":#{option}", :webmock do - let!(:history_stub) { - 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' }) - } + context 'limit options', :webmock do + let!(:history_stub) { + query_params = history_options.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) + before do + presence.history(history_options) + end + + context 'default' do + it 'is set to 100' do + expect(history_stub).to have_been_requested end + end - context 'with milliseconds since epoch value' do - let(:milliseconds) { as_since_epoch(Time.now) } - let(:options) { { option => milliseconds } } + context 'set to 1000' do + let(:history_options) do + { + direction: :backwards, + limit: 1000 + } + end - it 'uses this value in the history request' do - expect(history_stub).to have_been_requested - end + it 'is passes the limit query param value 1000' do + expect(history_stub).to have_been_requested end + end + end - context 'with Time object value' do - let(:time) { Time.now } - let(:milliseconds) { as_since_epoch(time) } - let(:options) { { option => time } } + context 'with time range options' do + [:start, :end].each do |option| + describe ":#{option}", :webmock do + let(:history_options) { + { + direction: :backwards, + limit: 100, + option => milliseconds + } + } + let!(:history_stub) { + query_params = history_options.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' }) + } - it 'converts the value to milliseconds since epoch in the hisotry request' do - expect(history_stub).to have_been_requested + before do + presence.history(history_options) end + + context 'with milliseconds since epoch value' do + let(:milliseconds) { as_since_epoch(Time.now) } + let(:options) { { option => milliseconds } } + + it 'uses this value in the history request' do + expect(history_stub).to have_been_requested + end + end + + context 'with Time object value' do + let(:time) { Time.now } + let(:milliseconds) { as_since_epoch(time) } + let(:options) { { option => time } } + + it 'converts the value to milliseconds since epoch in the hisotry request' do + expect(history_stub).to have_been_requested + end + end + end + end + + context 'when argument start is after end' do + let(:presence) { client.channel(random_str).presence } + let(:subject) { presence.history(start: as_since_epoch(Time.now), end: Time.now - 120) } + + it 'should raise an exception' do + expect { subject.items }.to raise_error ArgumentError end end end end end