Skip to content

Commit

Permalink
add test and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Sep 17, 2021
1 parent c905141 commit 014b4e5
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 33 deletions.
58 changes: 25 additions & 33 deletions src/lib/y2users/users_module/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def read_useradd_defaults
users_map = Yast::Users.GetLoginDefaults
# notes: groups are deprecated and usrskell and create_mail_spool is not in users
y2users_map = {
group: users_map["group"],
home: users_map["home"],
umask: users_map["umask"],
expiration: users_map["expire"],
inactivity_period: users_map["inactive"],
shell: users_map["shell"],
skel: users_map["skel"],
group: users_map["group"],
home: users_map["home"],
umask: users_map["umask"],
expiration: users_map["expire"],
inactivity_period: users_map["inactive"]&.to_i,
shell: users_map["shell"],
skel: users_map["skel"]
}
UseraddConfig.new(y2users_map)
end
Expand All @@ -72,10 +72,10 @@ def read_useradd_defaults
def read_autologin
res = LoginConfig.new
res.autologin_user = if Yast::Autologin.user.empty?
nil
else
Yast::Autologin.user
end
nil
else
Yast::Autologin.user
end
res.passwordless = Yast::Autologin.pw_less

res
Expand All @@ -85,17 +85,17 @@ def read_autologin
#
# @param config [Config]
def read_elements(config)
config.attach(users)
config.attach(groups)
config.attach(users(config))
end

# Returns the list of users
#
# @return [Array<User>] the collection of users
def users
def users(config)
all_local = Yast::Users.GetUsers("uid", "local").values +
Yast::Users.GetUsers("uid", "system").values
all_local.map { |u| user(u) }
all_local.map { |u| user(u, config) }
end

def groups
Expand All @@ -108,17 +108,17 @@ def groups
#
# @param user_attrs [Hash] a user representation in the format used by Users
# @return [User]
def user(user_attrs)
def user(user_attrs, config)
user = User.new(user_attrs["uid"])

user.uid = attr_value(:uidNumber, user_attrs)
user.gid = attr_value(:gidNumber, user_attrs)
user.uid = attr_value(:uidNumber, user_attrs)&.to_s
user.gid = attr_value(:gidNumber, user_attrs)&.to_s
user.gid ||= config.groups.by_name(attr_value["groupname"])&.gid

user.shell = attr_value(:loginShell, user_attrs)
user.home = attr_value(:homeDirectory, user_attrs)
user.gecos = [attr_value(:cn, user_attrs), *user_attrs["addit_data"].split(",")].compact
if !user.uid && user_attrs["type"] == "system"
user.system = true
end
user.system = true if !user.uid && user_attrs["type"] == "system"

# set password only if specified, nil means not touch it
user.password = create_password(user_attrs) if user_attrs["userPassword"]
Expand All @@ -136,7 +136,7 @@ def user(user_attrs)
def group(group_attrs)
group = Group.new(group_attrs["cn"])

group.gid = attr_value(:gidNumber, group_attrs)
group.gid = attr_value(:gidNumber, group_attrs)&.to_s
group.users_name = group_attrs["userlist"].keys

# TODO: no system support for groups
Expand Down Expand Up @@ -175,21 +175,13 @@ def create_password(user)
expiration = user["shadowExpire"]

password.aging = PasswordAging.new(last_change) if last_change
password.minimum_age = user["shadowMin"]
password.maximum_age = user["shadowMax"]
password.warning_period = user["shadowWarning"]
password.inactivity_period = user["shadowInactive"]
password.minimum_age = user["shadowMin"]&.to_s
password.maximum_age = user["shadowMax"]&.to_s
password.warning_period = user["shadowWarning"]&.to_s
password.inactivity_period = user["shadowInactive"]&.to_s
password.account_expiration = AccountExpiration.new(expiration) if expiration
end
end
end
end
end

if $0 == __FILE__
require "pp"

Yast::Users.Read
Yast::Autologin.Read
pp Y2Users::UsersModule::Reader.new.read
end
159 changes: 159 additions & 0 deletions test/lib/y2users/users_module/reader_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/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/users_module/reader"

