Don't access the database in comment replies tests

As mentioned in commits like a586ba806, a7664ad81, 006128da5, b41fbfa52
and c480cdd91, accessing the database after starting the browser with
the `visit` method sometimes results in database corruption and failing
tests on our CI due to the process running the test accessing the
database after the process running the browser has started.

In this case, we're avoiding the usage of `user.subscriptions_token` and
`Comment.last`. In the future, we should probably simplify these tests
by moving most of the checks to a mailer test.
This commit is contained in:
Javi Martín
2025-03-12 23:48:29 +01:00
parent 538958de3f
commit 1c2ec3f37e
2 changed files with 29 additions and 22 deletions

View File

@@ -1,14 +1,14 @@
module Comments module Comments
def reply_to(comment, replier: create(:user)) def reply_to(comment, with: "I like what you say", replier: create(:user))
login_as(replier) login_as(replier)
visit polymorphic_path(comment.commentable) visit polymorphic_path(comment.commentable)
click_link "Reply" click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do within "#js-comment-form-comment_#{comment.id}" do
fill_in "Leave your comment", with: "It will be done next week." fill_in "Leave your comment", with: with
click_button "Publish reply" click_button "Publish reply"
end end
expect(page).to have_content "It will be done next week." expect(page).to have_content with
end end
end end

View File

@@ -40,21 +40,25 @@ describe "Emails" do
end end
context "Comment replies" do context "Comment replies" do
let(:user) { create(:user, email_on_comment_reply: true) } let(:user) { create(:user, email_on_comment_reply: true, subscriptions_token: "commenter_token") }
let(:debate) { create(:debate) } let(:debate) { create(:debate, title: "Controversial topic") }
let!(:comment) { create(:comment, commentable: debate, user: user) } let!(:comment) { create(:comment, commentable: debate, user: user) }
scenario "Send email on comment reply" do scenario "Send email on comment reply" do
reply_to(comment) reply_to(comment, with: "It will be done next week")
email = open_last_email email = open_last_email
expect(email).to have_subject("Someone has responded to your comment") expect(email).to have_subject "Someone has responded to your comment"
expect(email).to deliver_to(user) expect(email).to deliver_to user
expect(email).not_to have_body_text(debate_path(debate)) expect(email).not_to have_body_text debate_path(debate)
expect(email).to have_body_text(comment_path(Comment.last)) expect(email).to have_body_text "It will be done next week"
expect(email).to have_body_text("To unsubscribe from these emails, visit") expect(email).to have_link "Controversial topic"
expect(email).to have_body_text(edit_subscriptions_path(token: user.subscriptions_token)) expect(email).to have_body_text "To unsubscribe from these emails, visit"
expect(email).to have_body_text('and uncheck "Notify me by email when someone replies to my comments"') expect(email).to have_link "Notifications", href: edit_subscriptions_url(
host: Mailer.default_url_options[:host],
token: "commenter_token"
)
expect(email).to have_body_text 'and uncheck "Notify me by email when someone replies to my comments"'
end end
scenario "Do not send email about own replies to own comments" do scenario "Do not send email about own replies to own comments" do
@@ -312,20 +316,23 @@ describe "Emails" do
context "Polls" do context "Polls" do
scenario "Send email on poll comment reply" do scenario "Send email on poll comment reply" do
user = create(:user, email_on_comment_reply: true) user = create(:user, email_on_comment_reply: true, subscriptions_token: "user_token")
poll = create(:poll, author: create(:user)) poll = create(:poll, author: create(:user), name: "Important questions")
comment = create(:comment, commentable: poll, author: user) comment = create(:comment, commentable: poll, author: user)
reply_to(comment) reply_to(comment)
email = open_last_email email = open_last_email
expect(email).to have_subject("Someone has responded to your comment") expect(email).to have_subject "Someone has responded to your comment"
expect(email).to deliver_to(user) expect(email).to deliver_to user
expect(email).not_to have_body_text(poll_path(poll)) expect(email).not_to have_body_text poll_path(poll)
expect(email).to have_body_text(comment_path(Comment.last)) expect(email).to have_body_text "Important questions"
expect(email).to have_body_text("To unsubscribe from these emails, visit") expect(email).to have_body_text "To unsubscribe from these emails, visit"
expect(email).to have_body_text(edit_subscriptions_path(token: user.subscriptions_token)) expect(email).to have_link "Notifications", href: edit_subscriptions_url(
expect(email).to have_body_text('and uncheck "Notify me by email when someone replies to my comments"') host: Mailer.default_url_options[:host],
token: "user_token"
)
expect(email).to have_body_text 'and uncheck "Notify me by email when someone replies to my comments"'
end end
end end