Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store: add missing case for list-keys #4245

Merged
merged 5 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions snapcraft_legacy/_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2016-2022 Canonical Ltd
# Copyright 2016-2023 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -435,11 +435,11 @@ def list_keys():
"""Lists keys available to sign assertions."""
keys = list(_get_usable_keys())
account_info = StoreClientCLI().get_account_information()
enabled_keys = {
enabled_keys = [
account_key["public-key-sha3-384"]
for account_key in account_info["account_keys"]
}
if keys and enabled_keys:
]
if keys:
tabulated_keys = tabulate(
[
(
Expand All @@ -453,19 +453,29 @@ def list_keys():
headers=["", "Name", "SHA3-384 fingerprint", ""],
tablefmt="plain",
)
print(
"The following keys are available on this system:"
)
print(tabulated_keys)
elif not keys and enabled_keys:
registered_keys = "\n".join([f"- {key}" for key in enabled_keys])
else:
print(
"No keys have been created on this system. "
" See 'snapcraft create-key --help' to create a key.\n"
"The following SHA3-384 key fingerprints have been registered "
f"but are not available on this system:\n{registered_keys}"
"See 'snapcraft create-key --help' to create a key."
)
if enabled_keys:
local_hashes = {key["sha3-384"] for key in keys}
registered_keys = "\n".join(
(f"- {key}" for key in enabled_keys if key not in local_hashes)
)
if registered_keys:
print(
"The following SHA3-384 key fingerprints have been registered "
f"but are not available on this system:\n{registered_keys}"
)
else:
print(
"No keys have been registered."
" See 'snapcraft register-key --help' to register a key."
"No keys have been registered with this account. "
"See 'snapcraft register-key --help' to register a key."
)


Expand Down
35 changes: 20 additions & 15 deletions tests/legacy/unit/commands/test_list_keys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2016-2019 Canonical Ltd
# Copyright (C) 2016-2023 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -59,16 +59,14 @@ def test_list_keys_successfully(self):
self.assertThat(result.exit_code, Equals(0))
self.assertThat(
result.output,
Contains(
Equals(
dedent(
"""\
Name SHA3-384 fingerprint
* default {default_sha3_384}
- another {another_sha3_384} (not registered)
"""
).format(
default_sha3_384=get_sample_key("default")["sha3-384"],
another_sha3_384=get_sample_key("another")["sha3-384"],
f"""\
The following keys are available on this system:
Name SHA3-384 fingerprint
* default {get_sample_key("default")["sha3-384"]}
- another {get_sample_key("another")["sha3-384"]} (not registered)
"""
)
),
)
Expand All @@ -95,10 +93,10 @@ def test_list_keys_no_keys_on_system(self):
self.assertThat(result.exit_code, Equals(0))
self.assertThat(
result.output,
Contains(
Equals(
dedent(
"""\
No keys have been created on this system. See 'snapcraft create-key --help' to create a key.
No keys have been created on this system. See 'snapcraft create-key --help' to create a key.
The following SHA3-384 key fingerprints have been registered but are not available on this system:
- vdEeQvRxmZ26npJCFaGnl-VfGz0lU2jZZkWp_s7E-RxVCNtH2_mtjcxq2NkDKkIp
"""
Expand Down Expand Up @@ -132,8 +130,15 @@ def test_list_keys_without_registered(self):
self.assertThat(result.exit_code, Equals(0))
self.assertThat(
result.output,
Contains(
"No keys have been registered. "
"See 'snapcraft register-key --help' to register a key."
Equals(
dedent(
f"""\
The following keys are available on this system:
Name SHA3-384 fingerprint
- default {get_sample_key("default")["sha3-384"]} (not registered)
- another {get_sample_key("another")["sha3-384"]} (not registered)
No keys have been registered with this account. See 'snapcraft register-key --help' to register a key.
"""
)
),
)
78 changes: 78 additions & 0 deletions tests/legacy/unit/store/test_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2023 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 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, see <http://www.gnu.org/licenses/>.
from unittest import mock

import pytest

from snapcraft_legacy import _store

NO_KEYS_OUTPUT = """\
No keys have been created on this system. See 'snapcraft create-key --help' to create a key.
No keys have been registered with this account. See 'snapcraft register-key --help' to register a key.
"""
ONLY_CREATED_KEYS_OUTPUT = """\
The following keys are available on this system:
Name SHA3-384 fingerprint
- ctrl ModelM (not registered)
- alt Dvorak (not registered)
No keys have been registered with this account. See 'snapcraft register-key --help' to register a key.
"""
UNAVAILABLE_KEYS_OUTPUT = """\
No keys have been created on this system. See 'snapcraft create-key --help' to create a key.
The following SHA3-384 key fingerprints have been registered but are not available on this system:
- Bach
- ModelM
"""
SOME_KEYS_OVERLAP_OUTPUT = """\
The following keys are available on this system:
Name SHA3-384 fingerprint
* ctrl ModelM
- alt Dvorak (not registered)
The following SHA3-384 key fingerprints have been registered but are not available on this system:
- Bach
"""
LOCAL_KEYS = [
{"name": "ctrl", "sha3-384": "ModelM"},
{"name": "alt", "sha3-384": "Dvorak"},
]
REMOTE_KEYS = [
{"public-key-sha3-384": "Bach"},
{"public-key-sha3-384": "ModelM"},
]
NO_KEYS = []


@pytest.mark.parametrize(
("usable_keys", "account_keys", "stdout"),
[
(NO_KEYS, NO_KEYS, NO_KEYS_OUTPUT),
(LOCAL_KEYS, NO_KEYS, ONLY_CREATED_KEYS_OUTPUT),
(NO_KEYS, REMOTE_KEYS, UNAVAILABLE_KEYS_OUTPUT),
(LOCAL_KEYS, REMOTE_KEYS, SOME_KEYS_OVERLAP_OUTPUT),
],
)
def test_list_keys(monkeypatch, capsys, usable_keys, account_keys, stdout):
monkeypatch.setattr(_store, "_get_usable_keys", lambda: usable_keys)
mock_client = mock.Mock(spec_set=_store.StoreClientCLI)
mock_client.get_account_information.return_value = {"account_keys": account_keys}
monkeypatch.setattr(_store, "StoreClientCLI", lambda: mock_client)

_store.list_keys()

out, err = capsys.readouterr()

assert out == stdout
assert err == ""
Loading