Skip to content

Commit

Permalink
Merge pull request #1438 from DSD-DBS/fix-test-deprecation-warning
Browse files Browse the repository at this point in the history
fix: Update user test to eliminate deprecation warning in request
  • Loading branch information
MoritzWeber0 committed Mar 21, 2024
2 parents f78bbc7 + 24ab500 commit f2b2261
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend/capellacollab/sessions/hooks/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def session_connection_hook( # type: ignore[override]
return interface.SessionConnectionHookResult(
warnings=[
core_models.Message(
err_code="REDIRECT_URL_DERIVATION_FAILED",
title="Couldn't derive the redirect URL.",
reason="Please check the backend logs for more information.",
)
Expand Down
1 change: 1 addition & 0 deletions backend/capellacollab/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Role(enum.Enum):
class BaseUser(pydantic.BaseModel):
model_config = pydantic.ConfigDict(from_attributes=True)

id: int
name: str
role: Role

Expand Down
50 changes: 50 additions & 0 deletions backend/tests/sessions/hooks/test_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import logging

from capellacollab.sessions import models as sessions_models
from capellacollab.sessions.hooks import http
from capellacollab.sessions.hooks import interface as sessions_hooks_interface
from capellacollab.tools import models as tools_models


def test_http_hook(session: sessions_models.DatabaseSession):
session.environment = {"TEST": "test"}
connection_method = tools_models.HTTPConnectionMethod(
redirect_url="http://localhost:8000/{TEST}",
cookies={"test": "{TEST}"},
)
result = http.HTTPIntegration().session_connection_hook(
db_session=session,
connection_method=connection_method,
logger=logging.getLogger(),
)

assert result["cookies"] == {"test": "test"}
assert result["redirect_url"] == "http://localhost:8000/test"
assert not result["warnings"]


def test_skip_http_hook_if_guacamole(session: sessions_models.DatabaseSession):
result = http.HTTPIntegration().session_connection_hook(
db_session=session,
connection_method=tools_models.GuacamoleConnectionMethod(),
logger=logging.getLogger(),
)
assert result == sessions_hooks_interface.SessionConnectionHookResult()


def test_fail_derive_redirect_url(session: sessions_models.DatabaseSession):
session.environment = {"TEST": "test"}
connection_method = tools_models.HTTPConnectionMethod(
redirect_url="http://localhost:8000/{TEST2}"
)
result = http.HTTPIntegration().session_connection_hook(
db_session=session,
connection_method=connection_method,
logger=logging.getLogger(),
)

assert len(result["warnings"]) == 1
assert result["warnings"][0].err_code == "REDIRECT_URL_DERIVATION_FAILED"
61 changes: 61 additions & 0 deletions backend/tests/sessions/test_session_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from capellacollab.sessions import operators
from capellacollab.sessions.operators import k8s
from capellacollab.tools import models as tools_models
from capellacollab.users import crud as users_crud
from capellacollab.users import models as users_models


Expand Down Expand Up @@ -221,3 +222,63 @@ def test_validate_session_token_with_valid_token(
)

assert response.is_success


def test_get_all_sessions(
db: orm.Session,
client: testclient.TestClient,
admin: users_models.DatabaseUser,
tool: tools_models.DatabaseTool,
tool_version: tools_models.DatabaseVersion,
):
session = sessions_models.DatabaseSession(
str(uuid.uuid1()),
created_at=datetime.datetime.now(),
type=sessions_models.SessionType.READONLY,
environment={},
owner=admin,
tool=tool,
version=tool_version,
connection_method_id=tool.config.connection.methods[0].id,
)
sessions_crud.create_session(db, session)

response = client.get("/api/v1/sessions")
assert response.is_success
assert len(response.json()) == 1


def test_own_sessions(
db: orm.Session,
client: testclient.TestClient,
user: users_models.DatabaseUser,
tool: tools_models.DatabaseTool,
session: sessions_models.DatabaseSession,
tool_version: tools_models.DatabaseVersion,
):
another_user = users_crud.create_user(
db, "other-user", users_models.Role.USER
)

session_of_other_user = sessions_models.DatabaseSession(
str(uuid.uuid1()),
created_at=datetime.datetime.now(),
type=sessions_models.SessionType.PERSISTENT,
environment={},
owner=another_user,
tool=tool,
version=tool_version,
connection_method_id=tool.config.connection.methods[0].id,
)
sessions_crud.create_session(db, session_of_other_user)

response = client.get(f"/api/v1/users/{user.id}/sessions")

assert response.is_success
assert len(response.json()) == 1
assert response.json()[0]["owner"]["id"] == user.id
assert response.json()[0]["id"] == session.id

# Check that environment and config are not exposed
assert "environment" not in response.json()[0]
assert "config" not in response.json()[0]
2 changes: 1 addition & 1 deletion backend/tests/test_event_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_create_user_removed_from_project_event(
response = client.request(
"DELETE",
f"/api/v1/projects/{project.slug}/users/{user.id}",
data=reason,
content=reason,
headers={"Content-Type": "text/plain"},
)

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/user/tools/capella/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
remotely. In addition, the session owner can also see the events in the UI.
In your session, please follow these steps:

1. Open the Capella search
1. Search for `Event logs` and click on the following entry:
1. Open the Capella search via the search icon.
1. Search for `Event Logs (General)` and open the view.
1. You should now see the list of events and you can try to find an entry with
a matching timestamp.

Expand Down

0 comments on commit f2b2261

Please sign in to comment.