describe Y2Users::UsersModule::Reader do
let(:users) do
[
# real data with data dumper from perl after modifications in UI
{
"addit_data" => "",
"btrfs_subvolume" => false,
"chown_home" => true,
"cn" => "test5",
"create_home" => true,
"encrypted" => true,
"gidNumber" => 100,
"givenName" => "",
"grouplist" => {
"wheel" => 1
},
"groupname" => "users",
"homeDirectory" => "/home/test5",
"home_mode" => "755",
"loginShell" => "/bin/bash",
"modified" => "added",
"no_skeleton" => true,
"org_homeDirectory" => "/home/test5",
"org_uid" => "test5",
"org_uidNumber" => 1002,
"plugins" => [],
"shadowExpire" => "",
"shadowFlag" => "",
"shadowInactive" => "-1",
"shadowLastChange" => "18887",
"shadowMax" => "99999",
"shadowMin" => "0",
"shadowWarning" => "7",
"sn" => "",
"text_userpassword" => "test",
"type" => "local",
"uid" => "test5",
"uidNumber" => 1002,
"userPassword" => "$6$CIrJOmyF8WBnHsAn$Sh.pjryO9CD.Dfm9KzDdVYYXblxiTw05b9b0GVpMbckbU" \
"gK/fFvn7nM.ipqooa3Ks5fGgzV.6gPBGG1l8hs7L.",
"what" => "add_user"
}
]
end

let(:sys_groups) do
[
# real data with data dumper from perl after modifications in UI
{
"cn" => "wheel",
"gidNumber" => 497,
"modified" => "edited",
"more_users" => {},
"org_cn" => "wheel",
"org_gidNumber" => 497,
"type" => "system",
"userPassword" => "x",
"userlist" => {
"test5" => 1
},
"what" => "user_change"
}
]
end

let(:local_groups) do
[
{
"cn" => "users",
"gidNumber" => 100,
"more_users" => {},
"org_cn" => "users",
"org_gidNumber" => 100,
"type" => "local",
"userPassword" => "x",
"userlist" => {}
}
]
end

let(:login_config) do
{
"expire" => "",
"group" => "100",
"groups" => "",
"home" => "/home",
"inactive" => "-1",
"shell" => "/bin/bash",
"skel" => "/etc/skel",
"umask" => "022"
}
end

before do
allow(Yast::Users).to receive(:GetLoginDefaults).and_return(login_config)
mapped_users = Hash[users.map { |u| [u["uid"], u] }]
allow(Yast::Users).to receive(:GetUsers).and_return({}, mapped_users)
mapped_sys_groups = Hash[sys_groups.map { |g| [g["cn"], g] }]
mapped_local_groups = Hash[local_groups.map { |g| [g["cn"], g] }]
allow(Yast::Users).to receive(:GetGroups).and_return(mapped_sys_groups, mapped_local_groups)
end

describe "#read" do
it "generates a config with read data" do
config = subject.read

expect(config).to be_a(Y2Users::Config)

expect(config.users.size).to eq 1
expect(config.groups.size).to eq 2

test_user = config.users.by_name("test5")
expect(test_user.uid).to eq "1002"
expect(test_user.home).to eq "/home/test5"
expect(test_user.shell).to eq "/bin/bash"
expect(test_user.primary_group.name).to eq "users"
expect(test_user.password.value.encrypted?).to eq true
expect(test_user.password.value.content).to match(/^\$6\$CI/)
expect(test_user.password.aging.content).to eq("18887")
expect(test_user.password.account_expiration.content).to eq("")

test_group = config.groups.by_name("wheel")
expect(test_group.gid).to eq "497"
expect(test_group.users.map(&:name)).to eq(["test5"])

useradd = config.useradd
expect(useradd.group).to eq "100"
expect(useradd.expiration).to eq ""
expect(useradd.inactivity_period).to eq(-1)
expect(useradd.umask).to eq "022"
end
end
end

0 comments on commit 014b4e5

Please sign in to comment.