Skip to content

Commit

Permalink
Add a widget to list SSH public keys
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Oct 30, 2018
1 parent 311c498 commit 7bb9c7f
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ ylibdialog_DATA = \
ywidgetdir = @ylibdir@/users/widgets
ywidget_DATA = \
lib/users/widgets/disk_selector.rb \
lib/users/widgets/public_key_selector.rb
lib/users/widgets/public_key_selector.rb \
lib/users/widgets/public_keys_list.rb

ylibdir = @ylibdir@/users
ylib_DATA = \
Expand Down
102 changes: 102 additions & 0 deletions src/lib/users/widgets/public_keys_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# encoding: utf-8

# Copyright (c) [2018] 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 "cwm"

module Y2Users
module Widgets
# This widget is a simple list of SSH public keys. It features a "remove" button for each key in
# order to remove them from the list.
class PublicKeysList < ::CWM::CustomWidget
# @return [Array<SSHPublicKey>] List of SSH public keys
attr_reader :keys

# Constructor
#
# @param keys [Array<SSHPublicKey>] Initial list of keys
def initialize(keys = [])
@keys = keys
self.widget_id = "public_keys_list"
end

# @return [Yast::Term] Dialog content
# @see CWM::CustomWidget
def contents
ReplacePoint(Id(:public_keys_list_items), keys_list)
end

# Adds a key to the list
#
# @param key [SSHPublicKey] Key to add to the current list
def add(key)
change_items(keys + [key])
end

# Updates the list of keys
#
# @param keys [Array<SSHPublicKey>] Updated list of keys
def change_items(keys)
@keys = keys
Yast::UI.ReplaceWidget(:public_keys_list_items, keys_list)
end

# Events handler
#
# @see CWM::AbstractWdiget
def handle(event)
remove_key(event) if event["ID"].to_s.start_with?("remove_")

nil
end

# Forces this widget to listen to all events
#
# @return [Boolean]
def handle_all_events
true
end

private

# Returns the keys list
#
# @return [Yast::Term]
def keys_list
rows = keys.each_with_index.map do |key, i|
HBox(
Left(Label(key.fingerprint)),
Right(PushButton(Id("remove_#{i}"), Opt(:notify), "Remove"))
)
end
VBox(*rows)
end

# Remove the selected key
#
# @param event [Hash] Event containing the information about the key to remove
def remove_key(event)
_prefix, index = event["ID"].split("_")
keys.delete_at(index.to_i)
change_items(keys)
end
end
end
end
1 change: 1 addition & 0 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ TESTS = \
lib/users/ssh_public_key_test.rb \
lib/users/widgets/disk_selector_test.rb \
lib/users/widgets/public_key_selector_test.rb \
lib/users/widgets/public_keys_list_test.rb \
dialogs_test.rb \
ssh_authorized_keys_test.rb \
users_test.rb \
Expand Down
62 changes: 62 additions & 0 deletions test/lib/users/widgets/public_keys_list_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env rspec
# encoding: utf-8

# Copyright (c) [2018] 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 "users/widgets/public_keys_list"
require "users/ssh_public_key"
require "cwm/rspec"

describe Y2Users::Widgets::PublicKeysList do
subject(:widget) { described_class.new([key1, key2]) }

include_examples "CWM::CustomWidget"

let(:key1) { instance_double(Y2Users::SSHPublicKey, fingerprint: "SHA256:123") }
let(:key2) { instance_double(Y2Users::SSHPublicKey, fingerprint: "SHA256:456") }

describe "#handle" do
let(:event) { { "ID" => "remove_0" } }

it "removes the selected key" do
widget.handle(event)
expect(widget.keys).to eq([key2])
end
end

describe "#add" do
let(:key3) { instance_double(Y2Users::SSHPublicKey, fingerprint: "SHA256:456") }

it "adds the key and updates the list" do
expect(widget).to receive(:change_items).with([key1, key2, key3]).and_call_original
widget.add(key3)
expect(widget.keys).to eq([key1, key2, key3])
end
end

describe "#change_items" do
it "updates the keys list" do
expect(Yast::UI).to receive(:ReplaceWidget).with(:public_keys_list_items, Yast::Term)
widget.change_items([key1])
expect(widget.keys).to eq([key1])
end
end
end

0 comments on commit 7bb9c7f

Please sign in to comment.