Skip to content

Commit

Permalink
WIP: Adapt unit testing (still needing improvements)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Sep 23, 2021
1 parent ce7974d commit 3a72d6e
Showing 1 changed file with 107 additions and 70 deletions.
177 changes: 107 additions & 70 deletions test/lib/y2users/linux/writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
require "y2users/commit_config_collection"

describe Y2Users::Linux::Writer do
subject(:writer) { described_class.new(config, initial_config) }
subject(:writer) { described_class.new(config, initial_config, commit_configs) }

describe "#write" do
let(:initial_config) do
Expand All @@ -45,12 +45,13 @@

let(:config) { initial_config.copy }

let(:commit_configs) { Y2Users::CommitConfigCollection.new }

let(:users) { [] }

let(:user) do
user = Y2Users::User.new(username)
user.password = password
user.home = Y2Users::Home.new("/home/#{username}")
user.receive_system_mail = false
user
end
Expand Down Expand Up @@ -78,20 +79,19 @@
before do
initial_config.useradd = initial_useradd

allow(File).to receive(:exist?)
allow(Yast::Execute).to receive(:on_target!)
allow(Yast::Users::SSHAuthorizedKeyring).to receive(:new).and_return(keyring)
allow(Yast::Autologin).to receive(:Write)
end

RSpec.shared_examples "writing authorized keys" do
let(:home) { Y2Users::Home.new("/home/y2test") }

before do
current_user = config.users.by_id(user.id)
current_user.home = home
end

context "when home is defined" do
before do
current_user = config.users.by_id(user.id)
current_user.home = Y2Users::Home.new("/home/#{user.name}")
end

it "requests to write authorized keys" do
expect(keyring).to receive(:write_keys)

Expand All @@ -100,7 +100,10 @@
end

context "when home is not defined" do
let(:home) { nil }
before do
current_user = config.users.by_id(user.id)
current_user.home = nil
end

it "does not request to write authorized keys" do
expect(keyring).to_not receive(:write_keys)
Expand All @@ -116,7 +119,7 @@
config.users.by_id(user.id).home&.btrfs_subvol = true
end

it "executes useradd with the right --btrfs-subvolume-home argument" do
it "executes useradd with with --btrfs-subvolume-home argument" do
expect(Yast::Execute).to receive(:on_target!).with(/useradd/, any_args) do |*args|
expect(args.last).to eq user.name
expect(args).to include("--btrfs-subvolume-home")
Expand All @@ -137,49 +140,6 @@
end
end

# FIXME: improve it
RSpec.shared_examples "using an empty home" do
subject(:writer) { described_class.new(config, initial_config, commit_configs) }

let(:empty_home) { false }

let(:commit_configs) do
Y2Users::CommitConfigCollection.new.tap { |collection| collection.add(commit_config) }
end

let(:commit_config) do
Y2Users::CommitConfig.new.tap do |config|
config.username = user.name
config.empty_home = empty_home
end
end

context "when home must be empty" do
let(:empty_home) { true }

it "removes all the home content after useradd execution" do
expect(Yast::Execute).to receive(:on_target!).with(/useradd/, any_args) do |*args|
expect(args).to include(user.home.path)
end

expect(Yast::Execute).to receive(:on_target!).with(/find/, any_args) do |*args|
expect(args).to include(user.home.path)
expect(args).to include("-delete")
end

writer.write
end
end

context "when home must not be empty" do
it "does not clear recently created user home" do
expect(Yast::Execute).to_not receive(:on_target!).with(/find/, any_args)

writer.write
end
end
end

RSpec.shared_examples "setting user as root alias" do
context "and user has been set for receiving system mails" do
before { user.receive_system_mail = true }
Expand Down Expand Up @@ -454,11 +414,17 @@
end

context "whose home was changed" do
let(:current_user) { config.users.by_id(user.id) }

before do
current_user = config.users.by_id(user.id)
current_user.home = Y2Users::Home.new("/home/new")
commit_configs.add(commit_config)

allow(File).to receive(:exist?).with(current_user.home.path).and_return(true)
end

let(:commit_config) { Y2Users::CommitConfig.new.tap { |c| c.username = user.name } }

