Skip to content

Commit

Permalink
use status
Browse files Browse the repository at this point in the history
  • Loading branch information
jschlyter committed Dec 2, 2024
1 parent 135f0dd commit 764395a
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def _test_enroll(data_key: JWK, x509_key: PrivateKey, requested_name: str | None
response = admin_client.post(
urljoin(server, "/api/v1/node"), params={"name": requested_name} if requested_name else None
)
if response.status_code != 201:
if response.status_code != status.HTTP_201_CREATED:
raise FailedToCreateNode
assert response.status_code == 201
assert response.status_code == status.HTTP_201_CREATED
create_response = response.json()
name = create_response["name"]
secret = create_response["secret"]
Expand All @@ -76,7 +76,7 @@ def _test_enroll(data_key: JWK, x509_key: PrivateKey, requested_name: str | None
# Get node information

response = admin_client.get(node_url)
assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK
node_information = response.json()
assert node_information["name"] == name
assert node_information["activated"] is None
Expand All @@ -101,7 +101,7 @@ def _test_enroll(data_key: JWK, x509_key: PrivateKey, requested_name: str | None
node_enroll_url = f"{node_url}/enroll"

response = client.post(node_enroll_url, json=enrollment_request)
assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK

enrollment_response = response.json()
print(json.dumps(enrollment_response, indent=4))
Expand All @@ -112,13 +112,13 @@ def _test_enroll(data_key: JWK, x509_key: PrivateKey, requested_name: str | None
# Enroll created node again (should fail)

response = client.post(node_enroll_url, json=enrollment_request)
assert response.status_code == 400
assert response.status_code == status.HTTP_400_BAD_REQUEST

######################
# Get node information

response = admin_client.get(node_url)
assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK
node_information = response.json()
print(json.dumps(node_information, indent=4))
assert node_information["name"] == name
Expand All @@ -130,11 +130,11 @@ def _test_enroll(data_key: JWK, x509_key: PrivateKey, requested_name: str | None
public_key_url = f"{node_url}/public_key"

response = client.get(public_key_url, headers={"Accept": "application/json"})
assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK
_ = JWK.from_json(response.text)

response = client.get(public_key_url, headers={"Accept": "application/x-pem-file"})
assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK
_ = load_pem_public_key(response.text.encode())

#########################
Expand All @@ -161,7 +161,7 @@ def _test_enroll(data_key: JWK, x509_key: PrivateKey, requested_name: str | None
renew_request = jws.serialize()

response = client.post(f"{node_url}/renew", json=renew_request)
assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK

renew_response = response.json()
print(json.dumps(renew_response, indent=4))
Expand All @@ -173,10 +173,10 @@ def _test_enroll(data_key: JWK, x509_key: PrivateKey, requested_name: str | None
# Clean up

response = admin_client.delete(node_url)
assert response.status_code == 204
assert response.status_code == status.HTTP_204_NO_CONTENT

response = admin_client.delete(node_url)
assert response.status_code == 404
assert response.status_code == status.HTTP_404_NOT_FOUND


def test_enroll_p256() -> None:
Expand Down Expand Up @@ -240,7 +240,7 @@ def test_enroll_bad_hmac_signature() -> None:
logging.basicConfig(level=logging.DEBUG)

response = client.post(urljoin(server, "/api/v1/node"), auth=BACKEND_CREDENTIALS)
assert response.status_code == 201
assert response.status_code == status.HTTP_201_CREATED
create_response = response.json()
name = create_response["name"]

Expand All @@ -262,10 +262,10 @@ def test_enroll_bad_hmac_signature() -> None:

url = urljoin(server, f"/api/v1/node/{name}/enroll")
response = client.post(url, json=enrollment_request)
assert response.status_code == 401
assert response.status_code == status.HTTP_401_UNAUTHORIZED

response = client.delete(urljoin(server, f"/api/v1/node/{name}"), auth=BACKEND_CREDENTIALS)
assert response.status_code == 204
assert response.status_code == status.HTTP_204_NO_CONTENT


def test_enroll_bad_data_signature() -> None:
Expand All @@ -282,7 +282,7 @@ def test_enroll_bad_data_signature() -> None:
logging.basicConfig(level=logging.DEBUG)

response = admin_client.post(urljoin(server, "/api/v1/node"))
assert response.status_code == 201
assert response.status_code == status.HTTP_201_CREATED
create_response = response.json()
name = create_response["name"]
secret = create_response["secret"]
Expand All @@ -306,10 +306,10 @@ def test_enroll_bad_data_signature() -> None:

url = urljoin(server, f"/api/v1/node/{name}/enroll")
response = client.post(url, json=enrollment_request)
assert response.status_code == 401
assert response.status_code == status.HTTP_401_UNAUTHORIZED

response = admin_client.delete(urljoin(server, f"/api/v1/node/{name}"))
assert response.status_code == 204
assert response.status_code == status.HTTP_204_NO_CONTENT


def test_admin() -> None:
Expand All @@ -320,10 +320,10 @@ def test_admin() -> None:

for _ in range(ADMIN_TEST_NODE_COUNT):
response = client.post(urljoin(server, "/api/v1/node"))
assert response.status_code == 201
assert response.status_code == status.HTTP_201_CREATED

response = client.get(urljoin(server, "/api/v1/nodes"))
assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK

node_collection = response.json()
assert len(node_collection["nodes"]) >= ADMIN_TEST_NODE_COUNT
Expand All @@ -335,7 +335,7 @@ def test_admin() -> None:
for node in node_collection["nodes"]:
name = node["name"]
response = client.delete(f"{server}/api/v1/node/{name}")
assert response.status_code == 204
assert response.status_code == status.HTTP_204_NO_CONTENT


def test_backend_authentication() -> None:
Expand All @@ -344,19 +344,19 @@ def test_backend_authentication() -> None:

# correct password
response = client.get(urljoin(server, "/api/v1/nodes"), auth=BACKEND_CREDENTIALS)
assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK

# no password
response = client.get(urljoin(server, "/api/v1/nodes"))
assert response.status_code == 401
assert response.status_code == status.HTTP_401_UNAUTHORIZED

# invalid user
response = client.get(urljoin(server, "/api/v1/nodes"), auth=("invalid", ""))
assert response.status_code == 401
assert response.status_code == status.HTTP_401_UNAUTHORIZED

# wrong password for existing user
response = client.get(urljoin(server, "/api/v1/nodes"), auth=(BACKEND_CREDENTIALS[0], "wrong"))
assert response.status_code == 401
assert response.status_code == status.HTTP_401_UNAUTHORIZED


def test_not_found() -> None:
Expand All @@ -366,13 +366,13 @@ def test_not_found() -> None:
name = str(uuid.uuid4())

response = client.get(urljoin(server, f"/api/v1/node/{name}"))
assert response.status_code == 404
assert response.status_code == status.HTTP_404_NOT_FOUND

response = client.post(urljoin(server, f"/api/v1/node/{name}/enroll"))
assert response.status_code == 404
assert response.status_code == status.HTTP_404_NOT_FOUND

response = client.get(urljoin(server, f"/api/v1/node/{name}/public_key"), headers={"Accept": MIME_TYPE_JWK})
assert response.status_code == 404
assert response.status_code == status.HTTP_404_NOT_FOUND

response = client.get(urljoin(server, f"/api/v1/node/{name}/public_key"), headers={"Accept": MIME_TYPE_PEM})
assert response.status_code == 404
assert response.status_code == status.HTTP_404_NOT_FOUND

0 comments on commit 764395a

Please sign in to comment.