Skip to content

Commit 2b1e126

Browse files
committed
Refactor the stubbed_users methods out of UserInfoApiClient; refactor tests.
1 parent 6ea9cfc commit 2b1e126

File tree

7 files changed

+88
-108
lines changed

7 files changed

+88
-108
lines changed

lib/user_info_api_client.rb

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ 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?
1110

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

2120
def find_user_by_email(email)
2221
return nil if email.blank?
23-
return stubbed_user_by_email(email) if bypass_oauth?
2422

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

3735
private
3836

39-
def bypass_oauth?
40-
ENV.fetch('BYPASS_OAUTH', nil) == 'true'
41-
end
42-
4337
def transform_user(user)
4438
user.transform_keys { |k| k.to_s.underscore.to_sym }
4539
end
@@ -59,49 +53,5 @@ def conn
5953
f.response :json # decode response bodies as JSON
6054
end
6155
end
62-
63-
def stubbed_users(user_ids)
64-
user_ids.map do |user_id|
65-
{
66-
id: user_id,
67-
email: "user-#{user_id}@example.com",
68-
username: nil,
69-
parentalEmail: nil,
70-
name: 'School Owner',
71-
nickname: 'Owner',
72-
country: 'United Kingdom',
73-
country_code: 'GB',
74-
postcode: nil,
75-
dateOfBirth: nil,
76-
verifiedAt: '2024-01-01T12:00:00.000Z',
77-
createdAt: '2024-01-01T12:00:00.000Z',
78-
updatedAt: '2024-01-01T12:00:00.000Z',
79-
discardedAt: nil,
80-
lastLoggedInAt: '2024-01-01T12:00:00.000Z',
81-
roles: ''
82-
}
83-
end
84-
end
85-
86-
def stubbed_user_by_email(email)
87-
{
88-
id: Digest::UUID.uuid_v5(Digest::UUID::DNS_NAMESPACE, email),
89-
email: email,
90-
username: nil,
91-
parentalEmail: nil,
92-
name: 'School Owner',
93-
nickname: 'Owner',
94-
country: 'United Kingdom',
95-
country_code: 'GB',
96-
postcode: nil,
97-
dateOfBirth: nil,
98-
verifiedAt: '2024-01-01T12:00:00.000Z',
99-
createdAt: '2024-01-01T12:00:00.000Z',
100-
updatedAt: '2024-01-01T12:00:00.000Z',
101-
discardedAt: nil,
102-
lastLoggedInAt: '2024-01-01T12:00:00.000Z',
103-
roles: ''
104-
}
105-
end
10656
end
10757
end

spec/features/school/importing_schools_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
before do
2828
authenticated_in_hydra_as(admin_user)
29-
allow(UserInfoApiClient).to receive(:find_user_by_email).and_return({ id: SecureRandom.uuid, email: '[email protected]' })
29+
stub_user_info_api_find_by_email(
30+
31+
user: { id: SecureRandom.uuid, email: '[email protected]' }
32+
)
3033
allow(SchoolImportJob).to receive(:perform_later).and_return(instance_double(SchoolImportJob, job_id: 'test-job-id'))
3134
end
3235

spec/jobs/school_import_job_spec.rb

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@
3131
end
3232

3333
before do
34-
allow(UserInfoApiClient).to receive(:find_user_by_email)
35-
36-
.and_return({ id: owner1_id, email: '[email protected]' })
37-
38-
allow(UserInfoApiClient).to receive(:find_user_by_email)
39-
40-
.and_return({ id: owner2_id, email: '[email protected]' })
41-
4234
allow(ProfileApiClient).to receive(:create_school).and_return(true)
4335
end
4436

