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.
This commit is contained in:
Javi Martín
2025-03-12 23:38:00 +01:00
parent 1b8a079727
commit 29ab5cebd4
2 changed files with 44 additions and 32 deletions

View File

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

View File

@@ -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 <strong>Keith</strong>"
end
end