Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove file ending in diagram route #1090

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions backend/capellacollab/projects/toolmodels/diagrams/routes.py
MoritzWeber0 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import logging
import pathlib
from urllib import parse

import fastapi
Expand Down Expand Up @@ -67,14 +68,24 @@ async def get_diagram_metadata(
)


@router.get("/{diagram_uuid}", response_class=fastapi.responses.Response)
@router.get(
"/{diagram_uuid_or_filename}", response_class=fastapi.responses.Response
)
async def get_diagram(
diagram_uuid: str,
diagram_uuid_or_filename: str,
handler: git_handler.GitHandler = fastapi.Depends(
git_injectables.get_git_handler
),
logger: logging.LoggerAdapter = fastapi.Depends(log.get_request_logger),
):
fileextension = pathlib.PurePosixPath(diagram_uuid_or_filename).suffix
if fileextension and fileextension.lower() != ".svg":
raise fastapi.HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={"reason": f"File extension {fileextension} not supported"},
)

diagram_uuid = pathlib.PurePosixPath(diagram_uuid_or_filename).stem
try:
_, diagram = await handler.get_file_from_repository_or_artifacts(
f"diagram_cache/{parse.quote(diagram_uuid, safe='')}.svg",
Expand Down
115 changes: 115 additions & 0 deletions backend/tests/projects/toolmodels/test_diagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,121 @@ def test_get_single_diagram_from_artifacts(
assert response.content == EXAMPLE_SVG


@responses.activate
@pytest.mark.parametrize(
"git_type,git_response_status,git_query_params",
[
(
git_models.GitType.GITLAB,
404,
[
{
"path": "diagram_cache/index.json",
"ref_name": "diagram-cache/main",
},
{
"path": "diagram_cache/_c90e4Hdf2d2UosmJBo0GTw.svg",
"ref_name": "diagram-cache/main",
},
],
),
(
git_models.GitType.GITHUB,
404,
[
{
"path": "diagram_cache/index.json",
"sha": "diagram-cache/main",
},
{
"path": "diagram_cache/_c90e4Hdf2d2UosmJBo0GTw.svg",
"sha": "diagram-cache/main",
},
],
),
],
)
@pytest.mark.usefixtures(
"project_user",
"git_instance",
"git_model",
"mock_git_diagram_cache_from_repo_api",
"mock_git_rest_api_for_artifacts",
"mock_git_diagram_cache_index_api",
"mock_git_diagram_cache_svg",
"mock_git_get_commit_information_api",
)
@pytest.mark.usefixtures("project_user", "git_instance", "git_model")
def test_get_single_diagram_from_artifacts_with_file_ending(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
client: testclient.TestClient,
):
response = client.get(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/diagrams/_c90e4Hdf2d2UosmJBo0GTw.svg",
)

assert response.status_code == 200
assert response.content == EXAMPLE_SVG


@responses.activate
@pytest.mark.parametrize(
"git_type,git_response_status,git_query_params",
[
(
git_models.GitType.GITLAB,
404,
[
{
"path": "diagram_cache/index.json",
"ref_name": "diagram-cache/main",
},
{
"path": "diagram_cache/_c90e4Hdf2d2UosmJBo0GTw.svg",
"ref_name": "diagram-cache/main",
},
],
),
(
git_models.GitType.GITHUB,
404,
[
{
"path": "diagram_cache/index.json",
"sha": "diagram-cache/main",
},
{
"path": "diagram_cache/_c90e4Hdf2d2UosmJBo0GTw.svg",
"sha": "diagram-cache/main",
},
],
),
],
)
@pytest.mark.usefixtures(
"project_user",
"git_instance",
"git_model",
"mock_git_diagram_cache_from_repo_api",
"mock_git_rest_api_for_artifacts",
"mock_git_diagram_cache_index_api",
"mock_git_diagram_cache_svg",
"mock_git_get_commit_information_api",
)
@pytest.mark.usefixtures("project_user", "git_instance", "git_model")
def test_get_single_diagram_from_artifacts_with_wrong_file_ending(
project: project_models.DatabaseProject,
capella_model: toolmodels_models.CapellaModel,
client: testclient.TestClient,
):
response = client.get(
f"/api/v1/projects/{project.slug}/models/{capella_model.slug}/diagrams/_c90e4Hdf2d2UosmJBo0GTw.png",
)

assert response.status_code == 400


@responses.activate
@pytest.mark.parametrize(
"git_type,git_query_params",
Expand Down
19 changes: 19 additions & 0 deletions docs/user/docs/tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ authenticate with that against the Collaboration Manager API. One example is:
```zsh
curl -u [username]:[token] https://[baseURL]/api/v1/projects
```

Another example is working with the diagram cache of py-capellambse.
The implementation of the capella modelling tool `capellambse` uses Python and lets you read and
write models. For more information have a look at the
[documentation](https://dsd-dbs.github.io/py-capellambse/) or the
[Github repository](https://github.com/DSD-DBS/py-capellambse).

```python
model = capellambse.model.MelodyModel(
path="<path to the model on your machine>",
diagram_cache={
"path": "https://<your backend url>/api/v1/projects/<your project slug>/models/<your model slug>/diagrams/%s",
"username": "<username>",
"password": "<your PAT>",
}
)
```

Having created a model like that you can e.g. with `model.diagrams[0]` get the first diagram.
Loading