# frozen_string_literal: true require 'rails_helper' describe PresenceController do fab!(:user) { Fabricate(:user) } fab!(:group) { Fabricate(:group).tap { |g| g.add(user) } } let(:ch1) { PresenceChannel.new('/test/public1') } let(:ch2) { PresenceChannel.new('/test/public2') } let(:secure_user_channel) { PresenceChannel.new('/test/secureuser') } let(:secure_group_channel) { PresenceChannel.new('/test/securegroup') } let(:allowed_user_channel) { PresenceChannel.new('/test/alloweduser') } let(:allowed_group_channel) { PresenceChannel.new('/test/allowedgroup') } let(:count_only_channel) { PresenceChannel.new('/test/countonly') } before do PresenceChannel.clear_all! secure_user = Fabricate(:user) secure_group = Fabricate(:group) PresenceChannel.register_prefix("test") do |channel| case channel when /\A\/test\/public\d*\z/ PresenceChannel::Config.new(public: true) when "/test/secureuser" PresenceChannel::Config.new(allowed_user_ids: [ secure_user.id ]) when "/test/securegroup" PresenceChannel::Config.new(allowed_group_ids: [ secure_group.id ]) when "/test/alloweduser" PresenceChannel::Config.new(allowed_user_ids: [ user.id ]) when "/test/allowedgroup" PresenceChannel::Config.new(allowed_group_ids: [ group.id ]) when "/test/countonly" PresenceChannel::Config.new(public: true, count_only: true) else nil end end end after do PresenceChannel.clear_all! PresenceChannel.unregister_prefix("test") end describe "#update" do it "works" do sign_in(user) client_id = SecureRandom.hex expect(ch1.user_ids).to eq([]) expect(ch2.user_ids).to eq([]) post "/presence/update.json", params: { client_id: client_id, present_channels: [ch1.name, ch2.name] } expect(response.status).to eq(200) expect(ch1.user_ids).to eq([user.id]) expect(ch2.user_ids).to eq([user.id]) post "/presence/update.json", params: { client_id: client_id, present_channels: [ch1.name], leave_channels: [ch2.name] } expect(response.status).to eq(200) expect(ch1.user_ids).to eq([user.id]) expect(ch2.user_ids).to eq([]) post "/presence/update.json", params: { client_id: client_id, present_channels: [], leave_channels: [ch1.name] } expect(response.status).to eq(200) expect(ch1.user_ids).to eq([]) expect(ch2.user_ids).to eq([]) end it "returns true/false based on channel existence/security" do sign_in(user) client_id = SecureRandom.hex expect(ch1.user_ids).to eq([]) expect(secure_user_channel.user_ids).to eq([]) expect(secure_group_channel.user_ids).to eq([]) post "/presence/update.json", params: { client_id: client_id, present_channels: [ ch1.name, secure_user_channel.name, secure_group_channel.name, allowed_user_channel.name, allowed_group_channel.name, "/test/nonexistent" ] } expect(response.status).to eq(200) expect(response.parsed_body).to eq({ ch1.name => true, secure_user_channel.name => false, secure_group_channel.name => false, allowed_user_channel.name => true, allowed_group_channel.name => true, "/test/nonexistent" => false, }) expect(ch1.user_ids).to eq([user.id]) expect(secure_user_channel.user_ids).to eq([]) expect(secure_group_channel.user_ids).to eq([]) expect(allowed_user_channel.user_ids).to eq([user.id]) expect(allowed_group_channel.user_ids).to eq([user.id]) end end describe "#get" do let(:user2) { Fabricate(:user) } let(:user3) { Fabricate(:user) } it "works" do get "/presence/get", params: { channel: ch1.name } expect(response.status).to eq(200) body = response.parsed_body expect(body["users"]).to eq([]) expect(body["count"]).to eq(0) expect(body["last_message_id"]).to eq(MessageBus.last_id(ch1.message_bus_channel_name)) ch1.present(user_id: user.id, client_id: SecureRandom.hex) ch1.present(user_id: user2.id, client_id: SecureRandom.hex) ch1.present(user_id: user3.id, client_id: SecureRandom.hex) get "/presence/get", params: { channel: ch1.name } body = response.parsed_body expect(body["users"].map { |u| u["id"] }).to contain_exactly(user.id, user2.id, user3.id) expect(body["users"][0].keys).to contain_exactly("avatar_template", "id", "name", "username") expect(body["count"]).to eq(3) expect(body["last_message_id"]).to eq(MessageBus.last_id(ch1.message_bus_channel_name)) end it "respects the existence/security of the channel" do sign_in user get "/presence/get", params: { channel: ch1.name } expect(response.status).to eq(200) get "/presence/get", params: { channel: secure_user_channel.name } expect(response.status).to eq(404) get "/presence/get", params: { channel: secure_group_channel.name } expect(response.status).to eq(404) get "/presence/get", params: { channel: allowed_user_channel.name } expect(response.status).to eq(200) get "/presence/get", params: { channel: allowed_group_channel.name } expect(response.status).to eq(200) get "/presence/get", params: { channel: "/test/nonexistent" } expect(response.status).to eq(404) end it "works for count_only channels" do get "/presence/get", params: { channel: count_only_channel.name } expect(response.status).to eq(200) expect(response.parsed_body.keys).to contain_exactly("count", "last_message_id") expect(response.parsed_body["count"]).to eq(0) count_only_channel.present(user_id: user.id, client_id: "a") get "/presence/get", params: { channel: count_only_channel.name } expect(response.status).to eq(200) expect(response.parsed_body["count"]).to eq(1) end end end