Merge pull request #7659 from betagouv/fix-random-tests

Fix random tests errors
This commit is contained in:
Paul Chavard 2022-08-04 10:01:22 +02:00 committed by GitHub
commit 21637da8ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 39 deletions

View file

@ -138,8 +138,9 @@ GEM
browser (5.3.1)
builder (3.2.4)
byebug (11.1.3)
capybara (3.35.3)
capybara (3.37.1)
addressable
matrix
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
@ -149,7 +150,7 @@ GEM
capybara-email (3.0.2)
capybara (>= 2.4, < 4.0)
mail
capybara-screenshot (1.0.25)
capybara-screenshot (1.0.26)
capybara (>= 1.0, < 4)
launchy
case_transform (0.2)
@ -450,7 +451,7 @@ GEM
net-protocol
netrc (0.11.0)
nio4r (2.5.8)
nokogiri (1.13.7)
nokogiri (1.13.8)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
open4 (1.3.4)
@ -496,7 +497,7 @@ GEM
pry (~> 0.13.0)
pry-rails (0.3.9)
pry (>= 0.10.4)
public_suffix (4.0.6)
public_suffix (4.0.7)
puma (5.6.4)
nio4r (~> 2.0)
pundit (2.1.0)

View file

@ -18,7 +18,7 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
has_many :revision_types_de_champ, -> { ordered }, foreign_key: :parent_id, class_name: 'ProcedureRevisionTypeDeChamp', inverse_of: :parent, dependent: :destroy
has_one :procedure, through: :revision
scope :root, -> { where(parent: nil) }
scope :ordered, -> { order(:position) }
scope :ordered, -> { order(:position, :id) }
scope :revision_ordered, -> { order(:revision_id) }
scope :public_only, -> { joins(:type_de_champ).where(types_de_champ: { private: false }) }
scope :private_only, -> { joins(:type_de_champ).where(types_de_champ: { private: true }) }

View file

@ -105,7 +105,7 @@ module SystemHelpers
def select_combobox(libelle, fill_with, value, check: true)
fill_in libelle, with: fill_with
find('li[role="option"]', text: value).click
find('li[role="option"]', text: value, wait: 5).click
if check
check_selected_value(libelle, with: value)
end

View file

@ -1,3 +1,3 @@
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
WebMock.disable_net_connect!(allow_localhost: true, net_http_connect_on_start: true)

View file

