Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.check(post, trust_level:)

!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
Expand Down
66 changes: 24 additions & 42 deletions spec/lib/first_accepted_post_solution_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)
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 "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)

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
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 "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 "with specified trust level that is not 'any'" do
# the automation indicates "users under this Trust Level will trigger this automation"

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 "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)

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
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
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
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)

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)
expect(described_class.check(post, trust_level: TrustLevel[2])).to eq(true)
end
end

Expand Down