|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Google Auth requests' do |
| 6 | + let(:headers) { { Authorization: UserProfileMock::TOKEN } } |
| 7 | + let(:school) { create(:school) } |
| 8 | + let(:owner) { create(:owner, school:) } |
| 9 | + |
| 10 | + before do |
| 11 | + authenticated_in_hydra_as(owner) |
| 12 | + end |
| 13 | + |
| 14 | + describe 'POST /api/google_auth/exchange_code' do |
| 15 | + let(:params) do |
| 16 | + { |
| 17 | + google_auth: { |
| 18 | + code: 'test-authorization-code', |
| 19 | + redirect_uri: 'https://example.com/callback' |
| 20 | + } |
| 21 | + } |
| 22 | + end |
| 23 | + |
| 24 | + let(:google_token_response) do |
| 25 | + { |
| 26 | + 'access_token' => 'test-access-token', |
| 27 | + 'expires_in' => 3599, |
| 28 | + 'token_type' => 'Bearer', |
| 29 | + 'scope' => 'openid email profile', |
| 30 | + 'id_token' => 'test-id-token' |
| 31 | + } |
| 32 | + end |
| 33 | + |
| 34 | + around do |example| |
| 35 | + ClimateControl.modify( |
| 36 | + GOOGLE_CLIENT_ID: 'test-client-id', |
| 37 | + GOOGLE_CLIENT_SECRET: 'test-client-secret' |
| 38 | + ) do |
| 39 | + example.run |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context 'when token exchange is successful' do |
| 44 | + before do |
| 45 | + stub_request(:post, Api::GoogleAuthController::TOKEN_EXCHANGE_URL) |
| 46 | + .with( |
| 47 | + body: { |
| 48 | + code: 'test-authorization-code', |
| 49 | + client_id: 'test-client-id', |
| 50 | + client_secret: 'test-client-secret', |
| 51 | + redirect_uri: 'https://example.com/callback', |
| 52 | + grant_type: 'authorization_code' |
| 53 | + } |
| 54 | + ) |
| 55 | + .to_return( |
| 56 | + status: 200, |
| 57 | + body: google_token_response.to_json, |
| 58 | + headers: { 'Content-Type' => 'application/json' } |
| 59 | + ) |
| 60 | + end |
| 61 | + |
| 62 | + it 'returns success response' do |
| 63 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 64 | + expect(response).to have_http_status(:ok) |
| 65 | + end |
| 66 | + |
| 67 | + it 'returns token response from Google' do |
| 68 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 69 | + expect(response.parsed_body).to eq(google_token_response) |
| 70 | + end |
| 71 | + |
| 72 | + it 'includes access_token in response' do |
| 73 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 74 | + expect(response.parsed_body['access_token']).to eq('test-access-token') |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + context 'when token exchange fails with error from Google' do |
| 79 | + let(:error_response) do |
| 80 | + { |
| 81 | + 'error' => 'invalid_grant', |
| 82 | + 'error_description' => 'Bad Request' |
| 83 | + } |
| 84 | + end |
| 85 | + |
| 86 | + before do |
| 87 | + stub_request(:post, Api::GoogleAuthController::TOKEN_EXCHANGE_URL) |
| 88 | + .to_return( |
| 89 | + status: 400, |
| 90 | + body: error_response.to_json, |
| 91 | + headers: { 'Content-Type' => 'application/json' } |
| 92 | + ) |
| 93 | + end |
| 94 | + |
| 95 | + it 'returns unauthorized response' do |
| 96 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 97 | + expect(response).to have_http_status(:unauthorized) |
| 98 | + end |
| 99 | + |
| 100 | + it 'returns error message' do |
| 101 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 102 | + expect(response.parsed_body['error']).to eq('Bad Request') |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + context 'when network error occurs' do |
| 107 | + before do |
| 108 | + stub_request(:post, Api::GoogleAuthController::TOKEN_EXCHANGE_URL) |
| 109 | + .to_raise(Faraday::ConnectionFailed.new('Connection failed')) |
| 110 | + end |
| 111 | + |
| 112 | + it 'returns service unavailable response' do |
| 113 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 114 | + expect(response).to have_http_status(:service_unavailable) |
| 115 | + end |
| 116 | + |
| 117 | + it 'returns error message' do |
| 118 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 119 | + expect(response.parsed_body['error']).to eq('Connection failed') |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + context 'when code parameter is missing' do |
| 124 | + let(:params) do |
| 125 | + { |
| 126 | + google_auth: { |
| 127 | + redirect_uri: 'https://example.com/callback' |
| 128 | + } |
| 129 | + } |
| 130 | + end |
| 131 | + |
| 132 | + it 'returns bad request response' do |
| 133 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 134 | + expect(response).to have_http_status(:bad_request) |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + context 'when redirect_uri parameter is missing' do |
| 139 | + let(:params) do |
| 140 | + { |
| 141 | + google_auth: { |
| 142 | + code: 'test-authorization-code' |
| 143 | + } |
| 144 | + } |
| 145 | + end |
| 146 | + |
| 147 | + it 'returns bad request response' do |
| 148 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 149 | + expect(response).to have_http_status(:bad_request) |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + context 'when google_auth params are missing' do |
| 154 | + it 'returns bad request response' do |
| 155 | + post('/api/google/auth/exchange-code', headers:) |
| 156 | + expect(response).to have_http_status(:bad_request) |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + context 'when user is not authenticated' do |
| 161 | + before do |
| 162 | + unauthenticated_in_hydra |
| 163 | + end |
| 164 | + |
| 165 | + it 'returns unauthorized response' do |
| 166 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 167 | + expect(response).to have_http_status(:unauthorized) |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + context 'when user is not authorized' do |
| 172 | + let(:student) { create(:student, school:) } |
| 173 | + |
| 174 | + before do |
| 175 | + authenticated_in_hydra_as(student) |
| 176 | + end |
| 177 | + |
| 178 | + it 'returns forbidden response' do |
| 179 | + post('/api/google/auth/exchange-code', params:, headers:) |
| 180 | + expect(response).to have_http_status(:forbidden) |
| 181 | + end |
| 182 | + end |
| 183 | + end |
| 184 | +end |
0 commit comments