|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Listing school email domains', type: :request do |
| 6 | + let(:headers) { { Authorization: UserProfileMock::TOKEN } } |
| 7 | + let(:school) { create(:school) } |
| 8 | + let(:school_email_domains) { create_list(:school_email_domain, 5, school: school) } |
| 9 | + |
| 10 | + before do |
| 11 | + school_email_domains |
| 12 | + end |
| 13 | + |
| 14 | + describe '#index' do |
| 15 | + shared_examples 'a successful school email domains index' do |
| 16 | + it 'responds 200 OK' do |
| 17 | + expect(response).to have_http_status(:ok) |
| 18 | + end |
| 19 | + |
| 20 | + context 'when the school has domains' do |
| 21 | + it 'returns a list of domains for the school' do |
| 22 | + data = JSON.parse(response.body, symbolize_names: true) |
| 23 | + expect(data).to match_array(school.school_email_domains.pluck(:domain)) |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + context 'when domains do not belong to the school' do |
| 28 | + let(:other_school) { create(:school) } |
| 29 | + let(:school_email_domains) { create_list(:school_email_domain, 5, school: other_school) } |
| 30 | + |
| 31 | + it 'returns an empty array' do |
| 32 | + data = JSON.parse(response.body, symbolize_names: true) |
| 33 | + expect(data).to eq([]) |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'with an authorised owner' do |
| 39 | + let(:owner) { create(:owner, school:, name: 'School Owner') } |
| 40 | + |
| 41 | + before do |
| 42 | + authenticated_in_hydra_as(owner) |
| 43 | + get("/api/schools/#{school.id}/school_email_domains", headers:) |
| 44 | + end |
| 45 | + |
| 46 | + it_behaves_like 'a successful school email domains index' |
| 47 | + end |
| 48 | + |
| 49 | + context 'with an authorised teacher' do |
| 50 | + let(:teacher) { create(:teacher, school:, name: 'School Teacher') } |
| 51 | + |
| 52 | + before do |
| 53 | + authenticated_in_hydra_as(teacher) |
| 54 | + get("/api/schools/#{school.id}/school_email_domains", headers:) |
| 55 | + end |
| 56 | + |
| 57 | + it_behaves_like 'a successful school email domains index' |
| 58 | + end |
| 59 | + |
| 60 | + context 'when the user does not have access' do |
| 61 | + it 'responds 403 Forbidden when the user is a school-owner for a different school' do |
| 62 | + other_school = create(:school) |
| 63 | + other_owner = create(:owner, school: other_school, name: 'School Owner') |
| 64 | + authenticated_in_hydra_as(other_owner) |
| 65 | + |
| 66 | + get("/api/schools/#{school.id}/school_email_domains", headers:) |
| 67 | + expect(response).to have_http_status(:forbidden) |
| 68 | + end |
| 69 | + |
| 70 | + it 'responds 403 Forbidden when the user is a school-teacher for a different school' do |
| 71 | + other_school = create(:school) |
| 72 | + other_teacher = create(:teacher, school: other_school, name: 'School Teacher') |
| 73 | + authenticated_in_hydra_as(other_teacher) |
| 74 | + |
| 75 | + get("/api/schools/#{school.id}/school_email_domains", headers:) |
| 76 | + expect(response).to have_http_status(:forbidden) |
| 77 | + end |
| 78 | + |
| 79 | + it 'responds 403 Forbidden when the user is a student at the school' do |
| 80 | + student = create(:student, school:) |
| 81 | + authenticated_in_hydra_as(student) |
| 82 | + |
| 83 | + get("/api/schools/#{school.id}/school_email_domains", headers:) |
| 84 | + expect(response).to have_http_status(:forbidden) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context 'when the user is not authenticated' do |
| 89 | + it 'responds 401 Unauthorized when no token is given' do |
| 90 | + get "/api/schools/#{school.id}/school_email_domains" |
| 91 | + expect(response).to have_http_status(:unauthorized) |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments