Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
amtuannguyen committed Oct 24, 2024
1 parent bece3a5 commit b1d6709
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 31 deletions.
16 changes: 10 additions & 6 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ $(document).ready(function () {
})
});

$(document).on('change', 'form.file-upload input.file', function() {
if ($(this).val()) {
var allowed = $(this).data('ext').split(',');
var ext = '.' + $(this).val().split('.').pop().toLowerCase();
$(document).on('click', '#file_upload_button', function(e) {
file = $(this).parents('form:first').find('input:file');
if (file.val()) {
var allowed = file.data('ext').split(',');
var ext = '.' + file.val().split('.').pop().toLowerCase();
if($.inArray(ext, allowed) == -1) {
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);
return true;
}
} else {
alert('Please choose a file to upload.');
}
e.preventDefault();
return false;
});
2 changes: 1 addition & 1 deletion app/views/documents/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ html: { multipart: true, class: "file-upload" } do |f| %>
<%= f.hidden_field :usage, value: @document.usage %>
<%= f.hidden_field :supplemental, value: @document.supplemental %>

<%= f.button :submit, "Upload", class: "btn btn-primary file-upload", disabled: true %>
<%= f.button :submit, "Upload", class: "btn btn-primary file-upload", id: "file_upload_button", disabled: false %>

<% if (defined? modal).nil? || !modal %>
<% if current_user.role == User::STUDENT && @document.usage == 'licence' %>
Expand Down
3 changes: 0 additions & 3 deletions test/system/app_settings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ 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'
Expand Down Expand Up @@ -196,12 +195,10 @@ 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"
Expand Down
12 changes: 2 additions & 10 deletions test/system/students_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class StudentsTest < ApplicationSystemTestCase
end

## Page 2 and 3 tests
should "upload files" do
should "upload primary file" do
@thesis = FactoryGirl.create(:thesis)
login_as(@thesis.student)
visit root_url
Expand Down Expand Up @@ -262,9 +262,6 @@ 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/)
Expand All @@ -285,6 +282,7 @@ 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)
Expand Down Expand Up @@ -321,9 +319,6 @@ 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")
Expand Down Expand Up @@ -364,9 +359,6 @@ 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."
Expand Down
11 changes: 0 additions & 11 deletions test/system/theses_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ 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/)
Expand All @@ -205,7 +204,6 @@ 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")
Expand All @@ -230,7 +228,6 @@ 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")
Expand All @@ -242,7 +239,6 @@ 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")
Expand All @@ -255,7 +251,6 @@ 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

Expand All @@ -271,7 +266,6 @@ 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"

Expand All @@ -285,7 +279,6 @@ 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"
Expand All @@ -297,12 +290,10 @@ 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/)

Expand All @@ -318,12 +309,10 @@ 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/)

Expand Down

0 comments on commit b1d6709

Please sign in to comment.