import lab3 files

This commit is contained in:
Голиков Maximillian Антон
2026-10-18 21:14:01 +03:00
parent ec540a1a72
commit 76322040e5
64 changed files with 4446 additions and 0 deletions
+112
View File
@@ -0,0 +1,112 @@
#!/usr/bin/env python3
import sys
import uuid
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, LAB, Failure, arguments, check, run # noqa: E402
def main():
args = arguments('Собственные тесты негативных сценариев')
c = Client(args.base_url, args.ca_file)
alice, bob, eve, chat = c.bootstrap()
path = '/api/v1/chats/' + chat['id']
# --- некорректный JSON ---
malformed = b'{"username":"alice","display_name":"A"'
c.call('POST', '/api/v1/users', 400, raw=malformed,
headers={'Content-Type': 'application/json'})
# --- лишнее поле ---
c.call('POST', '/api/v1/users', 400,
body={'username': 'u_' + uuid.uuid4().hex[:8],
'display_name': 'X', 'extra': 'мусор'})
# --- неверный тип поля ---
c.call('POST', '/api/v1/users', 400,
body={'username': 123, 'display_name': 'X'})
# --- пустой display_name ---
c.call('POST', '/api/v1/users', 400,
body={'username': 'u_' + uuid.uuid4().hex[:8], 'display_name': ''})
# --- display_name только из пробелов ---
c.call('POST', '/api/v1/users', 400,
body={'username': 'u_' + uuid.uuid4().hex[:8], 'display_name': ' '})
# --- заглавные в username ---
c.call('POST', '/api/v1/users', 400,
body={'username': 'UPPERCASE', 'display_name': 'X'})
# --- дубликат username ---
c.call('POST', '/api/v1/users', 409, body=alice['registration'])
# --- пустое сообщение ---
c.call('POST', path + '/messages', 400, actor=alice, body={'text': ''})
# --- сообщение только из пробелов ---
c.call('POST', path + '/messages', 400, actor=alice, body={'text': ' '})
# --- чужой чат ---
c.call('GET', path, 404, actor=eve)
c.call('GET', path + '/messages', 404, actor=eve)
c.call('POST', path + '/messages', 404, actor=eve, body={'text': 'чужое'})
# --- чат без создателя в member_ids ---
c.call('POST', '/api/v1/chats', 400, actor=alice,
body={'title': 'Без меня', 'member_ids': [bob['id']]})
# --- чат с неизвестным участником ---
c.call('POST', '/api/v1/chats', 400, actor=alice,
body={'title': 'С фантомом',
'member_ids': [alice['id'], '00000000-0000-4000-8000-000000000000']})
# --- чат с дубликатами ---
c.call('POST', '/api/v1/chats', 400, actor=alice,
body={'title': 'Дубликаты', 'member_ids': [alice['id'], alice['id']]})
# --- обход нескольких страниц ---
for i in range(5):
c.message(alice, chat, 'page-test-%d' % i)
seen = []
cursor = None
for _ in range(20):
query = path + '/messages?limit=2'
if cursor:
from urllib.parse import quote
query += '&cursor=' + quote(cursor, safe='')
page = c.call('GET', query, actor=alice, schema='MessagePage')['json']
seen.extend(x['id'] for x in page['items'])
cursor = page['next_cursor']
if cursor is None:
break
check(len(seen) == len(set(seen)), 'пагинация вернула дубликаты')
check(len(seen) >= 5, 'пагинация потеряла сообщения')
# --- cursor от другого чата ---
other_chat = c.call('POST', '/api/v1/chats', 201, actor=alice,
body={'title': 'Другой чат',
'member_ids': [alice['id'], bob['id']]},
schema='Chat')['json']
other_path = '/api/v1/chats/' + other_chat['id']
c.message(alice, other_chat, 'other-1')
c.message(alice, other_chat, 'other-2')
other_page = c.call('GET', other_path + '/messages?limit=1', actor=alice,
schema='MessagePage')['json']
other_cursor = other_page['next_cursor']
check(other_cursor is not None, 'ожидался cursor от другого чата, но получен null')
from urllib.parse import quote
c.call('GET', path + '/messages?limit=2&cursor=' + quote(other_cursor, safe=''),
400, actor=alice)
print('PASS: негативные сценарии прошли')
if __name__ == '__main__':
run(main)