2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-10-26 11:44:53 +02:00
|
|
|
|
module SystemHelpers
|
2018-11-06 11:22:17 +01:00
|
|
|
|
include ActiveJob::TestHelper
|
|
|
|
|
|
2015-08-10 11:05:06 +02:00
|
|
|
|
def login_admin
|
2015-08-13 15:55:19 +02:00
|
|
|
|
user = create :user
|
2015-08-10 11:05:06 +02:00
|
|
|
|
login_as user, scope: :user
|
|
|
|
|
user
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
|
def login_instructeur
|
|
|
|
|
instructeur = create(:instructeur)
|
|
|
|
|
login_as instructeur, scope: :instructeur
|
2015-09-22 10:15:12 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-03 11:11:02 +02:00
|
|
|
|
def sign_in_with(email, password, sign_in_by_link = false)
|
2018-11-06 17:06:45 +01:00
|
|
|
|
fill_in :user_email, with: email
|
|
|
|
|
fill_in :user_password, with: password
|
2018-10-03 11:11:02 +02:00
|
|
|
|
|
2019-07-31 15:04:49 +02:00
|
|
|
|
if sign_in_by_link
|
2021-11-25 15:38:01 +01:00
|
|
|
|
User.find_by(email: email)&.instructeur&.update!(bypass_email_login_token: false)
|
2019-07-31 15:04:49 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-03 11:11:02 +02:00
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
|
click_on 'Se connecter'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if sign_in_by_link
|
|
|
|
|
mail = ActionMailer::Base.deliveries.last
|
2018-12-26 17:35:28 +01:00
|
|
|
|
message = mail.html_part.body.raw_source
|
2021-02-16 15:05:04 +01:00
|
|
|
|
instructeur_id = message[/".+\/connexion-par-jeton\/(.+)\?jeton=(.*)"/, 1]
|
|
|
|
|
jeton = message[/".+\/connexion-par-jeton\/(.+)\?jeton=(.*)"/, 2]
|
2018-10-03 11:11:02 +02:00
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
|
visit sign_in_by_link_path(instructeur_id, jeton: jeton)
|
2018-10-03 11:11:02 +02:00
|
|
|
|
end
|
2018-11-06 17:06:45 +01:00
|
|
|
|
end
|
2018-11-06 11:22:17 +01:00
|
|
|
|
|
2023-03-03 14:16:15 +01:00
|
|
|
|
def sign_up_with(email, password = SECURE_PASSWORD)
|
2018-11-06 11:22:17 +01:00
|
|
|
|
fill_in :user_email, with: email
|
|
|
|
|
fill_in :user_password, with: password
|
|
|
|
|
|
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
|
click_button 'Créer un compte'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-02-24 11:55:29 +01:00
|
|
|
|
def click_confirmation_link_for(email, in_another_browser: false)
|
2018-11-06 11:22:17 +01:00
|
|
|
|
confirmation_email = open_email(email)
|
2020-02-24 11:55:29 +01:00
|
|
|
|
confirmation_link = confirmation_email.body.match(/href="[^"]*(\/users\/confirmation[^"]*)"/)[1]
|
2018-11-06 11:22:17 +01:00
|
|
|
|
|
2020-02-24 11:55:29 +01:00
|
|
|
|
if in_another_browser
|
|
|
|
|
# Simulate the user opening the link in another browser, thus loosing the session cookie
|
|
|
|
|
Capybara.reset_session!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
visit confirmation_link
|
2018-11-06 11:22:17 +01:00
|
|
|
|
end
|
2019-01-14 16:26:53 +01:00
|
|
|
|
|
2020-02-11 18:17:09 +01:00
|
|
|
|
def click_procedure_sign_in_link_for(email)
|
|
|
|
|
confirmation_email = open_email(email)
|
|
|
|
|
procedure_sign_in_link = confirmation_email.body.match(/href="([^"]*\/commencer\/[^"]*)"/)[1]
|
|
|
|
|
|
|
|
|
|
visit procedure_sign_in_link
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-21 11:17:23 +02:00
|
|
|
|
def click_reset_password_link_for(email)
|
|
|
|
|
reset_password_email = open_email(email)
|
2021-09-02 21:21:10 +02:00
|
|
|
|
reset_password_url = reset_password_email.body.match(/http[s]?:\/\/[^\/]+(\/[^\s]+reset_password_token=[^\s"]+)/)[1]
|
2019-10-21 11:17:23 +02:00
|
|
|
|
|
2021-09-02 21:21:10 +02:00
|
|
|
|
visit reset_password_url
|
2019-10-21 11:17:23 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-27 10:19:55 +01:00
|
|
|
|
# Add a new type de champ in the procedure editor
|
2023-10-17 12:25:58 +02:00
|
|
|
|
def add_champ
|
2024-01-12 10:03:54 +01:00
|
|
|
|
click_on 'Ajouter un champ'
|
2019-11-27 10:19:55 +01:00
|
|
|
|
end
|
|
|
|
|
|
2023-10-17 12:25:58 +02:00
|
|
|
|
def remove_flash_message
|
2024-01-12 10:03:54 +01:00
|
|
|
|
expect(page).to have_button('Ajouter un champ', disabled: false)
|
2023-10-17 12:25:58 +02:00
|
|
|
|
expect(page).to have_content('Formulaire enregistré')
|
|
|
|
|
execute_script("document.querySelector('#flash_message').remove();")
|
2023-11-15 12:14:12 +01:00
|
|
|
|
execute_script("document.querySelector('#autosave-notice').remove();")
|
2019-11-27 10:19:55 +01:00
|
|
|
|
end
|
|
|
|
|
|
2023-11-23 14:45:54 +01:00
|
|
|
|
def hide_autonotice_message
|
|
|
|
|
expect(page).to have_text('Formulaire enregistré')
|
|
|
|
|
execute_script("document.querySelector('#autosave-notice').classList.add('hidden');")
|
2019-11-27 10:19:55 +01:00
|
|
|
|
end
|
|
|
|
|
|
2019-02-06 18:19:27 +01:00
|
|
|
|
def blur
|
2023-11-14 09:23:00 +01:00
|
|
|
|
if page.has_css?('body', wait: 0)
|
|
|
|
|
page.find('body').click
|
|
|
|
|
else # page after/inside a `within` block does not match body
|
|
|
|
|
page.first('div').click
|
|
|
|
|
end
|
2019-02-06 18:19:27 +01:00
|
|
|
|
end
|
2019-06-04 15:06:59 +02:00
|
|
|
|
|
2024-09-11 10:52:51 +02:00
|
|
|
|
def playwright_debug
|
|
|
|
|
page.driver.with_playwright_page do |page|
|
|
|
|
|
page.context.enable_debug_console!
|
|
|
|
|
page.pause
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-04 15:06:59 +02:00
|
|
|
|
def pause
|
|
|
|
|
$stderr.write 'Spec paused. Press enter to continue:'
|
|
|
|
|
$stdin.gets
|
|
|
|
|
end
|
2019-06-04 16:17:21 +02:00
|
|
|
|
|
|
|
|
|
def wait_until
|
|
|
|
|
Timeout.timeout(Capybara.default_max_wait_time) do
|
|
|
|
|
sleep(0.1) until (value = yield)
|
|
|
|
|
value
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-11-26 16:30:45 +01:00
|
|
|
|
|
2024-05-07 21:37:54 +02:00
|
|
|
|
def select_combobox(libelle, value, custom_value: false)
|
|
|
|
|
fill_in libelle, with: custom_value ? "#{value}," : value
|
|
|
|
|
if !custom_value
|
|
|
|
|
find_field(libelle).send_keys(:down, :enter)
|
2022-01-05 11:46:49 +01:00
|
|
|
|
end
|
2021-02-11 15:27:39 +01:00
|
|
|
|
end
|
|
|
|
|
|
2021-10-01 12:57:33 +02:00
|
|
|
|
def log_out
|
2022-08-18 17:06:58 +02:00
|
|
|
|
within('.fr-header .fr-container .fr-header__tools .fr-btns-group') do
|
2024-06-18 15:13:54 +02:00
|
|
|
|
click_button(title: 'Mon profil')
|
2022-10-04 15:07:46 +02:00
|
|
|
|
expect(page).to have_selector('#account.fr-collapse--expanded', visible: true)
|
2022-08-18 17:06:58 +02:00
|
|
|
|
click_on 'Se déconnecter'
|
|
|
|
|
end
|
2022-10-07 18:23:41 +02:00
|
|
|
|
expect(page).to have_current_path(root_path, wait: 30)
|
2021-09-28 15:11:15 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-26 16:30:45 +01:00
|
|
|
|
# Keep the brower window open after a test success of failure, to
|
|
|
|
|
# allow inspecting the page or the console.
|
|
|
|
|
#
|
|
|
|
|
# Usage:
|
|
|
|
|
# 1. Disable the 'headless' mode in `spec_helper.rb`
|
|
|
|
|
# 2. Call `leave_browser_open` at the beginning of your scenario
|
|
|
|
|
def leave_browser_open
|
|
|
|
|
Selenium::WebDriver::Chrome::Service.class_eval do
|
|
|
|
|
def stop
|
|
|
|
|
STDOUT.puts "#{self.class}#stop is a no-op, because leave_browser_open is enabled"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Selenium::WebDriver::Driver.class_eval do
|
|
|
|
|
def quit
|
|
|
|
|
STDOUT.puts "#{self.class}#quit is a no-op, because leave_browser_open is enabled"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Capybara::Selenium::Driver.class_eval do
|
|
|
|
|
def reset!
|
|
|
|
|
STDOUT.puts "#{self.class}#reset! is a no-op, because leave_browser_open is enabled"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-01-05 11:46:49 +01:00
|
|
|
|
|
|
|
|
|
def find_hidden_field_for(libelle, name: 'value')
|
|
|
|
|
find("#{form_group_id_for(libelle)} input[type=\"hidden\"][name$=\"[#{name}]\"]")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def form_group_id_for(libelle)
|
|
|
|
|
"#champ-#{form_id_for(libelle).gsub('-input', '')}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def form_id_for(libelle)
|
|
|
|
|
find(:xpath, ".//label[contains(text()[normalize-space()], '#{libelle}')]")[:for]
|
|
|
|
|
end
|
2022-10-04 12:27:03 +02:00
|
|
|
|
|
2023-08-29 15:47:28 +02:00
|
|
|
|
def wait_for_autosave
|
2022-10-04 12:27:03 +02:00
|
|
|
|
blur
|
2022-10-14 17:08:04 +02:00
|
|
|
|
expect(page).to have_css('.debounced-empty') # no more debounce
|
|
|
|
|
expect(page).to have_css('.autosave-state-idle') # no more in flight promise
|
2022-10-04 12:27:03 +02:00
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
RSpec.configure do |config|
|
2021-10-26 11:44:53 +02:00
|
|
|
|
config.include SystemHelpers, type: :system
|
2015-08-20 17:30:17 +02:00
|
|
|
|
end
|