2019-05-08 22:20:51 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe ReviewableClaimedTopicsController do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:moderator)
|
2019-05-08 22:20:51 +08:00
|
|
|
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:topic)
|
2020-02-22 01:11:50 +08:00
|
|
|
fab!(:reviewable) { Fabricate(:reviewable_flagged_post, topic: topic) }
|
|
|
|
|
2019-05-08 22:20:51 +08:00
|
|
|
describe "#create" do
|
2020-02-22 01:11:50 +08:00
|
|
|
let(:params) { { reviewable_claimed_topic: { topic_id: topic.id } } }
|
2019-05-08 22:20:51 +08:00
|
|
|
|
2020-02-22 01:11:50 +08:00
|
|
|
it "requires user to be logged in" do
|
2019-05-08 22:20:51 +08:00
|
|
|
post "/reviewable_claimed_topics.json", params: params
|
2020-02-22 01:11:50 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
2019-05-08 22:20:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in" do
|
|
|
|
before do
|
2022-12-01 07:09:57 +08:00
|
|
|
SiteSetting.reviewable_claiming = "optional"
|
2019-05-08 22:20:51 +08:00
|
|
|
sign_in(moderator)
|
|
|
|
end
|
|
|
|
|
2020-02-22 01:11:50 +08:00
|
|
|
it "works" do
|
2022-11-25 08:07:29 +08:00
|
|
|
messages =
|
|
|
|
MessageBus.track_publish("/reviewable_claimed") do
|
|
|
|
post "/reviewable_claimed_topics.json", params: params
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2019-05-08 22:20:51 +08:00
|
|
|
expect(
|
|
|
|
ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: topic.id).exists?,
|
|
|
|
).to eq(true)
|
2020-02-11 07:40:01 +08:00
|
|
|
expect(
|
|
|
|
topic
|
|
|
|
.reviewables
|
|
|
|
.first
|
|
|
|
.history
|
|
|
|
.where(reviewable_history_type: ReviewableHistory.types[:claimed])
|
|
|
|
.size,
|
|
|
|
).to eq(1)
|
2020-02-22 01:11:50 +08:00
|
|
|
expect(messages.size).to eq(1)
|
2022-11-25 08:07:29 +08:00
|
|
|
|
|
|
|
message = messages[0]
|
|
|
|
|
|
|
|
expect(message.data[:topic_id]).to eq(topic.id)
|
|
|
|
expect(message.data[:user][:id]).to eq(moderator.id)
|
|
|
|
expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "publishes reviewable claimed changes to the category moderators of the topic's category" do
|
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
SiteSetting.reviewable_claiming = "optional"
|
|
|
|
|
|
|
|
group = Fabricate(:group)
|
2024-09-04 09:38:46 +08:00
|
|
|
Fabricate(:category_moderation_group, category: topic.category, group:)
|
2022-11-25 08:07:29 +08:00
|
|
|
|
|
|
|
messages =
|
|
|
|
MessageBus.track_publish("/reviewable_claimed") do
|
|
|
|
post "/reviewable_claimed_topics.json", params: params
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.size).to eq(1)
|
|
|
|
|
|
|
|
message = messages[0]
|
|
|
|
|
|
|
|
expect(message.data[:topic_id]).to eq(topic.id)
|
|
|
|
expect(message.data[:user][:id]).to eq(moderator.id)
|
|
|
|
expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff], group.id)
|
2019-05-08 22:20:51 +08:00
|
|
|
end
|
2020-01-10 01:32:05 +08:00
|
|
|
|
2020-02-25 21:49:23 +08:00
|
|
|
it "works with deleted topics" do
|
|
|
|
first_post = topic.first_post || Fabricate(:post, topic: topic)
|
|
|
|
PostDestroyer.new(Discourse.system_user, first_post).destroy
|
|
|
|
|
|
|
|
post "/reviewable_claimed_topics.json", params: params
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(
|
|
|
|
ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: topic.id).exists?,
|
|
|
|
).to eq(true)
|
|
|
|
end
|
|
|
|
|
2020-02-22 01:11:50 +08:00
|
|
|
it "raises an error if user cannot claim the topic" do
|
2022-12-01 07:09:57 +08:00
|
|
|
SiteSetting.reviewable_claiming = "disabled"
|
2020-02-22 01:11:50 +08:00
|
|
|
post "/reviewable_claimed_topics.json", params: params
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error if topic is already claimed" do
|
2020-01-10 01:32:05 +08:00
|
|
|
post "/reviewable_claimed_topics.json", params: params
|
|
|
|
expect(
|
|
|
|
ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: topic.id).exists?,
|
|
|
|
).to eq(true)
|
2020-02-22 01:11:50 +08:00
|
|
|
|
2020-01-10 01:32:05 +08:00
|
|
|
post "/reviewable_claimed_topics.json", params: params
|
2020-02-22 01:11:50 +08:00
|
|
|
expect(response.status).to eq(409)
|
2020-01-10 01:32:05 +08:00
|
|
|
end
|
2022-12-01 07:09:57 +08:00
|
|
|
|
|
|
|
it "queues a sidekiq job to refresh reviewable counts for users who can see the reviewable" do
|
2022-12-08 09:44:29 +08:00
|
|
|
SiteSetting.navigation_menu = "sidebar"
|
2022-12-01 07:09:57 +08:00
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
|
|
|
|
not_notified = Fabricate(:user)
|
|
|
|
|
|
|
|
group = Fabricate(:group)
|
2024-09-04 09:38:46 +08:00
|
|
|
Fabricate(:category_moderation_group, category: topic.category, group:)
|
2022-12-01 07:09:57 +08:00
|
|
|
|
|
|
|
notified = Fabricate(:user)
|
|
|
|
group.add(notified)
|
|
|
|
|
|
|
|
expect_enqueued_with(
|
|
|
|
job: :refresh_users_reviewable_counts,
|
|
|
|
args: {
|
|
|
|
group_ids: [Group::AUTO_GROUPS[:staff], group.id],
|
|
|
|
},
|
|
|
|
) do
|
|
|
|
post "/reviewable_claimed_topics.json", params: params
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
2019-05-08 22:20:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#destroy" do
|
2020-02-11 07:40:01 +08:00
|
|
|
fab!(:claimed) { Fabricate(:reviewable_claimed_topic, topic: topic) }
|
2019-05-08 22:20:51 +08:00
|
|
|
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
2020-02-22 01:11:50 +08:00
|
|
|
it "works" do
|
|
|
|
SiteSetting.reviewable_claiming = "optional"
|
|
|
|
|
2022-11-25 08:07:29 +08:00
|
|
|
messages =
|
|
|
|
MessageBus.track_publish("/reviewable_claimed") do
|
|
|
|
delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2020-02-22 01:11:50 +08:00
|
|
|
|
|
|
|
expect(ReviewableClaimedTopic.where(topic_id: claimed.topic_id).exists?).to eq(false)
|
|
|
|
expect(
|
|
|
|
topic
|
|
|
|
.reviewables
|
|
|
|
.first
|
|
|
|
.history
|
|
|
|
.where(reviewable_history_type: ReviewableHistory.types[:unclaimed])
|
|
|
|
.size,
|
|
|
|
).to eq(1)
|
|
|
|
expect(messages.size).to eq(1)
|
2022-11-25 08:07:29 +08:00
|
|
|
|
|
|
|
message = messages[0]
|
|
|
|
|
|
|
|
expect(message.data[:topic_id]).to eq(topic.id)
|
|
|
|
expect(message.data[:user]).to eq(nil)
|
|
|
|
expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])
|
2019-05-08 22:20:51 +08:00
|
|
|
end
|
|
|
|
|
2020-02-25 21:49:23 +08:00
|
|
|
it "works with deleted topics" do
|
|
|
|
SiteSetting.reviewable_claiming = "optional"
|
|
|
|
first_post = topic.first_post || Fabricate(:post, topic: topic)
|
|
|
|
PostDestroyer.new(Discourse.system_user, first_post).destroy
|
|
|
|
|
|
|
|
delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: topic.id).exists?).to eq(
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-02-22 01:11:50 +08:00
|
|
|
it "raises an error if topic is missing" do
|
|
|
|
delete "/reviewable_claimed_topics/111111111.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
2019-05-08 22:20:51 +08:00
|
|
|
end
|
|
|
|
|
2020-02-22 01:11:50 +08:00
|
|
|
it "raises an error if user cannot claim the topic" do
|
2019-05-08 22:20:51 +08:00
|
|
|
delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
|
2020-02-22 01:11:50 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
2019-05-08 22:20:51 +08:00
|
|
|
end
|
2022-12-01 07:09:57 +08:00
|
|
|
|
|
|
|
it "queues a sidekiq job to refresh reviewable counts for users who can see the reviewable" do
|
|
|
|
SiteSetting.reviewable_claiming = "optional"
|
2022-12-08 09:44:29 +08:00
|
|
|
SiteSetting.navigation_menu = "sidebar"
|
2022-12-01 07:09:57 +08:00
|
|
|
SiteSetting.enable_category_group_moderation = true
|
|
|
|
|
|
|
|
not_notified = Fabricate(:user)
|
|
|
|
|
|
|
|
group = Fabricate(:group)
|
2024-09-04 09:38:46 +08:00
|
|
|
Fabricate(:category_moderation_group, category: topic.category, group:)
|
2022-12-01 07:09:57 +08:00
|
|
|
|
|
|
|
notified = Fabricate(:user)
|
|
|
|
group.add(notified)
|
|
|
|
|
|
|
|
expect_enqueued_with(
|
|
|
|
job: :refresh_users_reviewable_counts,
|
|
|
|
args: {
|
|
|
|
group_ids: [Group::AUTO_GROUPS[:staff], group.id],
|
|
|
|
},
|
|
|
|
) do
|
|
|
|
delete "/reviewable_claimed_topics/#{claimed.topic_id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
2019-05-08 22:20:51 +08:00
|
|
|
end
|
|
|
|
end
|