121 lines
2.7 KiB
Python
121 lines
2.7 KiB
Python
import base64
|
|
import concurrent.futures
|
|
import json
|
|
import secrets
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
BASE = "http://" + "127.0.0.1:8080"
|
|
PASSWORD = "StrongPassword123!"
|
|
|
|
|
|
def call(method, path, data=None, authorization=None):
|
|
headers = {}
|
|
if data is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
if authorization:
|
|
headers["Authorization"] = authorization
|
|
|
|
req = urllib.request.Request(
|
|
BASE + path,
|
|
data=json.dumps(data).encode() if data is not None else None,
|
|
headers=headers,
|
|
method=method,
|
|
)
|
|
|
|
try:
|
|
response = urllib.request.urlopen(req, timeout=15)
|
|
except urllib.error.HTTPError as exc:
|
|
response = exc
|
|
|
|
with response:
|
|
raw = response.read()
|
|
return response.status, json.loads(raw) if raw else None
|
|
|
|
|
|
def register(username):
|
|
return call("POST", "/api/v1/users", {
|
|
"username": username,
|
|
"display_name": "Concurrent User",
|
|
"password": PASSWORD,
|
|
})[0]
|
|
|
|
|
|
print("1. Concurrent registration")
|
|
|
|
username = "race_" + secrets.token_hex(5)
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
|
statuses = list(executor.map(register, [username] * 10))
|
|
|
|
assert statuses.count(201) == 1, statuses
|
|
assert statuses.count(409) == 9, statuses
|
|
print("PASS: one registration, nine conflicts")
|
|
|
|
|
|
print("2. Incorrect password")
|
|
|
|
credentials = base64.b64encode(
|
|
(username + ":WrongPassword123!").encode()
|
|
).decode()
|
|
|
|
status, _ = call(
|
|
"POST",
|
|
"/api/v1/auth/sessions",
|
|
authorization="Basic " + credentials,
|
|
)
|
|
|
|
assert status == 401, status
|
|
print("PASS: incorrect password rejected")
|
|
|
|
|
|
print("3. Invalid Bearer token")
|
|
|
|
status, _ = call(
|
|
"GET",
|
|
"/api/v1/users/me",
|
|
authorization="Bearer invalid-token",
|
|
)
|
|
|
|
assert status == 401, status
|
|
print("PASS: invalid token rejected")
|
|
for bad_token in ("é", "abc def", "", "x" * 513, "abc\tdef"):
|
|
status, _ = call("GET", "/api/v1/users/me", authorization="Bearer " + bad_token)
|
|
assert status == 401, (repr(bad_token), status)
|
|
print("PASS: malformed and non-ASCII Bearer tokens rejected")
|
|
|
|
|
|
print("4. Unknown chat member")
|
|
|
|
credentials = base64.b64encode(
|
|
(username + ":" + PASSWORD).encode()
|
|
).decode()
|
|
|
|
status, result = call(
|
|
"POST",
|
|
"/api/v1/auth/sessions",
|
|
authorization="Basic " + credentials,
|
|
)
|
|
|
|
assert status == 201, status
|
|
token = result["token"]
|
|
user_id = result["user"]["id"]
|
|
|
|
import uuid
|
|
|
|
status, _ = call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
{
|
|
"title": "Rollback test",
|
|
"member_ids": [user_id, str(uuid.uuid4())],
|
|
},
|
|
authorization="Bearer " + token,
|
|
)
|
|
|
|
assert status == 400, status
|
|
print("PASS: unknown member rejected")
|
|
|
|
|
|
print("\nALL ADVANCED API CHECKS PASSED")
|