FIX: Exclude the first post itself when checking if an existing solution from the user exists (#378)

This commit makes sure to exclude the first solution when checking for an existing solution and has better tests -- the existing test does not accept the post prior to making the check.
This commit is contained in:
Natalie Tay 2025-07-02 11:45:14 +08:00 committed by GitHub
parent 0350b46bda
commit 041b58eed1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 50 deletions

View File

@ -12,7 +12,7 @@ module DiscourseSolved
!DiscourseSolved::SolvedTopic !DiscourseSolved::SolvedTopic
.joins(:answer_post) .joins(:answer_post)
.where("posts.user_id = ?", post.user_id) .where("posts.user_id = ? AND posts.id != ?", post.user_id, post.id)
.exists? .exists?
end end
end end

View File

@ -3,59 +3,41 @@
describe DiscourseSolved::FirstAcceptedPostSolutionValidator do describe DiscourseSolved::FirstAcceptedPostSolutionValidator do
fab!(:user_tl1) { Fabricate(:user, trust_level: TrustLevel[1], refresh_auto_groups: true) } fab!(:user_tl1) { Fabricate(:user, trust_level: TrustLevel[1], refresh_auto_groups: true) }
context "when user is under max trust level" do context "when trust level is 'any'" do
context "with no post accepted yet" do
it "validates the post" do it "validates the post" do
post_1 = create_post(user: user_tl1) post = Fabricate(:post, user: user_tl1)
expect(described_class.check(post_1, trust_level: TrustLevel[2])).to eq(true) DiscourseSolved.accept_answer!(post, Discourse.system_user)
end
end
context "with already had accepted posts" do expect(described_class.check(post, trust_level: "any")).to eq(true)
before do
accepted_post = create_post(user: user_tl1)
DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user)
end
it "doesnt validate the post" do
post_1 = create_post(user: user_tl1)
expect(described_class.check(post_1, trust_level: TrustLevel[2])).to eq(false)
end
end
end
context "when a user is above or equal max trust level" do
context "with no post accepted yet" do
it "doesnt validate the post" do
post_1 = create_post(user: user_tl1)
expect(described_class.check(post_1, trust_level: TrustLevel[1])).to eq(false)
end
end
context "when a post is already accepted" do
before do
accepted_post = create_post(user: user_tl1)
DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user)
end
it "doesnt validate the post" do
post_1 = create_post(user: user_tl1)
expect(described_class.check(post_1, trust_level: TrustLevel[1])).to eq(false)
end
end
end
context "when using any trust level" do
it "validates the post" do
post_1 = create_post(user: user_tl1)
expect(described_class.check(post_1, trust_level: "any")).to eq(true)
end end
it "invalidates if post user already has an accepted post" do it "invalidates if post user already has an accepted post" do
accepted_post = create_post(user: user_tl1) previously_accepted_post = Fabricate(:post, user: user_tl1)
DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user) DiscourseSolved.accept_answer!(previously_accepted_post, Discourse.system_user)
post_1 = create_post(user: user_tl1)
expect(described_class.check(post_1, trust_level: "any")).to eq(false) 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)
end
end
context "with specified trust level that is not 'any'" do
# the automation indicates "users under this Trust Level will trigger this automation"
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)
expect(described_class.check(post, trust_level: TrustLevel[0])).to eq(false)
expect(described_class.check(post, trust_level: TrustLevel[1])).to eq(false)
end
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)
expect(described_class.check(post, trust_level: TrustLevel[2])).to eq(true)
end end
end end