Files
grecia/spec/system/nested_documentable_spec.rb
taitus 979171ec45 Extract proposals from shared nested documentable to system specs
Removed the now-unused 'documentable_fill_new_valid_proposal' method
from common actions.

Note that it does not seem necessary to create an administrator with the user, as was
done in the original shared example. Also, as in the previous commit, it appears that
we do not need to set the user as the author when creating the documentable.

Also removed the documentable_redirected_to_resource_show_or_navigate_to method,
which was only used for the :proposal factory but was not necessary.
- In the "Proposal new" case (this commit), after submitting the form we are
redirected to the "created" page, where the link "Not now, go to my proposal"
does not appear. This caused the method to always raise a
Capybara::ElementNotFound and return nil.

Instead, this "created" page already displays a preview of the proposal
and a link to publish it. Since we can verify that the proposal was created
successfully here, no redirection or click is needed.

- In the "Proposal edit" case (next commit), the user is redirected directly
to the proposal's "show" page after update, so again, the method is
unnecessary and has been removed.
2025-06-06 15:56:27 +02:00

311 lines
9.9 KiB
Ruby

require "rails_helper"
describe "Nested documentable" do
factories = [
:budget_investment,
:dashboard_action,
:proposal
]
let(:factory) { factories.sample }
let!(:documentable) { create(factory) }
let!(:user) { create(:user, :level_two) }
let(:path) do
case factory
when :budget_investment
[
new_budget_investment_path(budget_id: documentable.budget_id),
new_management_budget_investment_path(budget_id: documentable.budget_id)
].sample
when :dashboard_action then new_admin_dashboard_action_path
when :proposal then new_proposal_path
end
end
let(:submit_button_text) do
case factory
when :budget_investment then "Create Investment"
when :dashboard_action then "Save"
when :proposal then "Create proposal"
end
end
let(:notice_text) do
case factory
when :budget_investment then "Budget Investment created successfully."
when :dashboard_action then "Action created successfully"
when :proposal then "Proposal created successfully"
end
end
context "New path" do
describe "When allow attached documents setting is enabled" do
before do
create(:administrator, user: user) if admin_section?
end
scenario "Should show new document link when max documents allowed limit is not reached" do
do_login_for(user, management: management_section?)
visit path
expect(page).to have_link "Add new document"
end
scenario "Should not show new document link when
documentable max documents allowed limit is reached" do
do_login_for(user, management: management_section?)
visit path
documentable.class.max_documents_allowed.times.each do
click_link "Add new document"
end
expect(page).not_to have_css "#new_document_link"
end
scenario "Should not show max documents warning when no documents added" do
do_login_for(user, management: management_section?)
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?)
visit path
documentable.class.max_documents_allowed.times.each do
documentable_attach_new_file(file_fixture("empty.pdf"))
end
expect(page).to have_css ".max-documents-notice"
expect(page).to have_content "Remove document"
end
scenario "Should hide max documents warning after any document removal" do
do_login_for(user, management: management_section?)
visit path
documentable.class.max_documents_allowed.times.each do
click_link "Add new document"
end
all("a", text: "Cancel").last.click
expect(page).not_to have_css ".max-documents-notice"
end
scenario "Should update nested document file name after choosing a file" do
do_login_for(user, management: management_section?)
visit path
click_link "Add new document"
within "#nested-documents" do
attach_file "Choose document", file_fixture("empty.pdf")
expect(page).to have_css ".loading-bar.complete"
end
expect(page).to have_css ".file-name", text: "empty.pdf"
end
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?)
visit path
documentable_attach_new_file(file_fixture("empty.pdf"))
expect_document_has_title(0, "empty.pdf")
end
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?)
visit path
click_link "Add new document"
within "#nested-documents" do
input = find("input[name$='[title]']")
fill_in input[:id], with: "My Title"
attach_file "Choose document", file_fixture("empty.pdf")
expect(page).to have_css ".loading-bar.complete"
end
expect_document_has_title(0, "My Title")
end
scenario "Should update loading bar style after valid file upload" do
do_login_for(user, management: management_section?)
visit path
documentable_attach_new_file(file_fixture("empty.pdf"))
expect(page).to have_css ".loading-bar.complete"
end
scenario "Should update loading bar style after invalid file upload" do
do_login_for(user, management: management_section?)
visit path
documentable_attach_new_file(file_fixture("logo_header.gif"), false)
expect(page).to have_css ".loading-bar.errors"
end
scenario "Should update document cached_attachment field after valid file upload" do
do_login_for(user, management: management_section?)
visit path
click_link "Add new document"
cached_attachment_field = find("input[name$='[cached_attachment]']", visible: :hidden)
expect(cached_attachment_field.value).to be_empty
attach_file "Choose document", file_fixture("empty.pdf")
expect(page).to have_css(".loading-bar.complete")
expect(cached_attachment_field.value).not_to be_empty
end
scenario "Should not update document cached_attachment field after invalid file upload" do
do_login_for(user, management: management_section?)
visit path
documentable_attach_new_file(file_fixture("logo_header.gif"), false)
cached_attachment_field = find("input[name$='[cached_attachment]']", visible: :hidden)
expect(cached_attachment_field.value).to be_empty
end
scenario "Should show document errors after documentable submit with empty document fields" do
do_login_for(user, management: management_section?)
visit path
click_link "Add new document"
click_button submit_button_text
within "#nested-documents .document-fields" do
expect(page).to have_content("can't be blank", count: 2)
end
end
scenario "Should delete document after valid file upload and click on remove button" do
do_login_for(user, management: management_section?)
visit path
documentable_attach_new_file(file_fixture("empty.pdf"))
click_link "Remove document"
expect(page).not_to have_css("#nested-documents .document-fields")
end
scenario "Should show successful notice when resource filled correctly without any nested documents" do
do_login_for(user, management: management_section?)
visit path
fill_in_required_fields
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?)
visit path
fill_in_required_fields
documentable_attach_new_file(file_fixture("empty.pdf"))
click_button submit_button_text
expect(page).to have_content notice_text
end
context "Budget investments and proposals" 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?)
visit path
fill_in_required_fields
documentable_attach_new_file(file_fixture("empty.pdf"))
click_button submit_button_text
expect(page).to have_content notice_text
expect(page).to have_content "Documents"
expect(page).to have_content "empty.pdf"
# Review
# Doble check why the file is stored with a name different to empty.pdf
expect(page).to have_link href: /.pdf\Z/
end
scenario "Should show resource with new document after successful creation with
maximum allowed uploaded files" do
do_login_for(user, management: management_section?)
visit path
fill_in_required_fields
%w[clippy empty logo].take(documentable.class.max_documents_allowed).each do |filename|
documentable_attach_new_file(file_fixture("#{filename}.pdf"))
end
expect(page).not_to have_link "Add new document"
click_button submit_button_text
expect(page).to have_content notice_text
expect(page).to have_content "Documents (#{documentable.class.max_documents_allowed})"
end
end
end
describe "When allow attached documents setting is disabled" 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?)
visit path
expect(page).not_to have_content("Add new document")
end
end
end
def fill_in_required_fields
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
end