/
local_reader_test.rb
executable file
·111 lines (88 loc) · 3.5 KB
/
local_reader_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
#!/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 "y2users/config"
require "y2users/linux/local_reader"
describe Y2Users::Linux::LocalReader do
subject { described_class.new(root_dir) }
let(:root_dir) { File.join(FIXTURES_PATH, "/root/") }
around do |example|
# Let's use test/fixtures/home as src root for reading authorized keys from there
change_scr_root(FIXTURES_PATH.join("home")) { example.run }
end
before do
allow(Yast::Execute).to receive(:on_target!).with(/useradd/, "-D", anything)
.and_return(useradd_default_values)
allow(Yast::ShadowConfig).to receive(:fetch)
allow(Yast::ShadowConfig).to receive(:fetch).with(:umask).and_return("044")
end
describe "#read" do
context "when all expected files are present" do
let(:root_home) { FIXTURES_PATH.join("home", "root").to_s }
let(:expected_root_auth_keys) { authorized_keys_from(root_home) }
it "generates a config with read data" do
config = subject.read
expect(config).to be_a(Y2Users::Config)
expect(config.users.size).to eq 18
expect(config.groups.size).to eq 37
root_user = config.users.root
expect(root_user.uid).to eq "0"
expect(root_user.home.path).to eq "/root"
expect(root_user.shell).to eq "/bin/bash"
expect(root_user.primary_group.name).to eq "root"
expect(root_user.password.value.encrypted?).to eq true
expect(root_user.password.value.content).to match(/^\$6\$pL/)
expect(root_user.authorized_keys).to eq(expected_root_auth_keys)
useradd = config.useradd
expect(useradd.group).to eq "100"
expect(useradd.expiration).to eq ""
expect(useradd.inactivity_period).to eq(-1)
expect(useradd.create_mail_spool).to eq true
expect(useradd.umask).to eq "044"
expect(config.login?).to eq(false)
end
end
context "when any file is missing" do
let(:root_dir) { File.join(FIXTURES_PATH, "/root-missing-files/") }
it "does not crash" do
expect { subject.read }.to_not raise_error
end
it "logs an error" do
expect(subject.log).to receive(:error)
.with(/File.*does not exist/)
.at_least(:once)
subject.read
end
it "generates a config" do
config = subject.read
expect(config).to be_a(Y2Users::Config)
expect(config.users.size).to eq 0
expect(config.groups.size).to eq 0
useradd = config.useradd
expect(useradd.group).to eq "100"
expect(useradd.expiration).to eq ""
expect(useradd.inactivity_period).to eq(-1)
expect(useradd.create_mail_spool).to eq true
expect(useradd.umask).to eq "044"
expect(config.login?).to eq(false)
end
end
end
end