mirror of
https://github.com/Katakate/k7.git
synced 2026-09-25 03:01:59 +00:00
76a8139774
HTTPS-by-default for k7-api, cluster-wide Cilium isolation, first-class --docker on Kata and k7d, and RuntimeClass k7-fc. Playbook pins k7d 0.6.0.
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
"""Unit tests for API key generation / namespace scoping."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from typer.testing import CliRunner
|
|
|
|
from k7.cli.k7 import app
|
|
|
|
runner = CliRunner()
|
|
|
|
|
|
class TestGenerateApiKeyNamespaces:
|
|
def test_scoped_namespaces_persisted(self, tmp_path: Path):
|
|
keys_file = tmp_path / "api_keys.json"
|
|
with patch("k7.cli.k7.API_KEYS_FILE", keys_file):
|
|
result = runner.invoke(
|
|
app,
|
|
["generate-api-key", "scoped", "-n", "a", "--namespace", "b"],
|
|
)
|
|
assert result.exit_code == 0, result.output
|
|
data = json.loads(keys_file.read_text())
|
|
assert len(data) == 1
|
|
entry = next(iter(data.values()))
|
|
assert entry["name"] == "scoped"
|
|
assert entry["namespaces"] == ["a", "b"]
|
|
|
|
def test_unscoped_key_omits_namespaces_field(self, tmp_path: Path):
|
|
keys_file = tmp_path / "api_keys.json"
|
|
with patch("k7.cli.k7.API_KEYS_FILE", keys_file):
|
|
result = runner.invoke(app, ["generate-api-key", "open"])
|
|
assert result.exit_code == 0, result.output
|
|
data = json.loads(keys_file.read_text())
|
|
entry = next(iter(data.values()))
|
|
assert "namespaces" not in entry
|
|
|
|
def test_list_shows_namespaces_column(self, tmp_path: Path):
|
|
keys_file = tmp_path / "api_keys.json"
|
|
keys_file.write_text(
|
|
json.dumps(
|
|
{
|
|
"h1": {
|
|
"name": "scoped",
|
|
"created": 1,
|
|
"expires": 2,
|
|
"last_used": None,
|
|
"namespaces": ["alpha"],
|
|
},
|
|
"h2": {
|
|
"name": "open",
|
|
"created": 1,
|
|
"expires": 2,
|
|
"last_used": None,
|
|
},
|
|
}
|
|
)
|
|
)
|
|
with patch("k7.cli.k7.API_KEYS_FILE", keys_file):
|
|
result = runner.invoke(app, ["list-api-keys"])
|
|
assert result.exit_code == 0, result.output
|
|
assert "Namespaces" in result.output
|
|
assert "alpha" in result.output
|
|
assert "*" in result.output
|