Skip to content

Commit 0b37824

Browse files
authored
test(carbonserver): unit tests were escaped (#1068)
1 parent ad5efea commit 0b37824

6 files changed

Lines changed: 23 additions & 24 deletions

File tree

carbonserver/carbonserver/api/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ class AccessLevel(Enum):
332332
# Used in the responses to the user
333333
class ProjectToken(BaseModel):
334334
id: UUID
335-
project_id: UUID
335+
project_id: UUID | str
336336
name: Optional[str]
337337
token: Optional[str] = None
338338
last_used: Optional[datetime] = None

carbonserver/carbonserver/api/services/auth_context.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515

1616
class AuthContext:
17-
1817
def __init__(
1918
self,
2019
user_repository: UserRepository,
@@ -30,7 +29,7 @@ def isOperationAuthorizedOnOrg(self, organization_id, user: User):
3029
organization_id=organization_id, user=user
3130
)
3231

33-
def isOperationAuthorizedOnProject(self, project_id: UUID, user: User):
32+
def isOperationAuthorizedOnProject(self, project_id: UUID | str, user: User):
3433
return self._user_repository.is_user_authorized_on_project(project_id, user.id)
3534

3635
def can_read_project(self, project_id: UUID, user: Optional[User]):

carbonserver/tests/api/routers/test_emissions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"ram_energy": 2.0,
5656
"energy_consumed": 57.21874,
5757
"wue": 0,
58+
"cpu_utilization_percent": None,
59+
"gpu_utilization_percent": None,
60+
"ram_utilization_percent": None,
5861
}
5962

6063
EMISSION_2 = {

carbonserver/tests/api/routers/test_project_tokens.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import re
12
from unittest import mock
23

34
import pytest
5+
from api.mocks import FakeAuthContext, FakeUserWithAuthDependency
6+
from dependency_injector import providers
47
from fastapi import FastAPI, status
58
from fastapi.testclient import TestClient
69

@@ -9,6 +12,7 @@
912
)
1013
from carbonserver.api.routers import project_api_tokens
1114
from carbonserver.api.schemas import ProjectToken
15+
from carbonserver.api.services.auth_service import MandatoryUserWithAuthDependency
1216
from carbonserver.container import ServerContainer
1317

1418
PROJECT_ID = "f52fe339-164d-4c2b-a8c0-f562dfce066d"
@@ -25,9 +29,10 @@
2529
"id": PROJECT_TOKEN_ID,
2630
"project_id": PROJECT_ID,
2731
"name": "Token API Code Carbon",
28-
"token": "token",
32+
"token": "cpt_some_token",
2933
"access": 2,
3034
"last_used": None,
35+
"revoked": False,
3136
}
3237

3338

@@ -38,6 +43,10 @@ def custom_test_server():
3843
app = FastAPI()
3944
app.container = container
4045
app.include_router(project_api_tokens.router)
46+
app.dependency_overrides[MandatoryUserWithAuthDependency] = (
47+
FakeUserWithAuthDependency
48+
)
49+
app.container.auth_context.override(providers.Factory(FakeAuthContext))
4150
yield app
4251

4352

@@ -60,7 +69,13 @@ def test_add_project_token(client, custom_test_server):
6069
actual_project_token = response.json()
6170

6271
assert response.status_code == status.HTTP_201_CREATED
63-
assert actual_project_token == expected_project_token
72+
# Check all fields except 'token'
73+
for key in expected_project_token:
74+
if key != "token":
75+
assert actual_project_token[key] == expected_project_token[key]
76+
77+
# Check token format: cpt_ + 32 alphanumeric chars
78+
assert re.fullmatch(r"cpt_[a-zA-Z0-9_\-]{43,44}", actual_project_token["token"])
6479

6580

6681
def test_delete_project_token(client, custom_test_server):

carbonserver/tests/api/routers/test_users.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,6 @@ def client(custom_test_server):
6969
yield TestClient(custom_test_server)
7070

7171

72-
def test_list_users_list_all_existing_users_with_200(client, custom_test_server):
73-
repository_mock = mock.Mock(spec=UsersRepository)
74-
expected_user = USER_1
75-
expected_user_2 = USER_2
76-
expected_user_list = [expected_user, expected_user_2]
77-
repository_mock.list_users.return_value = [
78-
User(**expected_user),
79-
User(**expected_user_2),
80-
]
81-
82-
with custom_test_server.container.user_repository.override(repository_mock):
83-
response = client.get("/users")
84-
actual_user_list = response.json()
85-
86-
assert response.status_code == status.HTTP_200_OK
87-
assert actual_user_list == expected_user_list
88-
89-
9072
def test_get_user_by_id_returns_correct_user_with_correct_id(
9173
client, custom_test_server
9274
):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pre-commit-install = { cmd = "pre-commit install", help = "Install pre-commit ho
174174
dashboard = "cd carbonserver && uvicorn main:app --reload"
175175
dashboard-ci = "cd carbonserver && uvicorn main:app --host 0.0.0.0 --port 8008"
176176
docker = "docker-compose up -d"
177-
test-api-unit = "cd carbonserver && python -m pytest -vv tests/api/routers/test_projects.py && python -m pytest -vv tests/api/service/ && python -m pytest -vv tests/api/usecase/"
177+
test-api-unit = "cd carbonserver && python -m pytest -vv tests/api/routers && python -m pytest -vv tests/api/service/ && python -m pytest -vv tests/api/usecase/"
178178
test-api-integ = "cd carbonserver && python -m pytest -vv tests/api/integration/"
179179
setup-db = "cd carbonserver && python3 -m alembic -c carbonserver/database/alembic.ini upgrade head"
180180
downgrade-db = "cd carbonserver && python -m alembic -c carbonserver/database/alembic.ini downgrade base"

0 commit comments

Comments
 (0)