Skip to content

Commit 5f26d02

Browse files
committed
Retain BYPASS_OAUTH funcationlity but defer to stubs.
1 parent dfa6b03 commit 5f26d02

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lib/user_info_api_client.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class UserInfoApiClient
77
class << self
88
def fetch_by_ids(user_ids)
99
return [] if user_ids.blank?
10+
return stubbed_users(user_ids) if bypass_oauth?
1011

1112
response = conn.get do |r|
1213
r.url '/users'
@@ -19,6 +20,7 @@ def fetch_by_ids(user_ids)
1920

2021
def find_user_by_email(email)
2122
return nil if email.blank?
23+
return stubbed_user_by_email(email) if bypass_oauth?
2224

2325
response = conn.get do |r|
2426
r.url "/users/#{CGI.escape(email)}"
@@ -34,6 +36,10 @@ def find_user_by_email(email)
3436

3537
private
3638

39+
def bypass_oauth?
40+
ENV.fetch('BYPASS_OAUTH', nil) == 'true'
41+
end
42+
3743
def transform_user(user)
3844
user.transform_keys { |k| k.to_s.underscore.to_sym }
3945
end
@@ -53,5 +59,17 @@ def conn
5359
f.response :json # decode response bodies as JSON
5460
end
5561
end
62+
63+
# Development/test stubbing methods - only active when BYPASS_OAUTH=true
64+
# Delegates to UserInfoApiMock to avoid code duplication
65+
def stubbed_users(user_ids)
66+
require_relative '../spec/support/user_info_api_mock' unless defined?(UserInfoApiMock)
67+
UserInfoApiMock.default_stubbed_users(user_ids)
68+
end
69+
70+
def stubbed_user_by_email(email)
71+
require_relative '../spec/support/user_info_api_mock' unless defined?(UserInfoApiMock)
72+
UserInfoApiMock.default_stubbed_user_by_email(email)
73+
end
5674
end
5775
end

spec/models/user_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@
7373
it 'returns a user without a username' do
7474
expect(user.username).to be_nil
7575
end
76+
77+
context 'when BYPASS_OAUTH is true' do
78+
around do |example|
79+
ClimateControl.modify(BYPASS_OAUTH: 'true') do
80+
example.run
81+
end
82+
end
83+
84+
it 'does not call the API' do
85+
user
86+
expect(WebMock).not_to have_requested(:get, /.*/)
87+
end
88+
89+
it 'returns a stubbed user' do
90+
expect(user.name).to eq('School Owner')
91+
end
92+
end
7693
end
7794

7895
context 'when logged into a student account' do
@@ -353,6 +370,25 @@
353370
it 'returns a user with the correct email' do
354371
expect(user.email).to eq 'school-owner@example.com'
355372
end
373+
374+
context 'when BYPASS_OAUTH is true' do
375+
around do |example|
376+
ClimateControl.modify(BYPASS_OAUTH: 'true') do
377+
example.run
378+
end
379+
end
380+
381+
let(:owner) { create(:owner, school:, id: '00000000-0000-0000-0000-000000000000') }
382+
383+
it 'does not call the API' do
384+
user
385+
expect(WebMock).not_to have_requested(:get, /.*/)
386+
end
387+
388+
it 'returns a stubbed user' do
389+
expect(user.name).to eq('School Owner')
390+
end
391+
end
356392
end
357393

358394
describe '#schools' do

spec/support/user_info_api_mock.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,7 @@ def default_stubbed_user(id:, email:)
5050
roles: ''
5151
}
5252
end
53+
54+
# Class methods for use by UserInfoApiClient when BYPASS_OAUTH=true
55+
module_function :default_stubbed_users, :default_stubbed_user_by_email, :default_stubbed_user
5356
end

0 commit comments

Comments
 (0)