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

feat(applet-duplication): add optional report server config flag (M2-7830) #1623

Merged
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
4 changes: 3 additions & 1 deletion src/apps/applets/api/applets.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ async def applet_duplicate(
await CheckAccessService(session, user.id).check_applet_duplicate_access(applet_id)
applet_for_duplicate = await service.get_by_id_for_duplicate(applet_id)

applet = await service.duplicate(applet_for_duplicate, schema.display_name, schema.encryption)
applet = await service.duplicate(
applet_for_duplicate, schema.display_name, schema.encryption, schema.include_report_server
)
return Response(result=public_detail.Applet.from_orm(applet))


Expand Down
1 change: 1 addition & 0 deletions src/apps/applets/domain/applet_create_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ class AppletReportConfiguration(AppletReportConfigurationBase, InternalModel):
class AppletDuplicateRequest(InternalModel):
display_name: str
encryption: Encryption
include_report_server: bool = False
2 changes: 1 addition & 1 deletion src/apps/applets/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@

router.post(
"/{applet_id}/duplicate",
description="""This endpoint using for duplicate existing applet""",
description="""Duplicate an existing applet, and optionally its report server configuration""",
response_model_by_alias=True,
response_model=Response[public_detail.Applet],
status_code=status.HTTP_201_CREATED,
Expand Down
22 changes: 19 additions & 3 deletions src/apps/applets/service/applet.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from apps.applets.domain.applet_duplicate import AppletDuplicate
from apps.applets.domain.applet_full import AppletFull
from apps.applets.domain.applet_link import AppletLink, CreateAccessLink
from apps.applets.domain.base import Encryption
from apps.applets.domain.base import AppletReportConfigurationBase, Encryption
from apps.applets.errors import (
AccessLinkDoesNotExistError,
AppletAlreadyExist,
Expand Down Expand Up @@ -219,6 +219,7 @@ async def duplicate(
applet_exist: AppletDuplicate,
new_name: str,
encryption: Encryption,
include_report_server: bool,
):
activity_key_id_map = dict()

Expand All @@ -232,7 +233,7 @@ async def duplicate(
)
manager_role = Role.EDITOR if has_editor else Role.MANAGER

create_data = self._prepare_duplicate(applet_exist, new_name, encryption)
create_data = self._prepare_duplicate(applet_exist, new_name, encryption, include_report_server)

applet = await self._create(create_data, self.user_id)

Expand All @@ -252,7 +253,9 @@ async def duplicate(
return applet

@staticmethod
def _prepare_duplicate(applet_exist: AppletDuplicate, new_name: str, encryption: Encryption) -> AppletCreate:
def _prepare_duplicate(
applet_exist: AppletDuplicate, new_name: str, encryption: Encryption, include_report_server: bool
) -> AppletCreate:
activities = list()
for activity in applet_exist.activities:
activities.append(
Expand Down Expand Up @@ -289,7 +292,20 @@ def _prepare_duplicate(applet_exist: AppletDuplicate, new_name: str, encryption:
)
)

report_server_config = (
AppletReportConfigurationBase(
report_server_ip=applet_exist.report_server_ip,
report_public_key=applet_exist.report_public_key,
report_include_user_id=applet_exist.report_include_user_id,
report_include_case_id=applet_exist.report_include_case_id,
report_email_body=applet_exist.report_email_body,
).dict()
if include_report_server
else {}
)

return AppletCreate(
**report_server_config,
display_name=new_name,
description=applet_exist.description,
about=applet_exist.about,
Expand Down
68 changes: 67 additions & 1 deletion src/apps/applets/tests/test_applet.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
from apps.activity_assignments.crud.assignments import ActivityAssigmentCRUD
from apps.activity_assignments.db.schemas import ActivityAssigmentSchema
from apps.applets.domain.applet_create_update import AppletCreate, AppletUpdate
from apps.applets.domain.applet_create_update import AppletCreate, AppletReportConfiguration, AppletUpdate
from apps.applets.domain.applet_full import AppletFull
from apps.applets.domain.base import AppletReportConfigurationBase, Encryption
from apps.applets.errors import AppletAlreadyExist, AppletVersionNotFoundError
Expand Down Expand Up @@ -245,6 +245,72 @@ async def test_duplicate_applet(
assert response.status_code == http.HTTPStatus.CREATED
assert response.json()["result"]["displayName"] == new_name

async def test_duplicate_applet_default_exclude_report_server_config(
self, client: TestClient, tom: User, applet_one: AppletFull, encryption: Encryption, session: AsyncSession
):
await AppletService(session, tom.id).set_report_configuration(
applet_one.id,
AppletReportConfiguration(
report_server_ip="ipaddress",
report_public_key="public key",
report_recipients=["recipient1", "recipient1"],
report_include_user_id=True,
report_include_case_id=True,
report_email_body="email body",
),
)

client.login(tom)
new_name = "New Name"
response = await client.post(
self.applet_duplicate_url.format(pk=applet_one.id),
data=dict(display_name=new_name, encryption=encryption.dict()),
)
assert response.status_code == http.HTTPStatus.CREATED

result = response.json()["result"]
assert result["displayName"] == new_name
assert result["reportServerIp"] == ""
assert result["reportPublicKey"] == ""
assert result["reportRecipients"] == []
assert result["reportIncludeUserId"] is False
assert result["reportIncludeCaseId"] is False
assert result["reportEmailBody"] == ""

async def test_duplicate_applet_include_report_server_config(
self, client: TestClient, tom: User, applet_one: AppletFull, encryption: Encryption, session: AsyncSession
):
await AppletService(session, tom.id).set_report_configuration(
applet_one.id,
AppletReportConfiguration(
report_server_ip="ipaddress",
report_public_key="public key",
report_recipients=["recipient1", "recipient1"],
report_include_user_id=True,
report_include_case_id=True,
report_email_body="email body",
),
)

client.login(tom)
new_name = "New Name"
response = await client.post(
self.applet_duplicate_url.format(pk=applet_one.id),
data=dict(display_name=new_name, encryption=encryption.dict(), include_report_server=True),
)
assert response.status_code == http.HTTPStatus.CREATED

result = response.json()["result"]
assert result["displayName"] == new_name
assert result["reportServerIp"] == "ipaddress"
assert result["reportPublicKey"] == "public key"
assert result["reportIncludeUserId"] is True
assert result["reportIncludeCaseId"] is True
assert result["reportEmailBody"] == "email body"

# Recipients are excluded
assert result["reportRecipients"] == []

async def test_duplicate_applet_name_already_exists(
self, client: TestClient, tom: User, applet_one: AppletFull, encryption: Encryption
):
Expand Down
Loading