From addda0c77319f9285e65ac2434cafe6c38eff099 Mon Sep 17 00:00:00 2001 From: taitus Date: Thu, 15 May 2025 17:16:47 +0200 Subject: [PATCH] Unify duplicated methods in common_actions folder Replaced the user with a level_two one in the "Default whole city" test to bypass the full name requirement, which only applies to unverified users. --- spec/shared/system/mappable.rb | 12 +-- spec/support/common_actions.rb | 24 ++++-- spec/support/common_actions/attachables.rb | 23 ++++++ spec/support/common_actions/maps.rb | 13 --- spec/system/nested_documentable_spec.rb | 93 +++++++--------------- spec/system/nested_imageable_spec.rb | 54 ++----------- spec/system/proposals_spec.rb | 2 +- 7 files changed, 83 insertions(+), 138 deletions(-) diff --git a/spec/shared/system/mappable.rb b/spec/shared/system/mappable.rb index 680c15ea7..7669fdb08 100644 --- a/spec/shared/system/mappable.rb +++ b/spec/shared/system/mappable.rb @@ -21,7 +21,7 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name, do_login_for user, management: management visit send(mappable_new_path, arguments) - send("fill_in_#{mappable_factory_name}_form") + send("fill_in_#{mappable_factory_name}") within ".map-location" do expect(page).not_to have_css(".map-icon") @@ -32,7 +32,7 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name, do_login_for user, management: management visit send(mappable_new_path, arguments) - send("fill_in_#{mappable_factory_name}_form") + send("fill_in_#{mappable_factory_name}") find("#new_map_location").click within ".map-location" do @@ -44,7 +44,7 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name, do_login_for user, management: management visit send(mappable_new_path, arguments) - send("fill_in_#{mappable_factory_name}_form") + send("fill_in_#{mappable_factory_name}") find("#new_map_location").click send("submit_#{mappable_factory_name}_form") @@ -57,7 +57,7 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name, do_login_for user, management: management visit send(mappable_new_path, arguments) - send("fill_in_#{mappable_factory_name}_form") + send("fill_in_#{mappable_factory_name}") expect(page).to have_css ".map-location" send("submit_#{mappable_factory_name}_form") @@ -69,7 +69,7 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name, do_login_for user, management: management visit send(mappable_new_path, arguments) - send("fill_in_#{mappable_factory_name}_form") + send("fill_in_#{mappable_factory_name}") expect(page).not_to have_css ".map-location" send("submit_#{mappable_factory_name}_form") @@ -171,7 +171,7 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name, do_login_for user, management: management visit send(mappable_new_path, arguments) - send("fill_in_#{mappable_factory_name}_form") + send("fill_in_#{mappable_factory_name}") send("submit_#{mappable_factory_name}_form") expect(page).not_to have_content "Map location can't be blank" diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index 07586dd5e..d1411f2bf 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -42,12 +42,24 @@ module CommonActions end def fill_in_proposal - fill_in_new_proposal_title with: "Help refugees" - fill_in "Proposal summary", with: "In summary, what we want is..." - fill_in_ckeditor "Proposal text", with: "This is very important because..." - fill_in "External video URL", with: "https://www.youtube.com/watch?v=yPQfcG-eimk" - fill_in "Full name of the person submitting the proposal", with: "Isabel Garcia" - check "I agree to the Privacy Policy and the Terms and conditions of use" + fill_in_new_proposal_title with: "Proposal title" + fill_in "Proposal summary", with: "Proposal summary" + check :proposal_terms_of_service + end + + def fill_in_budget + fill_in "Name", with: "Budget name" + end + + def fill_in_dashboard_action + fill_in :dashboard_action_title, with: "Dashboard title" + fill_in_ckeditor "Description", with: "Dashboard description" + end + + def fill_in_budget_investment + fill_in_new_investment_title with: "Budget investment title" + fill_in_ckeditor "Description", with: "Budget investment description" + check :budget_investment_terms_of_service end def fill_in_new_proposal_title(with:) diff --git a/spec/support/common_actions/attachables.rb b/spec/support/common_actions/attachables.rb index c6df4f705..75e9e1ffa 100644 --- a/spec/support/common_actions/attachables.rb +++ b/spec/support/common_actions/attachables.rb @@ -28,4 +28,27 @@ module Attachables end end end + + def admin_section?(path) + path.starts_with?("/admin/") + end + + def management_section?(path) + path.starts_with?("/management/") + end + + def edit_path?(path) + path.ends_with?("/edit") + end + + def fill_in_required_fields(factory, path) + return if edit_path?(path) + + case factory + when :budget then fill_in_budget + when :budget_investment then fill_in_budget_investment + when :dashboard_action then fill_in_dashboard_action + when :proposal then fill_in_proposal + end + end end diff --git a/spec/support/common_actions/maps.rb b/spec/support/common_actions/maps.rb index bde0f93c6..15aa6ff6e 100644 --- a/spec/support/common_actions/maps.rb +++ b/spec/support/common_actions/maps.rb @@ -1,11 +1,5 @@ module Maps - def fill_in_proposal_form - fill_in_new_proposal_title with: "Help refugees" - fill_in "Proposal summary", with: "In summary, what we want is..." - end - def submit_proposal_form - check :proposal_terms_of_service click_button "Create proposal" expect(page).to have_content "Proposal created successfully." @@ -16,14 +10,7 @@ module Maps end end - def fill_in_budget_investment_form - fill_in_new_investment_title with: "Budget investment title" - fill_in_ckeditor "Description", with: "Budget investment description" - check :budget_investment_terms_of_service - end - def submit_budget_investment_form - check :budget_investment_terms_of_service click_button "Create Investment" expect(page).to have_content "Budget Investment created successfully" end diff --git a/spec/system/nested_documentable_spec.rb b/spec/system/nested_documentable_spec.rb index 283e2fc5c..d464da133 100644 --- a/spec/system/nested_documentable_spec.rb +++ b/spec/system/nested_documentable_spec.rb @@ -30,7 +30,7 @@ describe "Nested documentable" do when :budget_investment then "Create Investment" when :dashboard_action then "Save" when :proposal - if edit_path? + if edit_path?(path) "Save changes" else "Create proposal" @@ -42,7 +42,7 @@ describe "Nested documentable" do when :budget_investment then "Budget Investment created successfully." when :dashboard_action then "Action created successfully" when :proposal - if edit_path? + if edit_path?(path) "Proposal updated successfully" else "Proposal created successfully" @@ -53,12 +53,12 @@ describe "Nested documentable" do context "New and edit path" do describe "When allow attached documents setting is enabled" do before do - create(:administrator, user: user) if admin_section? - documentable.update!(author: user) if edit_path? + create(:administrator, user: user) if admin_section?(path) + documentable.update!(author: user) if edit_path?(path) end scenario "Should show new document link when max documents allowed limit is not reached" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path expect(page).to have_link "Add new document" @@ -66,7 +66,7 @@ describe "Nested documentable" do scenario "Should not show new document link when documentable max documents allowed limit is reached" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path documentable.class.max_documents_allowed.times.each do @@ -77,14 +77,14 @@ describe "Nested documentable" do end scenario "Should not show max documents warning when no documents added" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path expect(page).not_to have_css ".max-documents-notice" end scenario "Should show max documents warning when max documents allowed limit is reached" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path documentable.class.max_documents_allowed.times.each do @@ -96,7 +96,7 @@ describe "Nested documentable" do end scenario "Should hide max documents warning after any document removal" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path documentable.class.max_documents_allowed.times.each do @@ -109,7 +109,7 @@ describe "Nested documentable" do end scenario "Should update nested document file name after choosing a file" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path click_link "Add new document" @@ -124,7 +124,7 @@ describe "Nested documentable" do scenario "Should update nested document file title with file name after choosing a file when no title defined" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path documentable_attach_new_file(file_fixture("empty.pdf")) @@ -134,7 +134,7 @@ describe "Nested documentable" do scenario "Should not update nested document file title with file name after choosing a file when title already defined" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path click_link "Add new document" @@ -150,7 +150,7 @@ describe "Nested documentable" do end scenario "Should update loading bar style after valid file upload" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path documentable_attach_new_file(file_fixture("empty.pdf")) @@ -159,7 +159,7 @@ describe "Nested documentable" do end scenario "Should update loading bar style after invalid file upload" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path documentable_attach_new_file(file_fixture("logo_header.gif"), success: false) @@ -168,7 +168,7 @@ describe "Nested documentable" do end scenario "Should update document cached_attachment field after valid file upload" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path click_link "Add new document" @@ -183,7 +183,7 @@ describe "Nested documentable" do end scenario "Should not update document cached_attachment field after invalid file upload" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path documentable_attach_new_file(file_fixture("logo_header.gif"), success: false) @@ -193,7 +193,7 @@ describe "Nested documentable" do end scenario "Should show document errors after documentable submit with empty document fields" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path click_link "Add new document" @@ -205,7 +205,7 @@ describe "Nested documentable" do end scenario "Should delete document after valid file upload and click on remove button" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path documentable_attach_new_file(file_fixture("empty.pdf")) @@ -215,20 +215,20 @@ describe "Nested documentable" do end scenario "Should show successful notice when resource filled correctly without any nested documents" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path - fill_in_required_fields + fill_in_required_fields(factory, path) click_button submit_button_text expect(page).to have_content notice_text end scenario "Should show successful notice when resource filled correctly and after valid file uploads" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path - fill_in_required_fields + fill_in_required_fields(factory, path) documentable_attach_new_file(file_fixture("empty.pdf")) click_button submit_button_text @@ -240,10 +240,10 @@ describe "Nested documentable" do let(:factory) { (factories - [:dashboard_action]).sample } scenario "Should show new document after successful creation with one uploaded file" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path - fill_in_required_fields + fill_in_required_fields(factory, path) documentable_attach_new_file(file_fixture("empty.pdf")) click_button submit_button_text @@ -259,10 +259,10 @@ describe "Nested documentable" do scenario "Should show resource with new document after successful creation with maximum allowed uploaded files" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path - fill_in_required_fields + fill_in_required_fields(factory, path) %w[clippy empty logo].take(documentable.class.max_documents_allowed).each do |filename| documentable_attach_new_file(file_fixture("#{filename}.pdf")) @@ -344,7 +344,7 @@ describe "Nested documentable" do before { Setting["feature.allow_attached_documents"] = false } scenario "Add new document button should not be available" do - do_login_for(user, management: management_section?) + do_login_for(user, management: management_section?(path)) visit path expect(page).not_to have_content("Add new document") @@ -352,45 +352,6 @@ describe "Nested documentable" do end end - def fill_in_required_fields - return if edit_path? - - case factory - when :budget_investment then fill_budget_investment - when :dashboard_action then fill_dashboard_action - when :proposal then fill_proposal - end - end - - def fill_dashboard_action - fill_in :dashboard_action_title, with: "Dashboard title" - fill_in_ckeditor "Description", with: "Dashboard description" - end - - def fill_budget_investment - fill_in_new_investment_title with: "Budget investment title" - fill_in_ckeditor "Description", with: "Budget investment description" - check :budget_investment_terms_of_service - end - - def fill_proposal - fill_in_new_proposal_title with: "Proposal title #{rand(9999)}" - fill_in "Proposal summary", with: "Proposal summary" - check :proposal_terms_of_service - end - - def admin_section? - path.starts_with?("/admin/") - end - - def management_section? - path.starts_with?("/management/") - end - - def edit_path? - path.ends_with?("/edit") - end - def expect_document_has_title(index, title) document = all(".document-fields")[index] diff --git a/spec/system/nested_imageable_spec.rb b/spec/system/nested_imageable_spec.rb index 072ba42d8..0986ff182 100644 --- a/spec/system/nested_imageable_spec.rb +++ b/spec/system/nested_imageable_spec.rb @@ -29,7 +29,7 @@ describe "Nested imageable" do when :budget_investment then "Create Investment" when :future_poll_question_option then "Save image" when :proposal - if edit_path? + if edit_path?(path) "Save changes" else "Create proposal" @@ -42,7 +42,7 @@ describe "Nested imageable" do when :budget_investment then "Budget Investment created successfully." when :future_poll_question_option then "Image uploaded successfully" when :proposal - if edit_path? + if edit_path?(path) "Proposal updated successfully" else "Proposal created successfully" @@ -52,9 +52,9 @@ describe "Nested imageable" do context "New and edit path" do before do - create(:administrator, user: user) if admin_section? - imageable.update!(author: user) if edit_path? - do_login_for(user, management: management_section?) + create(:administrator, user: user) if admin_section?(path) + imageable.update!(author: user) if edit_path?(path) + do_login_for(user, management: management_section?(path)) visit path end @@ -136,7 +136,7 @@ describe "Nested imageable" do let(:factory) { (factories - [:future_poll_question_option]).sample } scenario "Should show successful notice when resource filled correctly without any nested images" do - fill_in_required_fields + fill_in_required_fields(factory, path) click_button submit_button_text @@ -145,7 +145,7 @@ describe "Nested imageable" do end scenario "Should show successful notice when resource filled correctly and after valid file uploads" do - fill_in_required_fields + fill_in_required_fields(factory, path) imageable_attach_new_file(file_fixture("clippy.jpg")) @@ -155,7 +155,7 @@ describe "Nested imageable" do end scenario "Should show new image after successful creation with one uploaded file" do - fill_in_required_fields + fill_in_required_fields(factory, path) imageable_attach_new_file(file_fixture("clippy.jpg")) @@ -205,42 +205,4 @@ describe "Nested imageable" do expect(page).to have_css ".image-fields", count: 1, visible: :all end end - - def fill_in_required_fields - return if edit_path? - - case factory - when :budget then fill_budget - when :budget_investment then fill_budget_investment - when :proposal then fill_proposal - end - end - - def fill_proposal - fill_in_new_proposal_title with: "Proposal title" - fill_in "Proposal summary", with: "Proposal summary" - check :proposal_terms_of_service - end - - def fill_budget - fill_in "Name", with: "Budget name" - end - - def fill_budget_investment - fill_in_new_investment_title with: "Budget investment title" - fill_in_ckeditor "Description", with: "Budget investment description" - check :budget_investment_terms_of_service - end - - def admin_section? - path.starts_with?("/admin/") - end - - def management_section? - path.starts_with?("/management/") - end - - def edit_path? - path.ends_with?("/edit") - end end diff --git a/spec/system/proposals_spec.rb b/spec/system/proposals_spec.rb index 4e80dbbcc..bcb50ab13 100644 --- a/spec/system/proposals_spec.rb +++ b/spec/system/proposals_spec.rb @@ -559,7 +559,7 @@ describe "Proposals" do scenario "Default whole city" do create(:geozone) - author = create(:user) + author = create(:user, :level_two) login_as(author) visit new_proposal_path