-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a widget to list SSH public keys
- Loading branch information
Showing
4 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |