Skip to content

Commit

Permalink
refactor: Remove usage of deprecated get_username injectable
Browse files Browse the repository at this point in the history
  • Loading branch information
MoritzWeber0 committed Dec 13, 2024
1 parent ebe2c33 commit 0ee0e03
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 40 deletions.
19 changes: 10 additions & 9 deletions backend/capellacollab/projects/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ def get_projects(
users_injectables.get_own_user
),
db: orm.Session = fastapi.Depends(database.get_db),
username: str = fastapi.Depends(auth_injectables.get_username),
log: logging.LoggerAdapter = fastapi.Depends(
core_logging.get_request_logger
),
) -> list[models.DatabaseProject]:
if auth_injectables.RoleVerification(
required_role=users_models.Role.ADMIN, verify=False
)(username, db):
if user.role == users_models.Role.ADMIN:
log.debug("Fetching all projects")
return list(crud.get_projects(db))

Expand All @@ -70,7 +67,7 @@ def get_projects(
for association in user.projects
if auth_injectables.ProjectRoleVerification(
minimum_role, verify=False
)(association.project.slug, username, db)
)(association.project.slug, user.name, db)
]

return projects
Expand All @@ -93,7 +90,9 @@ def update_project(
project: models.DatabaseProject = fastapi.Depends(
projects_injectables.get_existing_project
),
username: str = fastapi.Depends(auth_injectables.get_username),
user: users_models.DatabaseUser = fastapi.Depends(
users_injectables.get_own_user
),
db: orm.Session = fastapi.Depends(database.get_db),
) -> models.DatabaseProject:
if patch_project.name:
Expand All @@ -102,7 +101,7 @@ def update_project(
if project.slug != new_slug and crud.get_project_by_slug(db, new_slug):
raise exceptions.ProjectAlreadyExistsError(project.slug)
if patch_project.is_archived:
_delete_all_pipelines_for_project(db, project, username)
_delete_all_pipelines_for_project(db, project, user)
return crud.update_project(db, project, patch_project)


Expand Down Expand Up @@ -185,13 +184,15 @@ def delete_project(


def _delete_all_pipelines_for_project(
db: orm.Session, project: models.DatabaseProject, username: str
db: orm.Session,
project: models.DatabaseProject,
user: users_models.DatabaseUser,
):
pipelines: list[backups_models.DatabaseBackup] = []
for model in project.models:
pipelines.extend(backups_crud.get_pipelines_for_tool_model(db, model))
for pipeline in pipelines:
backups_core.delete_pipeline(db, pipeline, username, True)
backups_core.delete_pipeline(db, pipeline, user, True)


router.include_router(
Expand Down
10 changes: 2 additions & 8 deletions backend/capellacollab/projects/toolmodels/backups/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import requests
from sqlalchemy import orm

from capellacollab.core.authentication import injectables as auth_injectables
from capellacollab.projects.toolmodels import models as toolmodels_models
from capellacollab.projects.toolmodels.modelsources.git import (
models as git_models,
Expand Down Expand Up @@ -64,7 +63,7 @@ def get_environment(
def delete_pipeline(
db: orm.Session,
pipeline: models.DatabaseBackup,
username: str,
user: users_models.DatabaseUser,
force: bool,
):
try:
Expand All @@ -80,12 +79,7 @@ def delete_pipeline(
exc_info=True,
)

if not (
force
and auth_injectables.RoleVerification(
required_role=users_models.Role.ADMIN, verify=False
)(username=username, db=db)
):
if not (force and user.role == users_models.Role.ADMIN):
raise exceptions.PipelineOperationFailedT4CServerUnreachable(
exceptions.PipelineOperation.DELETE
)
Expand Down
9 changes: 6 additions & 3 deletions backend/capellacollab/projects/toolmodels/backups/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
interface as t4c_repository_interface,
)
from capellacollab.tools import crud as tools_crud
from capellacollab.users import injectables as users_injectables
from capellacollab.users import models as users_models

from .. import exceptions as toolmodels_exceptions
from . import core, crud, exceptions, injectables, models
Expand Down Expand Up @@ -74,7 +76,6 @@ def create_backup(
toolmodels_injectables.get_existing_capella_model
),
db: orm.Session = fastapi.Depends(database.get_db),
username: str = fastapi.Depends(auth_injectables.get_username),
):
git_model = git_injectables.get_existing_git_model(
body.git_model_id, toolmodel, db
Expand Down Expand Up @@ -148,10 +149,12 @@ def delete_pipeline(
injectables.get_existing_pipeline
),
db: orm.Session = fastapi.Depends(database.get_db),
username: str = fastapi.Depends(auth_injectables.get_username),
user: users_models.DatabaseUser = fastapi.Depends(
users_injectables.get_own_user
),
force: bool = False,
):
core.delete_pipeline(db, pipeline, username, force)
core.delete_pipeline(db, pipeline, user, force)


router.include_router(
Expand Down
5 changes: 1 addition & 4 deletions backend/capellacollab/projects/users/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,8 @@ def get_current_project_user(
projects_injectables.get_existing_project
),
db: orm.Session = fastapi.Depends(database.get_db),
username: str = fastapi.Depends(auth_injectables.get_username),
) -> models.ProjectUserAssociation | models.ProjectUser:
if auth_injectables.RoleVerification(
required_role=users_models.Role.ADMIN, verify=False
)(username, db):
if user.role == users_models.Role.ADMIN:
return models.ProjectUser(
role=models.ProjectUserRole.ADMIN,
permission=models.ProjectUserPermission.WRITE,
Expand Down
14 changes: 14 additions & 0 deletions backend/tests/projects/test_projects_users_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,17 @@ def test_no_user_rights_on_internal_permissions(
assert response.status_code == 404
assert "detail" in response.json()
assert "reason" in response.json()["detail"]


@pytest.mark.usefixtures("admin")
def test_get_current_project_user_as_admin(
client: testclient.TestClient,
project: projects_models.DatabaseProject,
):
response = client.get(
f"/api/v1/projects/{project.slug}/users/current",
)

assert response.status_code == 200
assert response.json()["role"] == "administrator"
assert response.json()["permission"] == "write"
6 changes: 4 additions & 2 deletions backend/tests/projects/toolmodels/pipelines/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import uuid

import pytest
from sqlalchemy import orm

Expand Down Expand Up @@ -47,8 +49,8 @@ def fixture_pipeline(
t4c_model=t4c_model,
created_by=executor_name,
model=capella_model,
t4c_username="no",
t4c_password="no",
t4c_username="techuser-" + str(uuid.uuid4()),
t4c_password="password",
include_commit_history=include_commit_history,
run_nightly=run_nightly,
)
Expand Down
73 changes: 59 additions & 14 deletions backend/tests/projects/toolmodels/pipelines/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest
import requests.exceptions
import responses
from fastapi import testclient
from sqlalchemy import orm

Expand Down Expand Up @@ -166,30 +167,20 @@ def mock_add_user_to_repository(

@pytest.mark.usefixtures(
"project_manager",
"mockoperator",
)
def test_delete_pipeline(
db: orm.Session,
project: project_models.DatabaseProject,
capella_model: toolmodels_models.ToolModel,
pipeline: pipelines_models.DatabaseBackup,
client: testclient.TestClient,
monkeypatch: pytest.MonkeyPatch,
mockoperator: MockOperator,
run_nightly: bool,
t4c_instance: t4c_models.DatabaseT4CInstance,
):
def mock_remove_user_from_repository(
# pylint: disable=unused-argument
instance: t4c_models.DatabaseT4CInstance,
repository_name: str,
username: str,
):
return

monkeypatch.setattr(
t4c_repositories_interface,
"remove_user_from_repository",
mock_remove_user_from_repository,
responses.delete(
f"{t4c_instance.rest_api}/users/{pipeline.t4c_username}?repositoryName={pipeline.t4c_model.repository.name}",
status=200,
)

response = client.delete(
Expand All @@ -202,3 +193,57 @@ def mock_remove_user_from_repository(

if run_nightly:
assert mockoperator.cronjob_counter == -1


@responses.activate
@pytest.mark.usefixtures(
"project_manager",
"mockoperator",
)
def test_delete_pipeline_server_unreachable(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.ToolModel,
pipeline: pipelines_models.DatabaseBackup,
client: testclient.TestClient,
t4c_instance: t4c_models.DatabaseT4CInstance,
):
responses.delete(
f"{t4c_instance.rest_api}/users/{pipeline.t4c_username}?repositoryName={pipeline.t4c_model.repository.name}",
status=500,
)

response = client.delete(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/backups/pipelines/{pipeline.id}",
)

assert response.status_code == 422
assert (
response.json()["detail"]["err_code"]
== "PIPELINE_OPERATION_FAILED_T4C_SERVER_UNREACHABLE"
)


@responses.activate
@pytest.mark.usefixtures(
"admin",
"mockoperator",
)
def test_delete_pipeline_server_unreachable_force(
db: orm.Session,
project: project_models.DatabaseProject,
capella_model: toolmodels_models.ToolModel,
pipeline: pipelines_models.DatabaseBackup,
client: testclient.TestClient,
t4c_instance: t4c_models.DatabaseT4CInstance,
):
responses.delete(
f"{t4c_instance.rest_api}/users/{pipeline.t4c_username}?repositoryName={pipeline.t4c_model.repository.name}",
status=500,
)

response = client.delete(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/backups/pipelines/{pipeline.id}?force=True",
)

assert response.status_code == 204
assert not pipelines_crud.get_pipeline_by_id(db, pipeline.id)

0 comments on commit 0ee0e03

Please sign in to comment.