Skip to content

Commit 7bad25c

Browse files
Add update_school_email_domains to ProfileApiClient
Allow the client to PATCH a collection of domains
1 parent d82883f commit 7bad25c

3 files changed

Lines changed: 90 additions & 12 deletions

File tree

lib/profile_api_client.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ProfileApiClient
77
}.freeze
88

99
# rubocop:disable Naming/MethodName
10-
School = Data.define(:id, :schoolCode, :updatedAt, :createdAt, :discardedAt)
10+
School = Data.define(:id, :schoolCode, :studentEmailDomains, :updatedAt, :createdAt, :discardedAt)
1111
SafeguardingFlag = Data.define(:id, :userId, :schoolId, :flag, :email, :createdAt, :updatedAt, :discardedAt)
1212
Student = Data.define(:id, :schoolId, :name, :username, :createdAt, :updatedAt, :discardedAt, :email, :ssoProviders)
1313
# rubocop:enable Naming/MethodName
@@ -51,13 +51,14 @@ def check_auth(token:)
5151
false
5252
end
5353

54-
def create_school(token:, id:, code:)
55-
return { 'id' => id, 'schoolCode' => code } if ENV['BYPASS_OAUTH'].present?
54+
def create_school(token:, id:, code:, school_email_domains: [])
55+
return { 'id' => id, 'schoolCode' => code, 'studentEmailDomains' => school_email_domains } if ENV['BYPASS_OAUTH'].present?
5656

5757
response = connection(token).post('/api/v1/schools') do |request|
5858
request.body = {
5959
id:,
60-
schoolCode: code
60+
schoolCode: code,
61+
studentEmailDomains: school_email_domains
6162
}
6263
end
6364

