Split some system tests checking the database

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.

IMHO this is also a bad practice for system tests, since these tests
should be checking what users experience.

In these cases, however, I haven't been able to test the user
experience. For example, it looks like failed census calls for
unregistered users aren't displayed anywhere and can only be accessed by
manually checking the database. Similarly, there's no interface showing
that all the options from a poll have been deleted (which makes sense,
since we only display options in the context of their poll) or a place
showing the responsible name for a proposal.

So we're splitting the tests in two, with the controller test running
the database checks.
This commit is contained in:
Javi Martín
2025-03-12 17:13:41 +01:00
parent 990f9e9664
commit a28967817e
10 changed files with 83 additions and 21 deletions

View File

@@ -0,0 +1,16 @@
require "rails_helper"
describe Admin::AdminNotificationsController, :admin do
describe "POST deliver" do
it "sends notifications to every recipient" do
2.times { create(:user) }
notification = create(:admin_notification, segment_recipient: :all_users)
post :deliver, params: { id: notification }
User.find_each do |user|
expect(user.notifications.count).to eq 1
end
end
end
end

View File

@@ -8,4 +8,17 @@ describe Admin::Poll::PollsController, :admin do
expect { get :index }.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
describe "DELETE destroy" do
it "deletes every question and every option from that poll" do
poll = create(:poll, name: "Do you support CONSUL?")
create(:poll_question, :yes_no, poll: poll)
delete :destroy, params: { id: poll }
expect(Poll.count).to eq 0
expect(Poll::Question.count).to eq 0
expect(Poll::Question::Option.count).to eq 0
end
end
end

View File

@@ -0,0 +1,22 @@
require "rails_helper"
describe Officing::ResidenceController do
describe "POST create" do
it "creates a failed census called when the census data is invalid" do
officer = create(:poll_officer)
create(:poll_officer_assignment, officer: officer)
sign_in(officer.user)
expect do
post :create, params: {
residence: { document_type: "1", document_number: "23456789A", year_of_birth: "1980" }
}
end.to change { FailedCensusCall.count }.by(1)
failed_census_call = FailedCensusCall.last
expect(failed_census_call.poll_officer).to eq officer
expect(officer.failed_census_calls).to eq [failed_census_call]
end
end
end

View File

@@ -20,6 +20,7 @@ describe Officing::VotersController do
end.each(&:join)
expect(Poll::Voter.count).to eq 1
expect(Poll::Voter.last.officer_id).to eq(officer.id)
end
end
end

View File

@@ -31,4 +31,29 @@ describe ProposalsController do
expect(other_proposal.reload.map_location).not_to be nil
end
end
describe "POST create" do
before { InvisibleCaptcha.timestamp_enabled = false }
after { InvisibleCaptcha.timestamp_enabled = true }
it "assigns the responsible name to the proposal" do
sign_in(create(:user, document_number: "13572468A"))
post :create, params: {
proposal: {
translations_attributes: {
"0" => {
locale: "en",
title: "I'm responsible",
summary: "I have a document number",
description: "But you won't see my document number"
}
},
terms_of_service: "1"
}
}
expect(Proposal.last.responsible_name).to eq "13572468A"
end
end
end

View File

@@ -193,10 +193,7 @@ describe "Admin Notifications", :admin do
accept_confirm { click_button "Send notification" }
expect(page).to have_content "Notification sent successfully"
User.find_each do |user|
expect(user.notifications.count).to eq(1)
end
expect(page).to have_content "3 users got notified"
end
scenario "A sent Admin notification can not be sent" do

View File

@@ -129,11 +129,8 @@ describe "Admin polls", :admin do
accept_confirm { click_button "Delete" }
end
expect(page).to have_content("Poll deleted successfully")
expect(page).not_to have_content("Do you support CONSUL?")
expect(Poll::Question.count).to eq(0)
expect(Poll::Question::Option.count).to eq(0)
expect(page).to have_content "Poll deleted successfully"
expect(page).not_to have_content "Do you support CONSUL?"
end
scenario "Can destroy polls with options including videos" do

View File

@@ -53,7 +53,6 @@ describe "Residence", :with_frozen_time do
end
scenario "Error on Census (document number)" do
initial_failed_census_calls_count = officer.failed_census_calls_count
within("#side_menu") do
click_link "Validate document"
end
@@ -65,13 +64,6 @@ describe "Residence", :with_frozen_time do
click_button "Validate document"
expect(page).to have_content "The Census was unable to verify this document"
officer.reload
fcc = FailedCensusCall.last
expect(fcc).to be
expect(fcc.poll_officer).to eq(officer)
expect(officer.failed_census_calls.last).to eq(fcc)
expect(officer.failed_census_calls_count).to eq(initial_failed_census_calls_count + 1)
end
scenario "Error on Census (year of birth)" do

View File

@@ -30,8 +30,6 @@ describe "Voters" do
page.evaluate_script("window.location.reload()")
expect(page).to have_content "Has already participated in this poll"
expect(page).not_to have_button "Confirm vote"
expect(Poll::Voter.last.officer_id).to eq(officer.id)
end
scenario "Cannot vote" do

View File

@@ -435,7 +435,7 @@ describe "Proposals" do
expect(page).to have_field "Full name of the person submitting the proposal", with: "Isabel Garcia"
end
scenario "Responsible name field is not shown for verified users" do
scenario "Responsible name field is not shown anywhere" do
author = create(:user, :level_two)
login_as(author)
@@ -453,7 +453,8 @@ describe "Proposals" do
click_link "No, I want to publish the proposal"
click_link "Not now, go to my proposal"
expect(Proposal.last.responsible_name).to eq(author.document_number)
expect(page).to have_css "h1", exact_text: "Help refugees"
expect(page).not_to have_content author.document_number
end
scenario "Errors on create" do