2017-09-07 15:40:18 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2017-10-02 12:00:43 +08:00
|
|
|
describe ::Presence::PresencesController do
|
2017-09-07 15:40:18 +08:00
|
|
|
before do
|
|
|
|
SiteSetting.presence_enabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user1) { Fabricate(:user) }
|
|
|
|
let(:user2) { Fabricate(:user) }
|
|
|
|
let(:user3) { Fabricate(:user) }
|
|
|
|
|
2017-09-09 04:06:27 +08:00
|
|
|
let(:post1) { Fabricate(:post) }
|
|
|
|
let(:post2) { Fabricate(:post) }
|
|
|
|
|
2017-11-20 22:24:59 +08:00
|
|
|
let(:manager) { ::Presence::PresenceManager }
|
|
|
|
|
2017-10-02 12:00:43 +08:00
|
|
|
after do
|
2017-09-09 04:06:27 +08: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 15:40:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not logged in' do
|
|
|
|
it 'should raise the right error' do
|
2018-01-12 11:42:05 +08:00
|
|
|
post '/presence/publish.json'
|
|
|
|
expect(response.status).to eq(403)
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when logged in' do
|
|
|
|
before do
|
|
|
|
sign_in(user1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't produce an error" do
|
|
|
|
expect { post '/presence/publish.json' }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
2017-09-09 04:06:27 +08:00
|
|
|
it "uses guardian to secure endpoint" do
|
|
|
|
private_post = Fabricate(:private_message_post)
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { action: 'edit', post_id: private_post.id }
|
|
|
|
}
|
|
|
|
|
2017-09-09 04:06:27 +08:00
|
|
|
expect(response.code.to_i).to eq(403)
|
|
|
|
|
|
|
|
group = Fabricate(:group)
|
|
|
|
category = Fabricate(:private_category, group: group)
|
|
|
|
private_topic = Fabricate(:topic, category: category)
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { action: 'edit', topic_id: private_topic.id }
|
|
|
|
}
|
|
|
|
|
2017-09-09 04:06:27 +08:00
|
|
|
expect(response.code.to_i).to eq(403)
|
|
|
|
end
|
|
|
|
|
2017-09-07 15:40:18 +08:00
|
|
|
it "returns a response when requested" do
|
|
|
|
messages = MessageBus.track_publish do
|
2017-08-31 12:06:56 +08:00
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { compose_state: 'open', action: 'edit', post_id: post1.id }, response_needed: true
|
|
|
|
}
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
|
|
|
|
2017-10-02 12:00:43 +08:00
|
|
|
expect(messages.count).to eq(1)
|
2017-09-07 15:40:18 +08:00
|
|
|
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
|
2017-09-09 04:06:27 +08:00
|
|
|
expect(data['messagebus_channel']).to eq("/presence/post/#{post1.id}")
|
|
|
|
expect(data['messagebus_id']).to eq(MessageBus.last_id("/presence/post/#{post1.id}"))
|
2017-09-07 15:40:18 +08:00
|
|
|
expect(data['users'][0]["id"]).to eq(user1.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return a response when not requested" do
|
|
|
|
messages = MessageBus.track_publish do
|
2017-08-31 12:06:56 +08:00
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { compose_state: 'open', action: 'edit', post_id: post1.id }
|
|
|
|
}
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
|
|
|
|
2017-10-02 12:00:43 +08:00
|
|
|
expect(messages.count).to eq(1)
|
2017-09-07 15:40:18 +08:00
|
|
|
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
expect(data).to eq({})
|
|
|
|
end
|
|
|
|
|
2017-12-18 12:41:20 +08:00
|
|
|
it "does send duplicate messagebus messages" do
|
2017-09-07 15:40:18 +08:00
|
|
|
messages = MessageBus.track_publish do
|
2017-08-31 12:06:56 +08:00
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { compose_state: 'open', action: 'edit', post_id: post1.id }
|
|
|
|
}
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2017-10-02 12:00:43 +08:00
|
|
|
expect(messages.count).to eq(1)
|
2017-09-07 15:40:18 +08:00
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
2017-08-31 12:06:56 +08:00
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { compose_state: 'open', action: 'edit', post_id: post1.id }
|
|
|
|
}
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2017-12-18 12:41:20 +08:00
|
|
|
# we do this cause we also publish time
|
|
|
|
expect(messages.count).to eq(1)
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "clears 'previous' state when supplied" do
|
|
|
|
messages = MessageBus.track_publish do
|
2017-08-31 12:06:56 +08:00
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { compose_state: 'open', action: 'edit', post_id: post1.id }
|
|
|
|
}
|
|
|
|
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { compose_state: 'open', action: 'edit', post_id: post2.id },
|
|
|
|
previous: { compose_state: 'open', action: 'edit', post_id: post1.id }
|
|
|
|
}
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2017-10-02 12:00:43 +08:00
|
|
|
expect(messages.count).to eq(3)
|
|
|
|
end
|
|
|
|
|
2017-11-20 22:24:59 +08:00
|
|
|
it 'cleans up old users when requested' do
|
|
|
|
freeze_time Time.zone.now do
|
|
|
|
manager.add('topic', post1.topic.id, user2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Anything older than 20 seconds should be cleaned up
|
|
|
|
freeze_time 30.seconds.from_now do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { compose_state: 'open', action: 'reply', topic_id: post1.topic.id }, response_needed: true
|
|
|
|
}
|
|
|
|
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(data['users'].length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-10-02 12:00:43 +08:00
|
|
|
describe 'when post has been deleted' do
|
|
|
|
it 'should return an empty response' do
|
|
|
|
post1.destroy!
|
|
|
|
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { compose_state: 'open', action: 'edit', post_id: post1.id }
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(JSON.parse(response.body)).to eq({})
|
|
|
|
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
current: { compose_state: 'open', action: 'edit', post_id: post2.id },
|
|
|
|
previous: { compose_state: 'open', action: 'edit', post_id: post1.id }
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(JSON.parse(response.body)).to eq({})
|
|
|
|
end
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|