# frozen_string_literal: true require 'rails_helper' RSpec.feature 'User manages contract templates', type: :feature do let(:current_user) { create(:user, :manager) } let(:project) { create(:project, members: current_user, account: current_user.primary_account) } let(:project2) { create(:project, members: current_user, account: current_user.primary_account, name: 'New project') } before do sign_in(current_user) end scenario 'splash page is shown if there are no contract templates' do visit project_contract_templates_path(project) expect(page).to have_content schedule_demo expect(page).to have_content create_release_template expect(page).to have_content import_release_template end scenario 'creating a new release template' do visit new_project_contract_template_path(project) fill_in 'Name', with: 'My Release Template' select 'Appearance Release', from: 'Release type' fill_in_trix body_field, with: 'You agree to this release.' fill_hidden guardian_clause_field, with: 'Your minor agrees to this release.' select 'All', from: 'Applicable Media' select 'Other', from: 'Territory' fill_in 'Describe other territory', with: 'North America only' select 'In perpetuity', from: 'Term' select 'None', from: 'Restriction' click_on create_release_template_button expect(page).to have_content(create_contract_template_success_message) end scenario 'creating new release template, all release types accept signature legal text' do all_release_types = ['Acquired Media', 'Appearance', 'Location', 'Material', 'Medical', 'Misc', 'Talent'] all_release_types.each do |release_type| visit new_project_contract_template_path(project) dropdown_selection = "#{release_type} Release" select dropdown_selection, from: 'Release type' fill_in 'Name', with: "My #{release_type} template" fill_in_trix signature_legal_text_field, with: 'LL' expect do click_on create_release_template_button end.to change(ContractTemplate, :count).by(1) end end scenario 'location release template has a amendment clause field' do visit new_project_contract_template_path(project) fill_in 'Name', with: 'My Release Template' select 'Location Release', from: 'Release type' fill_hidden amendment_clause_field, with: 'Amendment clause text' click_on create_release_template_button expect(page).to have_content(create_contract_template_success_message) expect(ContractTemplate.last.amendment_clause.body.to_s).to match /Amendment clause text/ end scenario 'appearance release template has an amendment clause field' do visit new_project_contract_template_path(project) fill_in 'Name', with: 'My Release Template' select 'Appearance Release', from: 'Release type' fill_hidden amendment_clause_field, with: 'Amendment clause text' click_on create_release_template_button expect(page).to have_content(create_contract_template_success_message) expect(ContractTemplate.last.amendment_clause.body.to_s).to match /Amendment clause text/ end scenario 'medical release template has a questionnaire legal text field' do visit new_project_contract_template_path(project) fill_in 'Name', with: 'My Release Template' select 'Medical Release', from: 'Release type' fill_hidden questionnaire_legal_text_field, with: 'Questionnaire legal text' click_on create_release_template_button expect(page).to have_content(create_contract_template_success_message) expect(ContractTemplate.last.questionnaire_legal_text.body.to_s).to match /Questionnaire legal text/ end scenario 'medical release template has a guardian clause field' do visit new_project_contract_template_path(project) fill_in 'Name', with: 'My Release Template' select 'Medical Release', from: 'Release type' fill_hidden guardian_clause_field, with: 'Guardian clause text' click_on create_release_template_button expect(page).to have_content(create_contract_template_success_message) expect(ContractTemplate.last.guardian_clause.body.to_s).to match /Guardian clause text/ end scenario 'misc release templates has guardian clause and questionnaire', js: true do visit new_project_contract_template_path(project) fill_in 'Name', with: 'My Misc Release' select 'Misc Release', from: 'Release type' expect(page).to have_selector('label', text: 'Guardian Clause') MiscRelease::NUMBER_OF_CUSTOM_FIELDS.times do |n| expect(page).to have_field(question_field(n+1)) end fill_in_trix 'contract_template_guardian_clause', with: 'Guardian clause text' fill_in question_field(1), with: 'How much experience do you have in the industry?' click_on create_release_template_button expect(page).to have_content(create_contract_template_success_message) end scenario 'preview new talent release template without guardian clause' do visit new_project_contract_template_path(project) select 'Talent Release', from: 'Release type' click_on 'Preview' expect(content_type).to eq('application/pdf') expect(content_disposition).to include('inline') expect(pdf_body).to have_content('PREVIEW ONLY') expect(pdf_body).to have_content('Talent Release') expect(pdf_body).to have_content('Body text goes here') expect(pdf_body).to have_content('Guardian') end scenario 'preview new talent release template with guardian clause' do visit new_project_contract_template_path(project) select 'Talent Release', from: 'Release type' fill_hidden guardian_clause_field, with: 'Your minor agrees to this release.' click_on 'Preview' expect(content_type).to eq('application/pdf') expect(content_disposition).to include('inline') expect(pdf_body).to have_content('PREVIEW ONLY') expect(pdf_body).to have_content('Talent Release') expect(pdf_body).to have_content('Body text goes here') expect(pdf_body).to have_content('Guardian') expect(pdf_body).to have_content('Your minor') end scenario 'preview new appearance release template without guardian clause' do visit new_project_contract_template_path(project) select 'Appearance Release', from: 'Release type' click_on 'Preview' expect(content_type).to eq('application/pdf') expect(content_disposition).to include('inline') expect(pdf_body).to have_content('PREVIEW ONLY') expect(pdf_body).to have_content('Appearance Release') expect(pdf_body).to have_content('Body text goes here') expect(pdf_body).to have_content('Guardian') end scenario 'preview new appearance release template with guardian clause' do visit new_project_contract_template_path(project) select 'Appearance Release', from: 'Release type' fill_hidden guardian_clause_field, with: 'Your minor agrees to this release.' click_on 'Preview' expect(content_type).to eq('application/pdf') expect(content_disposition).to include('inline') expect(pdf_body).to have_content('PREVIEW ONLY') expect(pdf_body).to have_content('Appearance Release') expect(pdf_body).to have_content('Body text goes here') expect(pdf_body).to have_content('Guardian') expect(pdf_body).to have_content('Your minor') end scenario 'different release type generates different pdf' do visit new_project_contract_template_path(project) select 'Acquired Media Release', from: 'Release type' click_on 'Preview' expect(content_type).to eq('application/pdf') expect(content_disposition).to include('inline') expect(pdf_body).to have_content('PREVIEW ONLY') expect(pdf_body).to have_content('Acquired Media Release') expect(pdf_body).to have_content('Guardian') visit new_project_contract_template_path(project) select 'Location Release', from: 'Release type' click_on 'Preview' expect(content_type).to eq('application/pdf') expect(content_disposition).to include('inline') expect(pdf_body).to have_content('PREVIEW ONLY') expect(pdf_body).to have_content('Location Release') expect(pdf_body).not_to have_content('Guardian') visit new_project_contract_template_path(project) select 'Material Release', from: 'Release type' click_on 'Preview' expect(content_type).to eq('application/pdf') expect(content_disposition).to include('inline') expect(pdf_body).to have_content('PREVIEW ONLY') expect(pdf_body).to have_content('Material Release') expect(pdf_body).to have_content('Guardian') end scenario 'contract template preview is shown before printing' do create(:appearance_release_contract_template, body: 'Contract legal language', project: project) visit project_contract_templates_path(project) click_on 'Manage' expect(page).to have_content('Print') click_link 'Print' expect(page).to have_content preview_heading expect(page).to have_selector 'embed' end scenario 'printing blank release template' do create(:appearance_release_contract_template, body: 'Contract legal language', project: project) visit project_contract_templates_path(project) click_on 'Manage' expect(page).to have_content('Print') click_link 'Print' fill_in 'Number of copies', with: '-1' click_on t('shared.print') expect(page).to have_content(t('contract_templates.blank_contracts.create.number_of_copies_invalid_notice')) fill_in 'Number of copies', with: 2 click_on t('shared.print') expect(content_type).to eq('application/pdf') expect(content_disposition).to include('inline') expect(pdf_body).to have_content('Contract legal language', count: 2) expect(pdf_body).to have_content('Appearance Release', count: 2) expect(pdf_body).to have_content('Name: _____', count: 4) #2 times for person name and 2 times for guardian name expect(pdf_body).to have_content(t 'blank_contracts.pdf.do_not_copy_warning', count: 2) end scenario 'archiving an existing release template', js: true do create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_content('Test template') click_on 'Manage' accept_alert do click_on 'Delete' end expect(page).to have_content('The release template has been archived') expect(page).not_to have_content('Test template') end scenario 'archived contract templates from other projects are not shown when importing contract templates' do create(:contract_template, :archived, project: project2, name: 'Archived template') create(:contract_template, project: project2, name: 'Active template') create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_content('Test template') click_on import_template_button expect(page).not_to have_content('Test template') expect(page).not_to have_content('Archived template') expect(page).to have_content('Active template') end scenario 'edit button is visible for not-signed contract template' do create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_link(edit_button_label, exact: true, count: 1) end scenario 'edit button is not visible for signed contract template' do ct = create(:contract_template, project: project) create(:appearance_release, contract_template: ct) visit project_contract_templates_path(project) expect(page).to have_link(edit_button_label, exact: true, count: 0) end scenario 'duplicate button is visible for not signed contract template' do create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_link(duplicate_button_label, exact: true, count: 1) end scenario 'duplicate button is visible for signed contract template' do ct = create(:contract_template, project: project) create(:appearance_release, contract_template: ct) visit project_contract_templates_path(project) expect(page).to have_link(duplicate_button_label, exact: true, count: 1) end scenario 'clicking edit button opens edit page for contract template' do ct = create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_content ct.name expect(page).not_to have_content 'Updated CT' expect(page).not_to have_content 'Medical' click_link edit_button_label expect(page).to have_content edit_page_heading fill_in name_field, with: 'Updated CT' select 'Medical Release', from: 'Release type' fill_in_trix body_field, with: 'Updated legal text' click_on update_contract_template_button_label expect(page).to have_content 'Updated CT' expect(page).to have_content 'Medical' end scenario 'clicking duplicate button opens edit page for newly created contract template' do ct = create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_content ct.name expect(page).not_to have_content 'Modified duplicate' expect(page).not_to have_content 'Misc' expect do click_link duplicate_button_label end.to change(ContractTemplate, :count).by(1) expect(page).to have_content edit_page_heading name_input = find("##{name_field}") expect(name_input.value).to eq duplicate_release_name(ct.name) fill_in name_field, with: 'Modified duplicate' select 'Misc Release', from: 'Release type' fill_in_trix body_field, with: 'Legal text for duplicate' click_on update_contract_template_button_label expect(page).to have_content 'Modified duplicate' expect(page).to have_content 'Misc' expect(ct.body.id).not_to eq ContractTemplate.last.body.id expect(ct.guardian_clause.id).not_to eq ContractTemplate.last.guardian_clause.id expect(ct.signature_legal_text.id).not_to eq ContractTemplate.last.signature_legal_text.id end scenario 'trix editor has underline button', js: true do visit new_project_contract_template_path(project) select 'Appearance Release', from: 'Release type' expect(page).to have_selector("button[data-trix-attribute='underline']") end context 'When the user is associate' do let(:current_user) { create(:user, :associate) } it 'does not show management buttons for release templates' do create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).not_to have_content('Create New Release Template') expect(page).not_to have_content(import_template_button) expect(page).not_to have_content('Delete') end it 'does not show create release button on splash page' do visit project_contract_templates_path(project) expect(page).to have_content schedule_demo expect(page).not_to have_content create_release_template expect(page).not_to have_content import_release_template end it 'does not show edit button' do create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_link(edit_button_label, exact: true, count: 0) end it 'does not show duplicate button' do create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_link(duplicate_button_label, exact: true, count: 0) end end context 'When the user is account manager' do let(:current_user) { create(:user, :account_manager) } it 'shows archive button for release template' do create(:contract_template, project: project) visit project_contract_templates_path(project) click_on 'Manage' expect(page).to have_content('Delete') end it 'shows create release button on splash page' do visit project_contract_templates_path(project) expect(page).to have_content schedule_demo expect(page).to have_content create_release_template expect(page).to have_content import_release_template end it 'shows edit button when contract template is not signed' do create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_link(edit_button_label, exact: true, count: 1) end it 'does not show edit button when contract template is signed' do ct = create(:contract_template, project: project) create(:appearance_release, contract_template: ct) visit project_contract_templates_path(project) expect(page).to have_link(edit_button_label, exact: true, count: 0) end it 'shows duplicate button when contract template is not signed' do create(:contract_template, project: project) visit project_contract_templates_path(project) expect(page).to have_link(duplicate_button_label, exact: true, count: 1) end it 'shows duplicate button when contract template is signed' do ct = create(:contract_template, project: project) create(:appearance_release, contract_template: ct) visit project_contract_templates_path(project) expect(page).to have_link(duplicate_button_label, exact: true, count: 1) end end private def import_template_button t 'contract_templates.index.actions.import' end def import_selected_templates_button t 'release_template_imports.new.actions.import' end def preview_heading t 'blank_contracts.new.preview_heading' end def fill_in_trix(id, with:) find("trix-editor##{id}").click.set(with) end def body_field 'contract_template_body' end def guardian_clause_field 'contract_template_guardian_clause_trix_input_contract_template' end def amendment_clause_field 'contract_template_amendment_clause_trix_input_contract_template' end def questionnaire_legal_text_field 'contract_template_questionnaire_legal_text_trix_input_contract_template' end def signature_legal_text_field 'contract_template_signature_legal_text' end def create_contract_template_success_message 'The release template has been created' end def question_field(n) "Question #{n} text" end def fill_hidden(id, with:) find(:xpath, "//input[@id='#{id}']", visible: false).set with end def schedule_demo t 'contract_templates.splash.actions.book_demo' end def create_release_template t 'contract_templates.splash.actions.create_template' end def import_release_template t 'contract_templates.splash.actions.import_template' end def signature_legal_text_trix_field 'Signature legal text' end def edit_button_label t 'contract_templates.contract_template.actions.edit' end def duplicate_button_label t 'contract_templates.contract_template.actions.duplicate' end def edit_page_heading t 'contract_templates.edit.heading' end def name_field 'contract_template_name' end def update_contract_template_button_label t 'helpers.submit.contract_template.update' end def duplicate_release_name(template_name = '') t 'contract_templates.duplicate.name_prefix', template_name: template_name end def create_release_template_button 'Create Release Template' end end