From 29ab5cebd4da1bc4d3393916201c42a2d70d7d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 12 Mar 2025 23:38:00 +0100 Subject: [PATCH] Don't access the database in direct message 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. Note that, in this case, in order to make the tests more readable, we're adding a bit of duplication. We should probably simplify these tests by moving most of the checks to mailer tests and then we could remove the duplication. The alternative would be to make the `create_direct_message` method way more complex than it is right now. --- spec/support/common_actions/emails.rb | 17 -------- spec/system/emails_spec.rb | 59 ++++++++++++++++++++------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/spec/support/common_actions/emails.rb b/spec/support/common_actions/emails.rb index ad4d91995..2e7173852 100644 --- a/spec/support/common_actions/emails.rb +++ b/spec/support/common_actions/emails.rb @@ -1,21 +1,4 @@ module Emails - def create_direct_message(sender, receiver) - login_as(sender) - visit user_path(receiver) - - click_link "Send private message" - - expect(page).to have_content "Send private message to #{receiver.name}" - - fill_in "direct_message_title", with: "Hey #{receiver.name}!" - fill_in "direct_message_body", with: "How are you doing? This is #{sender.name}" - - click_button "Send message" - - expect(page).to have_content "You message has been sent successfully." - DirectMessage.last - end - def fill_in_newsletter_form(options = {}) select (options[:segment_recipient] || "All users"), from: "Recipients" fill_in "Subject", with: options[:subject] || "This is a different subject" diff --git a/spec/system/emails_spec.rb b/spec/system/emails_spec.rb index f79cf50d6..d23b08e6d 100644 --- a/spec/system/emails_spec.rb +++ b/spec/system/emails_spec.rb @@ -86,33 +86,62 @@ describe "Emails" do context "Direct Message" do scenario "Receiver email" do - sender = create(:user, :level_two) - receiver = create(:user, :level_two) + sender = create(:user, :level_two, username: "John") + receiver = create(:user, :level_two, username: "Paul", subscriptions_token: "receiver_token") - direct_message = create_direct_message(sender, receiver) + login_as(sender) + visit user_path(receiver) + + click_link "Send private message" + + expect(page).to have_content "Send private message to Paul" + + fill_in "direct_message_title", with: "Hey!" + fill_in "direct_message_body", with: "How are you doing?" + + click_button "Send message" + + expect(page).to have_content "You message has been sent successfully." email = unread_emails_for(receiver.email).first - expect(email).to have_subject("You have received a new private message") - expect(email).to have_body_text(direct_message.title) - expect(email).to have_body_text(direct_message.body) - expect(email).to have_body_text(direct_message.sender.name) - expect(email).to have_body_text(user_path(direct_message.sender_id)) - expect(email).to have_body_text(edit_subscriptions_path(token: receiver.subscriptions_token)) + expect(email).to have_subject "You have received a new private message" + expect(email).to have_body_text "Hey!" + expect(email).to have_body_text "How are you doing?" + expect(email).to have_link "Reply to John", href: user_url( + sender, + host: Mailer.default_url_options[:host] + ) + expect(email).to have_link "Notifications", href: edit_subscriptions_url( + host: Mailer.default_url_options[:host], + token: "receiver_token" + ) end scenario "Sender email" do sender = create(:user, :level_two) - receiver = create(:user, :level_two) + receiver = create(:user, :level_two, username: "Keith") - direct_message = create_direct_message(sender, receiver) + login_as(sender) + visit user_path(receiver) + + click_link "Send private message" + + expect(page).to have_content "Send private message to Keith" + + fill_in "direct_message_title", with: "Hey!" + fill_in "direct_message_body", with: "How are you doing?" + + click_button "Send message" + + expect(page).to have_content "You message has been sent successfully." email = unread_emails_for(sender.email).first - expect(email).to have_subject("You have sent a new private message") - expect(email).to have_body_text(direct_message.title) - expect(email).to have_body_text(direct_message.body) - expect(email).to have_body_text(direct_message.receiver.name) + expect(email).to have_subject "You have sent a new private message" + expect(email).to have_body_text "Hey!" + expect(email).to have_body_text "How are you doing?" + expect(email).to have_body_text "You have sent a new private message to Keith" end end