forked from mtusi2026/mtusi2026lab3
62 lines
2.7 KiB
Python
62 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Reproduce teacher feedback through LB, including absence of side effects."""
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import uuid
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT/'tests'))
|
|
sys.path.insert(0, str(ROOT/'scripts'))
|
|
from client import Client
|
|
from actions import base_url
|
|
from resilience import history
|
|
|
|
|
|
def main():
|
|
c = Client(base_url())
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
path = '/api/v1/chats/'+chat['id']+'/messages'
|
|
results = []
|
|
|
|
def invalid(method, url, **kwargs):
|
|
response = c.call(method, url, 400, actor=alice, **kwargs)
|
|
assert response['json']['error']['code'] == 'invalid_request'
|
|
results.append({'method': method, 'path': url, 'status': 400})
|
|
|
|
bodies = [b'{"Text":"case-field"}', b'{"TEXT":"case-field"}',
|
|
b'{"text":"good","Text":"case-field"}',
|
|
b'{"\\u0054ext":"case-field"}',
|
|
b'{"text":"first","text":"second"}',
|
|
b'{"text":"first","te\\u0078t":"second"}']
|
|
before = history(c, bob, chat)
|
|
for body in bodies:
|
|
key = uuid.uuid4().hex
|
|
invalid('POST', path, raw=body,
|
|
headers={'Content-Type': 'application/json', 'Idempotency-Key': key})
|
|
assert history(c, bob, chat) == before, 'invalid request created a message'
|
|
# Invalid JSON must not reserve the key or store a 5xx/conflict result.
|
|
message = c.message(alice, chat, 'valid-'+key, key=key)['json']
|
|
assert c.message(alice, chat, 'valid-'+key, key=key)['json'] == message
|
|
before = history(c, bob, chat)
|
|
for endpoint in ('/api/v1/chats', path):
|
|
for query in ('limit=', 'cursor=', 'limit', 'cursor',
|
|
'limit=1&limit=2', 'cursor=&cursor=garbage'):
|
|
invalid('GET', endpoint+'?'+query)
|
|
for query in ('', '?limit=1', '?limit=100'):
|
|
c.call('GET', endpoint+query, actor=alice,
|
|
schema='ChatPage' if endpoint == '/api/v1/chats' else 'MessagePage')
|
|
invalid('POST', '/api/v1/chats', body={'Title': 'wrong case', 'member_ids': [alice['id']]})
|
|
c.call('POST', '/api/v1/users', 400,
|
|
body={'Username': 'review_'+uuid.uuid4().hex[:8], 'display_name': 'Review', 'password': 'long-password'})
|
|
report = {'pass': True, 'cases': results, 'exact_json_field_names': True,
|
|
'invalid_body_has_no_message_or_key_side_effect': True,
|
|
'empty_and_duplicate_pagination_rejected': True,
|
|
'absent_and_boundary_limits_accepted': True}
|
|
(ROOT/'evidence/review-regressions.json').write_text(json.dumps(report, indent=2)+'\n', encoding='utf-8')
|
|
print('PASS: exact JSON fields, pagination presence and no message/key side effects.')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|