2017-09-07 03:40:18 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe ::Presence::PresenceManager do
|
|
|
|
|
|
|
|
let(:user1) { Fabricate(:user) }
|
|
|
|
let(:user2) { Fabricate(:user) }
|
|
|
|
let(:user3) { Fabricate(:user) }
|
|
|
|
let(:manager) { ::Presence::PresenceManager }
|
|
|
|
|
2017-09-08 16:06:27 -04:00
|
|
|
let(:post1) { Fabricate(:post) }
|
|
|
|
let(:post2) { Fabricate(:post) }
|
|
|
|
|
2017-09-07 03:40:18 -04:00
|
|
|
after(:each) do
|
2017-09-08 16:06:27 -04:00
|
|
|
$redis.del("presence:topic:#{post1.topic.id}")
|
|
|
|
$redis.del("presence:topic:#{post2.topic.id}")
|
|
|
|
$redis.del("presence:post:#{post1.id}")
|
|
|
|
$redis.del("presence:post:#{post2.id}")
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds, removes and lists users correctly' do
|
2017-09-08 16:06:27 -04:00
|
|
|
expect(manager.get_users('post', post1.id).count).to eq(0)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2017-09-08 16:06:27 -04:00
|
|
|
expect(manager.add('post', post1.id, user1.id)).to be true
|
|
|
|
expect(manager.add('post', post1.id, user2.id)).to be true
|
|
|
|
expect(manager.add('post', post2.id, user3.id)).to be true
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2017-09-08 16:06:27 -04:00
|
|
|
expect(manager.get_users('post', post1.id).count).to eq(2)
|
|
|
|
expect(manager.get_users('post', post2.id).count).to eq(1)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2017-09-08 16:06:27 -04:00
|
|
|
expect(manager.get_users('post', post1.id)).to contain_exactly(user1, user2)
|
|
|
|
expect(manager.get_users('post', post2.id)).to contain_exactly(user3)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2017-09-08 16:06:27 -04:00
|
|
|
expect(manager.remove('post', post1.id, user1.id)).to be true
|
|
|
|
expect(manager.get_users('post', post1.id).count).to eq(1)
|
|
|
|
expect(manager.get_users('post', post1.id)).to contain_exactly(user2)
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'publishes correctly' do
|
2017-09-08 16:06:27 -04:00
|
|
|
expect(manager.get_users('post', post1.id).count).to eq(0)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2017-09-08 16:06:27 -04:00
|
|
|
manager.add('post', post1.id, user1.id)
|
|
|
|
manager.add('post', post1.id, user2.id)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
2017-09-08 16:06:27 -04:00
|
|
|
manager.publish('post', post1.id)
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.count).to eq (1)
|
|
|
|
message = messages.first
|
|
|
|
|
2017-09-08 16:06:27 -04:00
|
|
|
expect(message.channel).to eq("/presence/post/#{post1.id}")
|
2017-09-07 03:40:18 -04:00
|
|
|
|
|
|
|
expect(message.data["users"].map { |u| u[:id] }).to contain_exactly(user1.id, user2.id)
|
|
|
|
end
|
|
|
|
|
2017-09-08 16:06:27 -04:00
|
|
|
it 'publishes private message securely' do
|
|
|
|
private_post = Fabricate(:private_message_post)
|
|
|
|
manager.add('post', private_post.id, user2.id)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
manager.publish('post', private_post.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.count).to eq (1)
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.channel).to eq("/presence/post/#{private_post.id}")
|
|
|
|
|
|
|
|
user_ids = User.where('admin or moderator').pluck(:id)
|
|
|
|
user_ids += private_post.topic.allowed_users.pluck(:id)
|
|
|
|
expect(message.user_ids).to contain_exactly(*user_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'publishes private category securely' do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
category = Fabricate(:private_category, group: group)
|
|
|
|
private_topic = Fabricate(:topic, category: category)
|
|
|
|
|
|
|
|
manager.add('topic', private_topic.id, user2.id)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
manager.publish('topic', private_topic.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.count).to eq (1)
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.channel).to eq("/presence/topic/#{private_topic.id}")
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(*private_topic.secure_group_ids)
|
|
|
|
end
|
|
|
|
|
2017-09-07 03:40:18 -04:00
|
|
|
it 'cleans up correctly' do
|
|
|
|
freeze_time Time.zone.now do
|
2017-09-08 16:06:27 -04:00
|
|
|
expect(manager.add('post', post1.id, user1.id)).to be true
|
|
|
|
expect(manager.cleanup('post', post1.id)).to be false # Nothing to cleanup
|
|
|
|
expect(manager.get_users('post', post1.id).count).to eq(1)
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Anything older than 20 seconds should be cleaned up
|
|
|
|
freeze_time 30.seconds.from_now do
|
2017-09-08 16:06:27 -04:00
|
|
|
expect(manager.cleanup('post', post1.id)).to be true
|
|
|
|
expect(manager.get_users('post', post1.id).count).to eq(0)
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|