|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Batch deleting school students', type: :request do |
| 6 | + let(:school) { create(:school) } |
| 7 | + let(:owner) { create(:owner, school: school) } |
| 8 | + let(:teacher) { create(:teacher, school: school) } |
| 9 | + let(:school_class) { create(:school_class, school: school, teacher_ids: [teacher.id]) } |
| 10 | + let(:student_1) { create(:student, school: school) } |
| 11 | + let(:student_2) { create(:student, school: school) } |
| 12 | + let(:headers) { { Authorization: UserProfileMock::TOKEN } } |
| 13 | + |
| 14 | + before do |
| 15 | + authenticated_in_hydra_as(owner) |
| 16 | + stub_profile_api_create_safeguarding_flag |
| 17 | + create(:class_student, student_id: student_1.id, school_class: school_class) |
| 18 | + create(:class_student, student_id: student_2.id, school_class: school_class) |
| 19 | + end |
| 20 | + |
| 21 | + describe 'DELETE /api/schools/:school_id/students/batch' do |
| 22 | + before do |
| 23 | + stub_profile_api_delete_school_student |
| 24 | + end |
| 25 | + |
| 26 | + it 'calls ProfileApiClient to delete each student from the profile service' do |
| 27 | + delete "/api/schools/#{school.id}/students/batch", |
| 28 | + params: { student_ids: [student_1.id, student_2.id] }, |
| 29 | + headers: headers |
| 30 | + |
| 31 | + expect(response).to have_http_status(:ok) |
| 32 | + expect(ProfileApiClient).to have_received(:delete_school_student).with(token: UserProfileMock::TOKEN, school_id: school.id, student_id: student_1.id) |
| 33 | + expect(ProfileApiClient).to have_received(:delete_school_student).with(token: UserProfileMock::TOKEN, school_id: school.id, student_id: student_2.id) |
| 34 | + end |
| 35 | + |
| 36 | + it 'deletes all students and their projects' do |
| 37 | + project_1 = create(:project, user_id: student_1.id) |
| 38 | + project_2 = create(:project, user_id: student_2.id) |
| 39 | + |
| 40 | + expect(ClassStudent.where(student_id: student_1.id)).to exist |
| 41 | + expect(ClassStudent.where(student_id: student_2.id)).to exist |
| 42 | + expect(Project.where(id: project_1.id)).to exist |
| 43 | + expect(Project.where(id: project_2.id)).to exist |
| 44 | + |
| 45 | + delete "/api/schools/#{school.id}/students/batch", |
| 46 | + params: { student_ids: [student_1.id, student_2.id] }, |
| 47 | + headers: headers |
| 48 | + |
| 49 | + expect(response).to have_http_status(:ok) |
| 50 | + |
| 51 | + json = JSON.parse(response.body) |
| 52 | + expect(json['results'].size).to eq(2) |
| 53 | + expect(json['results'].all? { |r| r['error'].nil? }).to be true |
| 54 | + |
| 55 | + # Verify students removed from classes |
| 56 | + expect(ClassStudent.where(student_id: student_1.id)).not_to exist |
| 57 | + expect(ClassStudent.where(student_id: student_2.id)).not_to exist |
| 58 | + |
| 59 | + # Verify projects deleted |
| 60 | + expect(Project.where(id: project_1.id)).not_to exist |
| 61 | + expect(Project.where(id: project_2.id)).not_to exist |
| 62 | + end |
| 63 | + |
| 64 | + it 'responds 403 Forbidden when the user is a school-teacher' do |
| 65 | + authenticated_in_hydra_as(teacher) |
| 66 | + |
| 67 | + delete "/api/schools/#{school.id}/students/batch", |
| 68 | + params: { student_ids: [student_1.id, student_2.id] }, |
| 69 | + headers: headers |
| 70 | + |
| 71 | + expect(response).to have_http_status(:forbidden) |
| 72 | + end |
| 73 | + |
| 74 | + it 'returns error when no student IDs provided' do |
| 75 | + delete "/api/schools/#{school.id}/students/batch", |
| 76 | + headers: headers |
| 77 | + |
| 78 | + expect(response).to have_http_status(:unprocessable_entity) |
| 79 | + json = JSON.parse(response.body) |
| 80 | + expect(json['error']).to eq('No student IDs provided') |
| 81 | + end |
| 82 | + |
| 83 | + context 'when validating input parameters' do |
| 84 | + it 'removes duplicate student IDs before processing' do |
| 85 | + duplicate_ids = [student_1.id, student_1.id, student_2.id, student_2.id, student_1.id] |
| 86 | + |
| 87 | + delete "/api/schools/#{school.id}/students/batch", |
| 88 | + params: { student_ids: duplicate_ids }, |
| 89 | + headers: headers |
| 90 | + |
| 91 | + expect(response).to have_http_status(:ok) |
| 92 | + json = JSON.parse(response.body) |
| 93 | + # Should only process 2 unique students |
| 94 | + expect(json['results'].size).to eq(2) |
| 95 | + expect(json['results'].pluck('user_id')).to contain_exactly(student_1.id, student_2.id) |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context 'when handling non-existent student IDs' do |
| 100 | + it 'skips non-existent students and processes valid ones' do |
| 101 | + non_existent_id = SecureRandom.uuid |
| 102 | + project_1 = create(:project, user_id: student_1.id) |
| 103 | + |
| 104 | + delete "/api/schools/#{school.id}/students/batch", |
| 105 | + params: { student_ids: [student_1.id, non_existent_id] }, |
| 106 | + headers: headers |
| 107 | + |
| 108 | + expect(response).to have_http_status(:ok) |
| 109 | + |
| 110 | + json = JSON.parse(response.body) |
| 111 | + expect(json['results'].size).to eq(2) |
| 112 | + |
| 113 | + # Valid student should be removed |
| 114 | + valid_result = json['results'].find { |r| r['user_id'] == student_1.id } |
| 115 | + expect(valid_result['error']).to be_nil |
| 116 | + expect(Project.exists?(project_1.id)).to be false |
| 117 | + expect(ClassStudent.where(student_id: student_1.id)).not_to exist |
| 118 | + |
| 119 | + # Non-existent student should be skipped |
| 120 | + skipped_result = json['results'].find { |r| r['user_id'] == non_existent_id } |
| 121 | + expect(skipped_result['skipped']).to be true |
| 122 | + expect(skipped_result['reason']).to eq('no_role_in_school') |
| 123 | + end |
| 124 | + |
| 125 | + it 'returns success when all IDs are non-existent' do |
| 126 | + non_existent_id_1 = SecureRandom.uuid |
| 127 | + non_existent_id_2 = SecureRandom.uuid |
| 128 | + |
| 129 | + delete "/api/schools/#{school.id}/students/batch", |
| 130 | + params: { student_ids: [non_existent_id_1, non_existent_id_2] }, |
| 131 | + headers: headers |
| 132 | + |
| 133 | + expect(response).to have_http_status(:ok) |
| 134 | + |
| 135 | + json = JSON.parse(response.body) |
| 136 | + expect(json['results'].size).to eq(2) |
| 137 | + expect(json['results'].all? { |r| r['skipped'] == true }).to be true |
| 138 | + expect(json['results'].all? { |r| r['reason'] == 'no_role_in_school' }).to be true |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context 'when handling students from different schools' do |
| 143 | + let(:other_school) { create(:school) } |
| 144 | + let(:other_school_owner) { create(:owner, school: other_school) } |
| 145 | + let(:other_student) { create(:student, school: other_school) } |
| 146 | + |
| 147 | + before do |
| 148 | + other_school_class = create(:school_class, school: other_school) |
| 149 | + create(:class_student, student_id: other_student.id, school_class: other_school_class) |
| 150 | + end |
| 151 | + |
| 152 | + it 'skips students from different schools and processes own students' do |
| 153 | + project_1 = create(:project, user_id: student_1.id) |
| 154 | + other_project = create(:project, user_id: other_student.id) |
| 155 | + |
| 156 | + delete "/api/schools/#{school.id}/students/batch", |
| 157 | + params: { student_ids: [student_1.id, other_student.id] }, |
| 158 | + headers: headers |
| 159 | + |
| 160 | + expect(response).to have_http_status(:ok) |
| 161 | + |
| 162 | + json = JSON.parse(response.body) |
| 163 | + expect(json['results'].size).to eq(2) |
| 164 | + |
| 165 | + # Own student should be removed |
| 166 | + valid_result = json['results'].find { |r| r['user_id'] == student_1.id } |
| 167 | + expect(valid_result['error']).to be_nil |
| 168 | + expect(Project.exists?(project_1.id)).to be false |
| 169 | + expect(ClassStudent.where(student_id: student_1.id)).not_to exist |
| 170 | + |
| 171 | + # Other school's student should be skipped |
| 172 | + skipped_result = json['results'].find { |r| r['user_id'] == other_student.id } |
| 173 | + expect(skipped_result['skipped']).to be true |
| 174 | + expect(skipped_result['reason']).to eq('no_role_in_school') |
| 175 | + |
| 176 | + # Other school's student data should remain intact |
| 177 | + expect(Project.exists?(other_project.id)).to be true |
| 178 | + expect(Role.where(user_id: other_student.id, school_id: other_school.id, role: :student)).to exist |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + context 'when handling partial failures' do |
| 183 | + it 'returns 200 OK with error details when some deletions fail' do |
| 184 | + project_2 = create(:project, user_id: student_2.id) |
| 185 | + |
| 186 | + # Simulate ProfileApiClient failure for one student |
| 187 | + allow(ProfileApiClient).to receive(:delete_school_student).with( |
| 188 | + token: UserProfileMock::TOKEN, |
| 189 | + school_id: school.id, |
| 190 | + student_id: student_1.id |
| 191 | + ).and_raise(StandardError, 'Profile API error') |
| 192 | + |
| 193 | + allow(ProfileApiClient).to receive(:delete_school_student).with( |
| 194 | + token: UserProfileMock::TOKEN, |
| 195 | + school_id: school.id, |
| 196 | + student_id: student_2.id |
| 197 | + ).and_return(true) |
| 198 | + |
| 199 | + delete "/api/schools/#{school.id}/students/batch", |
| 200 | + params: { student_ids: [student_1.id, student_2.id] }, |
| 201 | + headers: headers |
| 202 | + |
| 203 | + expect(response).to have_http_status(:ok) |
| 204 | + |
| 205 | + json = JSON.parse(response.body) |
| 206 | + expect(json['results'].size).to eq(2) |
| 207 | + expect(json['error']).to eq('1 student(s) failed to be removed') |
| 208 | + |
| 209 | + # First student should have error |
| 210 | + failed_result = json['results'].find { |r| r['user_id'] == student_1.id } |
| 211 | + expect(failed_result['error']).to match(/StandardError: Profile API error/) |
| 212 | + |
| 213 | + # Second student should succeed |
| 214 | + success_result = json['results'].find { |r| r['user_id'] == student_2.id } |
| 215 | + expect(success_result['error']).to be_nil |
| 216 | + expect(Project.exists?(project_2.id)).to be false |
| 217 | + expect(ClassStudent.where(student_id: student_2.id)).not_to exist |
| 218 | + end |
| 219 | + |
| 220 | + it 'reports correct error count when multiple deletions fail' do |
| 221 | + allow(ProfileApiClient).to receive(:delete_school_student).and_raise(StandardError, 'Profile API down') |
| 222 | + |
| 223 | + delete "/api/schools/#{school.id}/students/batch", |
| 224 | + params: { student_ids: [student_1.id, student_2.id] }, |
| 225 | + headers: headers |
| 226 | + |
| 227 | + expect(response).to have_http_status(:ok) |
| 228 | + |
| 229 | + json = JSON.parse(response.body) |
| 230 | + expect(json['results'].size).to eq(2) |
| 231 | + expect(json['error']).to eq('2 student(s) failed to be removed') |
| 232 | + expect(json['results'].all? { |r| r['error'].present? }).to be true |
| 233 | + end |
| 234 | + end |
| 235 | + end |
| 236 | +end |
0 commit comments