|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Viewing an invitations', type: :request do |
| 6 | + include ActiveSupport::Testing::TimeHelpers |
| 7 | + |
| 8 | + let(:user) { create(:user) } |
| 9 | + let(:headers) { { Authorization: UserProfileMock::TOKEN } } |
| 10 | + |
| 11 | + context 'when user is not logged in' do |
| 12 | + it 'responds 401 Unauthorized' do |
| 13 | + get('/api/teacher_invitations/fake-token') |
| 14 | + expect(response).to have_http_status(:unauthorized) |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + context 'when user is logged in' do |
| 19 | + before do |
| 20 | + authenticated_in_hydra_as(user) |
| 21 | + end |
| 22 | + |
| 23 | + context 'when invitation does not exist' do |
| 24 | + let(:invitation) { build(:invitation) } |
| 25 | + let!(:token) { invitation.generate_token_for(:teacher_invitation) } |
| 26 | + |
| 27 | + it 'responds 404 Not Found' do |
| 28 | + get("/api/teacher_invitations/#{token}", headers:) |
| 29 | + expect(response).to have_http_status(:not_found) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + context 'when invitation exists' do |
| 34 | + let(:invitation_email) { user.email } |
| 35 | + let(:invitation) { create(:invitation, email_address: invitation_email) } |
| 36 | + let!(:token) { invitation.generate_token_for(:teacher_invitation) } |
| 37 | + |
| 38 | + context 'when invitation token is not valid because invitation email has changed' do |
| 39 | + before do |
| 40 | + invitation.update!(email_address: "not-#{invitation.email_address}") |
| 41 | + end |
| 42 | + |
| 43 | + it 'responds 403 Forbidden' do |
| 44 | + get("/api/teacher_invitations/#{token}", headers:) |
| 45 | + expect(response).to have_http_status(:forbidden) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context 'when invitation token is not valid because token has expired' do |
| 50 | + it 'responds 403 Forbidden' do |
| 51 | + travel 31.days do |
| 52 | + get("/api/teacher_invitations/#{token}", headers:) |
| 53 | + end |
| 54 | + expect(response).to have_http_status(:forbidden) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context 'when invitation email does not match user email' do |
| 59 | + let(:invitation_email) { "not-#{user.email}" } |
| 60 | + |
| 61 | + it 'responds 403 Forbidden' do |
| 62 | + get("/api/teacher_invitations/#{token}", headers:) |
| 63 | + expect(response).to have_http_status(:forbidden) |
| 64 | + end |
| 65 | + |
| 66 | + it 'includes error message in response' do |
| 67 | + get("/api/teacher_invitations/#{token}", headers:) |
| 68 | + |
| 69 | + json = JSON.parse(response.body) |
| 70 | + expect(json['error']).to eq('Invitation email does not match user email') |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context 'when invitation token is valid' do |
| 75 | + it 'responds 200 OK' do |
| 76 | + get("/api/teacher_invitations/#{token}", headers:) |
| 77 | + expect(response).to have_http_status(:ok) |
| 78 | + end |
| 79 | + |
| 80 | + it 'includes school name in response' do |
| 81 | + get("/api/teacher_invitations/#{token}", headers:) |
| 82 | + |
| 83 | + json = JSON.parse(response.body) |
| 84 | + expect(json['school_name']).to eq(invitation.school_name) |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments