Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 041b58e

Browse files
authored
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.
1 parent 0350b46 commit 041b58e

File tree

2 files changed

+25
-43
lines changed

2 files changed

+25
-43
lines changed

lib/discourse_solved/first_accepted_post_solution_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def self.check(post, trust_level:)
1212

1313
!DiscourseSolved::SolvedTopic
1414
.joins(:answer_post)
15-
.where("posts.user_id = ?", post.user_id)
15+
.where("posts.user_id = ? AND posts.id != ?", post.user_id, post.id)
1616
.exists?
1717
end
1818
end

spec/lib/first_accepted_post_solution_validator_spec.rb

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,41 @@
33
describe DiscourseSolved::FirstAcceptedPostSolutionValidator do
44
fab!(:user_tl1) { Fabricate(:user, trust_level: TrustLevel[1], refresh_auto_groups: true) }
55

6-
context "when user is under max trust level" do
7-
context "with no post accepted yet" do
8-
it "validates the post" do
9-
post_1 = create_post(user: user_tl1)
10-
expect(described_class.check(post_1, trust_level: TrustLevel[2])).to eq(true)
11-
end
6+
context "when trust level is 'any'" do
7+
it "validates the post" do
8+
post = Fabricate(:post, user: user_tl1)
9+
DiscourseSolved.accept_answer!(post, Discourse.system_user)
10+
11+
expect(described_class.check(post, trust_level: "any")).to eq(true)
1212
end
1313

14-
context "with already had accepted posts" do
15-
before do
16-
accepted_post = create_post(user: user_tl1)
17-
DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user)
18-
end
14+
it "invalidates if post user already has an accepted post" do
15+
previously_accepted_post = Fabricate(:post, user: user_tl1)
16+
DiscourseSolved.accept_answer!(previously_accepted_post, Discourse.system_user)
1917

20-
it "doesn’t validate the post" do
21-
post_1 = create_post(user: user_tl1)
22-
expect(described_class.check(post_1, trust_level: TrustLevel[2])).to eq(false)
23-
end
18+
newly_accepted_post = Fabricate(:post, user: user_tl1)
19+
DiscourseSolved.accept_answer!(newly_accepted_post, Discourse.system_user)
20+
21+
expect(described_class.check(newly_accepted_post, trust_level: "any")).to eq(false)
2422
end
2523
end
2624

27-
context "when a user is above or equal max trust level" do
28-
context "with no post accepted yet" do
29-
it "doesn’t validate the post" do
30-
post_1 = create_post(user: user_tl1)
31-
expect(described_class.check(post_1, trust_level: TrustLevel[1])).to eq(false)
32-
end
33-
end
25+
context "with specified trust level that is not 'any'" do
26+
# the automation indicates "users under this Trust Level will trigger this automation"
3427

35-
context "when a post is already accepted" do
36-
before do
37-
accepted_post = create_post(user: user_tl1)
38-
DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user)
39-
end
28+
it "invalidates if the user is higher than or equal to the specified trust level" do
29+
post = Fabricate(:post, user: user_tl1)
30+
DiscourseSolved.accept_answer!(post, Discourse.system_user)
4031

41-
it "doesn’t validate the post" do
42-
post_1 = create_post(user: user_tl1)
43-
expect(described_class.check(post_1, trust_level: TrustLevel[1])).to eq(false)
44-
end
32+
expect(described_class.check(post, trust_level: TrustLevel[0])).to eq(false)
33+
expect(described_class.check(post, trust_level: TrustLevel[1])).to eq(false)
4534
end
46-
end
4735

48-
context "when using any trust level" do
49-
it "validates the post" do
50-
post_1 = create_post(user: user_tl1)
51-
expect(described_class.check(post_1, trust_level: "any")).to eq(true)
52-
end
36+
it "validates the post when user is under specified trust level" do
37+
post = Fabricate(:post, user: user_tl1)
38+
DiscourseSolved.accept_answer!(post, Discourse.system_user)
5339

54-
it "invalidates if post user already has an accepted post" do
55-
accepted_post = create_post(user: user_tl1)
56-
DiscourseSolved.accept_answer!(accepted_post, Discourse.system_user)
57-
post_1 = create_post(user: user_tl1)
58-
expect(described_class.check(post_1, trust_level: "any")).to eq(false)
40+
expect(described_class.check(post, trust_level: TrustLevel[2])).to eq(true)
5941
end
6042
end
6143

0 commit comments

Comments
 (0)