Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6 and RustFS configuration, migrations, hooks, evidence and
scenario scripts and the CI job from the lab 4 solution commit d2744bc,
merge the lab 4 Makefile targets into the lab 5 skeleton Makefile
without touching its targets, and append the lab 4 solution variables
to the skeleton .env.example. The package and the application follow
the contract version 1.5.0, and the image and the throwaway test
containers are tagged lab5 so they do not collide with a lab 4 stand on
the same host.
This is the starting point of the lab 5 cycle: lint, own tests and the
offline skeleton check pass before any lab 5 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"'
|