forked from mtusi2026/mtusi2026lab4
@@ -0,0 +1,96 @@
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
import unittest
|
||||
from uuid import uuid4
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from helpers import Client
|
||||
|
||||
|
||||
def basic(username, password):
|
||||
value = base64.b64encode((username + ":" + password).encode("utf-8")).decode("ascii")
|
||||
return {"Authorization": "Basic " + value}
|
||||
|
||||
|
||||
class SessionTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.client = Client(os.environ.get("BASE_URL", "http://127.0.0.1:18104"))
|
||||
self.actor = self.client.user("auth")
|
||||
|
||||
def register(self, password):
|
||||
body = {"username": "p_" + uuid4().hex[:20], "display_name": "Пароль", "password": password}
|
||||
user = self.client.call("POST", "/api/v1/users", 201, body=body, schema="User")["json"]
|
||||
session = self.client.call("POST", "/api/v1/auth/sessions", 201,
|
||||
headers=basic(body["username"], password), schema="Session")["json"]
|
||||
self.assertEqual(session["user"], user)
|
||||
self.assertEqual(set(session), {"token", "token_type", "expires_at", "user"})
|
||||
self.assertTrue(re.fullmatch(r"[A-Za-z0-9_-]{43}", session["token"]), "Invalid token format")
|
||||
return body, session
|
||||
|
||||
def test_password_required_and_limits(self):
|
||||
source = self.actor["registration"]
|
||||
for password in ("x" * 11, "x" * 129, " " * 12, None, 123, True, "a" * 12 + "\x00"):
|
||||
body = {**source, "username": "p_" + uuid4().hex[:20], "password": password}
|
||||
self.client.call("POST", "/api/v1/users", 400, body=body)
|
||||
missing = {key: value for key, value in source.items() if key != "password"}
|
||||
self.client.call("POST", "/api/v1/users", 400, body=missing)
|
||||
raw = json.dumps({**source, "username": "p_" + uuid4().hex[:20], "password": "\ud800" * 12}).encode()
|
||||
self.client.call("POST", "/api/v1/users", 400, raw=raw, headers={"Content-Type": "application/json"})
|
||||
|
||||
def test_password_boundaries_unicode_colon_spaces(self):
|
||||
for password in ("x" * 12, "🙂" * 127 + ":", " Привет:пароль🙂 "):
|
||||
body, session = self.register(password)
|
||||
actor = {"id": session["user"]["id"], "token": session["token"]}
|
||||
self.client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
||||
changed = password.strip() if password != password.strip() else password + "!"
|
||||
self.client.call("POST", "/api/v1/auth/sessions", 401,
|
||||
headers=basic(body["username"], changed))
|
||||
|
||||
def test_wrong_and_unknown_credentials(self):
|
||||
source = self.actor["registration"]
|
||||
wrong = self.client.call("POST", "/api/v1/auth/sessions", 401,
|
||||
headers=basic(source["username"], "wrong-password-123"))
|
||||
unknown = self.client.call("POST", "/api/v1/auth/sessions", 401,
|
||||
headers=basic("unknown_" + uuid4().hex[:12], source["password"]))
|
||||
self.assertEqual(wrong["json"]["error"]["message"], unknown["json"]["error"]["message"])
|
||||
self.assertEqual(wrong["headers"]["WWW-Authenticate"], 'Basic realm="messenger", charset="UTF-8"')
|
||||
|
||||
def test_malformed_basic_and_body(self):
|
||||
for value in ("", "Basic", "Basic !!!", "Bearer " + self.actor["token"],
|
||||
"Basic " + base64.b64encode(b"missing-colon").decode(),
|
||||
"Basic " + base64.b64encode(b"\xff:some-password").decode()):
|
||||
self.client.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": value})
|
||||
source = self.actor["registration"]
|
||||
self.client.call("POST", "/api/v1/auth/sessions", 400, body={},
|
||||
headers=basic(source["username"], source["password"]))
|
||||
|
||||
def test_only_bearer_identifies_user(self):
|
||||
source = self.actor["registration"]
|
||||
for value in ("", "Bearer", "Bearer bad", "Bearer " + "x" * 43,
|
||||
basic(source["username"], source["password"])["Authorization"]):
|
||||
self.client.call("GET", "/api/v1/users/me", 401, headers={"Authorization": value})
|
||||
self.client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": self.actor["id"]})
|
||||
other = self.client.user("other")
|
||||
result = self.client.call("GET", "/api/v1/users/me", actor=self.actor,
|
||||
headers={"X-User-Id": other["id"]}, schema="User")
|
||||
self.assertEqual(result["json"]["id"], self.actor["id"])
|
||||
self.client.call("GET", "/api/v1/users/me", schema="User",
|
||||
headers={"Authorization": "bEaReR " + self.actor["token"]})
|
||||
|
||||
def test_logout_only_revokes_current_session(self):
|
||||
source = self.actor["registration"]
|
||||
second = self.client.call("POST", "/api/v1/auth/sessions", 201,
|
||||
headers=basic(source["username"], source["password"]), schema="Session")["json"]
|
||||
other_session = {"id": self.actor["id"], "token": second["token"]}
|
||||
self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.actor)
|
||||
for method, path in (("GET", "/api/v1/users/me"), ("DELETE", "/api/v1/auth/sessions/current")):
|
||||
self.client.call(method, path, 401, actor=self.actor)
|
||||
self.client.call("GET", "/api/v1/users/me", actor=other_session, schema="User")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
Reference in New Issue
Block a user