Files
mtusi2026lab3/student-tests/auth_and_chat.py
T

48 lines
2.2 KiB
Python

"""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)