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.
This commit is contained in:
taitus
2025-05-15 17:16:47 +02:00
parent 2cc23309b8
commit addda0c773
7 changed files with 83 additions and 138 deletions

View File

@@ -21,7 +21,7 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name,
do_login_for user, management: management do_login_for user, management: management
visit send(mappable_new_path, arguments) visit send(mappable_new_path, arguments)
send("fill_in_#{mappable_factory_name}_form") send("fill_in_#{mappable_factory_name}")
within ".map-location" do within ".map-location" do
expect(page).not_to have_css(".map-icon") 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 do_login_for user, management: management
visit send(mappable_new_path, arguments) visit send(mappable_new_path, arguments)
send("fill_in_#{mappable_factory_name}_form") send("fill_in_#{mappable_factory_name}")
find("#new_map_location").click find("#new_map_location").click
within ".map-location" do within ".map-location" do
@@ -44,7 +44,7 @@ shared_examples "mappable" do |mappable_factory_name, mappable_association_name,
do_login_for user, management: management do_login_for user, management: management
visit send(mappable_new_path, arguments) visit send(mappable_new_path, arguments)
send("fill_in_#{mappable_factory_name}_form") send("fill_in_#{mappable_factory_name}")
find("#new_map_location").click find("#new_map_location").click
send("submit_#{mappable_factory_name}_form") 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 do_login_for user, management: management
visit send(mappable_new_path, arguments) 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" expect(page).to have_css ".map-location"
send("submit_#{mappable_factory_name}_form") 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 do_login_for user, management: management
visit send(mappable_new_path, arguments) 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" expect(page).not_to have_css ".map-location"
send("submit_#{mappable_factory_name}_form") 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 do_login_for user, management: management
visit send(mappable_new_path, arguments) 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") send("submit_#{mappable_factory_name}_form")
expect(page).not_to have_content "Map location can't be blank" expect(page).not_to have_content "Map location can't be blank"

View File

@@ -42,12 +42,24 @@ module CommonActions
end end
def fill_in_proposal def fill_in_proposal
fill_in_new_proposal_title with: "Help refugees" fill_in_new_proposal_title with: "Proposal title"
fill_in "Proposal summary", with: "In summary, what we want is..." fill_in "Proposal summary", with: "Proposal summary"
fill_in_ckeditor "Proposal text", with: "This is very important because..." check :proposal_terms_of_service
fill_in "External video URL", with: "https://www.youtube.com/watch?v=yPQfcG-eimk" end
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" 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 end
def fill_in_new_proposal_title(with:) def fill_in_new_proposal_title(with:)

View File

@@ -28,4 +28,27 @@ module Attachables
end end
end 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 end

View File

@@ -1,11 +1,5 @@
module Maps 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 def submit_proposal_form
check :proposal_terms_of_service
click_button "Create proposal" click_button "Create proposal"
expect(page).to have_content "Proposal created successfully." expect(page).to have_content "Proposal created successfully."
@@ -16,14 +10,7 @@ module Maps
end end
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 def submit_budget_investment_form
check :budget_investment_terms_of_service
click_button "Create Investment" click_button "Create Investment"
expect(page).to have_content "Budget Investment created successfully" expect(page).to have_content "Budget Investment created successfully"
end end

View File

