Skip to content

Commit

Permalink
cli.apikey: use utils.validate_identifier
Browse files Browse the repository at this point in the history
Use utils.validate_identifier, replacing the custom validator.
  • Loading branch information
enku committed Mar 26, 2024
1 parent 1dd0cb9 commit 1191504
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 51 deletions.
33 changes: 8 additions & 25 deletions src/gentoo_build_publisher/cli/apikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
import gentoo_build_publisher._django_setup # pylint: disable=unused-import
from gentoo_build_publisher.models import ApiKey
from gentoo_build_publisher.settings import Settings
from gentoo_build_publisher.utils import create_secret_key, encrypt
from gentoo_build_publisher.utils import (
InvalidIdentifier,
create_secret_key,
encrypt,
validate_identifier,
)
from gentoo_build_publisher.utils.time import localtime

ROOT_KEY_NAME = "root"
Expand Down Expand Up @@ -49,7 +54,7 @@ def create_action(args: argparse.Namespace, console: Console) -> int:
except IntegrityError:
console.err.print("An API key with that name already exists.")
return StatusCode.NAME_EXISTS
except KeyNameError as error:
except InvalidIdentifier as error:
console.err.print(str(error.args[0]))
return StatusCode.INVALID_NAME

Expand Down Expand Up @@ -131,7 +136,7 @@ def save_api_key(api_key: str, name: str) -> ApiKey:
"""Save the given api_key to the repository with the given name"""
from django.conf import settings # pylint: disable=import-outside-toplevel

validate_key_name(name)
validate_identifier(name)
name = name.lower()
encode = partial(str.encode, encoding="ascii")

Expand All @@ -140,28 +145,6 @@ def save_api_key(api_key: str, name: str) -> ApiKey:
)


class KeyNameError(ValueError):
"""The name for the key is invalid"""


def validate_key_name(name: str) -> None:
"""Validate the key name
Raise KeyNameError if the name is invalid
"""
name_len = len(name)

if name_len > 128:
raise KeyNameError("Key name must not exceed 128 characters")

if name_len == 0:
raise KeyNameError("Key name must have at least 1 character")

for char in name:
if not char.isalnum():
raise KeyNameError("Key name must only contain alphanumeric characters")


class StatusCode(IntEnum):
"""Process exit codes"""

Expand Down
32 changes: 6 additions & 26 deletions tests/test_cli_apikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_create_empty_name(self) -> None:
status = apikey.handler(Namespace(action="create", name=""), Mock(), console)

self.assertEqual(status, 2)
self.assertEqual(stderr.getvalue(), "Key name must have at least 1 character\n")
self.assertEqual(stderr.getvalue(), "''\n")

def test_create_badchars_in_name(self) -> None:
console, _, stderr = string_console()
Expand All @@ -74,19 +74,16 @@ def test_create_badchars_in_name(self) -> None:
)

self.assertEqual(status, 2)
self.assertEqual(
stderr.getvalue(), "Key name must only contain alphanumeric characters\n"
)
self.assertEqual(stderr.getvalue(), "'b😈d'\n")

def test_create_name_too_long(self) -> None:
console, _, stderr = string_console()
console, _, stderr = string_console(width=200)
name = "x" * 129

status = apikey.handler(
Namespace(action="create", name="x" * 256), Mock(), console
)
status = apikey.handler(Namespace(action="create", name=name), Mock(), console)

self.assertEqual(status, 2)
self.assertEqual(stderr.getvalue(), "Key name must not exceed 128 characters\n")
self.assertEqual(stderr.getvalue(), f"{name!r}\n")

@patch("gentoo_build_publisher.cli.apikey.create_secret_key")
def test_root_key(self, create_secret_key: Mock) -> None:
Expand Down Expand Up @@ -190,20 +187,3 @@ def test(self) -> None:
parser = ArgumentParser()

apikey.parse_args(parser)


class ValidateKeyNameTests(TestCase):
def test(self) -> None:
apikey.validate_key_name("bob")

def test_empty_string(self) -> None:
with self.assertRaises(apikey.KeyNameError):
apikey.validate_key_name("")

def test_too_long(self) -> None:
with self.assertRaises(apikey.KeyNameError):
apikey.validate_key_name("x" * 256)

def test_non_alphanumeric(self) -> None:
with self.assertRaises(apikey.KeyNameError):
apikey.validate_key_name("bob.6")

0 comments on commit 1191504

Please sign in to comment.