From 1c2ec3f37e7ff8110101be8d0afecf05de99c302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 12 Mar 2025 23:48:29 +0100 Subject: [PATCH] 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. --- spec/support/common_actions/comments.rb | 6 ++-- spec/system/emails_spec.rb | 45 ++++++++++++++----------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/spec/support/common_actions/comments.rb b/spec/support/common_actions/comments.rb index 0a0f55d86..c6c7bcbe8 100644 --- a/spec/support/common_actions/comments.rb +++ b/spec/support/common_actions/comments.rb @@ -1,14 +1,14 @@ 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) visit polymorphic_path(comment.commentable) click_link "Reply" 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" end - expect(page).to have_content "It will be done next week." + expect(page).to have_content with end end diff --git a/spec/system/emails_spec.rb b/spec/system/emails_spec.rb index 8b7b504fe..abd911aa5 100644 --- a/spec/system/emails_spec.rb +++ b/spec/system/emails_spec.rb @@ -40,21 +40,25 @@ describe "Emails" do end context "Comment replies" do - let(:user) { create(:user, email_on_comment_reply: true) } - let(:debate) { create(:debate) } + let(:user) { create(:user, email_on_comment_reply: true, subscriptions_token: "commenter_token") } + let(:debate) { create(:debate, title: "Controversial topic") } let!(:comment) { create(:comment, commentable: debate, user: user) } scenario "Send email on comment reply" do - reply_to(comment) + reply_to(comment, with: "It will be done next week") email = open_last_email - expect(email).to have_subject("Someone has responded to your comment") - expect(email).to deliver_to(user) - 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("To unsubscribe from these emails, visit") - expect(email).to have_body_text(edit_subscriptions_path(token: user.subscriptions_token)) - expect(email).to have_body_text('and uncheck "Notify me by email when someone replies to my comments"') + expect(email).to have_subject "Someone has responded to your comment" + expect(email).to deliver_to user + expect(email).not_to have_body_text debate_path(debate) + expect(email).to have_body_text "It will be done next week" + expect(email).to have_link "Controversial topic" + expect(email).to have_body_text "To unsubscribe from these emails, visit" + 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 scenario "Do not send email about own replies to own comments" do @@ -312,20 +316,23 @@ describe "Emails" do context "Polls" do scenario "Send email on poll comment reply" do - user = create(:user, email_on_comment_reply: true) - poll = create(:poll, author: create(:user)) + user = create(:user, email_on_comment_reply: true, subscriptions_token: "user_token") + poll = create(:poll, author: create(:user), name: "Important questions") comment = create(:comment, commentable: poll, author: user) reply_to(comment) email = open_last_email - expect(email).to have_subject("Someone has responded to your comment") - expect(email).to deliver_to(user) - 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("To unsubscribe from these emails, visit") - expect(email).to have_body_text(edit_subscriptions_path(token: user.subscriptions_token)) - expect(email).to have_body_text('and uncheck "Notify me by email when someone replies to my comments"') + expect(email).to have_subject "Someone has responded to your comment" + expect(email).to deliver_to user + expect(email).not_to have_body_text poll_path(poll) + expect(email).to have_body_text "Important questions" + expect(email).to have_body_text "To unsubscribe from these emails, visit" + expect(email).to have_link "Notifications", href: edit_subscriptions_url( + 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