@@ -30,7 +30,7 @@ describe "Nested documentable" do
when :budget_investment then "Create Investment" when :budget_investment then "Create Investment"
when :dashboard_action then "Save" when :dashboard_action then "Save"
when :proposal when :proposal
if edit_path? if edit_path?(path)
"Save changes" "Save changes"
else else
"Create proposal" "Create proposal"
@@ -42,7 +42,7 @@ describe "Nested documentable" do
when :budget_investment then "Budget Investment created successfully." when :budget_investment then "Budget Investment created successfully."
when :dashboard_action then "Action created successfully" when :dashboard_action then "Action created successfully"
when :proposal when :proposal
if edit_path? if edit_path?(path)
"Proposal updated successfully" "Proposal updated successfully"
else else
"Proposal created successfully" "Proposal created successfully"
@@ -53,12 +53,12 @@ describe "Nested documentable" do
context "New and edit path" do context "New and edit path" do
describe "When allow attached documents setting is enabled" do describe "When allow attached documents setting is enabled" do
before do before do
create(:administrator, user: user) if admin_section? create(:administrator, user: user) if admin_section?(path)
documentable.update!(author: user) if edit_path? documentable.update!(author: user) if edit_path?(path)
end end
scenario "Should show new document link when max documents allowed limit is not reached" do 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 visit path
expect(page).to have_link "Add new document" expect(page).to have_link "Add new document"
@@ -66,7 +66,7 @@ describe "Nested documentable" do
scenario "Should not show new document link when scenario "Should not show new document link when
documentable max documents allowed limit is reached" do 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 visit path
documentable.class.max_documents_allowed.times.each do documentable.class.max_documents_allowed.times.each do
@@ -77,14 +77,14 @@ describe "Nested documentable" do
end end
scenario "Should not show max documents warning when no documents added" do 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 visit path
expect(page).not_to have_css ".max-documents-notice" expect(page).not_to have_css ".max-documents-notice"
end end
scenario "Should show max documents warning when max documents allowed limit is reached" do 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 visit path
documentable.class.max_documents_allowed.times.each do documentable.class.max_documents_allowed.times.each do
@@ -96,7 +96,7 @@ describe "Nested documentable" do
end end
scenario "Should hide max documents warning after any document removal" do 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 visit path
documentable.class.max_documents_allowed.times.each do documentable.class.max_documents_allowed.times.each do
@@ -109,7 +109,7 @@ describe "Nested documentable" do
end end
scenario "Should update nested document file name after choosing a file" do 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 visit path
click_link "Add new document" click_link "Add new document"
@@ -124,7 +124,7 @@ describe "Nested documentable" do
scenario "Should update nested document file title with scenario "Should update nested document file title with
file name after choosing a file when no title defined" do 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 visit path
documentable_attach_new_file(file_fixture("empty.pdf")) 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 scenario "Should not update nested document file title with
file name after choosing a file when title already defined" do 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 visit path
click_link "Add new document" click_link "Add new document"
@@ -150,7 +150,7 @@ describe "Nested documentable" do
end end
scenario "Should update loading bar style after valid file upload" do 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 visit path
documentable_attach_new_file(file_fixture("empty.pdf")) documentable_attach_new_file(file_fixture("empty.pdf"))
@@ -159,7 +159,7 @@ describe "Nested documentable" do
end end
scenario "Should update loading bar style after invalid file upload" do 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 visit path
documentable_attach_new_file(file_fixture("logo_header.gif"), success: false) documentable_attach_new_file(file_fixture("logo_header.gif"), success: false)
@@ -168,7 +168,7 @@ describe "Nested documentable" do
end end
scenario "Should update document cached_attachment field after valid file upload" do 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 visit path
click_link "Add new document" click_link "Add new document"
@@ -183,7 +183,7 @@ describe "Nested documentable" do
end end
scenario "Should not update document cached_attachment field after invalid file upload" do 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 visit path
documentable_attach_new_file(file_fixture("logo_header.gif"), success: false) documentable_attach_new_file(file_fixture("logo_header.gif"), success: false)
@@ -193,7 +193,7 @@ describe "Nested documentable" do
end end
scenario "Should show document errors after documentable submit with empty document fields" do 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 visit path
click_link "Add new document" click_link "Add new document"
@@ -205,7 +205,7 @@ describe "Nested documentable" do
end end
scenario "Should delete document after valid file upload and click on remove button" do 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 visit path
documentable_attach_new_file(file_fixture("empty.pdf")) documentable_attach_new_file(file_fixture("empty.pdf"))
@@ -215,20 +215,20 @@ describe "Nested documentable" do
end end
scenario "Should show successful notice when resource filled correctly without any nested documents" do 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 visit path
fill_in_required_fields fill_in_required_fields(factory, path)
click_button submit_button_text click_button submit_button_text
expect(page).to have_content notice_text expect(page).to have_content notice_text
end end
scenario "Should show successful notice when resource filled correctly and after valid file uploads" do 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 visit path
fill_in_required_fields fill_in_required_fields(factory, path)
documentable_attach_new_file(file_fixture("empty.pdf")) documentable_attach_new_file(file_fixture("empty.pdf"))
click_button submit_button_text click_button submit_button_text
@@ -240,10 +240,10 @@ describe "Nested documentable" do
let(:factory) { (factories - [:dashboard_action]).sample } let(:factory) { (factories - [:dashboard_action]).sample }
scenario "Should show new document after successful creation with one uploaded file" do 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 visit path
fill_in_required_fields fill_in_required_fields(factory, path)
documentable_attach_new_file(file_fixture("empty.pdf")) documentable_attach_new_file(file_fixture("empty.pdf"))
click_button submit_button_text click_button submit_button_text
@@ -259,10 +259,10 @@ describe "Nested documentable" do
scenario "Should show resource with new document after successful creation with scenario "Should show resource with new document after successful creation with
maximum allowed uploaded files" do maximum allowed uploaded files" do
do_login_for(user, management: management_section?) do_login_for(user, management: management_section?(path))
visit 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| %w[clippy empty logo].take(documentable.class.max_documents_allowed).each do |filename|
documentable_attach_new_file(file_fixture("#{filename}.pdf")) documentable_attach_new_file(file_fixture("#{filename}.pdf"))
@@ -344,7 +344,7 @@ describe "Nested documentable" do
before { Setting["feature.allow_attached_documents"] = false } before { Setting["feature.allow_attached_documents"] = false }
scenario "Add new document button should not be available" do 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 visit path
expect(page).not_to have_content("Add new document") expect(page).not_to have_content("Add new document")
@@ -352,45 +352,6 @@ describe "Nested documentable" do
end end
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) def expect_document_has_title(index, title)
document = all(".document-fields")[index] document = all(".document-fields")[index]

