Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.
This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 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"'
|