forked from mtusi2026/mtusi2026lab4
Bring the application, own tests, Dockerfile, toolchain, compose stack, HAProxy and k6 configuration, migrations, hooks, evidence and level 4 scripts and the CI job from the lab 3 solution commit 61c0c28, merge the lab 3 Makefile targets into the lab 4 skeleton Makefile without touching its targets, and append the lab 3 solution variables to the skeleton .env.example. The package and the application follow the contract version 1.4.0, and the image and the throwaway test containers are tagged lab4 so they do not collide with a lab 3 stand on the same host. This is the starting point of the lab 4 cycle: lint, own tests and the offline skeleton check pass before any lab 4 change. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
from base64 import b64encode
|
|
|
|
import pytest
|
|
|
|
from messenger.identity.entrypoints.authentication import (
|
|
basic_credentials,
|
|
bearer_token,
|
|
challenge_for,
|
|
)
|
|
|
|
|
|
def encoded(raw: bytes) -> str:
|
|
return "Basic " + b64encode(raw).decode()
|
|
|
|
|
|
def test_basic_splits_on_first_colon() -> None:
|
|
assert basic_credentials(encoded(b"alice:pa:ss:word")) == ("alice", "pa:ss:word")
|
|
|
|
|
|
def test_basic_decodes_utf8_password() -> None:
|
|
assert basic_credentials(encoded("alice:пароль👋".encode())) == ("alice", "пароль👋")
|
|
|
|
|
|
def test_basic_scheme_is_case_insensitive() -> None:
|
|
assert basic_credentials("bAsIc " + b64encode(b"alice:secret").decode()) == ("alice", "secret")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"raw",
|
|
[
|
|
"",
|
|
"Basic",
|
|
"Basic ",
|
|
"Basic !!!not-base64!!!",
|
|
"Basic YWxpY2U",
|
|
encoded(b"no-colon-here"),
|
|
encoded("алиса:пароль".encode()),
|
|
encoded(b"alice:\xff\xfe"),
|
|
"Bearer " + b64encode(b"alice:secret").decode(),
|
|
],
|
|
)
|
|
def test_basic_rejects_malformed(raw: str) -> None:
|
|
assert basic_credentials(raw) is None
|
|
|
|
|
|
def test_bearer_accepts_token68() -> None:
|
|
assert bearer_token("Bearer abc-DEF_123.~+/==") == "abc-DEF_123.~+/=="
|
|
assert bearer_token("bearer token") == "token"
|
|
|
|
|
|
@pytest.mark.parametrize("raw", ["", "Bearer", "Bearer ", "Bearer a b", "Basic abc", "x" * 40])
|
|
def test_bearer_rejects_malformed(raw: str) -> None:
|
|
assert bearer_token(raw) is None
|
|
|
|
|
|
def test_bearer_rejects_oversized_token() -> None:
|
|
assert bearer_token("Bearer " + "x" * 513) is None
|
|
|
|
|
|
def test_challenge_depends_only_on_path() -> None:
|
|
assert challenge_for("/api/v1/auth/sessions") == 'Basic realm="messenger", charset="UTF-8"'
|
|
assert challenge_for("/api/v1/auth/sessions/") == 'Basic realm="messenger", charset="UTF-8"'
|
|
assert challenge_for("/api/v1/auth/sessions/current") == 'Bearer realm="messenger"'
|
|
assert challenge_for("/api/v1/users/me") == 'Bearer realm="messenger"'
|