Implement PostgreSQL storage, sessions and persistence checks
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import concurrent.futures
|
||||
import json
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
|
||||
|
||||
def register(body):
|
||||
request = urllib.request.Request(
|
||||
BASE + "/api/v1/users",
|
||||
data=json.dumps(body).encode("utf-8"),
|
||||
headers={"Content-Type": "application/json"},
|
||||
method="POST",
|
||||
)
|
||||
try:
|
||||
response = urllib.request.urlopen(request, timeout=15)
|
||||
except urllib.error.HTTPError as error:
|
||||
response = error
|
||||
with response:
|
||||
data = json.load(response)
|
||||
assert response.headers.get("X-Request-Id"), "missing request ID"
|
||||
assert "password" not in data and "password_hash" not in data, "secret in response"
|
||||
return response.status, data
|
||||
|
||||
|
||||
def main():
|
||||
username = "reg_" + uuid.uuid4().hex[:20]
|
||||
body = {"username": username, "display_name": "Проверка регистрации", "password": "учебный:пароль"}
|
||||
status, user = register(body)
|
||||
assert status == 201, f"registration status={status}"
|
||||
assert set(user) == {"id", "username", "display_name", "created_at"}
|
||||
assert user["username"] == username and user["display_name"] == body["display_name"]
|
||||
uuid.UUID(user["id"])
|
||||
assert user["created_at"].endswith("Z"), "created_at must be UTC"
|
||||
print("PASS: registration 201, public User fields only")
|
||||
assert register(body)[0] == 409, "duplicate username must return 409"
|
||||
print("PASS: duplicate username 409")
|
||||
invalid = dict(body, username="bad_" + uuid.uuid4().hex[:20], password="short")
|
||||
assert register(invalid)[0] == 400, "short password must return 400"
|
||||
del invalid["password"]
|
||||
assert register(invalid)[0] == 400, "missing password must return 400"
|
||||
print("PASS: short and missing passwords 400")
|
||||
concurrent_body = dict(body, username="race_" + uuid.uuid4().hex[:20])
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
|
||||
results = list(executor.map(register, [concurrent_body, concurrent_body]))
|
||||
assert sorted(status for status, _ in results) == [201, 409], "registration race failed"
|
||||
print("PASS: concurrent same username -> 201 and 409")
|
||||
print("Created username for SQL inspection:", username)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user