[+] Completed lab 2

This commit is contained in:
Антон Архипов
2026-11-16 16:01:24 +03:00
parent 65003b6119
commit f77e500ccf
46 changed files with 4532 additions and 45 deletions
+47
View File
@@ -0,0 +1,47 @@
"""Test UTF-8 Basic credentials, exact JSON validation and chat membership."""
import base64
from pathlib import Path
import sys
import uuid
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, arguments, check, run
def main():
args = arguments('Student checks: Basic and chat validation')
client = Client(args.base_url, args.ca_file)
username = 'u_colon_' + uuid.uuid4().hex[:12]
password = 'Пароль:🔑' * 3
user = client.call('POST', '/api/v1/users', 201, schema='User',
body={'username': username, 'display_name': 'Студент', 'password': password})['json']
basic = base64.b64encode(f'{username}:{password}'.encode()).decode()
auth = {'Authorization': 'Basic ' + basic}
session = client.call('POST', '/api/v1/auth/sessions', 201,
headers=auth, schema='Session')['json']
check(session['user'] == user, 'UTF-8 password with colon failed')
client.call('POST', '/api/v1/auth/sessions', 400, headers=auth, body={})
alice = {'id': user['id'], 'token': session['token']}
bob = client.user('bob')
path = '/api/v1/chats'
title = 'Проверка участников'
members = [alice['id'], bob['id']]
for body in (
{'title': title, 'member_ids': [alice['id'], str(uuid.uuid4())]},
{'title': title, 'member_ids': [alice['id'], alice['id'].upper()]},
{'title': title, 'member_ids': [bob['id']]},
{'title': 123, 'member_ids': members},
{'title': title, 'member_ids': members, 'unexpected': True},
):
client.call('POST', path, 400, actor=alice, body=body)
page = client.call('GET', path, actor=alice, schema='ChatPage')['json']
check(page['items'] == [], 'invalid creation left a visible chat')
chat = client.call('POST', path, 201, actor=alice,
body={'title': title, 'member_ids': members}, schema='Chat')['json']
listed = client.call('GET', path, actor=bob, schema='ChatPage')['json']
check(listed['items'] == [chat], 'valid chat or membership is missing')
print('PASS: UTF-8 password with colon, strict JSON, and atomic chat creation')
if __name__ == '__main__':
run(main)
+21
View File
@@ -0,0 +1,21 @@
"""Run against an API with SESSION_TTL_SECONDS=2."""
from pathlib import Path
import sys
from time import sleep
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, arguments, run
def main():
args = arguments('Student check: two-second session TTL')
client = Client(args.base_url, args.ca_file)
actor = client.user('expiry')
client.call('GET', '/api/v1/users/me', actor=actor, schema='User')
sleep(2.2)
client.call('GET', '/api/v1/users/me', 401, actor=actor)
print('PASS: a valid token became unauthorized after the two-second TTL')
if __name__ == '__main__':
run(main)