/
user_test.rb
executable file
·670 lines (522 loc) · 15.6 KB
/
user_test.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
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#!/usr/bin/env rspec
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require_relative "test_helper"
require_relative "config_element_examples"
require "y2users"
require "date"
describe Y2Users::User do
describe ".create_root" do
it "returns a detached root user" do
user = described_class.create_root
expect(user).to be_a(described_class)
expect(user.root?).to eq(true)
expect(user.uid).to eq("0")
expect(user.gecos).to contain_exactly("root")
expect(user.home.path).to eq("/root")
expect(user.attached?).to eq(false)
end
end
subject { described_class.new("test") }
before do
subject.authorized_keys << "ssh-rsa auth-key"
end
include_examples "config element"
describe "#copy" do
before do
subject.home = Y2Users::Home.new("/home/test")
subject.password = Y2Users::Password.create_plain("test")
subject.assign_config(Y2Users::Config.new)
end
it "generates a copy of the user" do
other = subject.copy
expect(other).to be_a(described_class)
expect(other).to eq(subject)
end
it "generates a copy with a duplicated home" do
other = subject.copy
other.home.path = "/home/other"
expect(subject.home.path).to eq("/home/test")
end
it "generates a copy with a duplicated password" do
other = subject.copy
other.password.value = Y2Users::PasswordPlainValue.new("other")
expect(subject.password_content).to eq("test")
end
it "generates a copy with duplicated authorized keys" do
other = subject.copy
expect(other.authorized_keys).to eq(subject.authorized_keys)
other.authorized_keys << "ssh-rsa another-key"
expect(other.authorized_keys).to include("ssh-rsa another-key")
expect(subject.authorized_keys).to_not include("ssh-rsa another-key")
end
end
describe "#authorized_keys" do
it "returns an array holding authorized keys" do
expect(subject.authorized_keys).to be_an(Array)
expect(subject.authorized_keys).to contain_exactly("ssh-rsa auth-key")
end
end
describe "#primary_group" do
before do
group = Y2Users::Group.new("test")
group.gid = gid
config.attach(group)
end
let(:config) { Y2Users::Config.new }
let(:gid) { 100 }
context "if the user is not attached to any config" do
it "returns nil" do
expect(subject.primary_group).to be_nil
end
end
context "if the user is attached to a config" do
before do
config.attach(subject)
end
context "and the user has no gid" do
before do
subject.gid = nil
end
it "returns nil" do
expect(subject.primary_group).to be_nil
end
end
context "and the user has a gid" do
before do
subject.gid = 100
end
context "and there is no group in the config with such a gid" do
let(:gid) { 101 }
it "returns nil" do
expect(subject.primary_group).to be_nil
end
end
context "and there is a group in the config with such a gid" do
let(:gid) { 100 }
it "returns the group with such a gid" do
group = subject.primary_group
expect(group).to be_a(Y2Users::Group)
expect(group.gid).to eq(100)
end
end
end
end
end
describe "#groups" do
let(:group1) do
group = Y2Users::Group.new("test1")
group.gid = 100
group.users_name = []
group
end
let(:group2) do
group = Y2Users::Group.new("test2")
group.gid = 101
group.users_name = ["test", "other"]
group
end
let(:group3) do
group = Y2Users::Group.new("test3")
group.gid = 102
group.users_name = ["test"]
group
end
let(:group4) do
group = Y2Users::Group.new("test4")
group.gid = 103
group.users_name = ["other"]
group
end
let(:config) { Y2Users::Config.new }
let(:groups) { [group1, group2, group3, group4] }
before do
config.attach(groups)
end
context "if the user is not attached to any config" do
it "returns an empty list" do
expect(subject.groups).to be_empty
end
end
context "if the user is attached to a config" do
before do
config.attach(subject)
end
it "includes all groups from config that contain such a user name" do
expect(subject.groups).to contain_exactly(group2, group3)
end
context "and the config has no groups with such a user name" do
let(:groups) { [group1, group4] }
it "returns an empty list" do
expect(subject.groups).to be_empty
end
end
context "and the user indicates the gid of its primary group" do
before do
subject.gid = 100
end
context "and the config has a group with such a gid" do
it "includes the primary group" do
expect(subject.groups).to include(group1)
end
end
context "and the config has not a group with such a gid" do
let(:groups) { [group2, group3, group4] }
it "does not include the primary group" do
expect(subject.groups).to_not include(group1)
end
end
end
end
end
describe "#password_content" do
before do
subject.password = password
end
context "when the user has no password" do
let(:password) { nil }
it "returns nil" do
expect(subject.password_content).to be_nil
end
end
context "when the user has a password" do
let(:password) { Y2Users::Password.new(value) }
context "and the password has no value" do
let(:value) { nil }
it "returns nil" do
expect(subject.password_content).to be_nil
end
end
context "and the password has a value" do
let(:value) { Y2Users::PasswordPlainValue.new("s3cr3t") }
it "returns the content of the password value" do
expect(subject.password_content).to eq("s3cr3t")
end
end
end
end
describe "#expire_date" do
before do
subject.password = password
end
context "when the user has no password" do
let(:password) { nil }
it "returns nil" do
expect(subject.expire_date).to be_nil
end
end
context "when the user has a password" do
let(:password) { Y2Users::Password.create_plain("S3cr3T") }
before do
password.account_expiration = expiration
end
context "and the password has no expiration" do
let(:expiration) { nil }
it "returns nil" do
expect(subject.expire_date).to be_nil
end
end
context "and the password has expiration" do
let(:expiration) { Y2Users::AccountExpiration.new(date) }
let(:date) { Date.new(2021, 1, 2) }
it "returns the password expiration" do
expect(subject.expire_date).to eq(date)
end
end
end
end
describe "#full_name" do
before do
subject.gecos = gecos
end
context "when the user has GECOS" do
let(:gecos) { ["Test User", "other"] }
it "returns the first GECOS value" do
expect(subject.full_name).to eq("Test User")
end
end
context "when the user has no GECOS" do
let(:gecos) { [] }
it "returns the user name" do
expect(subject.full_name).to eq("test")
end
end
end
describe "#==" do
subject { described_class.new("test1") }
before do
subject.uid = 1000
subject.gid = 100
subject.shell = "/dev/bash"
subject.home = Y2Users::Home.new("/home/test1")
subject.gecos = ["User Test1", "Other"]
subject.source = [:ldap]
subject.receive_system_mail = true
subject.password = Y2Users::Password.create_plain("S3cr3T")
end
let(:other) { subject.copy }
context "when all the attributes are equal" do
it "returns true" do
expect(subject == other).to eq(true)
end
end
context "when the #name does not match" do
before do
other.name = "test2"
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when the #uid does not match" do
before do
other.uid = 1001
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when the #gid does not match" do
before do
other.gid = 101
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when the #shell does not match" do
before do
other.shell = "/bin/zsh"
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when the #home does not match" do
before do
other.home.path = "/home/test2"
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when the #gecos does not match" do
before do
other.gecos = []
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when the #source does not match" do
before do
other.source = :local
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when #receive_system_mail does not match" do
before do
other.receive_system_mail = false
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when the #password does not match" do
before do
other.password.value.content = "M0r3-S3cr3T"
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when #authorized_keys does not match" do
before do
other.authorized_keys << "ssh-rsa other-auth-key"
end
it "returns false" do
expect(subject == other).to eq(false)
end
end
context "when the given object is not a user" do
let(:other) { "This is not a user" }
it "returns false" do
expect(subject == other).to eq(false)
end
end
end
describe "#root?" do
before do
subject.name = name
end
context "when the user name is 'root'" do
let(:name) { "root" }
it "returns true" do
expect(subject.root?).to eq(true)
end
end
context "when the user name is not 'root'" do
let(:name) { "root1" }
it "returns false" do
expect(subject.root?).to eq(false)
end
end
end
describe "#system?" do
before do
allow(Yast::ShadowConfig).to receive(:fetch).with(:sys_uid_max).and_return("499")
subject.name = name
end
RSpec.shared_examples "system user if nobody" do
context "and the user name is 'nobody'" do
let(:name) { "nobody" }
it "returns true" do
expect(subject.system?).to eq(true)
end
end
context "and the user name is different from 'nobody'" do
let(:name) { "somebody" }
it "returns false" do
expect(subject.system?).to eq(false)
end
end
end
RSpec.shared_examples "system user anyways" do
context "and the user name is 'nobody'" do
let(:name) { "nobody" }
it "returns true" do
expect(subject.system?).to eq(true)
end
end
context "and the user name is different from 'nobody'" do
let(:name) { "somebody" }
it "returns true" do
expect(subject.system?).to eq(true)
end
end
end
context "when the user has uid" do
before do
subject.uid = uid
end
context "and the uid is bigger than the max system uid" do
let(:uid) { 500 }
include_examples "system user if nobody"
end
context "and the uid is smaller than the max system uid" do
let(:uid) { 300 }
include_examples "system user anyways"
end
end
context "when the user has no uid" do
before do
subject.system = is_system
end
context "and the user was configured as a system user" do
let(:is_system) { true }
include_examples "system user anyways"
end
context "and the user was not configured as a system user" do
let(:is_system) { false }
include_examples "system user if nobody"
end
end
end
describe "#system=" do
before do
subject.uid = uid
end
context "if the user has already an uid" do
let(:uid) { 1000 }
it "raises an error" do
expect { subject.system = true }.to raise_error(RuntimeError)
end
end
context "if the user has no uid yet" do
let(:uid) { nil }
context "and true is given" do
it "marks the user as a system user" do
subject.system = true
expect(subject.system?).to eq(true)
end
end
context "and false is given" do
it "marks the user as a local user" do
subject.system = false
expect(subject.system?).to eq(false)
end
end
end
end
describe "#receive_system_mail?" do
before do
subject.receive_system_mail = value
end
context "when the value is set to nil" do
let(:value) { nil }
it "returns false" do
expect(subject.receive_system_mail?).to eq(false)
end
end
context "when the value is set to false" do
let(:value) { false }
it "returns false" do
expect(subject.receive_system_mail?).to eq(false)
end
end
context "when the value is set to true" do
let(:value) { true }
it "returns true" do
expect(subject.receive_system_mail?).to eq(true)
end
end
end
describe "#issues" do
let(:user_validator) { instance_double(Y2Users::UserValidator, issues: Y2Issues::List.new) }
before do
allow(Y2Users::UserValidator).to receive(:new).and_return(user_validator)
end
it "creates a new UserValidator instance" do
expect(Y2Users::UserValidator).to receive(:new).with(subject)
subject.issues
end
it "executes user validations with given args" do
expect(user_validator).to receive(:issues).with(skip: [:password])
subject.issues(skip: [:password])
end
end
describe "#password_issues" do
let(:password_validator) do
instance_double(Y2Users::PasswordValidator, issues: Y2Issues::List.new)
end
before do
allow(Y2Users::PasswordValidator).to receive(:new).and_return(password_validator)
end
it "creates a new PasswordValidator instance" do
expect(Y2Users::PasswordValidator).to receive(:new).with(subject)
subject.password_issues
end
it "executes password validations" do
expect(password_validator).to receive(:issues)
subject.password_issues
end
end
end