-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuser_spec.rb
342 lines (264 loc) · 9.18 KB
/
user_spec.rb
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
# 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(: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
end
end
describe '.from_token' do
subject(:user) { described_class.from_token(token: UserProfileMock::TOKEN) }
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 'School Owner'
end
it 'returns a user with the correct email' do
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
context 'when the app is configured to bypass oauth' do
before do
allow(Rails.configuration).to receive(:bypass_oauth).and_return(true)
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
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,
'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
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 '#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
it 'returns false if roles are empty in Hydra' do
user = build(:user, roles: '')
expect(user).not_to be_admin
end
it 'returns false if roles are nil in Hydra' do
user = build(:user, roles: nil)
expect(user).not_to be_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 }
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
end
context 'when the app is configured to bypass oauth' do
let(:owner) { create(:owner, school:, id: '00000000-0000-0000-0000-000000000000') }
before do
allow(Rails.configuration).to receive(:bypass_oauth).and_return(true)
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
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