forked from mtusi2026/mtusi2026lab2
52 lines
3.0 KiB
Python
52 lines
3.0 KiB
Python
import base64
|
|
from uuid import uuid4
|
|
|
|
from helpers import ApiTestCase
|
|
|
|
|
|
class SessionTests(ApiTestCase):
|
|
def basic(self, username, password):
|
|
value = base64.b64encode((username + ":" + password).encode()).decode()
|
|
return {"Authorization": "Basic " + value}
|
|
|
|
def test_wrong_password_and_basic_only_for_login(self):
|
|
registration = self.alice["registration"]
|
|
self.assert_error("POST", "/api/v1/auth/sessions", 401, "unauthorized",
|
|
headers=self.basic(registration["username"], "wrong-password"))
|
|
self.assert_error("POST", "/api/v1/auth/sessions", 401, "unauthorized")
|
|
self.assert_error("POST", "/api/v1/auth/sessions", 401, "unauthorized",
|
|
headers={"Authorization": "Basic not-base64!"})
|
|
self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized",
|
|
headers=self.basic(registration["username"], registration["password"]))
|
|
|
|
def test_logout_revokes_only_one_session(self):
|
|
registration = self.alice["registration"]
|
|
second = self.client.call("POST", "/api/v1/auth/sessions", 201, schema="Session",
|
|
headers=self.basic(registration["username"], registration["password"]))["json"]
|
|
self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.alice)
|
|
self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized", actor=self.alice)
|
|
self.assert_error("DELETE", "/api/v1/auth/sessions/current", 401, "unauthorized", actor=self.alice)
|
|
self.client.call("GET", "/api/v1/users/me", schema="User", headers={"Authorization": "Bearer " + second["token"]})
|
|
|
|
def test_password_validation(self):
|
|
for password in ("short", "x" * 129, 123, None, " " * 12):
|
|
body = {"username": "pw_" + uuid4().hex[:20], "display_name": "Пароль", "password": password}
|
|
self.assert_error("POST", "/api/v1/users", 400, "invalid_request", body=body)
|
|
|
|
def test_unicode_password_with_colon_and_private_response(self):
|
|
username = "pw_" + uuid4().hex[:20]
|
|
password = "Пароль:с:двоеточием-123"
|
|
user = self.client.call("POST", "/api/v1/users", 201, schema="User",
|
|
body={"username": username, "display_name": "Пароль", "password": password})["json"]
|
|
session = self.client.call("POST", "/api/v1/auth/sessions", 201, schema="Session",
|
|
headers=self.basic(username, password))["json"]
|
|
self.assertEqual(session["user"], user)
|
|
self.assertEqual(set(user), {"id", "username", "display_name", "created_at"})
|
|
self.assertGreaterEqual(len(session["token"]), 32)
|
|
|
|
def test_sql_like_message_is_plain_text(self):
|
|
text = "'; DROP TABLE messages; -- Привет"
|
|
message = self.client.message(self.alice, self.chat, text)["json"]
|
|
self.assertEqual(message["text"], text)
|
|
self.assertEqual(self.read_all(self.path + "/messages", self.bob, "MessagePage"), [message])
|