|
| 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 | + let(:api_url) { 'http://example.com' } |
| 10 | + let(:api_key) { 'api-key' } |
| 11 | + |
| 12 | + before do |
| 13 | + allow(ENV).to receive(:fetch).and_call_original |
| 14 | + allow(ENV).to receive(:fetch).with('IDENTITY_URL').and_return(api_url) |
| 15 | + allow(ENV).to receive(:fetch).with('PROFILE_API_KEY').and_return(api_key) |
| 16 | + end |
| 17 | + |
| 18 | + describe 'GET /api/profile_auth_check' do |
| 19 | + context 'when the profile API authorises the current user' do |
| 20 | + it 'returns can_use_profile_api: true' do |
| 21 | + # Arrange |
| 22 | + authenticated_in_hydra_as(student) |
| 23 | + stub_request(:get, "#{ENV.fetch('IDENTITY_URL')}/api/v1/access").to_return(status: 200, headers:) |
| 24 | + |
| 25 | + # Act |
| 26 | + get '/api/profile_auth_check', headers: headers |
| 27 | + |
| 28 | + # Assert |
| 29 | + expect(response).to have_http_status(:ok) |
| 30 | + expect(response.parsed_body).to eq('can_use_profile_api' => true) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + context 'when the profile API returns unauthorized' do |
| 35 | + it 'returns can_use_profile_api: false' do |
| 36 | + # Arrange |
| 37 | + authenticated_in_hydra_as(student) |
| 38 | + stub_request(:get, "#{ENV.fetch('IDENTITY_URL')}/api/v1/access").to_return(status: 401, headers:) |
| 39 | + |
| 40 | + # Act |
| 41 | + get '/api/profile_auth_check', headers: headers |
| 42 | + |
| 43 | + # Assert |
| 44 | + expect(response).to have_http_status(:ok) |
| 45 | + expect(response.parsed_body).to eq('can_use_profile_api' => false) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context 'when there is no current user' do |
| 50 | + it 'returns can_use_profile_api: false' do |
| 51 | + # Arrange |
| 52 | + stub_request(:get, "#{ENV.fetch('IDENTITY_URL')}/api/v1/access").to_return(status: 400, headers:) |
| 53 | + |
| 54 | + # Act |
| 55 | + get '/api/profile_auth_check' |
| 56 | + |
| 57 | + # Assert |
| 58 | + expect(response).to have_http_status(:ok) |
| 59 | + expect(response.parsed_body).to eq('can_use_profile_api' => false) |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments