Files
mtusi2026lab4/tests/test_persistence.py
T

61 lines
2.4 KiB
Python

#!/usr/bin/env python3
import os
import sys
import time
import subprocess
import argparse
import hashlib
# Добавляем текущую папку в путь, чтобы импортировать client.py
sys.path.insert(0, os.path.dirname(__file__))
from client import Client, Failure, run
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--base-url', default='http://localhost:8080')
parser.add_argument('--action-script', required=True)
args = parser.parse_args()
c = Client(args.base_url)
alice, bob, eve, chat = c.bootstrap()
# Создаем сообщение и загружаем файл
# ВАЖНО: добавили schema='Attachment', чтобы клиент распарсил ответ
att = c.call('POST', f"/api/v1/chats/{chat['id']}/attachments?filename=test.png",
actor=alice, raw=open('tests/fixtures/sample.png', 'rb').read(),
headers={'Content-Type': 'image/png'}, expected=201, schema='Attachment')['json']
msg = c.message(alice, chat, "persist test", attachments=[att['id']])['json']
print(f"Created user={alice['id']} msg={msg['id']} att={att['id']}")
# Запускаем скрипт перезапуска
print(f"Running action: {args.action_script}")
subprocess.run(args.action_script, shell=True, check=True)
# Ждем готовности
print("Waiting for readiness...")
for _ in range(60):
try:
if c.call('GET', '/health/ready', expected=200)['status'] == 200:
break
except Exception:
pass
time.sleep(1)
# Проверяем сохранность токена
me = c.call('GET', '/api/v1/users/me', actor=alice, schema='User')['json']
assert me['id'] == alice['id'], "User ID changed after restart"
# Проверяем сохранность сообщения
history = c.call('GET', f"/api/v1/chats/{chat['id']}/messages", actor=alice, schema='MessagePage')['json']
assert any(m['id'] == msg['id'] for m in history['items']), "Message lost after restart"
# Проверяем сохранность файла (сравниваем SHA-256)
content = c.call('GET', f"/api/v1/attachments/{att['id']}/content", actor=alice)['bytes']
assert hashlib.sha256(content).hexdigest() == att['sha256'], "File content changed after restart"
print("PASS: persistence verified")
if __name__ == '__main__':
run(main)