Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Oct 29, 2018
1 parent b708c72 commit 199eb71
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 18 deletions.
30 changes: 30 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,30 @@
# use the shared YaST defaults
inherit_from:
/usr/share/YaST2/data/devtools/data/rubocop_yast_style.yml

# Redundant returns add legibility for developers used to other languages
Style/RedundantReturn:
Enabled: false

# Don't enforce any particular name for block params
SingleLineBlockParams:
Enabled: false

# The general YaST agreement is 100 characters, so it fits into standard
# resolutions and Github's pull request view. But let's open the threshold a
# little bit
Metrics/LineLength:
Max: 105

# Enforce if/unless at the end only for really short lines
Style/IfUnlessModifier:
MaxLineLength: 60

# some storage API have size method, but without empty? method
# for details see https://github.com/yast/yast-storage-ng/pull/83
Style/ZeroLengthPredicate:
Enabled: false

# the ".freeze" attribute for the constants is not nice
Style/MutableConstant:
Enabled: false
2 changes: 2 additions & 0 deletions package/yast2-users.spec
Expand Up @@ -38,6 +38,7 @@ BuildRequires: yast2-perl-bindings
BuildRequires: yast2-security
BuildRequires: yast2-testsuite
BuildRequires: rubygem(%rb_default_ruby_abi:rspec)
BuildRequires: openssh

Requires: cracklib
Requires: perl-Digest-SHA1
Expand All @@ -63,6 +64,7 @@ Requires: yast2 >= 3.2.8
Requires: yast2-core >= 2.21.0

Requires: yast2-ruby-bindings >= 1.0.0
Requires: openssh

Summary: YaST2 - User and Group Configuration
License: GPL-2.0-only
Expand Down
42 changes: 37 additions & 5 deletions src/lib/users/widgets/public_key_selector.rb
Expand Up @@ -36,9 +36,16 @@ class PublicKeySelector < ::CWM::CustomWidget
# @return [String] Public key content
attr_reader :value

alias_method :keys, :value

# Constructor
def initialize
textdomain "users"
self.value = []
end

def handle_all_events
true
end

# @return [String] Widget label
Expand All @@ -56,21 +63,30 @@ def contents
HStretch(),
PushButton(Id(:browse), "Browse..."),
),
ReplacePoint(Id(:fingerprint), Empty())
ReplacePoint(Id(:fingerprint), Empty()),
ReplacePoint(Id(:keys), Empty())
)
end

# Events handler
#
# @see CWM::AbstractWdiget
def handle(event)
read_key if event["ID"] == :browse
case event["ID"]
when :browse
read_key
when /\Aremove_\d+\z/
_prefix, index = event["ID"].split("_")
keys.delete_at(index.to_i)
refresh_public_keys_list
end

nil
end

# @see CWM::AbstractWdiget
def store
Yast::SSHAuthorizedKeys.import_keys("/root", [value]) if value
Yast::SSHAuthorizedKeys.import_keys("/root", value) if value
nil
end

Expand All @@ -97,8 +113,10 @@ def read_key
Yast::Path.new(".target.mount"), [disk_selector.value, dir], "-o ro"
)
if mounted
self.value = read_key_from(dir)
refresh_fingerprint
new_key = read_key_from(dir)
self.keys << new_key if new_key
refresh_public_keys_list
# refresh_fingerprint
else
report_mount_error(disk_selector.value)
end
Expand All @@ -123,6 +141,10 @@ def refresh_fingerprint
)
end

def refresh_public_keys_list
Yast::UI::ReplaceWidget(Id(:keys), public_keys_list)
end

# Displays an error about the device which failed to be mounted
#
# @param device [String] Device's name
Expand All @@ -142,6 +164,16 @@ def fingerprint(key)
)
output.split(" ").at(1).to_s
end

def public_keys_list
rows = keys.each_with_index.map do |key, i|
HBox(
Left(Label(fingerprint(key))),
Right(PushButton(Id("remove_#{i}"), Opt(:notify), "Remove"))
)
end
VBox(*rows)
end
end
end
end
14 changes: 9 additions & 5 deletions test/lib/users/widgets/disk_selector_test.rb
Expand Up @@ -27,22 +27,26 @@
describe Y2Users::Widgets::DiskSelector do
include_examples "CWM::ComboBox"

let(:devices) { [] }

before do
allow(Y2Users::LeafBlkDevice).to receive(:all).and_return(devices)
end

describe "#items" do
let(:usb_with_fs) do
Y2Users::LeafBlkDevice.new(
name: "/dev/sdb1", model: "MyBrand 8G", disk: "/dev/sdb", fstype: :vfat, removable: true
name: "/dev/sdb1", model: "MyBrand 8G", disk: "/dev/sdb", fstype: :vfat
)
end

let(:usb_no_fs) do
Y2Users::LeafBlkDevice.new(
name: "/dev/sdc1", model: "MyBrand 4G", disk: "/dev/sdc", fstype: nil, removable: true
name: "/dev/sdc1", model: "MyBrand 4G", disk: "/dev/sdc", fstype: nil
)
end

before do
allow(Y2Users::LeafBlkDevice).to receive(:all).and_return([usb_with_fs, usb_no_fs])
end
let(:devices) { [usb_with_fs, usb_no_fs] }

it "returns the list of devices containing a filesystem" do
expect(subject.items).to eq([
Expand Down
16 changes: 8 additions & 8 deletions test/lib/users/widgets/public_key_selector_test.rb
Expand Up @@ -32,8 +32,8 @@
describe "#handle" do
let(:tmpdir) { TESTS_PATH.join("tmp") }
let(:event) { { "ID" => :browse } }
let(:value) { "/dev/sr0" }
let(:disk_selector) { instance_double(Y2Users::Widgets::DiskSelector, value: value) }
let(:disk) { "/dev/sr0" }
let(:disk_selector) { instance_double(Y2Users::Widgets::DiskSelector, value: disk) }
let(:mounted?) { true }

before do
Expand Down Expand Up @@ -64,7 +64,7 @@

it "reads the key" do
widget.handle(event)
expect(widget.value).to eq(key_content)
expect(widget.value).to eq([key_content])
end
end

Expand All @@ -73,7 +73,7 @@

it "does not import any value" do
widget.handle(event)
expect(widget.value).to be_nil
expect(widget.value).to eq([])
end
end

Expand All @@ -89,20 +89,20 @@

describe "#store" do
before do
allow(widget).to receive(:value).and_return(value)
allow(widget).to receive(:value).and_return([key_content])
end

context "when a key was read" do
let(:value) { "some key" }
let(:key_content) { "some key" }

it "imports the key" do
expect(Yast::SSHAuthorizedKeys).to receive(:import_keys).with("/root", [value])
expect(Yast::SSHAuthorizedKeys).to receive(:import_keys).with("/root", [key_content])
widget.store
end
end

context "when no key was read" do
let(:value) { nil }
let(:key_content) { nil }

it "does not try to import the key" do
expect(Yast::SSHAuthorizedKeys).to_not receive(:import_keys)
Expand Down

0 comments on commit 199eb71

Please sign in to comment.