2021-11-12 16:00:48 +01:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
2025-03-21 11:45:19 +08:00
|
|
|
|
describe DiscourseSolved::FirstAcceptedPostSolutionValidator do
|
2023-12-14 13:28:50 +11:00
|
|
|
|
fab!(:user_tl1) { Fabricate(:user, trust_level: TrustLevel[1], refresh_auto_groups: true) }
|
2021-11-12 16:00:48 +01:00
|
|
|
|
|
2025-07-02 11:45:14 +08:00
|
|
|
|
context "when trust level is 'any'" do
|
|
|
|
|
|
it "validates the post" do
|
|
|
|
|
|
post = Fabricate(:post, user: user_tl1)
|
|
|
|
|
|
DiscourseSolved.accept_answer!(post, Discourse.system_user)
|
|
|
|
|
|
|
|
|
|
|
|
expect(described_class.check(post, trust_level: "any")).to eq(true)
|
2021-11-12 16:00:48 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-07-02 11:45:14 +08:00
|
|
|
|
it "invalidates if post user already has an accepted post" do
|
|
|
|
|
|
previously_accepted_post = Fabricate(:post, user: user_tl1)
|
|
|
|
|
|
DiscourseSolved.accept_answer!(previously_accepted_post, Discourse.system_user)
|
2021-11-12 16:00:48 +01:00
|
|
|
|
|
2025-07-02 11:45:14 +08:00
|
|
|
|
newly_accepted_post = Fabricate(:post, user: user_tl1)
|
|
|
|
|
|
DiscourseSolved.accept_answer!(newly_accepted_post, Discourse.system_user)
|
|
|
|
|
|
|
|
|
|
|
|
expect(described_class.check(newly_accepted_post, trust_level: "any")).to eq(false)
|
2021-11-12 16:00:48 +01:00
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-07-02 11:45:14 +08:00
|
|
|
|
context "with specified trust level that is not 'any'" do
|
|
|
|
|
|
# the automation indicates "users under this Trust Level will trigger this automation"
|
2021-11-12 16:00:48 +01:00
|
|
|
|
|
2025-07-02 11:45:14 +08:00
|
|
|
|
it "invalidates if the user is higher than or equal to the specified trust level" do
|
|
|
|
|
|
post = Fabricate(:post, user: user_tl1)
|
|
|
|
|
|
DiscourseSolved.accept_answer!(post, Discourse.system_user)
|
2021-11-12 16:00:48 +01:00
|
|
|
|
|
2025-07-02 11:45:14 +08:00
|
|
|
|
expect(described_class.check(post, trust_level: TrustLevel[0])).to eq(false)
|
|
|
|
|
|
expect(described_class.check(post, trust_level: TrustLevel[1])).to eq(false)
|
2021-11-12 16:00:48 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-07-02 11:45:14 +08:00
|
|
|
|
it "validates the post when user is under specified trust level" do
|
|
|
|
|
|
post = Fabricate(:post, user: user_tl1)
|
|
|
|
|
|
DiscourseSolved.accept_answer!(post, Discourse.system_user)
|
2025-06-30 15:40:16 +08:00
|
|
|
|
|
2025-07-02 11:45:14 +08:00
|
|
|
|
expect(described_class.check(post, trust_level: TrustLevel[2])).to eq(true)
|
2025-06-30 15:40:16 +08:00
|
|
|
|
end
|
2021-11-12 16:00:48 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
2022-12-23 20:36:08 +00:00
|
|
|
|
context "when user is system" do
|
|
|
|
|
|
it "doesn’t validate the post" do
|
2021-11-12 16:00:48 +01:00
|
|
|
|
post_1 = create_post(user: Discourse.system_user)
|
2022-12-23 20:36:08 +00:00
|
|
|
|
expect(described_class.check(post_1, trust_level: "any")).to eq(false)
|
2021-11-12 16:00:48 +01:00
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2022-12-23 20:36:08 +00:00
|
|
|
|
context "when post is a PM" do
|
|
|
|
|
|
it "doesn’t validate the post" do
|
2022-10-04 14:19:45 +10:00
|
|
|
|
Group.refresh_automatic_groups!
|
2022-12-23 20:36:08 +00:00
|
|
|
|
post_1 =
|
|
|
|
|
|
create_post(
|
|
|
|
|
|
user: user_tl1,
|
|
|
|
|
|
target_usernames: [user_tl1.username],
|
|
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
|
|
)
|
|
|
|
|
|
expect(described_class.check(post_1, trust_level: "any")).to eq(false)
|
2021-11-12 16:00:48 +01:00
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|