@@ -173,7 +174,7 @@ def update_school_student(token:, school_id:, student_id:, name: nil, username:
173174
end
174175

175176
unauthorized!(response)
176-
raise UnexpectedResponse, response unless response.status == 200
177+
raise UnexpectedResponse, response unless response.status == 201
177178

178179
build_student(response.body)
179180
rescue Faraday::BadRequestError => e
@@ -214,6 +215,23 @@ def delete_safeguarding_flag(token:, flag:)
214215
raise UnexpectedResponse, response unless response.status == 204
215216
end
216217

218+
def update_school_email_domains(token:, school_id:, school_email_domains: [])
219+
return { 'id' => school_id, 'studentEmailDomains' => school_email_domains } if ENV['BYPASS_OAUTH'].present?
220+
221+
response = connection(token).patch("/api/v1/schools/#{school_id}") do |request|
222+
request.body = {
223+
studentEmailDomains: school_email_domains
224+
}
225+
end
226+
227+
unauthorized!(response)
228+
puts("Status!!")
229+
puts(response.status)
230+
raise UnexpectedResponse, response unless response.status == 200
231+
232+
School.new(**response.body)
233+
end
234+
217235
private
218236

219237
def connection(token)

spec/lib/profile_api_client_spec.rb

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
stub_request(:post, create_school_url)
5959
.to_return(
6060
status: 201,
61-
body: '{"id":"","schoolCode":"","updatedAt":"","createdAt":"","discardedAt":""}',
61+
body: '{"id":"","schoolCode":"","updatedAt":"","createdAt":"","discardedAt":"","studentEmailDomains":[]}',
6262
headers: { 'content-type' => 'application/json' }
6363
)
6464
end
@@ -67,14 +67,15 @@
6767
it_behaves_like 'a request that handles standard HTTP errors', :post, url: -> { create_school_url }
6868
it_behaves_like 'a request that handles an unexpected response status', :post, url: -> { "#{api_url}/api/v1/schools" }, status: 200
6969

70-
it 'sends the school id and code in the request body as json' do
70+
it 'sends the school id, code and studentEmailDomains in the request body as json' do
7171
create_school_response
72-
expected_body = { id: school.id, schoolCode: school.code }.to_json
72+
expected_body = { id: school.id, schoolCode: school.code, studentEmailDomains: [] }.to_json
7373
expect(WebMock).to have_requested(:post, create_school_url).with(body: expected_body)
7474
end
7575

7676
it 'returns the created school if successful' do
77-
data = { id: 'id', schoolCode: 'code', updatedAt: '2024-07-09T10:31:13.196Z', createdAt: '2024-07-09T10:31:13.196Z', discardedAt: nil }
77+
data = { id: 'id', schoolCode: 'code', updatedAt: '2024-07-09T10:31:13.196Z', createdAt: '2024-07-09T10:31:13.196Z', discardedAt: nil,
78+
studentEmailDomains: [] }
7879
expected = ProfileApiClient::School.new(**data)
7980
stub_request(:post, create_school_url)
8081
.to_return(status: 201, body: data.to_json, headers: { 'Content-Type' => 'application/json' })
@@ -91,8 +92,8 @@
9192
expect(WebMock).not_to have_requested(:post, create_school_url)
9293
end
9394

94-
it 'returns the id and code of the school supplied' do
95-
expected = { 'id' => school.id, 'schoolCode' => school.code }
95+
it 'returns the id, code and email domains of the school supplied' do
96+
expected = { 'id' => school.id, 'schoolCode' => school.code, 'studentEmailDomains' => [] }
9697
expect(create_school_response).to eq(expected)
9798
end
9899
end
@@ -558,4 +559,62 @@ def school_student
558559
described_class.school_student(token:, school_id: school.id, student_id:)
559560
end
560561
end
562+
563+
describe '.update_school_email_domains' do
564+
subject(:update_school_email_domains_response) { update_school_email_domains }
565+
566+
let(:school) { build(:school, id: SecureRandom.uuid, code: SecureRandom.uuid) }
567+
let(:update_school_email_domains_url) { "#{api_url}/api/v1/schools/#{school.id}" }
568+
let(:school_email_domains) { ['student.example.edu', 'mail.example.org'] }
569+
570+
before do
571+
stub_request(:patch, update_school_email_domains_url)
572+
.to_return(
573+
status: 201,
574+
body: '{"id":"","schoolCode":"","updatedAt":"","createdAt":"","discardedAt":"","studentEmailDomains":[]}',
575+
headers: { 'content-type' => 'application/json' }
576+
)
577+
end
578+
579+
it_behaves_like 'an authenticated JSON API request', :patch, url: -> { update_school_email_domains_url }
580+
it_behaves_like 'a request that handles standard HTTP errors', :patch, url: -> { update_school_email_domains_url }
581+
it_behaves_like 'a request that handles an unexpected response status', :patch, url: -> { "#{api_url}/api/v1/schools/#{school.id}" }, status: 200
582+
583+
it 'sends studentEmailDomains in the JSON body' do
584+
update_school_email_domains_response
585+
expected_body = { studentEmailDomains: school_email_domains }.to_json
586+
expect(WebMock).to have_requested(:patch, update_school_email_domains_url).with(body: expected_body)
587+
end
588+
589+
it 'returns a school if successful' do
590+
data = { id: 'id', schoolCode: 'code', updatedAt: '2024-07-09T10:31:13.196Z', createdAt: '2024-07-09T10:31:13.196Z', discardedAt: nil,
591+
studentEmailDomains: school_email_domains }
592+
expected = ProfileApiClient::School.new(**data)
593+
stub_request(:patch, update_school_email_domains_url)
594+
.to_return(status: 200, body: data.to_json, headers: { 'Content-Type' => 'application/json' })
595+
expect(update_school_email_domains_response).to eq(expected)
596+
end
597+
598+
describe 'when BYPASS_OAUTH is true' do
599+
before do
600+
allow(ENV).to receive(:[]).with('BYPASS_OAUTH').and_return(true)
601+
end
602+
603+
it 'does not make a request to Profile API' do
604+
update_school_email_domains_response
605+
expect(WebMock).not_to have_requested(:patch, update_school_email_domains_url)
606+
end
607+
608+
it 'returns the id and email domains of the school supplied' do
609+
expected = { 'id' => school.id, 'studentEmailDomains' => school_email_domains }
610+
expect(update_school_email_domains_response).to eq(expected)
611+
end
612+
end
613+
614+
private
615+
616+
def update_school_email_domains
617+
described_class.update_school_email_domains(token:, school_id: school.id, school_email_domains:)
618+
end
619+
end
561620
end

spec/support/profile_api_mock.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ def stub_profile_api_create_school_student(user_id: SecureRandom.uuid)
3030
allow(ProfileApiClient).to receive(:create_school_student).and_return(created: [user_id])
3131
end
3232

33-
def stub_profile_api_create_school(id: SecureRandom.uuid, code: '99-12-34')
33+
def stub_profile_api_create_school(id: SecureRandom.uuid, code: '99-12-34', student_email_domains: [])
3434
now = Time.current.to_fs(:iso8601) # rubocop:disable Naming/VariableNumber
3535
allow(ProfileApiClient).to receive(:create_school).and_return(
3636
ProfileApiClient::School.new(
3737
id:,
3838
schoolCode: code,
39+
studentEmailDomains: student_email_domains,
3940
updatedAt: now,
4041
createdAt: now,
4142
discardedAt: nil

0 commit comments

Comments
 (0)