Don't hide records during a system test

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 were hiding a proposal after starting the process
running the browser to check what happens when accessing a notification
for a hidden proposal. We can avoid database access in the middle of the
test by hidding a proposal before starting the browser. The process to
create a notification using the browser is already tested in other
specs, so we don't need to do it here as well.

Note that, to simplify the test, we're extracting the `notify_users`
method. I wonder whether this method should be called in an
`after_create` callback instead... That's a topic for another time,
though.
This commit is contained in:
Javi Martín
2025-03-14 15:43:18 +01:00
parent 92fc45cbb4
commit 9a7681b75f
3 changed files with 11 additions and 17 deletions

View File

@@ -11,9 +11,8 @@ class ProposalNotificationsController < ApplicationController
@notification = ProposalNotification.new(proposal_notification_params) @notification = ProposalNotification.new(proposal_notification_params)
@proposal = Proposal.find(proposal_notification_params[:proposal_id]) @proposal = Proposal.find(proposal_notification_params[:proposal_id])
if @notification.save if @notification.save
@proposal.users_to_notify.each do |user| @proposal.notify_users(@notification)
Notification.add(user, @notification)
end
redirect_to @notification, notice: I18n.t("flash.actions.create.proposal_notification") redirect_to @notification, notice: I18n.t("flash.actions.create.proposal_notification")
else else
render :new render :new

View File

@@ -232,6 +232,12 @@ class Proposal < ApplicationRecord
followers - [author] followers - [author]
end end
def notify_users(notification)
users_to_notify.each do |user|
Notification.add(user, notification)
end
end
def self.proposals_orders(user) def self.proposals_orders(user)
orders = %w[hot_score confidence_score created_at relevance archival_date] orders = %w[hot_score confidence_score created_at relevance archival_date]

View File

@@ -280,25 +280,14 @@ describe "Proposal Notifications" do
expect(page).to have_css ".notification", count: 0 expect(page).to have_css ".notification", count: 0
end end
scenario "Proposal hidden" do scenario "Hidden proposal" do
author = create(:user) author = create(:user)
user = create(:user) user = create(:user)
proposal = create(:proposal, author: author, voters: [user], followers: [user]) proposal = create(:proposal, author: author, voters: [user], followers: [user])
notification = create(:proposal_notification, author: author, proposal: proposal)
login_as(author) proposal.notify_users(notification)
visit new_proposal_notification_path(proposal_id: proposal.id)
fill_in "proposal_notification_title", with: "Thank you for supporting my proposal"
fill_in "proposal_notification_body", with: "Please share it with " \
"others so we can make it happen!"
click_button "Send notification"
expect(page).to have_content "Your message has been sent correctly."
proposal.hide proposal.hide
logout
login_as user login_as user
visit root_path visit root_path