View File

@@ -29,7 +29,7 @@ describe "Nested imageable" do
when :budget_investment then "Create Investment" when :budget_investment then "Create Investment"
when :future_poll_question_option then "Save image" when :future_poll_question_option then "Save image"
when :proposal when :proposal
if edit_path? if edit_path?(path)
"Save changes" "Save changes"
else else
"Create proposal" "Create proposal"
@@ -42,7 +42,7 @@ describe "Nested imageable" do
when :budget_investment then "Budget Investment created successfully." when :budget_investment then "Budget Investment created successfully."
when :future_poll_question_option then "Image uploaded successfully" when :future_poll_question_option then "Image uploaded successfully"
when :proposal when :proposal
if edit_path? if edit_path?(path)
"Proposal updated successfully" "Proposal updated successfully"
else else
"Proposal created successfully" "Proposal created successfully"
@@ -52,9 +52,9 @@ describe "Nested imageable" do
context "New and edit path" do context "New and edit path" do
before do before do
create(:administrator, user: user) if admin_section? create(:administrator, user: user) if admin_section?(path)
imageable.update!(author: user) if edit_path? imageable.update!(author: user) if edit_path?(path)
do_login_for(user, management: management_section?) do_login_for(user, management: management_section?(path))
visit path visit path
end end
@@ -136,7 +136,7 @@ describe "Nested imageable" do
let(:factory) { (factories - [:future_poll_question_option]).sample } let(:factory) { (factories - [:future_poll_question_option]).sample }
scenario "Should show successful notice when resource filled correctly without any nested images" do 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 click_button submit_button_text
@@ -145,7 +145,7 @@ describe "Nested imageable" do
end end
scenario "Should show successful notice when resource filled correctly and after valid file uploads" do 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")) imageable_attach_new_file(file_fixture("clippy.jpg"))
@@ -155,7 +155,7 @@ describe "Nested imageable" do
end end
scenario "Should show new image after successful creation with one uploaded file" do 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")) 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 expect(page).to have_css ".image-fields", count: 1, visible: :all
end end
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 end

View File

@@ -559,7 +559,7 @@ describe "Proposals" do
scenario "Default whole city" do scenario "Default whole city" do
create(:geozone) create(:geozone)
author = create(:user) author = create(:user, :level_two)
login_as(author) login_as(author)
visit new_proposal_path visit new_proposal_path