|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Profile auth check API' do |
| 6 | + let(:headers) { { Authorization: UserProfileMock::TOKEN } } |
| 7 | + let(:school) { create(:school) } |
| 8 | + let(:student) { create(:student, school:) } |
| 9 | + |
| 10 | + identity_url = "#{ENV.fetch('IDENTITY_URL')}/api/v1/access" |
| 11 | + |
| 12 | + describe 'GET /api/profile_auth_check' do |
| 13 | + context 'when the profile API authorises the current user' do |
| 14 | + it 'returns can_use_profile_api: true' do |
| 15 | + # Arrange |
| 16 | + authenticated_in_hydra_as(student) |
| 17 | + stub_request(:get, identity_url).to_return(status: 200, headers:) |
| 18 | + |
| 19 | + # Act |
| 20 | + get '/api/profile_auth_check', headers: headers |
| 21 | + |
| 22 | + # Assert |
| 23 | + expect(response).to have_http_status(:ok) |
| 24 | + expect(response.parsed_body).to eq('can_use_profile_api' => true) |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + context 'when the profile API returns unauthorized' do |
| 29 | + it 'returns can_use_profile_api: false' do |
| 30 | + # Arrange |
| 31 | + authenticated_in_hydra_as(student) |
| 32 | + stub_request(:get, identity_url).to_return(status: 401, headers:) |
| 33 | + |
| 34 | + # Act |
| 35 | + get '/api/profile_auth_check', headers: headers |
| 36 | + |
| 37 | + # Assert |
| 38 | + expect(response).to have_http_status(:ok) |
| 39 | + expect(response.parsed_body).to eq('can_use_profile_api' => false) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context 'when there is no current user' do |
| 44 | + it 'returns can_use_profile_api: false' do |
| 45 | + # Arrange |
| 46 | + stub_request(:get, identity_url).to_return(status: 400, headers:) |
| 47 | + |
| 48 | + # Act |
| 49 | + get '/api/profile_auth_check' |
| 50 | + |
| 51 | + # Assert |
| 52 | + expect(response).to have_http_status(:ok) |
| 53 | + expect(response.parsed_body).to eq('can_use_profile_api' => false) |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | +end |
0 commit comments