diff --git a/test/api/campuses_test.rb b/test/api/campuses_test.rb index 3a0da3534..638d490df 100644 --- a/test/api/campuses_test.rb +++ b/test/api/campuses_test.rb @@ -50,12 +50,7 @@ def test_student_cannot_post_campuses } # auth_token and username added to header - auth_data_to_header = { - auth_token: auth_token(user_student), - username: user_student.username - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: auth_token(user_student), username: user_student.username) post_json "/api/campuses", data_to_post assert_equal 403, last_response.status @@ -85,12 +80,7 @@ def test_student_cannot_put_campuses } # auth_token and username added to header - auth_data_to_header = { - auth_token: auth_token(user_student), - username: user_student.username - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: auth_token(user_student), username: user_student.username) put_json "/api/campuses/#{data_to_put[:campus].id}", data_to_put assert_equal 403, last_response.status @@ -122,12 +112,7 @@ def test_student_delete_campus campus = FactoryBot.create(:campus, mode: 'timetable') # auth_token and username added to header - auth_data_to_header = { - auth_token: auth_token(user_student), - username: user_student.username - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: auth_token(user_student), username: user_student.username) # perform the delete delete_json "/api/campuses/#{campus.id}" diff --git a/test/api/csv_test.rb b/test/api/csv_test.rb index c7ccb1c7b..053a9166e 100644 --- a/test/api/csv_test.rb +++ b/test/api/csv_test.rb @@ -94,11 +94,7 @@ def test_download_csv_all_task_definitions_unit_with_incorrect_auth_token unit_id_to_test = 'string' # auth_token and username added to header - auth_data_to_header = { - auth_token: "wrong_token" - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: "wrong token", username: 'aadmin') # perform the get get "/api/csv/task_definitions?unit_id=#{unit_id_to_test}" @@ -157,11 +153,7 @@ def test_csv_upload_all_task_definitions_unit_incorrect_auth_token } # auth_token and username added to header - auth_data_to_header = { - auth_token: "wrong_token" - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(username: 'aadmin', auth_token: "wrong_token") # perform the POST post "/api/csv/task_definitions", data_to_post @@ -365,13 +357,8 @@ def test_download_csv_all_students_in_unit_with_incorrect_auth_token unit_id_to_test = '1' - # auth_token to header - auth_data_to_header = { - auth_token: 'wrong_token' - } - # auth_token and username added to header - add_auth_header_for(auth_data_to_header) + add_auth_header_for(username: 'aadmin', auth_token: 'wrong_token') # perform the get get "/api/csv/units/#{unit_id_to_test}" @@ -411,12 +398,7 @@ def test_csv_upload_all_students_in_unit } # auth_token and username added to header - auth_data_to_header = { - auth_token: auth_token(unit.main_convenor_user), - username: unit.main_convenor_user.username - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: auth_token(unit.main_convenor_user), username: unit.main_convenor_user.username) # perform the POST post "/api/csv/units/#{unit.id}", data_to_post @@ -439,12 +421,8 @@ def test_csv_upload_all_students_in_unit_incorrect_auth_token file: upload_file_csv('test_files/csv_test_files/COS10001-Students.csv') } - # auth_token and username added to header - auth_data_to_header = { - auth_token: "wrong_token" - } - - add_auth_header_for(auth_data_to_header) + # auth_token and username added to header + add_auth_header_for(username: 'aadmin', auth_token: "wrong_token") # perform the POST post "/api/csv/units/#{unit.id}", data_to_post @@ -499,12 +477,7 @@ def test_csv_upload_all_students_in_unit_incorrect_file_pdf } # auth_token and username added to header - auth_data_to_header = { - auth_token: auth_token(unit.main_convenor_user), - username: unit.main_convenor_user.username - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(user: unit.main_convenor_user) # perform the POST post "/api/csv/units/#{unit.id}", data_to_post @@ -522,12 +495,7 @@ def test_csv_upload_all_students_in_unit_no_file } # auth_token and username added to header - auth_data_to_header = { - auth_token: auth_token(unit.main_convenor_user), - username: unit.main_convenor_user.username - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: auth_token(unit.main_convenor_user), username: unit.main_convenor_user.username) # perform the POST post "/api/csv/units/#{unit.id}", data_to_post @@ -567,12 +535,7 @@ def test_csv_upload_students_un_enroll_in_unit } # auth_token and username added to header - auth_data_to_header = { - auth_token: auth_token(unit.main_convenor_user), - username: unit.main_convenor_user.username - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: auth_token(unit.main_convenor_user), username: unit.main_convenor_user.username) user_id_check = unit.projects.last.user_id @@ -598,11 +561,7 @@ def test_csv_upload_students_un_enroll_in_unit_incorrect_auth_token } # auth_token and username added to header - auth_data_to_header = { - auth_token: 'wrong_token' - } - - add_auth_header_for(auth_data_to_header) + add_auth_header_for(username: 'aadmin', auth_token: "wrong_token") # perform the POST to withdraw user from the unit post "/api/csv/units/#{unit.id}/withdraw", data_to_post @@ -620,8 +579,9 @@ def test_csv_upload_students_un_enroll_in_unit_incorrect_auth_token #POST /api/csv/units/{id}/withdraw def test_csv_upload_students_un_enroll_in_unit_empty_auth_token - unit = FactoryBot.create(:unit, code: 'COS10001', with_students: false, stream_count: 0) - unit.import_users_from_csv test_file_path 'csv_test_files/COS10001-Students.csv' + unit = FactoryBot.create(:unit, code: 'COS10001', with_students: false, stream_count: 0) + response = unit.import_users_from_csv test_file_path 'csv_test_files/COS10001-Students.csv' + assert_equal 1, unit.projects.count, response unit_id_to_test = '1' @@ -689,7 +649,7 @@ def test_csv_upload_students_un_enroll_in_unit_empty_unit_id } # auth_token and username added to header - add_auth_header_for(user: User.first) + add_auth_header_for(user: unit.main_convenor_user) user_id_check = unit.projects.last.user_id @@ -886,12 +846,8 @@ def test_download_csv_all_student_tasks_in_unit_with_incorrect_auth_token unit_id_to_test = '1' - auth_data_to_header = { - auth_token: 'wrong_token' - } - # Add authentication token to header - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: 'wrong_token') # perform the get get "/api/csv/units/#{unit_id_to_test}/task_completion" @@ -993,12 +949,8 @@ def test_download_csv_stats_tutor_assessed_with_incorrect_auth_token unit_id_to_test = '1' - auth_data_to_header = { - auth_token: 'wrong_token' - } - # Add authentication token to header - add_auth_header_for(auth_data_to_header) + add_auth_header_for(username: 'aadmin', auth_token: 'wrong_token') # perform the get get "/api/csv/units/#{unit_id_to_test}/tutor_assessments" @@ -1049,12 +1001,8 @@ def test_download_csv_all_users #GET /api/csv/users def test_download_csv_all_users_with_incorrect_auth_token - auth_data_to_header = { - auth_token: 'wrong_token' - } - # Add authentication token to header - add_auth_header_for(auth_data_to_header) + add_auth_header_for(username: 'aadmin', auth_token: 'wrong_token') # perform the get get "/api/csv/users" @@ -1109,12 +1057,8 @@ def test_csv_upload_users_incorrect_auth_token file: upload_file_csv('test_files/csv_test_files/doubtfire_users.csv') } - auth_data_to_header = { - auth_token: 'wrong_token' - } - # Add authentication token to header - add_auth_header_for(auth_data_to_header) + add_auth_header_for(username: 'aadmin', auth_token: 'wrong_token') # perform the POST to withdraw user from the unit post "/api/csv/users", data_to_post diff --git a/test/api/groups_api_test.rb b/test/api/groups_api_test.rb index b39f8c948..1e7459d1f 100644 --- a/test/api/groups_api_test.rb +++ b/test/api/groups_api_test.rb @@ -168,7 +168,7 @@ def test_pdf_comment_on_group_task post "/api/projects/#{project.id}/task_def_id/#{td.id}/comments", comment_data assert_equal 201, last_response.status - assert File.exists?(TaskComment.last.attachment_path) + assert File.exist?(TaskComment.last.attachment_path) td.destroy group.destroy @@ -295,7 +295,7 @@ def test_create_group # Add username and auth_token to Header add_auth_header_for(user: unit.main_convenor_user) - + # Try again as convenor post "/api/units/#{unit.id}/group_sets/#{gs_response['id']}/groups/#{group_response['id']}/members", {project_id: project.id} @@ -318,7 +318,7 @@ def test_group_student_count def test_group_switch_tutorial unit = FactoryBot.create :unit, group_sets: 1, groups: [{gs: 0, students: 0}] - + gs = unit.group_sets.first gs.update keep_groups_in_same_class: true, allow_students_to_manage_groups: true group1 = gs.groups.first @@ -330,15 +330,15 @@ def test_group_switch_tutorial group1.add_member p2 tutorial = FactoryBot.create :tutorial, unit: unit, campus: nil - + refute p1.enrolled_in? tutorial refute p2.enrolled_in? tutorial # Add username and auth_token to Header add_auth_header_for(user: unit.main_convenor_user) - + put "/api/units/#{unit.id}/group_sets/#{gs.id}/groups/#{group1.id}", { group: {tutorial_id: tutorial.id} } - + assert 201, last_response.status p1.reload @@ -353,7 +353,7 @@ def test_group_switch_tutorial def test_group_switch_tutorial_no_student_management unit = FactoryBot.create :unit, group_sets: 1, groups: [{gs: 0, students: 0}] - + gs = unit.group_sets.first gs.update keep_groups_in_same_class: true, allow_students_to_manage_groups: false group1 = gs.groups.first @@ -365,10 +365,10 @@ def test_group_switch_tutorial_no_student_management group1.add_member p2 tutorial = FactoryBot.create :tutorial, unit: unit, campus: nil - + refute p1.enrolled_in? tutorial refute p2.enrolled_in? tutorial - + add_auth_header_for(user: unit.main_convenor_user) put "/api/units/#{unit.id}/group_sets/#{gs.id}/groups/#{group1.id}", { group: {tutorial_id: tutorial.id} } @@ -386,7 +386,7 @@ def test_group_switch_tutorial_no_student_management def test_group_switch_tutorial_unenrolled_students unit = FactoryBot.create :unit, group_sets: 1, groups: [{gs: 0, students: 0}] - + gs = unit.group_sets.first gs.update keep_groups_in_same_class: true, allow_students_to_manage_groups: false, capacity: 2 group1 = gs.groups.first @@ -400,7 +400,7 @@ def test_group_switch_tutorial_unenrolled_students assert group1.at_capacity? tutorial = FactoryBot.create :tutorial, unit: unit, campus: nil - + refute p1.enrolled_in? tutorial refute p2.enrolled_in? tutorial diff --git a/test/api/tutorials_test.rb b/test/api/tutorials_test.rb index c3b951122..379c0e301 100644 --- a/test/api/tutorials_test.rb +++ b/test/api/tutorials_test.rb @@ -171,12 +171,8 @@ def test_post_tutorial_with_incorrect_auth_token tutorial: tutorial } - auth_data_to_header = { - auth_token: 'Incorrect_Auth_Token' - } - # Add username and auth_token to Header - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: 'Incorrect_Auth_Token', username: 'aadmin') # Number of tutorials before POST number_of_tutorials = Tutorial.all.length @@ -963,12 +959,8 @@ def test_put_tutorials_with_incorrect_auth_token tutorial: tutorial } - auth_data_to_header = { - auth_token: 'Incorrect auth token' - } - # Add username and auth_token to Header - add_auth_header_for(auth_data_to_header) + add_auth_header_for(username: 'aadmin', auth_token: 'Incorrect auth token') # perform the PUT with incorrect auth token put_json "/api/tutorials/#{tutorial_old.id}", data_to_put @@ -1239,12 +1231,8 @@ def test_delete_tutorials_with_incorrect_auth_token # Create a dummy tutorial tutorial = FactoryBot.create(:tutorial) - auth_data_to_header = { - auth_token: 'incorrect_auth_token' - } - # Add username and auth_token to Header - add_auth_header_for(auth_data_to_header) + add_auth_header_for(username: 'aadmin', auth_token: 'incorrect_auth_token') # Number of tutorials before DELETE number_of_tutorials = Tutorial.all.length diff --git a/test/api/units/task_definitions_api_test.rb b/test/api/units/task_definitions_api_test.rb index 88cac7470..2f2ab2b17 100644 --- a/test/api/units/task_definitions_api_test.rb +++ b/test/api/units/task_definitions_api_test.rb @@ -199,7 +199,7 @@ def test_submission_creates_folders task = project.task_for_task_definition(td) assert File.directory? FileHelper.student_work_dir(:new, task, false) - assert File.exists? File.join(FileHelper.student_work_dir(:new, task, false), '000-document.pdf') + assert File.exist? File.join(FileHelper.student_work_dir(:new, task, false), '000-document.pdf') task.destroy @@ -247,7 +247,7 @@ def test_change_to_group_after_submissions assert task.convert_submission_to_pdf path = task.zip_file_path_for_done_task assert path - assert File.exists? path + assert File.exist? path # Change it to a group task @@ -261,10 +261,10 @@ def test_change_to_group_after_submissions task.task_definition = td path = task.zip_file_path_for_done_task assert path - assert File.exists? path + assert File.exist? path td.destroy - assert_not File.exists? path + assert_not File.exist? path end def test_task_related_to_task_def diff --git a/test/api/units_api_test.rb b/test/api/units_api_test.rb index d1ee1b99d..bb9293a49 100644 --- a/test/api/units_api_test.rb +++ b/test/api/units_api_test.rb @@ -401,18 +401,15 @@ def test_fail_units_get_by_id assert_equal 404, last_response.status end - def test_put_update_unit_custom_token(token='abcdef') + def test_put_update_unit_custom_token() unit= Unit.first + token='abcdef' data_to_put = { unit: unit } - auth_data_to_header = { - auth_token: token - } - # Add username and auth_token to Header - add_auth_header_for(auth_data_to_header) + add_auth_header_for(auth_token: token, username: 'aadmin') put_json '/api/units/1', data_to_put assert_equal 419, last_response.status diff --git a/test/helpers/json_helper.rb b/test/helpers/json_helper.rb index 383874ef6..555e83d74 100644 --- a/test/helpers/json_helper.rb +++ b/test/helpers/json_helper.rb @@ -12,14 +12,15 @@ module JsonHelper # def post_json(endpoint, data) header 'CONTENT_TYPE', 'application/json' - post URI.encode(endpoint), data.to_json + + post URI::Parser.new.escape(endpoint), data.to_json end # # PUTs a hash data as JSON with content-type "application/json" # def put_json(endpoint, data) - put URI.encode(endpoint), data.to_json, 'CONTENT_TYPE' => 'application/json' + put URI::Parser.new.escape(endpoint), data.to_json, 'CONTENT_TYPE' => 'application/json' end # @@ -27,7 +28,7 @@ def put_json(endpoint, data) # def delete_json(endpoint) header 'CONTENT_TYPE', 'application/json' - delete URI.encode(endpoint) + delete URI::Parser.new.escape(endpoint) end # diff --git a/test/models/file_helper_test.rb b/test/models/file_helper_test.rb index 73920123d..717149f35 100644 --- a/test/models/file_helper_test.rb +++ b/test/models/file_helper_test.rb @@ -7,7 +7,7 @@ def test_convert_use_with_gif Dir.mktmpdir do |dir| dest_file = "#{dir}#{File.basename(in_file, ".*")}.jpg" assert FileHelper.compress_image_to_dest(in_file, dest_file, true) - assert File.exists? dest_file + assert File.exist? dest_file end end end diff --git a/test/models/task_test.rb b/test/models/task_test.rb index c1f213f41..36387451c 100644 --- a/test/models/task_test.rb +++ b/test/models/task_test.rb @@ -76,11 +76,11 @@ def test_pdf_creation_with_gif assert task.convert_submission_to_pdf path = task.zip_file_path_for_done_task assert path - assert File.exists? path - assert File.exists? task.final_pdf_path + assert File.exist? path + assert File.exist? task.final_pdf_path td.destroy - assert_not File.exists? path + assert_not File.exist? path end def test_image_upload @@ -120,7 +120,7 @@ def test_image_upload task = project.task_for_task_definition(td) task.move_files_to_in_process(FileHelper.student_work_dir(:new)) - assert File.exists? "#{Doubtfire::Application.config.student_work_dir}/in_process/#{task.id}/000-image.jpg" + assert File.exist? "#{Doubtfire::Application.config.student_work_dir}/in_process/#{task.id}/000-image.jpg" td.destroy end @@ -163,11 +163,11 @@ def test_pdf_creation_with_jpg assert task.convert_submission_to_pdf path = task.zip_file_path_for_done_task assert path - assert File.exists? path - assert File.exists? task.final_pdf_path + assert File.exist? path + assert File.exist? task.final_pdf_path td.destroy - assert_not File.exists? path + assert_not File.exist? path end def test_pdf_with_quotes_in_task_title @@ -207,10 +207,10 @@ def test_pdf_with_quotes_in_task_title task.convert_submission_to_pdf path = task.final_pdf_path - assert File.exists? path + assert File.exist? path td.destroy - assert_not File.exists? path + assert_not File.exist? path end def test_copy_draft_learning_summary @@ -247,10 +247,10 @@ def test_copy_draft_learning_summary project.reload assert project.uses_draft_learning_summary path = File.join(project.portfolio_temp_path, '000-document-LearningSummaryReport.pdf') - assert File.exists? path + assert File.exist? path unit.destroy - assert_not File.exists? path + assert_not File.exist? path end def test_draft_learning_summary_wont_copy @@ -265,7 +265,7 @@ def test_draft_learning_summary_wont_copy FileUtils.mkdir_p(project.portfolio_temp_path) FileUtils.cp Rails.root.join('test_files/unit_files/sample-learning-summary.pdf'), path - assert File.exists? path + assert File.exist? path data_to_post = { trigger: 'ready_for_feedback' @@ -289,6 +289,6 @@ def test_draft_learning_summary_wont_copy assert_not project.uses_draft_learning_summary unit.destroy - assert_not File.exists? path + assert_not File.exist? path end end diff --git a/test/models/unit_model_test.rb b/test/models/unit_model_test.rb index a4dccf8b5..51ab18458 100644 --- a/test/models/unit_model_test.rb +++ b/test/models/unit_model_test.rb @@ -25,10 +25,10 @@ class UnitModelTest < ActiveSupport::TestCase @unit.import_task_files_from_zip Rails.root.join('test_files',"#{@unit.code}-Tasks.zip") @unit.task_definitions.each do |td| - assert File.exists?(td.task_sheet), "#{td.abbreviation} task sheet missing" + assert File.exist?(td.task_sheet), "#{td.abbreviation} task sheet missing" end - assert File.exists? @unit.task_definitions.first.task_resources + assert File.exist? @unit.task_definitions.first.task_resources end test 'rollover of task files' do @@ -38,10 +38,10 @@ class UnitModelTest < ActiveSupport::TestCase unit2 = @unit.rollover TeachingPeriod.find(2), nil, nil unit2.task_definitions.each do |td| - assert File.exists?(td.task_sheet), 'task sheet is absent' + assert File.exist?(td.task_sheet), 'task sheet is absent' end - assert File.exists?(unit2.task_definitions.first.task_resources), 'task resource is absent' + assert File.exist?(unit2.task_definitions.first.task_resources), 'task resource is absent' unit2.destroy end @@ -596,12 +596,12 @@ def test_portfolio_zip unit.active_projects.each do |p| DatabasePopulator.generate_portfolio(p) assert p.has_portfolio - assert File.exists?(p.portfolio_path) + assert File.exist?(p.portfolio_path) paths << p.portfolio_path end filename = unit.get_portfolio_zip(unit.main_convenor_user) - assert File.exists? filename + assert File.exist? filename Zip::File.open(filename) do |zip_file| assert_equal unit.active_projects.count, zip_file.count end @@ -610,7 +610,7 @@ def test_portfolio_zip unit.destroy! paths.each do |path| - refute File.exists?(path) + refute File.exist?(path) end end