-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucpath_spec.rb
More file actions
637 lines (507 loc) · 18.7 KB
/
ucpath_spec.rb
File metadata and controls
637 lines (507 loc) · 18.7 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
require 'spec_helper'
require 'stub_helper'
require 'ldap_helper'
# rubocop:disable Lint/ConstantDefinitionInBlock, Style/MutableConstant, Metrics/BlockLength
describe UCPath do
it 'does not create a file if changelog returns zero records' do
stub_empty_change_log('2022-04-10', '2022-04-13')
Dir.mktmpdir do |dir|
outpath = Pathname.new(dir)
ARGV = ['--type', 'ucpath', '-s', '2022-04-10', '-e', '2022-04-13', '--outdir', outpath]
setup = Helpers::Setup.new
UCPath.run_ucpath setup
expect(File.exist?(setup.zip_path)).to be(false)
expect(File.exist?(setup.xml_path)).to be(false)
end
end
it 'runs ucpath' do
stub_change_log('2022-04-10', '2022-04-13', 'change_log_1')
stub_ucpath_user('10000003')
stub_ucpath_jobs('10000003', fixture: 'active_employee_jobs.json')
stub_ucpath_user('10000004')
stub_ucpath_jobs('10000004', fixture: 'active_employee_jobs.json')
# Mock LDAP
allow(LDAP::API).to receive(:fetch_ldap_rec).with('112823').and_return(nil)
allow(LDAP::API).to receive(:fetch_ldap_rec).with('1628831').and_return(nil)
Dir.mktmpdir do |dir|
outpath = Pathname.new(dir)
ARGV = ['--type', 'ucpath', '-s', '2022-04-10', '-e', '2022-04-13', '--outdir', outpath]
setup = Helpers::Setup.new
UCPath.run_ucpath setup
# Since the fixture is static but the start date value is dynamic we
# need to swap that out before we do the comparison
expected_file = File.read('spec/data/ucpath/expected_xml_1.xml')
expected_file.gsub!(%r{<start_date>2022-04-19</start_date>}, "<start_date>#{Date.today}</start_date>")
expect(File.exist?(setup.zip_path)).to be(true)
end
end
it 'runs ucpath for a specific user' do
id = '10000005'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'active_employee_jobs.json')
# Mock LDAP
allow(LDAP::API).to receive(:fetch_ldap_rec).with('112823').and_return(nil)
Dir.mktmpdir do |dir|
outpath = Pathname.new(dir)
ARGV = ['--type', 'ucpath', '--users', '10000005', '--outdir', outpath]
setup = Helpers::Setup.new
UCPath.run_ucpath setup
expect(File.exist?(setup.zip_path)).to be(true)
end
end
end
# rubocop:enable Lint/ConstantDefinitionInBlock, Style/MutableConstant, Metrics/BlockLength
describe UCPath::API do
it 'creates a change log' do
s = '2021-01-01'
e = '2021-01-03'
stub_change_log(s, e, 'change_log_1')
log = change_log(s, e)
expect(log).to include('10000003', '10000004')
end
it 'returns nil on faraday error' do
stub_request(:get, 'https://gateway.api.berkeley.edu/hr/v3/employees/dummy_id?id-type=hr-employee-id')
.to_raise('API Error')
expect { fetch_ucpath_rec('dummy_id') }.to raise_error(StandardError)
end
it 'retries on a 429 status' do
id = '10527060'
stub_ucpath_user(id)
stub_ucpath_jobs_rate(id)
u = UCPath::User.new(id)
expect(u.jobs).to be_nil
end
end
# rubocop:disable Metrics/BlockLength
describe UCPath::User do
before do
allow(Config.secrets.ldap).to receive(:pass).and_return('MISSING')
end
it 'is a ucpath user object' do
id = '10527060'
ldap_id = '1628831'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'active_employee_jobs.json')
allow(LDAP::API).to receive(:fetch_ldap_rec).with(ldap_id).and_return(nil)
u = UCPath::User.new(id)
expect(u).to be_kind_of(User)
end
it 'weeds out student affiliated users' do
ucpath_id = '10527060'
ldap_id = '1628831'
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'berkeleyeduaffiliations' => ['STUDENT-TYPE-REGISTERED'] }
])
u = UCPath::User.new(ucpath_id)
expect(u.eligible?).to be(false)
end
it 'weeds out users without an email address' do
ucpath_id = '123454321'
ldap_id = '1628831'
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] },
{ 'berkeleyedualternateid' => [''] } # No email address
])
u = UCPath::User.new(ucpath_id)
expect(u.eligible?).to be(false)
end
it 'uses names from ldap if missing names in ucpath record' do
ucpath_id = '12345678'
ldap_id = '1628831'
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] }
])
u = UCPath::User.new(ucpath_id)
expect(u.rec.first_name).to eq('test_first_name')
expect(u.rec.last_name).to eq('test_last_name')
end
describe 'user group assignment' do
let(:ucpath_id) { '10527060' }
let(:ldap_id) { '1628831' }
before do
stub_ucpath_user(ucpath_id)
allow(LDAP::API).to receive(:fetch_ldap_rec).with(ldap_id).and_return(nil)
end
[
{
description: 'LIBSTAFF',
overrides: { job_code: '000118' },
expected_group: 'LIBSTAFF'
},
{
description: 'UCB POST',
overrides: { job_code: '003252' },
expected_group: 'UCB POST'
},
{
description: 'UCBVISSCHOL',
overrides: { job_code: 'CWR003' },
expected_group: 'UCBVISSCHOL'
},
{
description: 'UCBAFFILI',
overrides: { job_code: 'CWR022' },
expected_group: 'UCBAFFILI'
},
{
description: 'UCEXT',
overrides: {
job_code: 'dummycode',
dept_code: 'EXADM',
classification_indc: 'A'
},
expected_group: 'UCEXT'
},
{
description: 'FACULTY',
overrides: { job_code: '001132' },
expected_group: 'FACULTY'
},
{
description: 'EXECUTIVE',
overrides: {
job_code: 'dummycode',
dept_code: 'dummycode',
classification_indc: '2'
},
expected_group: 'EXECUTIVE'
},
{
description: 'NONACAD',
overrides: {
job_code: 'dummycode',
dept_code: 'dummycode',
classification_indc: 'dummycode'
},
expected_group: 'NONACAD'
}
].each do |row|
it "sets user group #{row[:description]}" do
stub_ucpath_jobs(
ucpath_id,
fixture: 'user_group_base_jobs.json',
overrides: row[:overrides]
)
u = UCPath::User.new(ucpath_id)
expect(u.rec.user_group).to eq(row[:expected_group])
end
end
end
it 'uses the ldap phone number' do
ucpath_id = '12345678'
ldap_id = '1628831'
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] },
{ 'telephonenumber' => ['925-555-1234'] },
{ 'berkeleyedualternateid' => ['fake@email.edu'] }
])
u = UCPath::User.new(ucpath_id)
expect(u.rec.contact_info.phones.phone_number).to eq('925-555-1234')
end
it 'handles all known mangled telephone formats' do
ucpath_id = '12345678'
ldap_id = '1628831'
known_phone_formats = [
'510 645-1234',
'5106451234',
'645-1234',
'5-1234'
]
known_phone_formats.each do |number|
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] },
{ 'telephonenumber' => [number] }
])
u = UCPath::User.new(ucpath_id)
expect(u.rec.contact_info.phones.phone_number).to eq('510-645-1234')
end
end
it 'handles non-primary phone number preferred attribute' do
ucpath_id = '10000004'
ldap_id = '112823'
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] }
])
u = UCPath::User.new(ucpath_id)
expect(u.rec.contact_info.phones.preferred).to eq('false')
end
it 'handles primary phone number preferred attribute' do
ucpath_id = '10000005'
ldap_id = '112823'
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] }
])
u = UCPath::User.new(ucpath_id)
expect(u.rec.contact_info.phones.preferred).to eq('true')
end
it 'creates expected expiry date if month is < change date (7)' do
ucpath_id = '10000005'
ldap_id = '112823'
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_no_end_date_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] }
])
current_year = Date.today.year
expiration_year = current_year + 1
fake_month = 6
allow(Date).to receive(:today).and_return Date.new(current_year, fake_month, 1)
u = UCPath::User.new(ucpath_id)
expect(u.rec.expiry_date).to eq("#{expiration_year}-10-31")
end
it 'creates expected expiry date if month is >= change date (7)' do
ucpath_id = '10000005'
ldap_id = '112823'
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_no_end_date_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] }
])
current_year = Date.today.year
expiration_year = current_year + 2
fake_month = 8
allow(Date).to receive(:today).and_return Date.new(current_year, fake_month, 1)
u = UCPath::User.new(ucpath_id)
expect(u.rec.expiry_date).to eq("#{expiration_year}-10-31")
end
it 'logs an error if the ucpath record is missing a required field' do
id = '10000006'
ldap_id = '1628831'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'active_employee_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] }
])
u = UCPath::User.new(id)
expect(u.errors).to include("#{id} - Missing required field: ucpath_employee_id")
end
it 'records an error if the ucpath record is not returned' do
id = '10000666'
stub_ucpath_missing_user(id)
u = UCPath::User.new(id)
expect(u.errors).to include('Failed to fetch UCPath record')
end
it 'LDAP::API returns nil if fetch encounters an error' do
ucpath_id = '10527060'
stub_ucpath_user(ucpath_id)
stub_ucpath_jobs(ucpath_id, fixture: 'active_employee_jobs.json')
allow(Net::LDAP).to receive(:new).and_raise(StandardError.new('error'))
u = UCPath::User.new(ucpath_id)
expect(u.ldap).to be_nil
end
it 'returns the first job if no eligible jobs are found' do
id = '10145074'
ldap_id = '7165'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'multiple_ineligible_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'sn' => ['test_last_name'] },
{ 'givenname' => ['test_first_name'] }
])
u = UCPath::User.new(id)
expect(u.rec.job_description).to eq('FIRST_JOB_DESCRIPTION')
end
it 'skips a user if no jobs are found' do
id = '999999999'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'zero_jobs.json')
u = UCPath::User.new(id)
expect(u.eligible?).to be(false)
end
it 'skips users that have a termination date before the last Alma purge date' do
id = '888888888'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'terminated_before_purge_jobs.json')
u = UCPath::User.new(id)
expect(u.eligible?).to be(false)
end
it 'skips users with a contingent worker job code' do
id = '10000008'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'contingent_worker_jobs.json')
# Mock LDAP
ldap_id = '112823'
allow(LDAP::API).to receive(:fetch_ldap_rec).with(ldap_id).and_return(nil)
u = UCPath::User.new(id)
expect(u.eligible?).to be(false)
end
it 'eligibility should be true for priority jobs even if the user has a student affiliation' do
id = '10601882'
ldap_id = '1772216'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'priority_job_selected_jobs.json')
stub_ldap_entries(ldap_id, [
{ 'berkeleyeduaffiliations' => ['STUDENT-TYPE-REGISTERED'] }
])
u = UCPath::User.new(id)
expect(u.eligible?).to be(true)
end
it 'skips with VOID in first and last name fields' do
id = '10725309'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'active_employee_jobs.json')
u = UCPath::User.new(id)
expect(u.eligible?).to be(false)
end
it 'skips with zero Percentage of FullTime' do
id = '10725310'
stub_ucpath_user(id)
stub_ucpath_jobs(id, fixture: 'zero_full_time_jobs.json')
u = UCPath::User.new(id)
expect(u.eligible?).to be(false)
end
end
describe UCPath::Jobs do
subject(:jobs) { described_class.allocate }
# Small helper wrappers so specs read cleanly even though methods are private
def choose_job(job1, job2)
jobs.send(:choose_job, job1, job2)
end
def eligible(job)
jobs.send(:eligible?, job)
end
# Minimal structs to stand in for your “job” objects
let(:choose_job_struct) { Struct.new(:expected_end_date) }
let(:eligible_job_struct) do
Struct.new(:hr_status_code, :expected_end_date, :org_relationship_code, :job_code, :percent_of_fulltime, :percent_of_fulltime_job)
end
describe '#choose_job' do
context 'when job1 has no expected end date' do
let(:job1) { choose_job_struct.new('') }
let(:job2) { choose_job_struct.new('2026-01-01') }
it 'returns job1' do
expect(choose_job(job1, job2)).to be(job1)
end
end
context 'when job2 has no expected end date' do
let(:job1) { choose_job_struct.new('2026-01-01') }
let(:job2) { choose_job_struct.new('') }
it 'returns job2' do
expect(choose_job(job1, job2)).to be(job2)
end
end
context 'when both have expected end dates' do
let(:job1) { choose_job_struct.new('2026-01-01') }
let(:job2) { choose_job_struct.new('2026-06-01') }
it 'returns the job with the later date' do
expect(choose_job(job1, job2)).to be(job2)
end
end
context 'when dates are equal' do
let(:job1) { choose_job_struct.new('2026-01-01') }
let(:job2) { choose_job_struct.new('2026-01-01') }
it 'returns job2 (tie goes to second arg)' do
expect(choose_job(job1, job2)).to be(job2)
end
end
end
describe '#eligible?' do
let(:today) { Date.new(2026, 2, 23) }
before do
allow(Date).to receive(:today).and_return(today)
allow(Config).to receive(:skip_fte_check?).and_return(false)
end
context "when hr_status_code is not 'A'" do
let(:job) { eligible_job_struct.new('I', '', '', 'ANY', 1.0, 0.0) }
it 'returns false' do
expect(eligible(job)).to be(false)
end
end
context "when hr_status_code is 'A' and expected_end_date is blank" do
let(:job) { eligible_job_struct.new('A', '', '', 'ANY', 1.0, 0.0) }
it 'returns true' do
expect(eligible(job)).to be(true)
end
end
context "when hr_status_code is 'A' and expected_end_date is after today" do
let(:job) { eligible_job_struct.new('A', '2026-03-01', '', 'ANY', 1.0, 0.0) }
it 'returns true' do
expect(eligible(job)).to be(true)
end
end
context "when hr_status_code is 'A' and expected_end_date is today" do
let(:job) { eligible_job_struct.new('A', '2026-02-23', '', 'ANY', 1.0, 0.0) }
it 'returns false (<= today is not eligible per implementation)' do
expect(eligible(job)).to be(false)
end
end
context "when hr_status_code is 'A' and expected_end_date is before today" do
let(:job) { eligible_job_struct.new('A', '2026-02-01', '', 'ANY', 1.0, 0.0) }
it 'returns false' do
expect(eligible(job)).to be(false)
end
end
context 'when org_relationship_code is blank and other conditions pass' do
let(:job) { eligible_job_struct.new('A', '', '', 'ANY', 1.0, 0.0) }
it 'returns true' do
expect(eligible(job)).to be(true)
end
end
context "when org_relationship_code is 'CWR' and job_code is NOT in either allowlist" do
let(:job) { eligible_job_struct.new('A', '', 'CWR', 'NOT_ALLOWED', 1.0, 0.0) }
before do
allow(Config).to receive(:check_ucpath_code)
.with('visiting_scholar_job_code', 'NOT_ALLOWED')
.and_return(false)
allow(Config).to receive(:check_ucpath_code)
.with('ucb_academic_dept_affiliate_code', 'NOT_ALLOWED')
.and_return(false)
end
it 'returns false' do
expect(eligible(job)).to be(false)
end
end
context "when org_relationship_code is 'CWR' and job_code IS in visiting_scholar_job_code" do
let(:job) { eligible_job_struct.new('A', '', 'CWR', 'ALLOWED', 1.0, 0.0) }
before do
allow(Config).to receive(:check_ucpath_code)
.with('visiting_scholar_job_code', 'ALLOWED')
.and_return(true)
allow(Config).to receive(:check_ucpath_code)
.with('ucb_academic_dept_affiliate_code', 'ALLOWED')
.and_return(false)
end
it 'returns true' do
expect(eligible(job)).to be(true)
end
end
context "when org_relationship_code is 'CWR' and job_code IS in ucb_academic_dept_affiliate_code" do
let(:job) { eligible_job_struct.new('A', '', 'CWR', 'ALLOWED', 1.0, 0.0) }
before do
allow(Config).to receive(:check_ucpath_code)
.with('visiting_scholar_job_code', 'ALLOWED')
.and_return(false)
allow(Config).to receive(:check_ucpath_code)
.with('ucb_academic_dept_affiliate_code', 'ALLOWED')
.and_return(true)
end
it 'returns true' do
expect(eligible(job)).to be(true)
end
end
end
end
# rubocop:enable Metrics/BlockLength