|
| 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 | + it 'deletes all students and their projects' do |
| 23 | + project_1 = create(:project, user_id: student_1.id) |
| 24 | + project_2 = create(:project, user_id: student_2.id) |
| 25 | + |
| 26 | + expect(ClassStudent.where(student_id: student_1.id)).to exist |
| 27 | + expect(ClassStudent.where(student_id: student_2.id)).to exist |
| 28 | + expect(Project.where(id: project_1.id)).to exist |
| 29 | + expect(Project.where(id: project_2.id)).to exist |
| 30 | + |
| 31 | + delete "/api/schools/#{school.id}/students/batch", |
| 32 | + params: { student_ids: [student_1.id, student_2.id] }, |
| 33 | + headers: headers |
| 34 | + |
| 35 | + expect(response).to have_http_status(:ok) |
| 36 | + |
| 37 | + json = JSON.parse(response.body) |
| 38 | + expect(json['results'].size).to eq(2) |
| 39 | + expect(json['results'].all? { |r| r['error'].nil? }).to be true |
| 40 | + |
| 41 | + # Verify students removed from classes |
| 42 | + expect(ClassStudent.where(student_id: student_1.id)).not_to exist |
| 43 | + expect(ClassStudent.where(student_id: student_2.id)).not_to exist |
| 44 | + |
| 45 | + # Verify projects deleted |
| 46 | + expect(Project.where(id: project_1.id)).not_to exist |
| 47 | + expect(Project.where(id: project_2.id)).not_to exist |
| 48 | + end |
| 49 | + |
| 50 | + it 'returns error when no student IDs provided' do |
| 51 | + delete "/api/schools/#{school.id}/students/batch", |
| 52 | + headers: headers |
| 53 | + |
| 54 | + expect(response).to have_http_status(:unprocessable_entity) |
| 55 | + json = JSON.parse(response.body) |
| 56 | + expect(json['error']).to eq('No student IDs provided') |
| 57 | + end |
| 58 | + end |
| 59 | +end |
0 commit comments