58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
import base64
|
|
import json
|
|
import sys
|
|
import urllib.request
|
|
import urllib.error
|
|
|
|
BASE = (sys.argv[sys.argv.index("--base-url") + 1] if "--base-url" in sys.argv else "http://localhost:8080").rstrip("/")
|
|
PASSWORD = "Example-only-not-a-real-secret-123"
|
|
|
|
def post(path, body, headers=None):
|
|
req = urllib.request.Request(BASE + path, method="POST",
|
|
data=json.dumps(body).encode(),
|
|
headers={"Content-Type": "application/json", **(headers or {})})
|
|
try:
|
|
with urllib.request.urlopen(req) as r:
|
|
return r.status, json.loads(r.read())
|
|
except urllib.error.HTTPError as e:
|
|
return e.code, json.loads(e.read())
|
|
|
|
def get(path, headers=None):
|
|
req = urllib.request.Request(BASE + path, headers=headers or {})
|
|
try:
|
|
with urllib.request.urlopen(req) as r:
|
|
return r.status, json.loads(r.read())
|
|
except urllib.error.HTTPError as e:
|
|
return e.code, json.loads(e.read())
|
|
|
|
def main():
|
|
failures = []
|
|
|
|
# 1. Неверный JSON → 400
|
|
status, _ = post("/api/v1/users", "not-json")
|
|
if status != 400:
|
|
failures.append(f"invalid JSON: expected 400, got {status}")
|
|
|
|
# 2. Невалидный username → 400
|
|
status, _ = post("/api/v1/users", {"username": "AB", "display_name": "Test", "password": PASSWORD})
|
|
if status != 400:
|
|
failures.append(f"invalid username: expected 400, got {status}")
|
|
|
|
# 3. Отсутствие Bearer на /users/me → 401
|
|
status, _ = get("/api/v1/users/me")
|
|
if status != 401:
|
|
failures.append(f"no auth: expected 401, got {status}")
|
|
|
|
# 4. X-User-Id не даёт доступ → 401
|
|
status, _ = get("/api/v1/users/me", {"X-User-Id": "00000000-0000-0000-0000-000000000000"})
|
|
if status != 401:
|
|
failures.append(f"X-User-Id bypass: expected 401, got {status}")
|
|
|
|
if failures:
|
|
for f in failures:
|
|
print(f"FAIL: {f}")
|
|
sys.exit(1)
|
|
print("PASS: negative checks")
|
|
|
|
if __name__ == "__main__":
|
|
main() |