2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-07 03:40:18 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2017-10-02 00:00:43 -04:00
|
|
|
describe ::Presence::PresencesController do
|
2020-04-29 00:48:55 -04:00
|
|
|
describe '#handle_message' do
|
|
|
|
context 'when not logged in' do
|
|
|
|
it 'should raise the right error' do
|
|
|
|
post '/presence/publish.json'
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
context 'when logged in' do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:user2) { Fabricate(:user) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
fab!(:group) do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add(user)
|
|
|
|
group
|
|
|
|
end
|
2017-11-20 09:24:59 -05:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
fab!(:category) { Fabricate(:private_category, group: group) }
|
|
|
|
fab!(:private_topic) { Fabricate(:topic, category: category) }
|
|
|
|
fab!(:public_topic) { Fabricate(:topic, first_post: Fabricate(:post)) }
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
fab!(:private_message) do
|
|
|
|
Fabricate(:private_message_topic,
|
|
|
|
allowed_groups: [group]
|
|
|
|
)
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'returns the right response when user disables the presence feature' do
|
|
|
|
user.user_option.update_column(:hide_profile_and_presence, true)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
post '/presence/publish.json'
|
2018-10-10 13:00:08 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-10-09 17:18:44 -04:00
|
|
|
it 'returns the right response when user disables the presence feature and allow_users_to_hide_profile is disabled' do
|
|
|
|
user.user_option.update_column(:hide_profile_and_presence, true)
|
|
|
|
SiteSetting.allow_users_to_hide_profile = false
|
|
|
|
|
|
|
|
post '/presence/publish.json', params: { topic_id: public_topic.id, state: 'replying' }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'returns the right response when the presence site settings is disabled' do
|
|
|
|
SiteSetting.presence_enabled = false
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
post '/presence/publish.json'
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'returns the right response if required params are missing' do
|
|
|
|
post '/presence/publish.json'
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'returns the right response if topic_id is invalid' do
|
|
|
|
post '/presence/publish.json', params: { topic_id: -999, state: 'replying' }
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'returns the right response when user does not have access to the topic' do
|
|
|
|
group.remove(user)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
post '/presence/publish.json', params: { topic_id: private_topic.id, state: 'replying' }
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'returns the right response when an invalid state is provided with a post_id' do
|
2017-08-31 00:06:56 -04:00
|
|
|
post '/presence/publish.json', params: {
|
2020-04-29 00:48:55 -04:00
|
|
|
topic_id: public_topic.id,
|
|
|
|
post_id: public_topic.first_post.id,
|
|
|
|
state: 'some state'
|
2017-08-31 00:06:56 -04:00
|
|
|
}
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'returns the right response when user can not edit a post' do
|
|
|
|
Fabricate(:post, topic: private_topic, user: private_topic.user)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post '/presence/publish.json', params: {
|
2020-04-29 00:48:55 -04:00
|
|
|
topic_id: private_topic.id,
|
|
|
|
post_id: private_topic.first_post.id,
|
|
|
|
state: 'editing'
|
2017-08-31 00:06:56 -04:00
|
|
|
}
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'returns the right response when an invalid post_id is given' do
|
2017-08-31 00:06:56 -04:00
|
|
|
post '/presence/publish.json', params: {
|
2020-04-29 00:48:55 -04:00
|
|
|
topic_id: public_topic.id,
|
|
|
|
post_id: -9,
|
|
|
|
state: 'editing'
|
2017-08-31 00:06:56 -04:00
|
|
|
}
|
2020-04-29 00:48:55 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'publishes the right message for a public topic' do
|
|
|
|
freeze_time
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: { topic_id: public_topic.id, state: 'replying' }
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.channel).to eq("/presence/#{public_topic.id}")
|
|
|
|
expect(message.data.dig(:user, :id)).to eq(user.id)
|
|
|
|
expect(message.data[:published_at]).to eq(Time.zone.now.to_i)
|
|
|
|
expect(message.group_ids).to eq(nil)
|
|
|
|
expect(message.user_ids).to eq(nil)
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'publishes the right message for a restricted topic' do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: private_topic.id,
|
|
|
|
state: 'replying'
|
|
|
|
}
|
2017-10-02 00:00:43 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.channel).to eq("/presence/#{private_topic.id}")
|
|
|
|
expect(message.data.dig(:user, :id)).to eq(user.id)
|
|
|
|
expect(message.data[:published_at]).to eq(Time.zone.now.to_i)
|
|
|
|
expect(message.group_ids).to contain_exactly(group.id)
|
|
|
|
expect(message.user_ids).to eq(nil)
|
2017-11-20 09:24:59 -05:00
|
|
|
end
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'publishes the right message for a private message' do
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: private_message.id,
|
|
|
|
state: 'replying'
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
2017-11-20 09:24:59 -05:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(message.group_ids).to contain_exactly(
|
|
|
|
group.id,
|
|
|
|
Group::AUTO_GROUPS[:staff]
|
|
|
|
)
|
2017-11-20 09:24:59 -05:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(message.user_ids).to contain_exactly(
|
|
|
|
*private_message.topic_allowed_users.pluck(:user_id)
|
|
|
|
)
|
2017-11-20 09:24:59 -05:00
|
|
|
end
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'publishes the message to staff group when user is whispering' do
|
|
|
|
SiteSetting.enable_whispers = true
|
2017-11-20 09:24:59 -05:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: public_topic.id,
|
|
|
|
state: 'replying',
|
|
|
|
is_whisper: true
|
|
|
|
}
|
2017-10-02 00:00:43 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2017-10-02 00:00:43 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(messages.length).to eq(1)
|
2017-10-02 00:00:43 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
message = messages.first
|
2017-10-02 00:00:43 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])
|
|
|
|
expect(message.user_ids).to eq(nil)
|
2017-10-02 00:00:43 -04:00
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-05-13 02:07:54 -04:00
|
|
|
it 'publishes the message to staff group when staff_only param override is present' do
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: public_topic.id,
|
|
|
|
state: 'replying',
|
|
|
|
staff_only: true
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])
|
|
|
|
expect(message.user_ids).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'publishes the message to staff group when a staff is editing a whisper' do
|
|
|
|
SiteSetting.enable_whispers = true
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: public_topic.id,
|
|
|
|
post_id: public_topic.first_post.id,
|
|
|
|
state: 'editing',
|
|
|
|
is_whisper: true
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])
|
|
|
|
expect(message.user_ids).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'publishes the message to staff group when a staff is editing a locked post' do
|
|
|
|
SiteSetting.enable_whispers = true
|
|
|
|
sign_in(admin)
|
|
|
|
locked_post = Fabricate(:post, topic: public_topic, locked_by_id: admin.id)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: public_topic.id,
|
|
|
|
post_id: locked_post.id,
|
|
|
|
state: 'editing',
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])
|
|
|
|
expect(message.user_ids).to eq(nil)
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
|
2020-04-29 00:48:55 -04:00
|
|
|
it 'publishes the message to author, staff group and TL4 group when editing a public post' do
|
|
|
|
post = Fabricate(:post, topic: public_topic, user: user)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: public_topic.id,
|
|
|
|
post_id: post.id,
|
|
|
|
state: 'editing',
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(
|
|
|
|
Group::AUTO_GROUPS[:trust_level_4],
|
|
|
|
Group::AUTO_GROUPS[:staff]
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(message.user_ids).to contain_exactly(user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'publishes the message to author and staff group when editing a public post ' \
|
|
|
|
'if SiteSettings.trusted_users_can_edit_others is set to false' do
|
|
|
|
|
|
|
|
post = Fabricate(:post, topic: public_topic, user: user)
|
|
|
|
SiteSetting.trusted_users_can_edit_others = false
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: public_topic.id,
|
|
|
|
post_id: post.id,
|
|
|
|
state: 'editing',
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])
|
|
|
|
expect(message.user_ids).to contain_exactly(user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'publishes the message to SiteSetting.min_trust_to_edit_wiki_post group ' \
|
|
|
|
'and staff group when editing a wiki in a public topic' do
|
|
|
|
|
|
|
|
post = Fabricate(:post, topic: public_topic, user: user, wiki: true)
|
|
|
|
SiteSetting.min_trust_to_edit_wiki_post = TrustLevel.levels[:basic]
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: public_topic.id,
|
|
|
|
post_id: post.id,
|
|
|
|
state: 'editing',
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(
|
|
|
|
Group::AUTO_GROUPS[:trust_level_1],
|
|
|
|
Group::AUTO_GROUPS[:staff]
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(message.user_ids).to contain_exactly(user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'publishes the message to author and staff group when editing a private message' do
|
|
|
|
post = Fabricate(:post, topic: private_message, user: user)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: private_message.id,
|
|
|
|
post_id: post.id,
|
|
|
|
state: 'editing',
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(
|
|
|
|
Group::AUTO_GROUPS[:staff],
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(message.user_ids).to contain_exactly(user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'publishes the message to users with trust levels of SiteSetting.min_trust_to_edit_wiki_post ' \
|
|
|
|
'and staff group when editing a wiki in a private message' do
|
|
|
|
|
|
|
|
post = Fabricate(:post,
|
|
|
|
topic: private_message,
|
|
|
|
user: private_message.user,
|
|
|
|
wiki: true
|
|
|
|
)
|
|
|
|
|
|
|
|
user2.update!(trust_level: TrustLevel.levels[:newuser])
|
|
|
|
group.add(user2)
|
|
|
|
|
|
|
|
SiteSetting.min_trust_to_edit_wiki_post = TrustLevel.levels[:basic]
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: private_message.id,
|
|
|
|
post_id: post.id,
|
|
|
|
state: 'editing',
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(
|
|
|
|
Group::AUTO_GROUPS[:staff],
|
|
|
|
group.id
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(message.user_ids).to contain_exactly(
|
|
|
|
*private_message.allowed_users.pluck(:id)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
it 'publishes the right message when closing composer in public topic' do
|
2020-04-29 00:48:55 -04:00
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: public_topic.id,
|
|
|
|
state: described_class::CLOSED_STATE,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to eq(nil)
|
|
|
|
expect(message.user_ids).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
it 'publishes the right message when closing composer in private topic' do
|
2020-04-29 00:48:55 -04:00
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: private_topic.id,
|
|
|
|
state: described_class::CLOSED_STATE,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(group.id)
|
|
|
|
expect(message.user_ids).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
it 'publishes the right message when closing composer in private message' do
|
2020-04-29 00:48:55 -04:00
|
|
|
post = Fabricate(:post, topic: private_message, user: user)
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
post '/presence/publish.json', params: {
|
|
|
|
topic_id: private_message.id,
|
|
|
|
state: described_class::CLOSED_STATE,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.length).to eq(1)
|
|
|
|
|
|
|
|
message = messages.first
|
|
|
|
|
|
|
|
expect(message.group_ids).to contain_exactly(
|
|
|
|
Group::AUTO_GROUPS[:staff],
|
|
|
|
group.id
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(message.user_ids).to contain_exactly(
|
|
|
|
*private_message.allowed_users.pluck(:id)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|