From 041b58eed15e4b000f7c4d51025fc24f99bef5e2 Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Wed, 2 Jul 2025 11:45:14 +0800 Subject: [PATCH] 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. --- .../first_accepted_post_solution_validator.rb | 2 +- ...t_accepted_post_solution_validator_spec.rb | 80 +++++++------------ 2 files changed, 32 insertions(+), 50 deletions(-) diff --git a/lib/discourse_solved/first_accepted_post_solution_validator.rb b/lib/discourse_solved/first_accepted_post_solution_validator.rb index 62c8fb6..ae2b4b4 100644 --- a/lib/discourse_solved/first_accepted_post_solution_validator.rb +++ b/lib/discourse_solved/first_accepted_post_solution_validator.rb @@ -12,7 +12,7 @@ module DiscourseSolved !DiscourseSolved::SolvedTopic .joins(:answer_post) - .where("posts.user_id = ?", post.user_id) + .where("posts.user_id = ? AND posts.id != ?", post.user_id, post.id) .exists? end end diff --git a/spec/lib/first_accepted_post_solution_validator_spec.rb b/spec/lib/first_accepted_post_solution_validator_spec.rb index db80a18..87bd027 100644 --- a/spec/lib/first_accepted_post_solution_validator_spec.rb +++ b/spec/lib/first_accepted_post_solution_validator_spec.rb @@ -3,59 +3,41 @@ describe DiscourseSolved::FirstAcceptedPostSolutionValidator do fab!(:user_tl1) { Fabricate(:user, trust_level: TrustLevel[1], refresh_auto_groups: true) } - context "when user is under max trust level" do - context "with no post accepted yet" do - it "validates the post" do - post_1 = create_post(user: user_tl1) - expect(described_class.check(post_1, trust_level: TrustLevel[2])).to eq(true) - end - end - - context "with already had accepted posts" do - before do - accepted_post = create_post(user: user_tl1) - DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user) - end - - it "doesn’t 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 "doesn’t 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 "doesn’t 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 + context "when trust level is 'any'" 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) + post = Fabricate(:post, user: user_tl1) + DiscourseSolved.accept_answer!(post, Discourse.system_user) + + expect(described_class.check(post, trust_level: "any")).to eq(true) end it "invalidates if post user already has an accepted post" do - accepted_post = create_post(user: user_tl1) - DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user) - post_1 = create_post(user: user_tl1) - expect(described_class.check(post_1, trust_level: "any")).to eq(false) + previously_accepted_post = Fabricate(:post, user: user_tl1) + DiscourseSolved.accept_answer!(previously_accepted_post, Discourse.system_user) + + 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