it "executes usermod with the new values" do
expect(Yast::Execute).to receive(:on_target!).with(
/usermod/, "--home", "/home/new", user.name
Expand All @@ -468,10 +434,27 @@
end

context "and set for moving content to the new location" do
# TODO
before { commit_config.move_home = true }

it "executed usermod with --move-home argument" do
expect(Yast::Execute).to receive(:on_target!).with(
/usermod/, "--home", "/home/new", "--move-home", user.name
)

writer.write
end
end

context "and set for taking ownership of new location" do
# TODO
before { commit_config.adapt_home_ownership = true }

it "executed chown over home path" do
expect(Yast::Execute).to receive(:on_target!).with(
/chown/, "-R", "testuser", current_user.home.path
)

writer.write
end
end
end

Expand Down Expand Up @@ -706,7 +689,6 @@
user.shell = "/bin/y2shell"
user.gecos = ["First line of", "GECOS"]
user.home = Y2Users::Home.new("/home/y2test")
user.home.permissions = "777"

config.attach(user)
config.attach(group)
Expand All @@ -716,22 +698,81 @@
include_examples "setting password attributes"
include_examples "writing authorized keys"
include_examples "using btrfs subvolume"
include_examples "using an empty home"
include_examples "setting user as root alias"

it "executes useradd with all the parameters, including creation of home directory" do
# FIXME: it's ignoring home permissions set above, why?
expect(Yast::Execute).to receive(:on_target!).with(/useradd/, any_args) do |*args|
expect(args.last).to eq username
expect(args).to include(
"--uid", "--gid", "--shell", "--home-dir", "--create-home", "--groups",
"-K HOME_MODE=777"
"--uid", "--gid", "--shell", "--home-dir", "--create-home", "--groups"
)
end

writer.write
end

context "including the home permissions" do
before do
user.home.permissions = "777"
end

it "adds the HOME_MODE to useradd execution" do
expect(Yast::Execute).to receive(:on_target!).with(/useradd/, any_args) do |*args|
expect(args).to include("-K HOME_MODE=777")
end

writer.write
end
end

context "and home has been created" do
let(:commit_config) do
Y2Users::CommitConfig.new.tap do |config|
config.username = user.name
config.empty_home = empty_home
end
end

let(:empty_home) { false }

before do
commit_configs.add(commit_config)
allow(File).to receive(:exist?).with(user.home.path).and_return(true)
end

context "but it must be empty" do
let(:empty_home) { true }

it "removes all the home content" do
expect(Yast::Execute).to receive(:on_target!).with(/find/, any_args) do |*args|
expect(args).to include(user.home.path)
expect(args).to include("-delete")
end

writer.write
end
end

context "and it must not be empty" do
it "does not remove home content" do
expect(Yast::Execute).to_not receive(:on_target!).with(/find/, any_args)

writer.write
end
end
end

context "but home was not created" do
before do
allow(File).to receive(:exist?).with(user.home.path).and_return(false)
end

it "does not try to remove home content" do
expect(Yast::Execute).to_not receive(:on_target!).with(/find/, any_args)

writer.write
end
end
end

context "for a new regular user with no optional attributes specified" do
Expand All @@ -741,12 +782,9 @@

include_examples "setting password"
include_examples "setting password attributes"
include_examples "using btrfs subvolume"
include_examples "setting user as root alias"

it "executes useradd only with the argument to create the home directory" do
# FIXME: now failing because it got /usr/sbin/chpasswd call too
expect(Yast::Execute).to receive(:on_target!).with(/useradd/, "--create-home", username)
it "does not create the user home" do
expect(Yast::Execute).to receive(:on_target!).with(/useradd/, "--no-create-home", username)

writer.write
end
Expand All @@ -761,6 +799,7 @@
user.shell = "/bin/y2shell"
user.home = Y2Users::Home.new("/home/y2test")
user.gecos = ["First line of", "GECOS"]
user.password = nil # not needed for this test

config.attach(user2)
config.attach(user)
Expand All @@ -775,10 +814,8 @@
)
end

# FIXME: it's ignoring home permissions set above, why?

expect(Yast::Execute).to receive(:on_target!).ordered
.with(/useradd/, "--create-home", "testuser2")
.with(/useradd/, "--no-create-home", "testuser2")

writer.write
end
Expand Down

0 comments on commit 3a72d6e

Please sign in to comment.