4537
context 'when all schools can be created successfully' do
38+
before do
39+
stub_user_info_api_find_by_email(
40+
41+
user: { id: owner1_id, email: '[email protected]' }
42+
)
43+
44+
stub_user_info_api_find_by_email(
45+
46+
user: { id: owner2_id, email: '[email protected]' }
47+
)
48+
end
49+
4650
it 'creates schools and returns successful results' do
4751
results = described_class.new.perform(
4852
schools_data: schools_data,
@@ -67,9 +71,7 @@
6771

6872
context 'when owner email is not found' do
6973
before do
70-
allow(UserInfoApiClient).to receive(:find_user_by_email)
71-
72-
.and_return(nil)
74+
stub_user_info_api_find_by_email(email: '[email protected]', user: nil)
7375
end
7476

7577
it 'adds failed result for that school' do
@@ -91,6 +93,11 @@
9193

9294
before do
9395
Role.owner.create!(school_id: existing_school.id, user_id: owner1_id)
96+
97+
stub_user_info_api_find_by_email(
98+
99+
user: { id: owner1_id, email: '[email protected]' }
100+
)
94101
end
95102

96103
it 'adds failed result for that school' do
@@ -123,9 +130,10 @@
123130
end
124131

125132
before do
126-
allow(UserInfoApiClient).to receive(:find_user_by_email)
127-
128-
.and_return({ id: owner1_id, email: '[email protected]' })
133+
stub_user_info_api_find_by_email(
134+
135+
user: { id: owner1_id, email: '[email protected]' }
136+
)
129137
end
130138

131139
it 'handles string keys correctly' do

spec/models/user_spec.rb

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,6 @@
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
9376
end
9477

9578
context 'when logged into a student account' do
@@ -370,25 +353,6 @@
370353
it 'returns a user with the correct email' do
371354
expect(user.email).to eq '[email protected]'
372355
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
392356
end
393357

394358
describe '#schools' do

spec/rails_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
config.include PhraseIdentifierMock
8686
config.include ProfileApiMock
8787
config.include UserProfileMock
88+
config.include UserInfoApiMock
8889

8990
config.include SignInStubs, type: :request
9091
config.include SignInStubs, type: :system

spec/requests/admin/school_import_results_spec.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
sign_in_as(admin_user)
1010

1111
# Stub UserInfoApiClient to avoid external API calls
12-
allow(UserInfoApiClient).to receive(:fetch_by_ids).and_return([
13-
{
14-
id: admin_user.id,
15-
name: 'Test Admin',
16-
17-
}
18-
])
12+
stub_user_info_api_fetch_by_ids(
13+
user_ids: [admin_user.id],
14+
users: [{
15+
id: admin_user.id,
16+
name: 'Test Admin',
17+
18+
}]
19+
)
1920
end
2021

2122
describe 'GET /admin/school_import_results' do

spec/support/user_info_api_mock.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
module UserInfoApiMock
4+
def stub_user_info_api_fetch_by_ids(user_ids:, users: [])
5+
users = default_stubbed_users(user_ids) if users.empty?
6+
7+
allow(UserInfoApiClient).to receive(:fetch_by_ids)
8+
.with(user_ids)
9+
.and_return(users)
10+
end
11+
12+
def stub_user_info_api_find_by_email(email:, user: :not_provided)
13+
user = default_stubbed_user_by_email(email) if user == :not_provided
14+
15+
allow(UserInfoApiClient).to receive(:find_user_by_email)
16+
.with(email)
17+
.and_return(user)
18+
end
19+
20+
private
21+
22+
def default_stubbed_users(user_ids)
23+
user_ids.map { |user_id| default_stubbed_user(id: user_id, email: "user-#{user_id}@example.com") }
24+
end
25+
26+
def default_stubbed_user_by_email(email)
27+
default_stubbed_user(
28+
id: Digest::UUID.uuid_v5(Digest::UUID::DNS_NAMESPACE, email),
29+
email: email
30+
)
31+
end
32+
33+
def default_stubbed_user(id:, email:)
34+
{
35+
id: id,
36+
email: email,
37+
username: nil,
38+
parental_email: nil,
39+
name: 'School Owner',
40+
nickname: 'Owner',
41+
country: 'United Kingdom',
42+
country_code: 'GB',
43+
postcode: nil,
44+
date_of_birth: nil,
45+
verified_at: '2024-01-01T12:00:00.000Z',
46+
created_at: '2024-01-01T12:00:00.000Z',
47+
updated_at: '2024-01-01T12:00:00.000Z',
48+
discarded_at: nil,
49+
last_logged_in_at: '2024-01-01T12:00:00.000Z',
50+
roles: ''
51+
}
52+
end
53+
end

0 commit comments

Comments
 (0)