require "spec_helper" require "securerandom" describe "REST" do include Ably::Modules::Conversions [:msgpack, :json].each do |protocol| context "over #{protocol}" do let(:client) do Ably::Rest::Client.new(api_key: api_key, environment: environment, protocol: protocol) end let(:fixtures) do TestApp::APP_SPEC['channels'].first['presence'].map do |fixture| IdiomaticRubyWrapper(fixture, stop_at: [:client_data]) end end describe "fetching presence" do let(:channel) { client.channel("persisted:presence_fixtures") } let(:presence) { channel.presence.get } it "returns current members on the channel" do expect(presence.size).to eql(4) fixtures.each do |fixture| presence_message = presence.find { |client| client.client_id == fixture[:client_id] } expect(presence_message.client_data).to eq(fixture[:client_data]) end end end describe "presence history" do let(:channel) { client.channel("persisted:presence_fixtures") } let(:history) { channel.presence.history } it "returns recent presence activity" do expect(history.size).to eql(4) fixtures.each do |fixture| presence_message = history.find { |client| client.client_id == fixture['clientId'] } expect(presence_message.client_data).to eq(fixture[:client_data]) end end context 'with options' do let(:page_size) { 2 } let(:paged_history_forward) { channel.presence.history(limit: page_size, direction: :forwards) } it "returns recent presence activity with options passsed to Ably" do expect(paged_history_forward).to be_a(Ably::Models::PaginatedResource) expect(paged_history_forward.size).to eql(2) next_page = paged_history_forward.next_page expect(paged_history_forward.first.id).to eql(history.last.id) expect(next_page.first.id).to eql(history[page_size].id) end end end describe "options" do let(:channel_name) { "persisted:#{SecureRandom.hex(4)}" } let(:presence) { client.channel(channel_name).presence } let(:user) { 'appid.keyuid' } let(:secret) { SecureRandom.hex(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(api_key: "#{user}:#{secret}") end [:start, :end].each do |option| describe ":{option}", webmock: true do let!(:history_stub) { stub_request(:get, "#{endpoint}/channels/#{CGI.escape(channel_name)}/presence/history?live=true&#{option}=#{milliseconds}"). to_return(:body => '{}', :headers => { 'Content-Type' => 'application/json' }) } before do presence.history(options) end context 'with milliseconds since epoch' do let(:milliseconds) { as_since_epoch(Time.now) } let(:options) { { option => milliseconds } } specify 'are left unchanged' do expect(history_stub).to have_been_requested end end context 'with Time' do let(:time) { Time.now } let(:milliseconds) { as_since_epoch(time) } let(:options) { { option => time } } specify 'are left unchanged' do expect(history_stub).to have_been_requested end end end end end end end end