@ -4,6 +4,7 @@ describe 'The user' do
let!(:procedure) { create(:procedure, :published, :for_individual, :with_all_champs_mandatory) }
let(:user_dossier) { user.dossiers.first }
let!(:dossier_to_link) { create(:dossier) }
scenario 'fill a dossier', js: true do
log_in(user, procedure)
@ -39,38 +40,54 @@ describe 'The user' do
select_combobox('communes', 'Ambl', 'Ambléon (01300)')
check('engagement')
fill_in('dossier_link', with: '123')
fill_in('dossier_link', with: dossier_to_link.id)
find('.editable-champ-piece_justificative input[type=file]').attach_file(Rails.root + 'spec/fixtures/files/file.pdf')
expect(page).to have_css('span', text: 'Votre brouillon est automatiquement enregistré', visible: true)
blur
sleep(0.7)
expect(page).to have_css('span', text: 'Brouillon enregistré', visible: true)
# check data on the dossier
expect(user_dossier.brouillon?).to be true
expect(champ_value_for('text')).to eq('super texte')
expect(champ_value_for('textarea')).to eq('super textarea')
expect(champ_value_for('date')).to eq('2012-12-12')
expect(champ_value_for('datetime')).to eq('06/01/2030 07:05')
expect(champ_value_for('number')).to eq('42')
expect(champ_value_for('decimal_number')).to eq('17')
expect(champ_value_for('integer_number')).to eq('12')
expect(champ_value_for('checkbox')).to eq('on')
expect(champ_value_for('civilite')).to eq('Mme')
expect(champ_value_for('email')).to eq('loulou@yopmail.com')
expect(champ_value_for('phone')).to eq('0123456789')
expect(champ_value_for('yes_no')).to eq('false')
expect(champ_value_for('simple_drop_down_list')).to eq('val2')
expect(champ_value_for('simple_choice_drop_down_list_long')).to eq('bravo')
expect(JSON.parse(champ_value_for('multiple_choice_drop_down_list_long'))).to match(['alpha', 'charly'])
expect(JSON.parse(champ_value_for('multiple_drop_down_list'))).to match(['val1', 'val3'])
expect(champ_value_for('pays')).to eq('Australie')
expect(champ_value_for('regions')).to eq('Martinique')
expect(champ_value_for('departements')).to eq('02 - Aisne')
expect(champ_value_for('communes')).to eq('Ambléon (01300)')
expect(champ_value_for('engagement')).to eq('on')
expect(champ_value_for('dossier_link')).to eq('123')
expect(champ_value_for('piece_justificative')).to be_nil # antivirus hasn't approved the file yet
# check data on the dossier from db
# Sometimes, `user_dossier.champs` are not yet all updated with the new values
# when we first load `user_dossier`, causing random errors.
# Strategy is to retry & reload them if necessary for a few seconds,
# and raise expectation error instead of timeout error.
last_expection_error = nil
begin
Timeout.timeout(Capybara.default_max_wait_time) do
expect(user_dossier).to be_brouillon
expect(champ_value_for('text')).to eq('super texte')
expect(champ_value_for('textarea')).to eq('super textarea')
expect(champ_value_for('date')).to eq('2012-12-12')
expect(champ_value_for('datetime')).to eq('06/01/2030 07:05')
expect(champ_value_for('number')).to eq('42')
expect(champ_value_for('decimal_number')).to eq('17')
expect(champ_value_for('integer_number')).to eq('12')
expect(champ_value_for('checkbox')).to eq('on')
expect(champ_value_for('civilite')).to eq('Mme')
expect(champ_value_for('email')).to eq('loulou@yopmail.com')
expect(champ_value_for('phone')).to eq('0123456789')
expect(champ_value_for('yes_no')).to eq('false')
expect(champ_value_for('simple_drop_down_list')).to eq('val2')
expect(champ_value_for('simple_choice_drop_down_list_long')).to eq('bravo')
expect(JSON.parse(champ_value_for('multiple_choice_drop_down_list_long'))).to match(['alpha', 'charly'])
expect(JSON.parse(champ_value_for('multiple_drop_down_list'))).to match(['val1', 'val3'])
expect(champ_value_for('pays')).to eq('Australie')
expect(champ_value_for('regions')).to eq('Martinique')
expect(champ_value_for('departements')).to eq('02 - Aisne')
expect(champ_value_for('communes')).to eq('Ambléon (01300)')
expect(champ_value_for('engagement')).to eq('on')
expect(champ_value_for('dossier_link')).to eq(dossier_to_link.id.to_s)
expect(champ_value_for('piece_justificative')).to be_nil # antivirus hasn't approved the file yet
rescue RSpec::Expectations::ExpectationNotMetError => e
Rails.logger.debug "Error #{e.message.tr("\n", " ")}, will retry"
last_expection_error = e
sleep(0.1)
retry
end
rescue Timeout::Error => e
raise last_expection_error || e
end
## check data on the gui
@ -94,7 +111,7 @@ describe 'The user' do
check_selected_value('departements', with: '02 - Aisne')
check_selected_value('communes', with: 'Ambléon (01300)')
expect(page).to have_checked_field('engagement')
expect(page).to have_field('dossier_link', with: '123')
expect(page).to have_field('dossier_link', with: dossier_to_link.id.to_s)
expect(page).to have_text('file.pdf')
expect(page).to have_text('analyse antivirus en cours')
end
@ -178,13 +195,13 @@ describe 'The user' do
expect(page).to have_css('.card-title', text: 'Votre dossier va expirer', visible: true)
find('#test-user-repousser-expiration').click
expect(page).not_to have_selector('#test-user-repousser-expiration')
expect(page).to have_no_selector('#test-user-repousser-expiration')
Timecop.freeze(simple_procedure.duree_conservation_dossiers_dans_ds.month.from_now) do
visit brouillon_dossier_path(user_old_dossier)
expect(page).to have_css('.card-title', text: 'Votre dossier va expirer', visible: true)
find('#test-user-repousser-expiration').click
expect(page).not_to have_selector('#test-user-repousser-expiration')
expect(page).to have_no_selector('#test-user-repousser-expiration')
end
end
@ -341,7 +358,7 @@ describe 'The user' do
log_in(user, simple_procedure)
fill_individual
expect(page).not_to have_button('Enregistrer le brouillon')
expect(page).to have_no_button('Enregistrer le brouillon')
expect(page).to have_content('Votre brouillon est automatiquement enregistré')
fill_in('texte obligatoire', with: 'a valid user input')
@ -434,7 +451,7 @@ describe 'The user' do
end
def champ_value_for(libelle)
champs = user_dossier.champs
champs = user_dossier.reload.champs
champs.find { |c| c.libelle == libelle }.value
end