forked from mtusi2026/mtusi2026lab2
131 lines
3.3 KiB
Python
Executable File
131 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Verify PostgreSQL uniqueness and atomic chat creation through the public API."""
|
|
import base64
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import uuid
|
|
|
|
from client import Client, arguments, check, run
|
|
|
|
|
|
def register_same_user(base_url, ca_file, body):
|
|
client = Client(base_url, ca_file, timeout=15)
|
|
return client.call(
|
|
'POST',
|
|
'/api/v1/users',
|
|
[201, 409],
|
|
body=body,
|
|
schema='User',
|
|
)
|
|
|
|
|
|
def login(client, registration):
|
|
credential = base64.b64encode(
|
|
(
|
|
registration['username']
|
|
+ ':'
|
|
+ registration['password']
|
|
).encode()
|
|
).decode()
|
|
session = client.call(
|
|
'POST',
|
|
'/api/v1/auth/sessions',
|
|
201,
|
|
headers={'Authorization': 'Basic ' + credential},
|
|
schema='Session',
|
|
)['json']
|
|
return {
|
|
'id': session['user']['id'],
|
|
'user': session['user'],
|
|
'registration': registration,
|
|
'token': session['token'],
|
|
}
|
|
|
|
|
|
def main():
|
|
args = arguments(
|
|
'Level 4 PostgreSQL race and transaction checks; creates synthetic data'
|
|
)
|
|
client = Client(args.base_url, args.ca_file, timeout=15)
|
|
client.call('GET', '/health/ready', schema='Health')
|
|
|
|
registration = {
|
|
'username': 'race_' + uuid.uuid4().hex[:16],
|
|
'display_name': 'Concurrent registration',
|
|
'password': 'T3st-' + uuid.uuid4().hex,
|
|
}
|
|
|
|
with ThreadPoolExecutor(max_workers=4) as executor:
|
|
futures = [
|
|
executor.submit(
|
|
register_same_user,
|
|
args.base_url,
|
|
args.ca_file,
|
|
registration,
|
|
)
|
|
for _ in range(4)
|
|
]
|
|
results = [future.result() for future in futures]
|
|
|
|
statuses = [result['status'] for result in results]
|
|
check(
|
|
statuses.count(201) == 1 and statuses.count(409) == 3,
|
|
'concurrent duplicate registration must yield one 201 and three 409 responses',
|
|
)
|
|
|
|
actor = login(client, registration)
|
|
before = client.call(
|
|
'GET',
|
|
'/api/v1/chats?limit=100',
|
|
actor=actor,
|
|
schema='ChatPage',
|
|
)['json']
|
|
check(not before['items'], 'new race-test user unexpectedly has chats')
|
|
|
|
failed_title = 'Rollback proof ' + uuid.uuid4().hex[:8]
|
|
client.call(
|
|
'POST',
|
|
'/api/v1/chats',
|
|
400,
|
|
actor=actor,
|
|
body={
|
|
'title': failed_title,
|
|
'member_ids': [actor['id'], str(uuid.uuid4())],
|
|
},
|
|
)
|
|
|
|
after = client.call(
|
|
'GET',
|
|
'/api/v1/chats?limit=100',
|
|
actor=actor,
|
|
schema='ChatPage',
|
|
)['json']
|
|
check(
|
|
not after['items'],
|
|
'failed composite chat creation left visible partial data',
|
|
)
|
|
|
|
successful = client.call(
|
|
'POST',
|
|
'/api/v1/chats',
|
|
201,
|
|
actor=actor,
|
|
body={
|
|
'title': 'After rollback ' + uuid.uuid4().hex[:8],
|
|
'member_ids': [actor['id']],
|
|
},
|
|
schema='Chat',
|
|
)['json']
|
|
check(
|
|
successful['member_ids'] == [actor['id']],
|
|
'connection did not recover after rolled-back transaction',
|
|
)
|
|
|
|
print(
|
|
'PASS: concurrent username race produced one winner; '
|
|
'failed composite chat creation rolled back and the connection remained usable.'
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|