From 3536815e831fe0a90c1b60c44c7c8352c19d08eb Mon Sep 17 00:00:00 2001 From: Andrew Cain Date: Sat, 1 Jun 2024 14:24:05 +1000 Subject: [PATCH] test: fix permissions in group and unit api tests --- test/api/group_sets_api_test.rb | 30 +++++++++++++++--------------- test/api/units_api_test.rb | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/test/api/group_sets_api_test.rb b/test/api/group_sets_api_test.rb index bcc002e17..2d8c2778f 100644 --- a/test/api/group_sets_api_test.rb +++ b/test/api/group_sets_api_test.rb @@ -38,7 +38,7 @@ def test_post_add_a_new_groupset_to_a_unit_with_authorization # A groupSet we want to save new_group_set = FactoryBot.build(:group_set) - # Create a unit + # Create a unit new_unit = FactoryBot.create(:unit) # Data that we want to post @@ -58,11 +58,11 @@ def test_post_add_a_new_groupset_to_a_unit_with_authorization response_keys = %w(name allow_students_to_create_groups allow_students_to_manage_groups keep_groups_in_same_class) response_group_set = GroupSet.find(last_response_body['id']) assert_json_matches_model(response_group_set, last_response_body, response_keys) - assert_equal new_unit.id,response_group_set.unit.id - assert_equal new_group_set.name,response_group_set.name - assert_equal new_group_set.allow_students_to_create_groups,response_group_set.allow_students_to_create_groups - assert_equal new_group_set.allow_students_to_manage_groups,response_group_set.allow_students_to_manage_groups - assert_equal new_group_set.keep_groups_in_same_class,response_group_set.keep_groups_in_same_class + assert_equal new_unit.id, response_group_set.unit.id + assert_equal new_group_set.name, response_group_set.name + assert_equal new_group_set.allow_students_to_create_groups, response_group_set.allow_students_to_create_groups + assert_equal new_group_set.allow_students_to_manage_groups, response_group_set.allow_students_to_manage_groups + assert_equal new_group_set.keep_groups_in_same_class, response_group_set.keep_groups_in_same_class end def test_post_add_a_group_to_a_group_set_of_a_unit_without_authorization @@ -83,12 +83,12 @@ def test_post_add_a_group_to_a_group_set_of_a_unit_without_authorization unit_id: new_unit.id, group_set_id: new_group_set.id, group: { - name:new_group.name, - tutorial_id:new_tutorial.id + name: new_group.name, + tutorial_id: new_tutorial.id } } - add_auth_header_for user: User.first + add_auth_header_for user: User.find_by(username: 'student_1') # perform the POST post_json "/api/units/#{new_unit.id}/group_sets/#{new_group_set.id}/groups", data_to_post @@ -144,7 +144,7 @@ def test_get_all_groups_in_unit_without_authorization new_unit = new_group.group_set.unit get "/api/units/#{new_unit.id}/group_sets/#{new_group.group_set_id}/groups" - + # Check error code when an unauthorized user tries to get groups in a unit assert_equal 419, last_response.status, last_response_body end @@ -160,10 +160,10 @@ def test_get_all_groups_in_unit_with_authorization get "/api/units/#{new_unit.id}/group_sets/#{new_group.group_set_id}/groups" - #check returning number of groups + # check returning number of groups assert_equal new_group.group_set.groups.count, last_response_body.count - #Check response + # Check response response_keys = %w(id name) last_response_body.each do | data | grp = Group.find(data['id']) @@ -182,7 +182,7 @@ def test_get_groups_in_a_group_set_without_authorization # Obtain the unit from the group new_unit = new_group.group_set.unit - add_auth_header_for user: User.first + add_auth_header_for user: User.find_by(username: 'student_1') get "/api/units/#{new_unit.id}/group_sets/#{new_group_set.id}/groups" # Check error code @@ -214,7 +214,7 @@ def test_get_groups_in_a_group_set_with_authorization end assert_equal 200, last_response.status end - + def test_groups_unlocked_upon_creation unit = FactoryBot.create :unit @@ -247,7 +247,7 @@ def test_groups_lockable_only_by_staff group = Group.create!({group_set: group_set, name: 'test_groups_lockable_only_by_staff', tutorial: unit.tutorials.first }) group.save! group.add_member(unit.active_projects[0]) - + url = "api/units/#{unit.id}/group_sets/#{group_set.id}/groups/#{group.id}" lock_data = { group: { locked: true } } unlock_data = { group: { locked: false } } diff --git a/test/api/units_api_test.rb b/test/api/units_api_test.rb index 6828abde9..ae021f706 100644 --- a/test/api/units_api_test.rb +++ b/test/api/units_api_test.rb @@ -200,7 +200,7 @@ def test_permissions_on_get Doubtfire::Application.config.auditor_unit_start_before = Date.today # Test auditor can get all - except old - test_unit = FactoryBot.create :unit, start_date: 2.years.ago, end_date: 2.years.ago + 10.weeks, with_students: false, task_count: 0 + test_unit = FactoryBot.create :unit, start_date: 3.years.ago, end_date: 3.years.ago + 10.weeks, with_students: false, task_count: 0 total_units = Unit.count # Test admin can get all