-
Notifications
You must be signed in to change notification settings - Fork 16
/
users_writer_test.rb
executable file
·734 lines (534 loc) · 22.9 KB
/
users_writer_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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#!/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 "y2issues/list"
require "y2issues/issue"
require "y2users/linux/users_writer"
require "y2users/config"
require "y2users/commit_config_collection"
require "y2users/commit_config"
require "y2users/user"
require "y2users/password"
require "y2users/linux/delete_user_action"
require "y2users/linux/action_result"
describe Y2Users::Linux::UsersWriter do
subject { described_class.new(target_config, initial_config, commit_configs) }
let(:initial_config) do
Y2Users::Config.new.tap do |config|
config.attach(users)
end
end
let(:target_config) { initial_config.copy }
let(:commit_configs) { Y2Users::CommitConfigCollection.new([commit_config]) }
let(:users) { [test1, test2] }
let(:test1) { Y2Users::User.new("test1") }
let(:test2) { Y2Users::User.new("test2") }
let(:commit_config) { Y2Users::CommitConfig.new }
describe "#write" do
let(:create_user_action) { Y2Users::Linux::CreateUserAction }
let(:edit_user_action) { Y2Users::Linux::EditUserAction }
let(:delete_user_action) { Y2Users::Linux::DeleteUserAction }
let(:set_user_password_action) { Y2Users::Linux::SetUserPasswordAction }
let(:delete_user_password_action) { Y2Users::Linux::DeleteUserPasswordAction }
let(:remove_home_content_action) { Y2Users::Linux::RemoveHomeContentAction }
let(:set_home_ownership_action) { Y2Users::Linux::SetHomeOwnershipAction }
let(:set_auth_keys_action) { Y2Users::Linux::SetAuthKeysAction }
def mock_action(action, result, *users)
action_instance = instance_double(action, perform: result)
allow(action).to receive(:new).with(*users, any_args).and_return(action_instance)
action_instance
end
def success(*messages)
Y2Users::Linux::ActionResult.new(true, issues(messages))
end
def failure(*messages)
Y2Users::Linux::ActionResult.new(false, issues(messages))
end
def issues(messages)
issues = messages.map { |m| Y2Issues::Issue.new(m) }
Y2Issues::List.new(issues)
end
before do
allow(Yast::Execute).to receive(:on_target!)
# Prevent to perform real actions into the system
allow_any_instance_of(Y2Users::Linux::Action).to receive(:perform).and_return(success)
allow(Yast::MailAliases).to receive(:SetRootAlias)
end
it "deletes, edits and creates users in that order" do
deleted_user = target_config.users.by_id(test2.id)
target_config.detach(deleted_user)
edited_user = target_config.users.by_id(test1.id)
edited_user.name = "new_name"
new_user = Y2Users::User.new("test3")
target_config.attach(new_user)
delete_action = mock_action(delete_user_action, success, test2)
edit_action = mock_action(edit_user_action, success, test1, edited_user)
create_action = mock_action(create_user_action, success, new_user)
expect(delete_action).to receive(:perform).ordered
expect(edit_action).to receive(:perform).ordered
expect(create_action).to receive(:perform).ordered
subject.write
end
context "when a user is deleted from the target config" do
let(:deleted_user) { target_config.users.by_id(test2.id) }
before do
target_config.detach(deleted_user)
end
it "performs the action for deleting the user" do
action = mock_action(delete_user_action, success, test2)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(delete_user_action, success("deleting user issue"), test2)
issues = subject.write
expect(issues.map(&:message)).to include(/deleting user issue/)
end
end
context "when a user is edited in the target config" do
let(:initial_user) { test2 }
let(:target_user) { target_config.users.by_id(initial_user.id) }
before do
target_user.name = "new_name"
end
it "performs the action for editing the user" do
action = mock_action(edit_user_action, success, initial_user, target_user)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(edit_user_action, success("issue editing user"), initial_user, target_user)
issues = subject.write
expect(issues.map(&:message)).to include(/issue editing user/)
end
context "and the new home path already exists" do
before do
target_user.home.path = "/home/new_home"
allow(subject).to receive(:exist_user_home?).with(target_user).and_return(true)
end
let(:commit_config) do
Y2Users::CommitConfig.new.tap do |config|
config.username = target_user.name
config.move_home = true
end
end
it "forces to not move the content of the current home" do
action = instance_double(edit_user_action, perform: success)
expect(edit_user_action)
.to receive(:new).with(initial_user, target_user, anything) do |*args|
commit_config = args.last
expect(commit_config.move_home?).to eq(false)
end.and_return(action)
expect(action).to receive(:perform)
subject.write
end
end
context "and the action for editing the user successes" do
before do
mock_action(edit_user_action, success, initial_user, target_user)
allow(subject).to receive(:exist_user_home?).and_call_original
end
let(:commit_config) do
Y2Users::CommitConfig.new.tap do |config|
config.username = target_user.name
config.adapt_home_ownership = adapt_home_ownership
end
end
let(:adapt_home_ownership) { nil }
context "and the home ownership should be set" do
let(:adapt_home_ownership) { true }
before do
allow(subject).to receive(:exist_user_home?).with(target_user).and_return(exist_home)
end
context "and the user home exists" do
let(:exist_home) { true }
it "performs the action for setting the home ownership" do
action = mock_action(set_home_ownership_action, success, target_user)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(set_home_ownership_action,
success("issue adapting ownership"), target_user)
issues = subject.write
expect(issues.map(&:message)).to include(/issue adapting ownership/)
end
end
context "and the user home does not exist" do
let(:exist_home) { false }
it "does not perform the action for setting the home ownership" do
expect_any_instance_of(set_home_ownership_action).to_not receive(:perform)
subject.write
end
end
end
context "and the home ownership should not be set" do
let(:adapt_home_ownership) { false }
it "does not perform the action for setting the home ownership" do
expect_any_instance_of(set_home_ownership_action).to_not receive(:perform)
subject.write
end
end
context "and the user password has changed" do
before do
target_user.password = password
end
context "and the user has a password" do
let(:password) { Y2Users::Password.create_plain("s3cr3t") }
it "performs the action for setting the password" do
action = mock_action(set_user_password_action, success, target_user)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(set_user_password_action, success("issue setting password"), target_user)
issues = subject.write
expect(issues.map(&:message)).to include(/issue setting password/)
end
end
context "and the user has no password" do
let(:password) { nil }
before do
initial_user.password = Y2Users::Password.new("s3cr3t")
end
it "performs the action for deleting the password" do
action = mock_action(delete_user_password_action, success, target_user)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(delete_user_password_action,
success("issue deleting password"), target_user)
issues = subject.write
expect(issues.map(&:message)).to include(/issue deleting password/)
end
end
end
context "and the user password has not changed" do
it "does not perform the action for setting the password" do
expect_any_instance_of(set_user_password_action).to_not receive(:perform)
subject.write
end
it "does not perform the action for deleting the password" do
expect_any_instance_of(delete_user_password_action).to_not receive(:perform)
subject.write
end
end
context "and the authorized keys has changed" do
before do
target_user.authorized_keys = ["new-key"]
end
context "and the user home exists" do
before do
allow(subject).to receive(:exist_user_home?).with(target_user).and_return(true)
end
it "performs the action for setting the authorized keys" do
action = mock_action(set_auth_keys_action, success, target_user)
expect(action).to receive(:perform)
subject.write
end
it "provides previous keys to the action for setting authorized keys" do
action = instance_double(set_auth_keys_action, perform: success)
expect(set_auth_keys_action)
.to receive(:new).with(target_user, any_args) do |*args|
previous_keys = args.last
expect(previous_keys).to eq(initial_user.authorized_keys)
end.and_return(action)
subject.write
end
it "returns the generated issues" do
mock_action(set_auth_keys_action, success("issue auth keys"), target_user)
issues = subject.write
expect(issues.map(&:message)).to include(/issue auth keys/)
end
end
context "and the user home does not exist" do
before do
allow(subject).to receive(:exist_user_home?).with(target_user).and_return(false)
end
it "does not perform the action for setting the authorized keys" do
expect_any_instance_of(set_auth_keys_action).to_not receive(:perform)
subject.write
end
end
end
context "and the authorized keys has not changed" do
it "does not perform the action for setting the authorized keys" do
expect_any_instance_of(set_auth_keys_action).to_not receive(:perform)
subject.write
end
end
end
context "and the action for editing the user fails" do
before do
mock_action(edit_user_action, failure, initial_user, target_user)
end
it "does not perform more actions" do
expect_any_instance_of(Y2Users::Linux::Action).to_not receive(:perform)
subject.write
end
end
end
context "after working with users" do
# Initial config is empty
let(:users) { [] }
before do
# Successfully created user
test1 = Y2Users::User.new("test1").tap { |u| u.receive_system_mail = true }
target_config.attach(test1)
mock_action(create_user_action, success, test1)
# Successfully created, but does not receive system mail
test2 = Y2Users::User.new("test2")
target_config.attach(test2)
mock_action(create_user_action, success, test2)
# Create failure
test3 = Y2Users::User.new("test3").tap { |u| u.receive_system_mail = true }
target_config.attach(test3)
mock_action(create_user_action, failure, test3)
# Successfully edited
test4 = Y2Users::User.new("test4").tap { |u| u.receive_system_mail = true }
initial_config.attach(test4)
target_test4 = test4.copy.tap { |u| u.name = "test4_edited" }
target_config.attach(target_test4)
mock_action(edit_user_action, success, test4, target_test4)
# Successfully edited, but does not receive system mail
test5 = Y2Users::User.new("test5")
initial_config.attach(test5)
target_test5 = test5.copy.tap { |u| u.name = "test5_edited" }
target_config.attach(target_test5)
mock_action(edit_user_action, success, test5, target_test5)
# Edit failure
test6 = Y2Users::User.new("test6").tap { |u| u.receive_system_mail = true }
initial_config.attach(test6)
target_test6 = test6.copy.tap { |u| u.name = "test6_edited" }
target_config.attach(target_test6)
mock_action(edit_user_action, failure, test6, target_test6)
# Edit failure, but does not receive system mail
test7 = Y2Users::User.new("test7")
initial_config.attach(test7)
target_test7 = test7.copy.tap { |u| u.name = "test7_edited" }
target_config.attach(target_test7)
mock_action(edit_user_action, failure, test7, target_test7)
# Not edited
test8 = Y2Users::User.new("test8").tap { |u| u.receive_system_mail = true }
initial_config.attach(test8)
target_config.attach(test8.copy)
# Not edited, but does not receive system mail
test9 = Y2Users::User.new("test9")
initial_config.attach(test9)
target_config.attach(test9.copy)
# Successfully deleted
test10 = Y2Users::User.new("test10").tap { |u| u.receive_system_mail = true }
initial_config.attach(test10)
mock_action(delete_user_action, success, test10)
# Delete failure
test11 = Y2Users::User.new("test11").tap { |u| u.receive_system_mail = true }
initial_config.attach(test11)
mock_action(delete_user_action, failure, test11)
# Delete failure, but does not receive system mail
test12 = Y2Users::User.new("test12")
initial_config.attach(test12)
mock_action(delete_user_action, failure, test12)
end
it "writes the root aliases" do
# Root aliases should contain:
# * Successfully created users
# * Successfully edited users
# * Users that could not be edited (the initial user is added)
# * Users that could not be deleted
# * Not edited users
expect(Yast::MailAliases).to receive(:SetRootAlias)
.with("test1, test11, test4_edited, test6, test8")
subject.write
end
context "when setting the root aliases fails" do
before do
allow(Yast::MailAliases).to receive(:SetRootAlias).and_return(false)
end
it "returns an issue" do
issues = subject.write
expect(issues.map(&:message)).to include(/Error setting root mail aliases/)
end
end
end
context "when a user is added to the target config" do
let(:test3) { Y2Users::User.new("test3") }
before do
target_config.attach(test3)
end
it "performs the action for creating the user" do
action = mock_action(create_user_action, success, test3)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(create_user_action, success("creating user issue"), test3)
issues = subject.write
expect(issues.map(&:message)).to include(/creating user issue/)
end
context "and the action for creating the user successes" do
before do
mock_action(create_user_action, success, test3)
end
let(:commit_config) do
Y2Users::CommitConfig.new.tap do |config|
config.username = test3.name
config.home_without_skel = home_without_skel
config.adapt_home_ownership = adapt_home_ownership
end
end
let(:home_without_skel) { nil }
let(:adapt_home_ownership) { nil }
before do
allow(subject).to receive(:exist_user_home?).and_call_original
end
context "and the user home should be created without skel" do
let(:home_without_skel) { true }
before do
allow(subject).to receive(:exist_user_home?).with(test3).and_return(exist_home)
end
context "and the home already existed on disk" do
let(:exist_home) { true }
it "does not perform the action for removing the home content" do
expect_any_instance_of(remove_home_content_action).to_not receive(:perform)
subject.write
end
end
context "and the home did not exist on disk yet" do
let(:exist_home) { false }
context "and the home was created" do
before do
allow(subject).to receive(:exist_user_home?).with(test3)
.and_return(exist_home, true)
end
it "performs the action for removing the home content" do
action = mock_action(remove_home_content_action, success, test3)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(remove_home_content_action, success("issue removing home"), test3)
issues = subject.write
expect(issues.map(&:message)).to include(/issue removing home/)
end
end
end
end
context "and the user home should be created with skel" do
let(:home_without_skel) { false }
it "does not perform the action for removing the home content" do
expect_any_instance_of(remove_home_content_action).to_not receive(:perform)
subject.write
end
end
context "and the home ownership should be set" do
let(:adapt_home_ownership) { true }
before do
allow(subject).to receive(:exist_user_home?).with(test3).and_return(exist_home)
end
context "and the home was created" do
let(:exist_home) { true }
it "performs the action for setting the home ownership" do
action = mock_action(set_home_ownership_action, success, test3)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(set_home_ownership_action, success("issue adapting ownership"), test3)
issues = subject.write
expect(issues.map(&:message)).to include(/issue adapting ownership/)
end
end
context "and the home was not created" do
let(:exist_home) { false }
it "does not perform the action for setting the home ownership" do
expect_any_instance_of(set_home_ownership_action).to_not receive(:perform)
subject.write
end
end
end
context "and the home ownership should not be set" do
let(:adapt_home_ownership) { false }
it "does not perform the action for setting the home ownership" do
expect_any_instance_of(set_home_ownership_action).to_not receive(:perform)
subject.write
end
end
context "and the user has a password" do
before do
test3.password = Y2Users::Password.create_plain("s3cr3t")
end
it "performs the action for setting the password" do
action = mock_action(set_user_password_action, success, test3)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(set_user_password_action, success("issue setting password"), test3)
issues = subject.write
expect(issues.map(&:message)).to include(/issue setting password/)
end
end
context "and the user has no password" do
before do
test3.password = nil
end
it "does not perform the action for setting the password" do
expect_any_instance_of(set_user_password_action).to_not receive(:perform)
subject.write
end
end
context "and the user home exists" do
before do
allow(subject).to receive(:exist_user_home?).with(test3).and_return(true)
end
it "performs the action for setting the authorized keys" do
action = mock_action(set_auth_keys_action, success, test3)
expect(action).to receive(:perform)
subject.write
end
it "returns the generated issues" do
mock_action(create_user_action, success("issue auth keys"), test3)
issues = subject.write
expect(issues.map(&:message)).to include(/issue auth keys/)
end
end
context "and the user home does not exist" do
before do
allow(subject).to receive(:exist_user_home?).with(test3).and_return(false)
end
it "does not perform the action for setting the authorized keys" do
expect_any_instance_of(set_auth_keys_action).to_not receive(:perform)
subject.write
end
end
end
context "and the action for creating the user fails" do
before do
mock_action(create_user_action, failure, test3)
end
it "does not perform more actions" do
expect_any_instance_of(Y2Users::Linux::Action).to_not receive(:perform)
subject.write
end
end
end
end
end