Merge branch 'feature/cli-module'
This commit is contained in:
commit
80960d5a1f
4 changed files with 204 additions and 4 deletions
|
|
@ -2,7 +2,7 @@ from datetime import UTC, datetime, timedelta
|
|||
|
||||
from httpx import AsyncClient
|
||||
|
||||
from porchlight.models import MagicLink
|
||||
from porchlight.models import MagicLink, User
|
||||
|
||||
|
||||
async def test_register_invalid_token_returns_error_page(client: AsyncClient) -> None:
|
||||
|
|
@ -70,3 +70,39 @@ async def test_register_used_token_returns_error(client: AsyncClient) -> None:
|
|||
|
||||
res = await client.get("/register/used", follow_redirects=False)
|
||||
assert res.status_code == 400
|
||||
|
||||
|
||||
async def test_register_existing_user_logs_in_and_redirects(client: AsyncClient) -> None:
|
||||
"""When initial-admin creates a user, the invite link should log them in."""
|
||||
app = client._transport.app # type: ignore[union-attr]
|
||||
magic_link_repo = app.state.magic_link_repo
|
||||
user_repo = app.state.user_repo
|
||||
|
||||
# Pre-create the user (as initial-admin would)
|
||||
user = User(userid="lusab-bansen", username="admin", groups=["admin", "users"])
|
||||
await user_repo.create(user)
|
||||
|
||||
# Create invite for the same username
|
||||
await magic_link_repo.create(
|
||||
MagicLink(
|
||||
token="admin-setup",
|
||||
username="admin",
|
||||
expires_at=datetime.now(UTC) + timedelta(hours=1),
|
||||
)
|
||||
)
|
||||
|
||||
res = await client.get("/register/admin-setup", follow_redirects=False)
|
||||
assert res.status_code in (302, 303)
|
||||
assert "/manage/credentials" in res.headers["location"]
|
||||
assert "setup=1" in res.headers["location"]
|
||||
|
||||
# Token should be marked used
|
||||
link = await magic_link_repo.get_by_token("admin-setup")
|
||||
assert link is not None
|
||||
assert link.used is True
|
||||
|
||||
# Original user should still exist with original groups
|
||||
existing = await user_repo.get_by_username("admin")
|
||||
assert existing is not None
|
||||
assert existing.userid == "lusab-bansen"
|
||||
assert "admin" in existing.groups
|
||||
|
|
|
|||
86
tests/test_cli.py
Normal file
86
tests/test_cli.py
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import os
|
||||
import tempfile
|
||||
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from porchlight.cli import app
|
||||
|
||||
runner = CliRunner()
|
||||
|
||||
ENV = {"OIDC_OP_ISSUER": "https://example.com"}
|
||||
|
||||
|
||||
def _env(tmpdir: str) -> dict[str, str]:
|
||||
return {**ENV, "OIDC_OP_SQLITE_PATH": os.path.join(tmpdir, "test.db")}
|
||||
|
||||
|
||||
# -- create-invite -----------------------------------------------------------
|
||||
|
||||
|
||||
def test_create_invite_prints_registration_url() -> None:
|
||||
"""create-invite should print a URL containing /register/."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
result = runner.invoke(app, ["create-invite", "testuser"], env=_env(tmpdir))
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "https://example.com/register/" in result.output
|
||||
|
||||
|
||||
def test_create_invite_with_note() -> None:
|
||||
"""create-invite with --note should work."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
result = runner.invoke(app, ["create-invite", "testuser", "--note", "Welcome aboard"], env=_env(tmpdir))
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "https://example.com/register/" in result.output
|
||||
|
||||
|
||||
def test_create_invite_with_custom_ttl() -> None:
|
||||
"""create-invite with --ttl should use the custom TTL."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
result = runner.invoke(app, ["create-invite", "testuser", "--ttl", "3600"], env=_env(tmpdir))
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "https://example.com/register/" in result.output
|
||||
|
||||
|
||||
def test_create_invite_missing_username_shows_error() -> None:
|
||||
"""create-invite without a username should show an error."""
|
||||
result = runner.invoke(app, ["create-invite"])
|
||||
assert result.exit_code != 0
|
||||
|
||||
|
||||
# -- initial-admin ------------------------------------------------------------
|
||||
|
||||
|
||||
def test_initial_admin_creates_user_and_prints_url() -> None:
|
||||
"""initial-admin should create a user and print a registration URL."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
result = runner.invoke(app, ["initial-admin", "admin"], env=_env(tmpdir))
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "https://example.com/register/" in result.output
|
||||
|
||||
|
||||
def test_initial_admin_with_custom_groups() -> None:
|
||||
"""initial-admin with --group should assign those groups."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
result = runner.invoke(
|
||||
app, ["initial-admin", "admin", "--group", "admin", "--group", "superusers"], env=_env(tmpdir)
|
||||
)
|
||||
assert result.exit_code == 0, result.output
|
||||
assert "https://example.com/register/" in result.output
|
||||
|
||||
|
||||
def test_initial_admin_duplicate_username_fails() -> None:
|
||||
"""initial-admin should fail if username already exists."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
env = _env(tmpdir)
|
||||
# Create the user first
|
||||
result1 = runner.invoke(app, ["initial-admin", "admin"], env=env)
|
||||
assert result1.exit_code == 0, result1.output
|
||||
# Try again — should fail
|
||||
result2 = runner.invoke(app, ["initial-admin", "admin"], env=env)
|
||||
assert result2.exit_code != 0
|
||||
|
||||
|
||||
def test_initial_admin_missing_username_shows_error() -> None:
|
||||
"""initial-admin without a username should show an error."""
|
||||
result = runner.invoke(app, ["initial-admin"])
|
||||
assert result.exit_code != 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue