#!/usr/bin/env python3 """Negative-path checks: bad JSON, empty/oversized text, foreign chat, duplicate username, bad auth, invalid limit, extra fields.""" from _bootstrap import TESTS_DIR # noqa: F401 (adds tests/ to sys.path) import uuid from client import Client, arguments, run def main(): args = arguments('Student negative tests') c = Client(args.base_url, args.ca_file) alice, bob, eve, chat = c.bootstrap() path = '/api/v1/chats/' + chat['id'] # 1. Malformed JSON: the client asserts the server must answer 400. c.call('POST', '/api/v1/users', 400, raw=b'{not json', headers={'Content-Type': 'application/json'}) c.call('POST', path + '/messages', 400, actor=alice, raw=b'{"text":', headers={'Content-Type': 'application/json'}) # 2. Empty / whitespace / oversized text. c.call('POST', path + '/messages', 400, actor=alice, body={'text': ''}) c.call('POST', path + '/messages', 400, actor=alice, body={'text': ' '}) c.call('POST', path + '/messages', 400, actor=alice, body={'text': 'x' * 4001}) # 3. Foreign chat: eve is not a member. c.call('GET', path, 404, actor=eve) c.call('POST', path + '/messages', 404, actor=eve, body={'text': 'hack'}) # 4. Duplicate username. dup = {'username': 'uniq_' + uuid.uuid4().hex[:8], 'display_name': 'Dup'} c.call('POST', '/api/v1/users', 201, body=dup) c.call('POST', '/api/v1/users', 409, body=dup) # 5. Authentication edge cases. c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': str(uuid.uuid4())}) c.call('GET', '/api/v1/users/me', 401) c.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': 'not-a-uuid'}) # 6. Invalid limits. c.call('GET', path + '/messages?limit=0', 400, actor=alice) c.call('GET', path + '/messages?limit=101', 400, actor=alice) c.call('GET', path + '/messages?limit=abc', 400, actor=alice) c.call('GET', path + '/messages?limit=', 400, actor=alice) # 7. Invalid chat id in path. c.call('GET', '/api/v1/chats/not-a-uuid', 400, actor=alice) # 8. Extra fields are rejected (additionalProperties: false). c.call('POST', '/api/v1/users', 400, body={'username': 'extra_' + uuid.uuid4().hex[:6], 'display_name': 'X', 'password': 'p'}) c.call('POST', path + '/messages', 400, actor=alice, body={'text': 'hi', 'sender_id': alice['id']}) print('PASS: student negative tests') if __name__ == '__main__': run(main)