Skip to content

Commit

Permalink
Adapt code to use Home class
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Sep 24, 2021
1 parent 43e62f8 commit 3c68224
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 25 deletions.
5 changes: 3 additions & 2 deletions src/lib/y2users/autoinst/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require "y2users/parsers/shadow"
require "y2users/collision_checker"
require "y2users/user"
require "y2users/home"
require "y2users/group"
require "y2users/password"
require "y2users/useradd_config"
Expand Down Expand Up @@ -154,10 +155,10 @@ def read_users(issues)
res.gecos = [user_section.fullname]
# TODO: handle forename/lastname
res.gid = user_section.gid
res.home = user_section.home
res.home = Home.new(user_section.home)
res.home.btrfs_subvol = user_section.home_btrfs_subvolume
res.shell = user_section.shell
res.uid = user_section.uid
res.btrfs_subvolume_home = user_section.home_btrfs_subvolume
res.password = create_password(user_section)
res.authorized_keys = user_section.authorized_keys
users << res
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2users/linux/base_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def read_authorized_keys(config)
config.users.each do |user|
next unless user.home

user.authorized_keys = Yast::Users::SSHAuthorizedKeyring.new(user.home).read_keys
user.authorized_keys = Yast::Users::SSHAuthorizedKeyring.new(user.home.path).read_keys
end
end

Expand Down
3 changes: 2 additions & 1 deletion src/lib/y2users/parsers/passwd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# find current contact information at www.suse.com.

require "y2users/user"
require "y2users/home"

module Y2Users
module Parsers
Expand Down Expand Up @@ -46,7 +47,7 @@ def parse(content)
user.gid = values[PASSWD_MAPPING["gid"]]
user.shell = values[PASSWD_MAPPING["shell"]]
user.gecos = values[PASSWD_MAPPING["gecos"]].to_s.split(",")
user.home = values[PASSWD_MAPPING["home"]]
user.home = Home.new(values[PASSWD_MAPPING["home"]])
user
end
end
Expand Down
15 changes: 5 additions & 10 deletions src/lib/y2users/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require "y2users/config_element"
require "y2users/user_validator"
require "y2users/password_validator"
require "y2users/home"
require "yast2/equatable"

module Y2Users
Expand Down Expand Up @@ -69,18 +70,11 @@ class User < ConfigElement
# @return [String, nil] nil if unknown
attr_accessor :shell

# Path to the home directory
# User home
#
# @return [String, nil] nil if unknown
# @return [Home, nil] nil if unknown
attr_accessor :home

# Whether a btrfs subvolume is used as home directory, especially relevant when creating the
# user in the system
#
# @return [Boolean, nil] nil if irrelevant or unknown (some readers may not provide an accurate
# value for this attribute)
attr_accessor :btrfs_subvolume_home

# Fields for the GECOS entry
#
# @return [Array<String>]
Expand Down Expand Up @@ -115,7 +109,7 @@ def self.create_root
new("root").tap do |root|
root.uid = "0"
root.gecos = ["root"]
root.home = "/root"
root.home = Home.new("/root")
end
end

Expand Down Expand Up @@ -249,6 +243,7 @@ def system=(value)
# @return [User]
def copy
user = super
user.home = home.dup if home
user.password = password.copy if password
user.authorized_keys = authorized_keys.map(&:dup)

Expand Down
12 changes: 6 additions & 6 deletions test/lib/y2users/autoinst/reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@
{ "users" => [{ "username" => "test" }] }
end

it "sets User#btrfs_subvolume_home to nil" do
it "sets Home#btrfs_subvol to nil" do
user = subject.read.config.users.by_name("test")

expect(user.btrfs_subvolume_home).to be_nil
expect(user.home.btrfs_subvol).to be_nil
end
end

Expand All @@ -296,20 +296,20 @@
context "and set to true" do
let(:subvol) { true }

it "sets User#btrfs_subvolume_home to true" do
it "sets Home#btrfs_subvol to true" do
user = subject.read.config.users.by_name("test")

expect(user.btrfs_subvolume_home).to eq true
expect(user.home.btrfs_subvol).to eq true
end
end

context "and set to false" do
let(:subvol) { false }

it "sets User#btrfs_subvolume_home to false" do
it "sets Home#btrfs_subvol to false" do
user = subject.read.config.users.by_name("test")

expect(user.btrfs_subvolume_home).to eq false
expect(user.home.btrfs_subvol?).to eq false
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/lib/y2users/linux/reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

root_user = config.users.root
expect(root_user.uid).to eq "0"
expect(root_user.home).to eq "/root"
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
Expand Down
31 changes: 27 additions & 4 deletions test/lib/y2users/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
expect(user.root?).to eq(true)
expect(user.uid).to eq("0")
expect(user.gecos).to contain_exactly("root")
expect(user.home).to eq("/root")
expect(user.home.path).to eq("/root")
expect(user.attached?).to eq(false)
end
end
Expand All @@ -48,10 +48,33 @@

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 "uses a dup of authorized keys" do
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)
Expand Down Expand Up @@ -314,7 +337,7 @@
subject.uid = 1000
subject.gid = 100
subject.shell = "/dev/bash"
subject.home = "/home/test1"
subject.home = Y2Users::Home.new("/home/test1")
subject.gecos = ["User Test1", "Other"]
subject.source = [:ldap]
subject.password = Y2Users::Password.create_plain("S3cr3T")
Expand Down Expand Up @@ -370,7 +393,7 @@

context "when the #home does not match" do
before do
other.home = "/home/test2"
other.home.path = "/home/test2"
end

it "returns false" do
Expand Down

0 comments on commit 3c68224

Please sign in to comment.