From 3b7faa8c2e7b5927edd9a8c46b4795e93766d7ed Mon Sep 17 00:00:00 2001 From: Andrew Cain Date: Sat, 1 Jun 2024 15:48:03 +1000 Subject: [PATCH] test: fix auditor tests and database populator --- lib/helpers/database_populator.rb | 2 +- test/api/units_api_test.rb | 27 ++++++++++++++++++++++++--- test/models/unit_model_test.rb | 10 +++++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/helpers/database_populator.rb b/lib/helpers/database_populator.rb index ce21b78c5..577b60be1 100644 --- a/lib/helpers/database_populator.rb +++ b/lib/helpers/database_populator.rb @@ -510,7 +510,7 @@ def generate_tasks_for_unit(unit, unit_details) echo "----> Generating #{unit_details[:num_tasks]} tasks" csv_to_import = Rails.root.join('test_files', "#{unit.code}-Tasks.csv") - zip_to_import = Rails.root.join('test_files', "#{unit.code}-TaskFiles.zip") + zip_to_import = Rails.root.join('test_files', "#{unit.code}-Tasks.zip") if (File.exist? csv_to_import) && (File.exist? zip_to_import) echo "----> CSV file found, importing tasks from #{csv_to_import} \n" diff --git a/test/api/units_api_test.rb b/test/api/units_api_test.rb index ae021f706..b262a30d0 100644 --- a/test/api/units_api_test.rb +++ b/test/api/units_api_test.rb @@ -197,10 +197,19 @@ def test_permissions_on_get atutor = User.find_by(username: 'atutor') astudent = User.find_by(username: 'astudent') - Doubtfire::Application.config.auditor_unit_start_before = Date.today + Doubtfire::Application.config.auditor_unit_access_years = 2.years # Test auditor can get all - except old - test_unit = FactoryBot.create :unit, start_date: 3.years.ago, end_date: 3.years.ago + 10.weeks, with_students: false, task_count: 0 + start_before = DateTime.now - Doubtfire::Application.config.auditor_unit_access_years - 1.year + start_inside = DateTime.now - Doubtfire::Application.config.auditor_unit_access_years + 1.week + + end_inside = DateTime.now - 1.week + end_after = DateTime.now + 1.week + + test_unit_before = FactoryBot.create :unit, start_date: start_before, end_date: end_inside, with_students: false, task_count: 0 + test_unit_inside = FactoryBot.create :unit, start_date: start_inside, end_date: end_inside, with_students: false, task_count: 0 + test_unit_after = FactoryBot.create :unit, start_date: start_inside, end_date: end_after, with_students: false, task_count: 0 + total_units = Unit.count # Test admin can get all @@ -209,16 +218,28 @@ def test_permissions_on_get assert_equal 200, last_response.status assert_equal total_units, last_response_body.count + assert last_response_body.map { |r| r['id'] }.include? test_unit_inside.id + assert last_response_body.map { |r| r['id'] }.include? test_unit_before.id + assert last_response_body.map { |r| r['id'] }.include? test_unit_after.id + add_auth_header_for(user: aauditor) get '/api/units', { include_in_active: true } assert_equal 200, last_response.status - assert_equal total_units - 1, last_response_body.count + + assert last_response_body.map { |r| r['id'] }.include? test_unit_inside.id + refute last_response_body.map { |r| r['id'] }.include? test_unit_before.id + refute last_response_body.map { |r| r['id'] }.include? test_unit_after.id # Test convenor can not get all add_auth_header_for(user: aconvenor) get '/api/units' assert_equal 403, last_response.status + # Test tutor can not get all + add_auth_header_for(user: atutor) + get '/api/units' + assert_equal 403, last_response.status + # Test students cannot get all add_auth_header_for(user: astudent) get '/api/units' diff --git a/test/models/unit_model_test.rb b/test/models/unit_model_test.rb index 96da9af4c..f0d39411f 100644 --- a/test/models/unit_model_test.rb +++ b/test/models/unit_model_test.rb @@ -85,13 +85,13 @@ def test_sync_unit end def test_import_tasks_worked - @unit.import_tasks_from_csv File.open(Rails.root.join('test_files',"#{@unit.code}-Tasks.csv")) + @unit.import_tasks_from_csv File.open(Rails.root.join('test_files', "#{@unit.code}-Tasks.csv")) assert_equal 37, @unit.task_definitions.count, 'imported all task definitions' end def test_import_task_files - @unit.import_tasks_from_csv File.open(Rails.root.join('test_files',"#{@unit.code}-Tasks.csv")) - @unit.import_task_files_from_zip Rails.root.join('test_files',"#{@unit.code}-Tasks.zip") + @unit.import_tasks_from_csv File.open(Rails.root.join('test_files', "#{@unit.code}-Tasks.csv")) + @unit.import_task_files_from_zip Rails.root.join('test_files', "#{@unit.code}-Tasks.zip") @unit.task_definitions.each do |td| assert File.exist?(td.task_sheet), "#{td.abbreviation} task sheet missing" @@ -110,8 +110,8 @@ def test_import_task_files end def test_rollover_of_task_files - @unit.import_tasks_from_csv File.open(Rails.root.join('test_files',"#{@unit.code}-Tasks.csv")) - @unit.import_task_files_from_zip Rails.root.join('test_files',"#{@unit.code}-Tasks.zip") + @unit.import_tasks_from_csv File.open(Rails.root.join('test_files', "#{@unit.code}-Tasks.csv")) + @unit.import_task_files_from_zip Rails.root.join('test_files', "#{@unit.code}-Tasks.zip") unit2 = @unit.rollover TeachingPeriod.find(2), nil, nil