From bece3a53246268b3bf88973c449924e1722005eb Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Thu, 24 Oct 2024 13:00:00 -0400 Subject: [PATCH] test --- app/assets/javascripts/application.js | 5 +---- app/models/document.rb | 16 +++++++++++++-- test/models/document_test.rb | 28 +++++++++++++++++++++++++++ test/system/app_settings_test.rb | 3 +++ test/system/students_test.rb | 12 ++++++++++-- test/system/theses_test.rb | 11 +++++++++++ 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index eb17ba1..811368b 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -24,20 +24,17 @@ $(document).ready(function () { - $('.dropdown-toggle').dropdown(); // disable in browser form validations $('form').find('input').removeAttr('required'); - $('.theses .options a').click(function (e) { e.preventDefault(); $(this).tab('show'); $(".theses .options a").removeClass("active") $(this).addClass("active") }) - }); $(document).on('change', 'form.file-upload input.file', function() { @@ -45,7 +42,7 @@ $(document).on('change', 'form.file-upload input.file', function() { var allowed = $(this).data('ext').split(','); var ext = '.' + $(this).val().split('.').pop().toLowerCase(); if($.inArray(ext, allowed) == -1) { - alert('File extension "' + ext + '" is not allowed for this upload.'); + alert('File extension ' + ext + ' is not allowed.'); $(this).parents('form:first').find('input:submit').prop('disabled', true); } else { $(this).parents('form:first').find('input:submit').prop('disabled', false); diff --git a/app/models/document.rb b/app/models/document.rb index 27e5a4d..422b4bd 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -37,8 +37,20 @@ class Document < ApplicationRecord LICENCE_FILE_EXT = [ '.pdf' ].freeze #### ADDITIONAL METHODS - def allowed_extensions - list = SUPPLEMENTAL_FILE_EXT + def allowed_extensions + list = PRIMARY_FILE_EXT + + case document_type + when 'primary' + list = PRIMARY_FILE_EXT + when 'supplemental' + list = SUPPLEMENTAL_FILE_EXT + when 'licence' + list = LICENCE_FILE_EXT + when 'embargo' + list = EMBARGO_FILE_EXT + end + return list end diff --git a/test/models/document_test.rb b/test/models/document_test.rb index 194966b..cbcb6c5 100644 --- a/test/models/document_test.rb +++ b/test/models/document_test.rb @@ -422,4 +422,32 @@ class DocumentTest < ActiveSupport::TestCase assert_equal 4, Document.supplemental.count, 'The should be 4 supplemental documents' assert_equal 2, Document.primary.count, 'There should be 5 primary documents' end + + should 'allowed_extensions should match document usage' do + d = Document.new + + d.usage = 'thesis' + d.supplemental = false + assert_equal Document::PRIMARY_FILE_EXT, d.allowed_extensions + + d.usage = 'thesis' + d.supplemental = true + assert_equal Document::SUPPLEMENTAL_FILE_EXT, d.allowed_extensions + + d.usage = 'licence' + d.supplemental = false + assert_equal Document::LICENCE_FILE_EXT, d.allowed_extensions + + d.usage = 'licence' + d.supplemental = true + assert_equal Document::LICENCE_FILE_EXT, d.allowed_extensions + + d.usage = 'embargo' + d.supplemental = false + assert_equal Document::EMBARGO_FILE_EXT, d.allowed_extensions + + d.usage = 'embargo' + d.supplemental = true + assert_equal Document::EMBARGO_FILE_EXT, d.allowed_extensions + end end diff --git a/test/system/app_settings_test.rb b/test/system/app_settings_test.rb index fe2dfc3..5b58a77 100644 --- a/test/system/app_settings_test.rb +++ b/test/system/app_settings_test.rb @@ -155,6 +155,7 @@ class AppSettingsTest < ApplicationSystemTestCase click_link('Continue') click_link_or_button('Upload Primary Thesis File') attach_file('document_file', Rails.root.join('test/fixtures/files/pdf-document.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') click_link('Continue') assert_selector 'div.student-view.fitted.submit > div', text: 'Review Licence Info Text Test' @@ -195,10 +196,12 @@ class AppSettingsTest < ApplicationSystemTestCase click_link('Continue') click_link_or_button('Upload Primary Thesis File') attach_file('document_file', Rails.root.join('test/fixtures/files/pdf-document.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') click_link('Continue') click_link_or_button('Upload Licence File') attach_file('document_file', Rails.root.join('test/fixtures/files/pdf-document.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') check 'thesis_lac_licence_agreement' assert_selector "input[type=checkbox][id=thesis_lac_licence_agreement]:checked" diff --git a/test/system/students_test.rb b/test/system/students_test.rb index 1ed898b..91b9fb0 100644 --- a/test/system/students_test.rb +++ b/test/system/students_test.rb @@ -226,7 +226,7 @@ class StudentsTest < ApplicationSystemTestCase end ## Page 2 and 3 tests - should "upload primary file" do + should "upload files" do @thesis = FactoryGirl.create(:thesis) login_as(@thesis.student) visit root_url @@ -262,6 +262,9 @@ class StudentsTest < ApplicationSystemTestCase assert_not(page.has_css?("p", text: "Smith_Jane_E_2014_PhD.pdf"), "Should not show 'example text' as per Spring 2024 requirements") attach_file("document_file", Rails.root.join('test/fixtures/files/Tony_Rich_E_2012_Phd.pdf')) + + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") + click_button('Upload') assert_selector(".name", text: /\.pdf/) @@ -282,7 +285,6 @@ class StudentsTest < ApplicationSystemTestCase assert checkbox.checked?, "#thesis_lac_licence_agreement checkbox is not checked." - # Yorkspace Licence assert page.has_selector?('#thesis_yorkspace_licence_agreement', visible: true), "#thesis_yorkspace_licence_agreement not found." checkbox = find('#thesis_yorkspace_licence_agreement', visible: true) @@ -319,6 +321,9 @@ class StudentsTest < ApplicationSystemTestCase click_link_or_button('Upload Licence File') attach_file("document_file", Rails.root.join('test/fixtures/files/Tony_Rich_E_2012_Phd.pdf')) + + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") + click_button('Upload') click_button("Accept and Continue") @@ -359,6 +364,9 @@ class StudentsTest < ApplicationSystemTestCase ## Upload a file not accepted format. e.g. ruby .rb attach_file("document_file", Rails.root.join('test/factories/thesis.rb')) + + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") + click_button('Upload') assert page.has_selector?('#supplementary-help-info', visible: true), "#supplementary-help-info not found." diff --git a/test/system/theses_test.rb b/test/system/theses_test.rb index c175b35..3f93842 100644 --- a/test/system/theses_test.rb +++ b/test/system/theses_test.rb @@ -193,6 +193,7 @@ class ThesesTest < ApplicationSystemTestCase click_on("Upload Primary Thesis File") attach_file("document_file", Rails.root.join('test/fixtures/files/Tony_Rich_E_2012_Phd.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') assert_selector(".name", text: /\.pdf/) @@ -204,6 +205,7 @@ class ThesesTest < ApplicationSystemTestCase click_on("Upload Primary Thesis File") attach_file("document_file", Rails.root.join('test/fixtures/files/image-example.jpg')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') assert_selector(".invalid-feedback", text: "Primary file must be a PDF") @@ -228,6 +230,7 @@ class ThesesTest < ApplicationSystemTestCase click_on("Upload Supplementary Thesis Files") attach_file("document_file", Rails.root.join('test/fixtures/files/zip-file.zip')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') assert_selector(".invalid-feedback", text: "File Supplemental file must be a valid file type") @@ -239,6 +242,7 @@ class ThesesTest < ApplicationSystemTestCase click_on("Upload Supplementary Thesis Files") attach_file("document_file", Rails.root.join('test/fixtures/files/zip-file.zip')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') assert_selector(".invalid-feedback", text: "File Supplemental file must be a valid file type") @@ -251,6 +255,7 @@ class ThesesTest < ApplicationSystemTestCase click_on("Upload Supplementary Thesis Files") assert_selector "h2", text: "Upload Supplementary Thesis File", visible: :all attach_file("document_file", Rails.root.join('test/fixtures/files/pdf-document.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') assert_selector(".supplemental", text: /_supplemental_/) #Supplemental @@ -266,6 +271,7 @@ class ThesesTest < ApplicationSystemTestCase click_on("Upload Licence Agreements") assert_selector "h2", text: "Upload Licence File", visible: :all attach_file("document_file", Rails.root.join('test/fixtures/files/Tony_Rich_E_2012_Phd.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') assert_not_empty find('.licence-file').text, "The .licence-file element is empty, no file" @@ -279,6 +285,7 @@ class ThesesTest < ApplicationSystemTestCase assert_selector "h2", text: "Upload Embargo Documents", visible: :all attach_file("document_file", Rails.root.join('test/fixtures/files/Tony_Rich_E_2012_Phd.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') assert_not_empty find('.embargo-file').text, "The .embargo-file element is empty, no file" @@ -290,10 +297,12 @@ class ThesesTest < ApplicationSystemTestCase click_on("Upload Primary Thesis File") attach_file("document_file", Rails.root.join('test/fixtures/files/Tony_Rich_E_2012_Phd.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') click_link("Replace") attach_file("document_file", Rails.root.join('test/fixtures/files/Tony_Rich_E_2012_Phd.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') assert_selector(".name", text: /\.pdf/) @@ -309,10 +318,12 @@ class ThesesTest < ApplicationSystemTestCase click_on("Upload Supplementary Thesis Files") attach_file("document_file", Rails.root.join('test/fixtures/files/Tony_Rich_E_2012_Phd.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') click_link("Replace") attach_file("document_file", Rails.root.join('test/fixtures/files/Tony_Rich_E_2012_Phd.pdf')) + execute_script("$('#document_file').parents('form:first').find('input:submit').prop('disabled', false);") click_button('Upload') assert_selector(".name", text: /\.pdf/)