feat: moved project from lab2
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
"""Check the transferred lab2 API, optionally across make app-down/app-up."""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
import secrets
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
parser.add_argument("--restart", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
def call(method, path, expected=200, body=None, token=None, headers=None):
|
||||
headers = dict(headers or {})
|
||||
if token:
|
||||
headers["Authorization"] = "Bearer " + token
|
||||
data = None
|
||||
if body is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
data = json.dumps(body).encode("utf-8")
|
||||
request = Request(
|
||||
args.base_url + path, data=data, headers=headers, method=method
|
||||
)
|
||||
try:
|
||||
response = urlopen(request, timeout=10)
|
||||
except HTTPError as error:
|
||||
response = error
|
||||
with response:
|
||||
assert response.status == expected, (
|
||||
f"{method} {path}: expected {expected}, got {response.status}"
|
||||
)
|
||||
assert response.headers.get("X-Request-Id"), "Missing request ID"
|
||||
raw = response.read()
|
||||
return json.loads(raw) if raw else None
|
||||
|
||||
call("GET", "/health/ready")
|
||||
password = secrets.token_hex(16)
|
||||
users = []
|
||||
for name in ("alice", "bob"):
|
||||
username = name + "_" + secrets.token_hex(6)
|
||||
user = call(
|
||||
"POST",
|
||||
"/api/v1/users",
|
||||
201,
|
||||
{"username": username, "display_name": name, "password": password},
|
||||
)
|
||||
credentials = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
session = call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
201,
|
||||
headers={"Authorization": "Basic " + credentials},
|
||||
)
|
||||
users.append((user, session["token"]))
|
||||
alice, token = users[0]
|
||||
bob, bob_token = users[1]
|
||||
chat = call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
201,
|
||||
{"title": "Baseline lab3", "member_ids": [alice["id"], bob["id"]]},
|
||||
token,
|
||||
)
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
message = call(
|
||||
"POST",
|
||||
path,
|
||||
201,
|
||||
{"text": "Перенос из лабораторной 2"},
|
||||
token,
|
||||
{"Idempotency-Key": secrets.token_hex(16)},
|
||||
)
|
||||
assert call("GET", path, token=bob_token)["items"] == [message]
|
||||
call("GET", "/api/v1/users/me", 401)
|
||||
if args.restart:
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
subprocess.run(["make", "app-down"], cwd=root, check=True)
|
||||
subprocess.run(["make", "app-up"], cwd=root, check=True)
|
||||
assert call("GET", "/api/v1/users/me", token=token) == alice
|
||||
assert call("GET", path, token=bob_token)["items"] == [message]
|
||||
call("DELETE", "/api/v1/auth/sessions/current", 204, token=token)
|
||||
call("GET", "/api/v1/users/me", 401, token=token)
|
||||
print(
|
||||
"PASS: transferred API, shared sessions, chat, message, logout"
|
||||
+ (", persistence across app-down/app-up" if args.restart else "")
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,15 @@
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
|
||||
path = Path(__file__).resolve().parents[1] / ".env"
|
||||
if not path.exists():
|
||||
with path.open("x", encoding="utf-8") as file:
|
||||
file.write(
|
||||
"POSTGRES_DB=messenger\n"
|
||||
"POSTGRES_USER=messenger\n"
|
||||
f"POSTGRES_PASSWORD={secrets.token_hex(24)}\n"
|
||||
"SESSION_TTL_SECONDS=3600\n"
|
||||
)
|
||||
print("Created .env with a new local password.")
|
||||
else:
|
||||
print("Using existing .env.")
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --project-name highload-lab3 --env-file .env -f compose.yml restart db redis >/dev/null
|
||||
docker compose --project-name highload-lab3 --env-file .env -f compose.yml up -d --wait db redis >/dev/null
|
||||
docker compose --project-name highload-lab3 --env-file .env -f compose.yml restart api >/dev/null
|
||||
docker compose --project-name highload-lab3 --env-file .env -f compose.yml up -d --wait >/dev/null
|
||||
Reference in New Issue
Block a user