Skip to content

Commit

Permalink
test: fix auditor tests and database populator
Browse files Browse the repository at this point in the history
  • Loading branch information
macite committed Jun 1, 2024
1 parent 7366328 commit 3b7faa8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/database_populator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 24 additions & 3 deletions test/api/units_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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'
Expand Down
10 changes: 5 additions & 5 deletions test/models/unit_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down

0 comments on commit 3b7faa8

Please sign in to comment.