|
23 | 23 | let(:user_ids) { [SecureRandom.uuid, SecureRandom.uuid] } |
24 | 24 |
|
25 | 25 | before do |
| 26 | + user_ids |
26 | 27 | stub_profile_api_create_school_students_sso(user_ids:) |
27 | 28 | end |
28 | 29 |
|
|
34 | 35 | it 'makes a profile API call with correct parameters' do |
35 | 36 | described_class.call(school:, school_students_params:, current_user:) |
36 | 37 |
|
37 | | - # TODO: Replace with WebMock assertion once the profile API has been built. |
38 | 38 | expect(ProfileApiClient).to have_received(:create_school_students_sso) |
39 | 39 | .with(token: current_user.token, students: school_students_params, school_id: school.id) |
40 | 40 | end |
|
63 | 63 | end |
64 | 64 | end |
65 | 65 |
|
| 66 | + # This test fails if Role validations change. Check create_batch_sso insert_all usage is still safe, otherwise |
| 67 | + # add the validation to the expected list |
| 68 | + it 'fails if Role validations change to ensure CreateBatchSSO is updated' do |
| 69 | + actual_custom = Role._validate_callbacks |
| 70 | + .select { |cb| cb.filter.is_a?(Symbol) } |
| 71 | + .map(&:filter) |
| 72 | + .reject { |v| v.to_s.start_with?('cant_modify_encrypted_attributes', 'validate_associated_records') } |
| 73 | + .sort |
| 74 | + |
| 75 | + actual_builtin = Role.validators |
| 76 | + .map { |v| { attributes: v.attributes.sort, kind: v.class.name.demodulize } } |
| 77 | + .sort_by { |v| [v[:kind], v[:attributes]] } |
| 78 | + |
| 79 | + expected_custom = %i[students_cannot_have_additional_roles users_can_only_have_roles_in_one_school] |
| 80 | + expected_builtin = [ |
| 81 | + { attributes: [:role], kind: 'PresenceValidator' }, |
| 82 | + { attributes: [:school], kind: 'PresenceValidator' }, |
| 83 | + { attributes: [:user_id], kind: 'PresenceValidator' }, |
| 84 | + { attributes: [:role], kind: 'UniquenessValidator' } |
| 85 | + ] |
| 86 | + |
| 87 | + expect(actual_custom).to eq(expected_custom), |
| 88 | + "Custom Role validations changed! Got: #{actual_custom.inspect} |
| 89 | + Consider updating CreateBatchSSO to ensure insert_all usage is still safe." |
| 90 | + expect(actual_builtin).to eq(expected_builtin), |
| 91 | + "Built-in Role validations changed! Got: #{actual_builtin.inspect} |
| 92 | + Consider updating CreateBatchSSO to ensure insert_all usage is still safe." |
| 93 | + end |
| 94 | + |
66 | 95 | it 'returns the student data from Profile API' do |
67 | 96 | response = described_class.call(school:, school_students_params:, current_user:) |
68 | 97 | students = response[:school_students] |
|
83 | 112 | expect(second_student_item[:student].name).to eq('SSO Test Student 2') |
84 | 113 | expect(second_student_item[:success]).to be(true) |
85 | 114 | end |
| 115 | + |
| 116 | + context 'when roles already exist for some students' do |
| 117 | + let(:user_ids) { [SecureRandom.uuid, SecureRandom.uuid] } |
| 118 | + |
| 119 | + before do |
| 120 | + Role.create!(role: :student, school_id: school.id, user_id: user_ids[0]) |
| 121 | + end |
| 122 | + |
| 123 | + it 'does not duplicate existing roles' do |
| 124 | + roles_before_call = Role.student.where(school_id: school.id).to_a |
| 125 | + |
| 126 | + described_class.call(school:, school_students_params:, current_user:) |
| 127 | + |
| 128 | + roles_after_call = Role.student.where(school_id: school.id).to_a |
| 129 | + new_student_roles = roles_after_call - roles_before_call |
| 130 | + |
| 131 | + # Should only create 1 new student role (for second student) since first already exists |
| 132 | + expect(new_student_roles.length).to eq(1) |
| 133 | + expect(new_student_roles.first.user_id).to eq(user_ids[1]) |
| 134 | + end |
| 135 | + |
| 136 | + it 'does not raise an error' do |
| 137 | + expect do |
| 138 | + described_class.call(school:, school_students_params:, current_user:) |
| 139 | + end.not_to raise_error |
| 140 | + end |
| 141 | + |
| 142 | + it 'still returns all students in the response' do |
| 143 | + response = described_class.call(school:, school_students_params:, current_user:) |
| 144 | + expect(response[:school_students].length).to eq(2) |
| 145 | + end |
| 146 | + end |
86 | 147 | end |
87 | 148 |
|
88 | 149 | context 'when validation errors occur' do |
|
0 commit comments