forked from mtusi2026/mtusi2026lab4
feat: attachments over S3 (grade 3)
This commit is contained in:
Executable
+130
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Attachment contract checks beyond the public smoke; creates synthetic data."""
|
||||
import hashlib
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, check, run
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
PNG = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
|
||||
|
||||
def upload(c, actor, chat, content=PNG, filename='sample.png', content_type='image/png', expected=201):
|
||||
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename
|
||||
return c.call('POST', path, expected, actor=actor, raw=content, headers={'Content-Type': content_type}, schema='Attachment')
|
||||
|
||||
|
||||
def main():
|
||||
c = Client()
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
other = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Другой чат', 'member_ids': [alice['id'], bob['id']]}, schema='Chat')['json']
|
||||
notes = []
|
||||
|
||||
att = upload(c, alice, chat)['json']
|
||||
notes.append(f"upload 201 ready, sha256={att['sha256']}, size={att['size_bytes']}")
|
||||
check(att['sha256'] == hashlib.sha256(PNG).hexdigest(), 'sha256 mismatch')
|
||||
check(att['status'] == 'ready' and att['variants'] == [] and att['error_code'] is None, 'attachment shape')
|
||||
|
||||
jpeg_path = ROOT / 'scripts/fixtures/sample.jpg'
|
||||
if jpeg_path.is_file():
|
||||
jpeg = jpeg_path.read_bytes()
|
||||
jatt = upload(c, alice, chat, content=jpeg, filename='sample.jpg', content_type='image/jpeg')['json']
|
||||
check(jatt['content_type'] == 'image/jpeg' and jatt['sha256'] == hashlib.sha256(jpeg).hexdigest(),
|
||||
'jpeg metadata mismatch')
|
||||
got = c.call('GET', '/api/v1/attachments/' + jatt['id'] + '/content', 200, actor=bob)
|
||||
check(got['bytes'] == jpeg and got['headers'].get_content_type() == 'image/jpeg', 'jpeg download mismatch')
|
||||
upload(c, alice, chat, content=jpeg, filename='sample.jpg', content_type='image/png', expected=415)
|
||||
notes.append('jpeg upload/download byte-identical; jpeg declared as png -> 415')
|
||||
|
||||
large_path = ROOT / 'scripts/fixtures/large.png'
|
||||
if large_path.is_file():
|
||||
large = large_path.read_bytes()
|
||||
latt = upload(c, alice, chat, content=large, filename='large.png')['json']
|
||||
check(latt['size_bytes'] == len(large) and latt['status'] == 'ready', 'image at the pixel limit was rejected')
|
||||
got = c.call('GET', '/api/v1/attachments/' + latt['id'] + '/content', 200, actor=bob)
|
||||
check(got['bytes'] == large, 'large image download mismatch')
|
||||
notes.append('png with 40,000,000 pixels accepted and downloaded byte-identical')
|
||||
|
||||
upload(c, alice, chat, filename='bad/name.png', expected=400)
|
||||
notes.append('filename with slash -> 400')
|
||||
upload(c, alice, chat, filename='bad%00name.png', expected=400)
|
||||
notes.append('filename with NUL -> 400')
|
||||
upload(c, alice, chat, filename='', expected=400)
|
||||
notes.append('empty filename -> 400')
|
||||
upload(c, alice, chat, content_type='image/jpeg', expected=415)
|
||||
notes.append('declared jpeg with png body -> 415')
|
||||
upload(c, alice, chat, content=PNG[:-30], expected=415)
|
||||
notes.append('truncated png -> 415')
|
||||
upload(c, alice, chat, content=b'\x00' * (10485761), expected=413)
|
||||
notes.append('10485761 bytes -> 413')
|
||||
upload(c, alice, chat, content_type='text/plain', expected=415)
|
||||
notes.append('text/plain -> 415')
|
||||
|
||||
c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=thumbnail', 400, actor=bob)
|
||||
notes.append('unknown variant -> 400')
|
||||
c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=original', 200, actor=bob)
|
||||
notes.append('variant=original -> 200')
|
||||
c.call('GET', '/api/v1/attachments/' + str(uuid.uuid4()), 404, actor=bob)
|
||||
notes.append('unknown attachment id -> 404')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=x.png', 404, actor=eve,
|
||||
raw=PNG, headers={'Content-Type': 'image/png'}, schema='Attachment')
|
||||
notes.append('upload by outsider -> 404')
|
||||
|
||||
foreign = upload(c, alice, other)['json']
|
||||
key = uuid.uuid4().hex
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice,
|
||||
body={'text': '', 'attachment_ids': [foreign['id']]}, headers={'Idempotency-Key': key})
|
||||
notes.append('attachment from another chat -> 404')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice,
|
||||
body={'text': '', 'attachment_ids': [str(uuid.uuid4())]}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
notes.append('unknown attachment id in message -> 404')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': '', 'attachment_ids': [att['id']] * 2}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
notes.append('duplicate attachment_ids -> 400')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': '', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]},
|
||||
headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
notes.append('eleven attachment_ids -> 400')
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
|
||||
body={'text': ''}, headers={'Idempotency-Key': uuid.uuid4().hex})
|
||||
notes.append('empty text without attachments -> 400')
|
||||
|
||||
key = uuid.uuid4().hex
|
||||
first = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
|
||||
body={'text': 'вложение', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': key}, schema='Message')['json']
|
||||
replay = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
|
||||
body={'text': 'вложение', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': key}, schema='Message')['json']
|
||||
check(first == replay, 'idempotent replay with attachments differs')
|
||||
check(first['attachment_ids'] == [att['id']], 'message lost attachment_ids')
|
||||
notes.append('idempotent replay with same attachment set -> same message')
|
||||
|
||||
empty_key = uuid.uuid4().hex
|
||||
without = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
|
||||
body={'text': 'без вложений'}, headers={'Idempotency-Key': empty_key}, schema='Message')['json']
|
||||
implicit = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
|
||||
body={'text': 'без вложений', 'attachment_ids': []}, headers={'Idempotency-Key': empty_key}, schema='Message')['json']
|
||||
check(without == implicit, 'absent attachment_ids must equal an empty list')
|
||||
notes.append('absent attachment_ids equals [] for idempotency')
|
||||
|
||||
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=alice,
|
||||
body={'text': 'вложение', 'attachment_ids': []}, headers={'Idempotency-Key': key})
|
||||
notes.append('same key with a different attachment set -> 409')
|
||||
|
||||
history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=100', 200, actor=bob,
|
||||
schema='MessagePage')['json']['items']
|
||||
check(all(isinstance(m['attachment_ids'], list) for m in history), 'attachment_ids must always be an array')
|
||||
check(any(m['id'] == first['id'] and m['attachment_ids'] == [att['id']] for m in history),
|
||||
'history lost attachments')
|
||||
notes.append('history exposes attachment_ids on every message')
|
||||
|
||||
for line in notes:
|
||||
print('ok:', line)
|
||||
print(f'PASS: {len(notes)} attachment contract checks')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 124 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 653 B |
@@ -13,6 +13,11 @@ password=$(cat secrets/postgres_password)
|
||||
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$password" > secrets/database_url
|
||||
[ -f secrets/cache_url ] ||
|
||||
printf 'redis://redis:6379/0\n' > secrets/cache_url
|
||||
[ -f secrets/s3_access_key_id ] ||
|
||||
head -c 10 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_access_key_id
|
||||
[ -f secrets/s3_secret_access_key ] ||
|
||||
head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_secret_access_key
|
||||
|
||||
chmod 644 secrets/postgres_password secrets/database_url secrets/cache_url
|
||||
echo "secrets ready in ./secrets"
|
||||
chmod 644 secrets/postgres_password secrets/database_url secrets/cache_url \
|
||||
secrets/s3_access_key_id secrets/s3_secret_access_key
|
||||
echo "secrets ready in ./secrets"
|
||||
+2
-2
@@ -4,5 +4,5 @@ cd "$(dirname "$0")/.."
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
COMPOSE=${COMPOSE:-$([ "$ENGINE" = podman ] && echo podman-compose || echo "docker compose")}
|
||||
|
||||
$COMPOSE stop lb api redis postgres
|
||||
$COMPOSE start postgres redis api lb
|
||||
$COMPOSE stop lb api redis postgres s3
|
||||
$COMPOSE start postgres redis s3 api lb
|
||||
@@ -3,7 +3,7 @@ set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
COMPOSE=${COMPOSE:-$([ "$ENGINE" = podman ] && echo podman-compose || echo "docker compose")}
|
||||
PROJECT=${PROJECT:-messenger-lab3}
|
||||
PROJECT=${PROJECT:-messenger-lab4}
|
||||
|
||||
replicas() {
|
||||
$ENGINE ps -aq --no-trunc --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api" | sort
|
||||
@@ -20,7 +20,7 @@ wait_healthy() {
|
||||
exit 1
|
||||
}
|
||||
|
||||
$ENGINE build --build-arg "BUILD_ID=${BUILD_ID:-$(date +%s)}" -t localhost/messenger-api:lab3 .
|
||||
$ENGINE build --build-arg "BUILD_ID=${BUILD_ID:-$(date +%s)}" -t localhost/messenger-api:lab4 .
|
||||
$COMPOSE run --rm migrate
|
||||
|
||||
for old in $(replicas); do
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
|
||||
PROJECT=${PROJECT:-messenger-lab3}
|
||||
PROJECT=${PROJECT:-messenger-lab4}
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
|
||||
for id in $($ENGINE ps -q --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api"); do
|
||||
|
||||
Reference in New Issue
Block a user