/
user_test.rb
executable file
·191 lines (148 loc) · 5.98 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
#!/usr/bin/env rspec
# Copyright (c) [2018-2023] 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 "y2firstboot/clients/user"
require "y2users"
require "y2users/linux/writer"
describe Y2Firstboot::Clients::User do
subject(:client) { described_class.new }
describe "#run" do
before do
allow(Yast::InstUserFirstDialog).to receive(:new).and_return(dialog)
allow_any_instance_of(Y2Users::Linux::Writer).to receive(:write)
allow(Y2Users::ConfigManager.instance).to receive(:system).and_return(system_config)
allow(system_config).to receive(:copy).and_return(config)
end
let(:system_config) { Y2Users::Config.new.attach(root) }
let(:config) { system_config.copy.attach(user) }
let(:root) do
Y2Users::User.create_root.tap do |root|
root.password = Y2Users::Password.create_encrypted("$xaadfd545dft")
end
end
let(:user) do
Y2Users::User.new("test").tap do |user|
user.password = Y2Users::Password.create_encrypted("$xa9545dft")
end
end
let(:dialog) { instance_double(Yast::InstUserFirstDialog, run: dialog_result) }
let(:dialog_result) { :back }
context "if the client is executed for first time" do
before do
described_class.username = nil
end
it "opens the dialog with a new user" do
expect(Yast::InstUserFirstDialog).to receive(:new) do |_config, params|
expect(params[:user].attached?).to eq(false)
end.and_return(dialog)
subject.run
end
it "returns the dialog result" do
expect(subject.run).to eq(dialog_result)
end
end
context "if the client was already executed" do
before do
described_class.username = "test"
described_class.user_password = "S3cr3T"
described_class.root_password = "root-S3cr3T"
end
it "opens the dialog with the previously created user" do
expect(Yast::InstUserFirstDialog).to receive(:new) do |_config, params|
expect(params[:user].attached?).to eq(true)
expect(params[:user].name).to eq("test")
end.and_return(dialog)
subject.run
end
it "sets the saved plain password to the user" do
expect(Yast::InstUserFirstDialog).to receive(:new) do |_config, params|
expect(params[:user].password_content).to eq("S3cr3T")
end.and_return(dialog)
subject.run
end
it "sets the plain password to the root user" do
expect(Yast::InstUserFirstDialog).to receive(:new) do |config, _params|
expect(config.users.root.password_content).to eq("root-S3cr3T")
end.and_return(dialog)
subject.run
end
it "returns the dialog result" do
expect(subject.run).to eq(dialog_result)
end
end
context "when the dialog result is :next" do
let(:dialog_result) { :next }
before do
described_class.username = "test"
allow(writer).to receive(:write)
end
let(:writer) { instance_double(Y2Users::Linux::Writer) }
it "writes the config to the system" do
expect(Y2Users::Linux::Writer).to receive(:new) do |target, system, commit_config|
expect(target).to eql(config)
expect(system).to eql(system_config)
user_config = commit_config.user_configs.by_username(user.name)
expect(user_config.move_home?).to eq(true)
expect(user_config.remove_home?).to eq(true)
expect(user_config.adapt_home_ownership?).to eq(true)
expect(user_config.home_without_skel?).to eq(false)
end.and_return(writer)
expect(writer).to receive(:write)
subject.run
end
it "updates the saved user values for the next run" do
expect(Yast::InstUserFirstDialog).to receive(:new) do |config, params|
user = params[:user]
user.name = "test2"
user.password = Y2Users::Password.create_plain("more-S3cr3T")
config.users.root.password = Y2Users::Password.create_plain("root-more-S3cr3T")
end.and_return(dialog)
subject.run
expect(described_class.username).to eq("test2")
expect(described_class.user_password).to eq("more-S3cr3T")
expect(described_class.root_password).to eq("root-more-S3cr3T")
end
end
context "when dialog result is not :next" do
let(:dialog_result) { :back }
before do
described_class.username = "test"
described_class.user_password = "S3cr3T"
described_class.root_password = "root-S3cr3T"
end
it "does not write the users configuration" do
expect_any_instance_of(Y2Users::Linux::Writer).to_not receive(:write)
subject.run
end
it "does not update the saved user values for the next run" do
expect(Yast::InstUserFirstDialog).to receive(:new) do |config, params|
user = params[:user]
user.name = "test2"
user.password = Y2Users::Password.create_plain("more-S3cr3T")
config.users.root.password = Y2Users::Password.create_plain("more-root-S3cr3T")
end.and_return(dialog)
subject.run
expect(described_class.username).to eq("test")
expect(described_class.user_password).to eq("S3cr3T")
expect(described_class.root_password).to eq("root-S3cr3T")
end
end
end
end