tests: added more backend tests

This commit is contained in:
2025-02-10 20:55:50 +01:00
parent 630ad1f8d9
commit 85e8a84eaa
6 changed files with 129 additions and 12 deletions

View File

@ -0,0 +1,56 @@
import pytest
@pytest.fixture
def logged_in_client(backend):
client, _ = backend
response = client.post(
"/api/v1/user/login", json={"username": "admin@bec_atlas.ch", "password": "admin"}
)
assert response.status_code == 200
token = response.json()
assert isinstance(token, str)
assert len(token) > 20
client.headers.update({"Authorization": f"Bearer {token}"})
return client
@pytest.mark.timeout(60)
def test_get_deployment_credentials(logged_in_client):
"""
Test that the login endpoint returns a token.
"""
client = logged_in_client
deployments = client.get(
"/api/v1/deployments/realm", params={"realm": "demo_beamline_1"}
).json()
deployment_id = deployments[0]["_id"]
response = client.get("/api/v1/deploymentCredentials", params={"deployment_id": deployment_id})
assert response.status_code == 200
@pytest.mark.timeout(60)
def test_refresh_deployment_credentials(logged_in_client):
"""
Test that the login endpoint returns a token.
"""
client = logged_in_client
deployments = client.get(
"/api/v1/deployments/realm", params={"realm": "demo_beamline_1"}
).json()
deployment_id = deployments[0]["_id"]
old_token = client.get(
"/api/v1/deploymentCredentials", params={"deployment_id": deployment_id}
).json()["credential"]
response = client.post(
"/api/v1/deploymentCredentials/refresh", params={"deployment_id": deployment_id}
)
assert response.status_code == 200
out = response.json()
assert out == {"_id": deployment_id, "credential": out["credential"]}
assert out["credential"] != old_token

View File

@ -0,0 +1,59 @@
import pytest
@pytest.fixture
def logged_in_client(backend):
client, _ = backend
response = client.post(
"/api/v1/user/login", json={"username": "admin@bec_atlas.ch", "password": "admin"}
)
assert response.status_code == 200
token = response.json()
assert isinstance(token, str)
assert len(token) > 20
client.headers.update({"Authorization": f"Bearer {token}"})
return client
@pytest.mark.timeout(60)
@pytest.mark.parametrize("realm, num_deployments", [("test", 0), ("demo_beamline_1", 1)])
def test_get_deployment_by_realm(logged_in_client, realm, num_deployments):
"""
Test that the login endpoint returns a token.
"""
client = logged_in_client
response = client.get("/api/v1/deployments/realm", params={"realm": realm})
assert response.status_code == 200
deployments = response.json()
assert len(deployments) == num_deployments
@pytest.mark.timeout(60)
def test_get_deployment_by_id(logged_in_client):
"""
Test that the login endpoint returns a token.
"""
client = logged_in_client
deployments = client.get(
"/api/v1/deployments/realm", params={"realm": "demo_beamline_1"}
).json()
deployment_id = deployments[0]["_id"]
response = client.get("/api/v1/deployments/id", params={"deployment_id": deployment_id})
assert response.status_code == 200
deployment = response.json()
assert deployment["_id"] == deployment_id
assert deployment["realm_id"] == "demo_beamline_1"
@pytest.mark.timeout(60)
def test_get_deployment_by_id_wrong_id(logged_in_client):
"""
Test that the login endpoint returns a token.
"""
client = logged_in_client
response = client.get("/api/v1/deployments/id", params={"deployment_id": "wrong_id"})
assert response.status_code == 400
assert response.json() == {"detail": "Invalid deployment id"}

View File

@ -7,7 +7,7 @@ def backend_client(backend):
return client
@pytest.mark.timeout(60)
@pytest.mark.timeout(10)
def test_login(backend_client):
"""
Test that the login endpoint returns a token.
@ -21,7 +21,7 @@ def test_login(backend_client):
assert len(token) > 20
@pytest.mark.timeout(60)
@pytest.mark.timeout(20)
def test_login_wrong_password(backend_client):
"""
Test that the login returns a 401 when the password is wrong.
@ -33,7 +33,7 @@ def test_login_wrong_password(backend_client):
assert response.json() == {"detail": "User not found or password is incorrect"}
@pytest.mark.timeout(60)
@pytest.mark.timeout(10)
def test_login_unknown_user(backend_client):
"""
Test that the login returns a 401 when the user is unknown.