"""Removed member must get 404 on a repeated key before the body is compared. Membership is revoked with DELETE in Postgres, the same way an isolated database check does it. The script prints status codes only. """ import re import subprocess import sys import uuid from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) sys.path.insert(0, str(Path(__file__).resolve().parent)) from client import Client import stand UUID = re.compile(r'^[0-9a-fA-F-]{36}$') def revoke(user_id, chat_id): if UUID.fullmatch(user_id) is None or UUID.fullmatch(chat_id) is None: raise SystemExit('refusing to interpolate a non-uuid') sql = ( "DELETE FROM chat_members WHERE user_id = '%s' AND chat_id = '%s'" % (user_id, chat_id) ) stand.compose(['exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-c', sql]) def post(client, actor, chat_id, text, key, expected): result = client.call( 'POST', '/api/v1/chats/%s/messages' % chat_id, expected, actor=actor, body={'text': text}, headers={'Idempotency-Key': key}, schema='Message' if expected == 201 else 'Error', ) print('status', result['status'], 'expected', expected) return result def main(): stand.wait_ready() client = Client(stand.BASE, timeout=10) alice = client.user('acl') bob = client.user('aclpeer') chat = client.call( 'POST', '/api/v1/chats', 201, actor=alice, body={'title': 'Проверка ключа', 'member_ids': [alice['id'], bob['id']]}, schema='Chat', )['json'] key = 'same-' + uuid.uuid4().hex[:16] post(client, alice, chat['id'], 'первое', key, 201) post(client, alice, chat['id'], 'другое', key, 409) revoke(alice['id'], chat['id']) post(client, alice, chat['id'], 'другое', key, 404) post(client, alice, chat['id'], 'первое', key, 404) history = client.call( 'GET', '/api/v1/chats/%s/messages' % chat['id'], 404, actor=alice, schema='Error', ) print('history', history['status']) print('access check passed') if __name__ == '__main__': main()