-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuser_spec.rb
More file actions
420 lines (326 loc) · 11.4 KB
/
user_spec.rb
File metadata and controls
420 lines (326 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe User do
subject(:user) { build(:user) }
let(:school) { create(:school) }
let(:organisation_id) { school.id }
it { is_expected.to respond_to(:id) }
it { is_expected.to respond_to(:name) }
it { is_expected.to respond_to(:email) }
describe '.from_userinfo' do
subject(:users) { described_class.from_userinfo(ids:) }
let(:owner) { create(:owner, school:, name: 'School Owner', email: 'school-owner@example.com') }
let(:ids) { [owner.id] }
let(:user) { users.first }
before do
stub_user_info_api_for(owner)
end
it 'returns an Array' do
expect(users).to be_an Array
end
it 'returns an array of instances of the described class' do
expect(user).to be_a described_class
end
it 'returns a user with the correct ID' do
expect(user.id).to eq ids.first
end
it 'returns a user with the correct name' do
expect(user.name).to eq 'School Owner'
end
it 'returns a user with the correct email' do
expect(user.email).to eq 'school-owner@example.com'
end
end
describe '.from_token' do
subject(:user) { described_class.from_token(token: UserProfileMock::TOKEN) }
context 'when logged into a full account' do
let(:owner) { create(:owner, school:, name: 'School Owner', email: 'school-owner@example.com') }
before do
authenticated_in_hydra_as(owner)
end
it 'returns an instance of the described class' do
expect(user).to be_a described_class
end
it 'returns a user with the correct ID' do
expect(user.id).to eq owner.id
end
it 'returns a user with the correct name' do
expect(user.name).to eq owner.name
end
it 'returns a user with the correct email' do
expect(user.email).to eq 'school-owner@example.com'
end
it 'returns a user without a username' do
expect(user.username).to be_nil
end
context 'when BYPASS_OAUTH is true' do
around do |example|
ClimateControl.modify(BYPASS_OAUTH: 'true') do
example.run
end
end
it 'does not call the API' do
user
expect(WebMock).not_to have_requested(:get, /.*/)
end
it 'returns a stubbed user' do
expect(user.name).to eq('School Owner')
end
end
end
context 'when logged into a student account' do
let(:student) { create(:student, school:, name: 'School Student') }
before do
authenticated_in_hydra_as(student, :student)
end
it 'returns an instance of the described class' do
expect(user).to be_a described_class
end
it 'returns a user with the correct ID' do
expect(user.id).to eq student.id
end
it 'returns a user with the correct name' do
expect(user.name).to eq student.name
end
it 'returns a user with the correct username' do
expect(user.username).to eq student.username
end
it 'returns a user without an email' do
expect(user.email).to be_nil
end
end
context 'when the access token is invalid' do
before do
allow(Sentry).to receive(:capture_exception)
stub_request(:get, "#{HydraPublicApiClient::API_URL}/userinfo").to_return(status: 401)
end
it 'returns nil' do
expect(user).to be_nil
end
it 'reports the Faraday::UnauthorizedError exception to Sentry' do
user
expect(Sentry).to have_received(:capture_exception).with(instance_of(Faraday::UnauthorizedError))
end
end
end
describe '.from_omniauth' do
subject(:auth_subject) { described_class.from_omniauth(auth) }
let(:id) { 'f80ba5b2-2eee-457d-9f75-872b5c09be84' }
let(:info_without_organisations) do
{
'id' => id,
'email' => 'john.doe@example.com',
'name' => 'John Doe',
'roles' => 'school-student'
}
end
let(:info) { info_without_organisations }
let(:user) { described_class.new(info) }
let(:credentials) { { token: 'token' } }
let(:auth) do
OmniAuth::AuthHash.new(
{
provider: 'rpi',
uid: id,
extra: {
raw_info: info
},
credentials:
}
)
end
it 'returns a User object' do
expect(auth_subject).to be_a described_class
end
it 'returns a user with the correct ID' do
expect(auth_subject.id).to eq id
end
it 'returns a user with the correct name' do
expect(auth_subject.name).to eq 'John Doe'
end
it 'returns a user with the access token supplied in credentials' do
expect(auth_subject.token).to eq 'token'
end
it 'returns a user with the correct email' do
expect(user.email).to eq 'john.doe@example.com'
end
context 'with unusual keys in info' do
let(:info) { { foo: :bar, flibble: :woo } }
it { is_expected.to be_a described_class }
end
context 'with no info' do
let(:info) { nil }
it { is_expected.to be_a described_class }
end
context 'with no auth set' do
let(:auth) { nil }
it { is_expected.to be_nil }
end
context 'with no credentials set' do
let(:credentials) { nil }
it 'returns a user with no token' do
expect(auth_subject.token).to be_nil
end
end
end
describe '#school_owner?' do
subject(:user) { create(:user) }
let(:school) { create(:school) }
it 'returns true when the user has the owner role for this school' do
create(:owner_role, school:, user_id: user.id)
expect(user).to be_school_owner(school)
end
it 'returns false when the user does not have the owner role for this school' do
create(:teacher_role, school:, user_id: user.id)
expect(user).not_to be_school_owner(school)
end
end
describe '#school_teacher?' do
subject(:user) { create(:user) }
let(:school) { create(:school) }
it 'returns true when the user has the teacher role for this school' do
create(:teacher_role, school:, user_id: user.id)
expect(user).to be_school_teacher(school)
end
it 'returns false when the user does not have the teacher role for this school' do
create(:owner_role, school:, user_id: user.id)
expect(user).not_to be_school_teacher(school)
end
end
describe '#school_student?' do
subject(:user) { create(:user) }
let(:school) { create(:school) }
it 'returns true when the user has the student role for this school' do
create(:student_role, school:, user_id: user.id)
expect(user).to be_school_student(school)
end
it 'returns false when the user does not have the student role for this school' do
create(:owner_role, school:, user_id: user.id)
expect(user).not_to be_school_student(school)
end
end
describe '#student?' do
subject(:user) { create(:user) }
let(:school) { create(:school) }
it 'returns true when the user has a student role' do
create(:student_role, school:, user_id: user.id)
expect(user).to be_student
end
it 'returns false when the user does not have a student role' do
create(:owner_role, school:, user_id: user.id)
expect(user).not_to be_student
end
end
describe '#parsed_roles' do
it 'returns array of role names when roles is set to comma-separated string' do
user = build(:user, roles: 'role-1,role-2')
expect(user.parsed_roles).to eq(%w[role-1 role-2])
end
it 'strips leading & trailing spaces from role names' do
user = build(:user, roles: ' role-1 , role-2 ')
expect(user.parsed_roles).to eq(%w[role-1 role-2])
end
it 'returns empty array when roles is set to empty string' do
user = build(:user, roles: '')
expect(user.parsed_roles).to eq([])
end
it 'returns empty array when roles is set to nil' do
user = build(:user, roles: nil)
expect(user.parsed_roles).to eq([])
end
end
describe '#admin?' do
it 'returns true if the user has the editor-admin role in Hydra' do
user = build(:user, roles: 'editor-admin')
expect(user).to be_admin
end
it 'returns false if the user does not have the editor-admin role in Hydra' do
user = build(:user, roles: 'another-editor-admin')
expect(user).not_to be_admin
end
end
describe '#experience_cs_admin?' do
it 'returns true if the user has the experience-cs-admin role in Hydra' do
user = build(:experience_cs_admin_user)
expect(user).to be_experience_cs_admin
end
it 'returns false if the user does not have the experience-cs-admin role in Hydra' do
user = build(:user, roles: 'another-admin')
expect(user).not_to be_experience_cs_admin
end
end
describe '#school_roles' do
subject(:user) { build(:user) }
let(:school) { create(:school) }
context 'when the user has no roles' do
it 'returns an empty array if the user has no role in this school' do
expect(user.school_roles(school)).to be_empty
end
end
context 'when the user has an organisation and roles' do
before do
create(:role, school:, user_id: user.id, role: 'owner')
create(:role, school:, user_id: user.id, role: 'teacher')
end
it 'returns an array of the roles the user has at the school' do
expect(user.school_roles(school)).to match_array(%w[owner teacher])
end
end
end
describe '.where' do
subject(:user) { described_class.where(id: owner.id).first }
let(:owner) { create(:owner, school:, name: 'School Owner', email: 'school-owner@example.com') }
before do
stub_user_info_api_for(owner)
end
it 'returns an instance of the described class' do
expect(user).to be_a described_class
end
it 'returns a user with the correct ID' do
expect(user.id).to eq owner.id
end
it 'returns a user with the correct name' do
expect(user.name).to eq 'School Owner'
end
it 'returns a user with the correct email' do
expect(user.email).to eq 'school-owner@example.com'
end
context 'when BYPASS_OAUTH is true' do
around do |example|
ClimateControl.modify(BYPASS_OAUTH: 'true') do
example.run
end
end
let(:owner) { create(:owner, school:, id: '00000000-0000-0000-0000-000000000000') }
it 'does not call the API' do
user
expect(WebMock).not_to have_requested(:get, /.*/)
end
it 'returns a stubbed user' do
expect(user.name).to eq('School Owner')
end
end
end
describe '#schools' do
it 'includes schools where the user has the owner role' do
create(:owner_role, school:, user_id: user.id)
expect(user.schools).to eq([school])
end
it 'includes schools where the user has the teacher role' do
create(:teacher_role, school:, user_id: user.id)
expect(user.schools).to eq([school])
end
it 'includes schools where the user has the student role' do
create(:student_role, school:, user_id: user.id)
expect(user.schools).to eq([school])
end
it 'does not include schools where the user has no role' do
expect(user.schools).to be_empty
end
it 'only includes a school once even if the user has multiple roles' do
create(:owner_role, school:, user_id: user.id)
create(:teacher_role, school:, user_id: user.id)
expect(user.schools).to eq([school])
end
end
end