require "rails_helper" feature "User managing material releases" do let(:current_user) { create(:user, :manager) } let(:project) { create(:project, members: current_user, account: current_user.primary_account) } context "when signed out" do scenario "United States is default country" do contract_template = create(:contract_template, project: project) visit new_account_project_contract_template_material_release_path(project.account, project, contract_template) expect(country_field_value).to eq "US" end scenario "creating a release without photos", js: true do contract_template = create(:contract_template, project: project) visit new_account_project_contract_template_material_release_path(project.account, project, contract_template) fill_all_fields draw_signature file_fixture("signature.png"), "material_release_signature_base64" click_button submit_release_button expect(page).to have_content success_submit_message end scenario "creating a release with photos", js: true do contract_template = create(:contract_template, project: project) visit new_account_project_contract_template_material_release_path(project.account, project, contract_template) fill_all_fields draw_signature file_fixture("signature.png"), "material_release_signature_base64" drop_file Rails.root.join(file_fixture("material_photo.png")), type: :dropzone click_button submit_release_button expect(page).to have_content success_submit_message expect(MaterialRelease.last.files.attached?).to eq true end scenario "creating a release for a minor - guardian fields are required when minor checkbox is checked", js: true do contract_template = create(:contract_template, project: project) visit new_account_project_contract_template_material_release_path(project.account, project, contract_template) all('input[data-required-tag="guardian"]').each do |field| expect(field['required']).to eq 'false' expect(field).not_to be_visible end page.check person_is_minor_checkbox all('input[data-required-tag="guardian"]').each do |field| expect(field['required']).to eq 'true' expect(field).to be_visible end end scenario 'creating a release for a minor', js: true do project = create(:project, members: current_user, account: current_user.primary_account) contract_template = create(:contract_template, project: project) visit new_account_project_contract_template_material_release_path(project.account, project, contract_template) expect(page).not_to have_content guardian_information_heading.upcase expect(page).not_to have_content guardian_photo_heading.upcase page.check person_is_minor_checkbox expect(page).to have_content guardian_information_heading.upcase expect(page).to have_content guardian_photo_heading.upcase expect(page).to have_content guardian_email_field.titleize fill_all_fields fill_in guardian_first_name_field, with: 'Guardian' fill_in guardian_last_name_field, with: 'Name' fill_in guardian_phone_field, with: '001101' fill_in guardian_email_field, with: 'valid@email.com' attach_file guardian_photo_field, file_fixture('hemsworth.jpeg'), visible: :all fill_in_guardian_address_fields draw_signature file_fixture("signature.png"), signature_field click_button submit_release_button expect(page).to have_content(successful_submission_message) end scenario 'creating a release for a minor with two guardians', js: true do project = create(:project, members: current_user, account: current_user.primary_account) contract_template = create(:contract_template, project: project) visit new_account_project_contract_template_material_release_path(project.account, project, contract_template) expect(page).not_to have_content guardian_2_information_heading.upcase expect(page).not_to have_content guardian_2_photo_heading.upcase page.check person_is_minor_checkbox expect(page).to have_content guardian_information_heading.upcase expect(page).to have_content guardian_photo_heading.upcase expect(page).to have_content guardian_email_field.titleize expect(page).to have_content guardian_2_information_heading.upcase expect(page).to have_content guardian_2_photo_heading.upcase expect(page).to have_content guardian_2_phone_field.titleize fill_all_fields fill_in guardian_first_name_field, with: 'Guardian' fill_in guardian_last_name_field, with: 'Name' fill_in guardian_phone_field, with: '001101' fill_in guardian_email_field, with: 'valid@email.com' attach_file guardian_photo_field, file_fixture('hemsworth.jpeg'), visible: :all fill_in_guardian_address_fields draw_signature file_fixture("signature.png"), signature_field fill_in guardian_2_first_name_field, with: 'Second' fill_in guardian_2_last_name_field, with: 'Guardian' fill_in guardian_2_phone_field, with: '999' click_button submit_release_button expect(page).to have_content(successful_submission_message) expect(MaterialRelease.last.guardian_2_first_name).to eq 'Second' end scenario "creating release is possible only after filling all fields", js: true do contract_template = create(:contract_template, project: project) visit new_account_project_contract_template_material_release_path(project.account, project, contract_template) fill_in material_name_field, with: "Pepsi Logo" expect_failed_client_side_validation fill_in material_description_field, with: "Description text" expect_failed_client_side_validation fill_in person_first_name_field, with: "Jane" expect_failed_client_side_validation fill_in person_last_name_field, with: "Doe" expect_failed_client_side_validation fill_in person_phone_field, with: "2229929229" expect_failed_client_side_validation fill_in person_email_field, with: "mail@mail.com" expect_failed_client_side_validation fill_in person_company_field, with: "Company" expect_failed_client_side_validation fill_in person_title_field, with: "Mr." expect_failed_client_side_validation fill_in person_address_street1_field, with: "Street 1 address" expect_failed_client_side_validation fill_in person_city_field, with: "City" expect_failed_client_side_validation fill_in person_state_field, with: "State" expect_failed_client_side_validation fill_in person_zip_field, with: "ZIP" draw_signature file_fixture("signature.png"), "material_release_signature_base64" click_button submit_release_button expect(page).to have_content success_submit_message end scenario "creating a release, if contract template contains signature legal language, it is shown" do contract_template = create(:contract_template, project: project, signature_legal_text: dummy_signature_legal_text) visit new_account_project_contract_template_material_release_path(project.account, project, contract_template) expect(page).to have_content dummy_signature_legal_text end end context "when signed in" do before do sign_in current_user end scenario "index table shows owner info" do release = create(:material_release, :native, :with_address, project: project) visit project_material_releases_path(project) expect(page).to have_content owner_info_table_header expect(page).to have_content release.person_first_name expect(page).to have_content release.person_last_name expect(page).to have_content release.person_phone expect(page).to have_content release.person_email expect(page).to have_content release.person_address_street1 expect(page).to have_content release.person_address_city expect(page).to have_content release.person_address_state expect(page).to have_content release.person_address_zip expect(page).to have_content release.person_address_country end scenario "creating a release for and adult", js: true do visit new_project_material_release_path(project) by "attaching only a contract" do attach_file "material_release[contract]", Rails.root.join(file_fixture("contract.pdf")), visible: false click_button create_release_button expect(page).to have_invalid_field(material_name_field) end by "attaching photos" do drop_file Rails.root.join(file_fixture("material_photo.png")), type: :dropzone click_button create_release_button expect(page).to have_invalid_field(material_name_field) end by "filling out the remaining information" do fill_in_release_fields name: "Apple Laptop" click_button create_release_button expect(page).to have_content(create_release_notice) expect(page).to have_content("1") click_on "Manage" expect(page).to have_link("Download") end end scenario "creating a release for a minor - guardian fields are required when minor checkbox is checked", js: true do visit new_project_material_release_path(project) all('input[data-required-tag="guardian"]').each do |field| expect(field['required']).to eq 'false' expect(field).not_to be_visible end page.check person_is_minor_checkbox all('input[data-required-tag="guardian"]').each do |field| expect(field['required']).to eq 'true' expect(field).to be_visible end end scenario "creating a release for a minor", js: true do visit new_project_material_release_path(project) expect(page).not_to have_content guardian_information_heading.upcase expect(page).not_to have_content guardian_photo_heading page.check person_is_minor_checkbox expect(page).to have_content guardian_information_heading.upcase expect(page).to have_content guardian_photo_heading expect(page).to have_content guardian_email_field.titleize fill_in_release_fields name: "Apple Laptop" attach_file contract_field, Rails.root.join(file_fixture("contract.pdf")), visible: false fill_in guardian_first_name_field, with: 'Guardian' fill_in guardian_last_name_field, with: 'Name' fill_in guardian_phone_field, with: '001101' fill_in guardian_email_field, with: 'valid@email.com' attach_file guardian_photo_field, file_fixture('hemsworth.jpeg'), visible: :all fill_in_guardian_address_fields click_button import_release_button expect(page).to have_content successful_import_message expect(MaterialRelease.last.guardian_first_name).to eq 'Guardian' end scenario "creating a release for a minor with two guardians", js: true do visit new_project_material_release_path(project) expect(page).not_to have_content guardian_2_information_heading.upcase expect(page).not_to have_content guardian_2_photo_heading page.check person_is_minor_checkbox expect(page).to have_content guardian_information_heading.upcase expect(page).to have_content guardian_photo_heading expect(page).to have_content guardian_email_field.titleize expect(page).to have_content guardian_2_information_heading.upcase expect(page).to have_content guardian_2_photo_heading expect(page).to have_content guardian_2_phone_field.titleize fill_in_release_fields name: "Apple Laptop" attach_file contract_field, Rails.root.join(file_fixture("contract.pdf")), visible: false fill_in guardian_first_name_field, with: 'Guardian' fill_in guardian_last_name_field, with: 'Name' fill_in guardian_phone_field, with: '001101' fill_in guardian_email_field, with: 'valid@email.com' attach_file guardian_photo_field, file_fixture('hemsworth.jpeg'), visible: :all fill_in_guardian_address_fields fill_in guardian_2_first_name_field, with: 'Second' fill_in guardian_2_last_name_field, with: 'Guardian' fill_in guardian_2_phone_field, with: '999' click_button import_release_button expect(page).to have_content successful_import_message expect(MaterialRelease.last.guardian_first_name).to eq 'Guardian' expect(MaterialRelease.last.guardian_2_first_name).to eq 'Second' end scenario "updating an existing release", js: true do material_release = create(:material_release, :non_native, project: project) visit project_material_releases_path(project) click_on "Manage" click_link *update_material_release_link(material_release) fill_in_release_fields name: "New release name" select "Other", from: "Applicable Media" fill_in "Describe other applicable media", with: "New Test" click_button update_release_button expect(page).to have_content(update_release_notice) expect(page).to have_content("New release name") end scenario "deleting an existing release", js: true do material_release = create(:material_release, project: project) visit project_material_releases_path(project) click_on "Manage" accept_alert do click_link *destroy_material_release_link(material_release) end expect(page).to have_content(destroy_release_alert) expect(page).not_to have_content(material_release.name) end scenario "searching for a release", js: true do create(:material_release, name: "Apple MacBook Air", project: project) create(:material_release, name: "Microsoft Surface Pro", project: project) visit project_material_releases_path(project) within "form#search" do fill_in "Search", with: "Apple" click_on "button" end expect(page).to have_content("Apple MacBook Air") expect(page).not_to have_content("Microsoft Surface Pro") expect(page).to have_field("Search", with: "Apple") end scenario "adding photos to an existing release", js: true do create(:material_release, name: "Apple MacBook Air", project: project) visit project_material_releases_path(project) expect(page).to have_content("No Media") click_on "Manage" click_on "Media" expect(page).to have_content("Add Files") expect(page).to have_content("Apple MacBook Air") drop_file Rails.root.join(file_fixture("material_photo.png")), type: :dropzone click_on "Save Changes" expect(page).to have_content("Files added successfully to the release") expect(page).to have_content("1") end scenario "viewing the contract PDF" do material_release = create(:material_release_with_contract_template_and_photo, :native, project: project, name: "Test Materials", tag_list: "Soda Can", notes: [ build(:note, content: "Note 1", user: build(:user, email: "jane.doe@test.com"), email: "jane.doe@test.com", created_at: DateTime.new(2020, 2, 21, 12, 0, 0),), build(:note, content: "Note 2", user: build(:user, email: "john.doe@test.com"), email: "john.doe@test.com", created_at: DateTime.new(2020, 2, 20, 11, 0, 0),), ]) sign_in(current_user) visit project_material_releases_path(project) click_link *view_release_pdf_link_for(material_release) expect(content_type).to eq("application/pdf") expect(content_disposition).to include("inline") expect(pdf_filename).to include("test-materials") expect(pdf_body).to have_content("Test Materials") expect(pdf_body).to have_content("NOTES") expect(pdf_body).to have_content("Note 1") expect(pdf_body).to have_content("jane.doe@test.com") expect(pdf_body).to have_content("2/21/20 12:00 PM") expect(pdf_body).to have_content("Note 2") expect(pdf_body).to have_content("john.doe@test.com") expect(pdf_body).to have_content("2/20/20 11:00 AM") expect(pdf_body).to have_content("TAGS") expect(pdf_body).to have_content("Soda Can") expect(pdf_body).to have_content photos_heading.upcase expect(pdf_body).to have_content("material_photo.png") end end context "when the user is account manager" do end context "when the user is project manager" do end context "when the user is associate" do let(:current_user) { create(:user, :associate) } before do sign_in current_user end scenario "should not show download" do create(:material_release_with_contract_template, name: "Apple MacBook Air", project: project) visit project_material_releases_path(project) click_on "Manage" expect(page).not_to have_link("Download", exact: true) end end private def country_field_value find_field("material_release[person_address_country]").value end def photos_heading(photos_count = 1) t 'contracts.photos.heading', count: photos_count end def material_name_field "material_release[name]" end def material_description_field "material_release[description]" end def person_first_name_field "material_release[person_first_name]" end def person_last_name_field "material_release[person_last_name]" end def person_phone_field "material_release[person_phone]" end def person_email_field "material_release[person_email]" end def person_company_field "material_release[person_company]" end def person_title_field "material_release[person_title]" end def person_address_street1_field "material_release[person_address_street1]" end def person_city_field "material_release[person_address_city]" end def person_state_field "material_release[person_address_state]" end def person_zip_field "material_release[person_address_zip]" end def have_photo(filename, visible: true) have_selector("img[src*='#{filename}']", visible: visible) end def import_material_release_link(project) ["Import Release", href: new_project_material_release_path(project)] end def update_material_release_link(material_release) ["Edit", href: edit_material_release_path(material_release)] end def destroy_material_release_link(material_release) ["Delete", href: material_release_path(material_release)] end def view_release_pdf_link_for(material_release) ["Download", href: material_release_contracts_path(material_release, format: "pdf")] end def fill_in_release_fields(data) fill_in "material_release[name]", with: data[:name] end def create_release_button t "helpers.submit.material_release.create" end def submit_release_button t 'shared.submit_release_long' end def create_release_notice t "material_releases.create.notice" end def update_release_button t "helpers.submit.material_release.update" end def update_release_notice t "material_releases.update.notice" end def destroy_release_alert t "material_releases.destroy.alert" end def fill_in_exploitable_rights select "Other", from: "Applicable Media" fill_in "Describe other applicable media", with: "Test" select "Other", from: "Territory" fill_in "Describe other territory", with: "Test" select "Other", from: "Term" fill_in "Describe other term", with: "Test" select "Other", from: "Restriction" fill_in "Describe other restrictions", with: "Test" end def fill_all_fields fill_in material_name_field, with: "Pepsi Logo" fill_in material_description_field, with: "Description text" fill_in person_first_name_field, with: "Jane" fill_in person_last_name_field, with: "Doe" fill_in person_phone_field, with: "2229929229" fill_in person_email_field, with: "mail@mail.com" fill_in person_company_field, with: "Company" fill_in person_title_field, with: "Mr." fill_in person_address_street1_field, with: "Street 1 address" fill_in person_city_field, with: "City" fill_in person_state_field, with: "State" fill_in person_zip_field, with: "ZIP" end def success_submit_message 'Your release was successfully submitted. Thank you.' end def expect_failed_client_side_validation draw_signature file_fixture("signature.png"), "material_release_signature_base64" click_button submit_release_button expect(page).not_to have_content success_submit_message end def dummy_signature_legal_text 'Some signature legal language' end def person_is_minor_checkbox 'material_release_minor' end def guardian_2_first_name_field t 'helpers.label.material_release.guardian_2_first_name' end def guardian_2_last_name_field t 'helpers.label.material_release.guardian_2_last_name' end def guardian_2_phone_field t 'helpers.label.material_release.guardian_2_phone' end def guardian_first_name_field t 'helpers.label.material_release.guardian_first_name' end def guardian_last_name_field t 'helpers.label.material_release.guardian_last_name' end def guardian_phone_field t 'helpers.label.material_release.guardian_phone' end def guardian_email_field t 'helpers.label.material_release.guardian_email' end def guardian_address_street1_field t('helpers.label.material_release.guardian_address_street1') end def guardian_address_city_field t('helpers.label.material_release.guardian_address_city') end def guardian_address_state_field t('helpers.label.material_release.guardian_address_state') end def guardian_address_zip_field t('helpers.label.material_release.guardian_address_zip') end def guardian_photo_field 'material_release[guardian_photo]' end def guardian_2_photo_field 'material_release[guardian_2_photo]' end def fill_in_guardian_address_fields fill_in guardian_address_street1_field, with: "124 Test Lane" fill_in guardian_address_city_field, with: "New York" fill_in guardian_address_state_field, with: "NY" fill_in guardian_address_zip_field, with: '1000' end def submit_release_button 'I have read and agree to the above' end def successful_submission_message 'Your release was successfully submitted. Thank you.' end def guardian_information_heading t 'public.material_releases.new.guardian_info.heading' end def guardian_photo_heading t 'public.material_releases.new.guardian_photo.heading' end def guardian_2_information_heading t 'public.material_releases.new.guardian_2_info.heading' end def guardian_2_photo_heading t 'public.material_releases.new.guardian_2_photo.heading' end def contract_field 'material_release[contract]' end def import_release_button t 'helpers.submit.material_release.create' end def successful_import_message t 'material_releases.create.notice' end def signature_field 'material_release_signature_base64' end def owner_info_table_header t 'material_releases.index.table_headers.owner_info' end end