Skip to content

Commit

Permalink
fix: incorrect relation value when non admin is source and respondent…
Browse files Browse the repository at this point in the history
… but not target (M2-7517)  (#1530)

* add an additional check for a temp relation to return 'other' in case it is a temp take_now relation

* add another logical fix to the flow

* add testing to validate the new changes

* fix code quality checks duo to formating

---------

Co-authored-by: Joud Awad <[email protected]>
  • Loading branch information
mbanting and Joud Awad authored Aug 7, 2024
1 parent 209b789 commit 62f70cd
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/pr-open.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

create-preview-env:
needs: [create-database]
uses: ./.github/workflows/create-preview-env.yaml
uses: ChildMindInstitute/mindlogger-backend-refactor/.github/workflows/create-preview-env.yaml@develop
with:
env-name: "pr-${{ github.event.number }}"
env-snake-name: "pr_${{ github.event.number }}"
Expand Down
3 changes: 3 additions & 0 deletions src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ async def _get_answer_relation(

return Relation.other

if is_take_now_relation(relation) and is_valid_take_now_relation(relation):
return Relation.other

return relation.relation

async def _create_answer(self, applet_answer: AppletAnswerCreate) -> AnswerSchema:
Expand Down
94 changes: 94 additions & 0 deletions src/apps/answers/tests/test_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from apps.mailing.services import TestMail
from apps.shared.test import BaseTest
from apps.shared.test.client import TestClient
from apps.subjects.constants import Relation
from apps.subjects.db.schemas import SubjectSchema
from apps.subjects.domain import Subject, SubjectCreate
from apps.subjects.services import SubjectsService
Expand Down Expand Up @@ -1139,6 +1140,99 @@ async def test_answer_activity_items_create_temporary_relation_success(
relation_exists = await subject_service.get_relation(applet_one_sam_subject.id, target_subject.id)
assert not relation_exists

async def test_answer_activity_items_relation_equal_other_when_relation_is_temp(
self,
tom: User,
answer_create_applet_one: AppletAnswerCreate,
client: TestClient,
session: AsyncSession,
sam: User,
applet_one: AppletFull,
applet_one_sam_respondent,
applet_one_sam_subject,
) -> None:
client.login(tom)
subject_service = SubjectsService(session, tom.id)

data = answer_create_applet_one.copy(deep=True)

client.login(sam)
subject_service = SubjectsService(session, sam.id)
target_subject = await subject_service.create(
SubjectCreate(
applet_id=applet_one.id,
creator_id=tom.id,
first_name="target",
last_name="subject",
secret_user_id=f"{uuid.uuid4()}",
)
)
await subject_service.create_relation(
relation="take-now",
source_subject_id=applet_one_sam_subject.id,
subject_id=target_subject.id,
meta={
"expiresAt": (datetime.datetime.now() + datetime.timedelta(days=1)).isoformat(),
},
)

data.source_subject_id = applet_one_sam_subject.id
data.target_subject_id = target_subject.id
data.input_subject_id = applet_one_sam_subject.id

response = await client.post(self.answer_url, data=data)

assert response.status_code == http.HTTPStatus.CREATED, response.json()

answers, _ = await AnswersCRUD(session).get_applet_answers(applet_id=applet_one.id, page=1, limit=5)

assert answers[0].relation == Relation.other

async def test_answer_activity_items_relation_equal_other_when_relation_is_permanent(
self,
tom: User,
answer_create_applet_one: AppletAnswerCreate,
client: TestClient,
session: AsyncSession,
sam: User,
applet_one: AppletFull,
applet_one_sam_respondent,
applet_one_sam_subject,
) -> None:
client.login(tom)
subject_service = SubjectsService(session, tom.id)

data = answer_create_applet_one.copy(deep=True)

client.login(sam)
subject_service = SubjectsService(session, sam.id)
target_subject = await subject_service.create(
SubjectCreate(
applet_id=applet_one.id,
creator_id=tom.id,
first_name="target",
last_name="subject",
secret_user_id=f"{uuid.uuid4()}",
)
)
await subject_service.create_relation(
relation="father",
source_subject_id=applet_one_sam_subject.id,
subject_id=target_subject.id,
)

data.source_subject_id = applet_one_sam_subject.id
data.target_subject_id = target_subject.id
data.input_subject_id = applet_one_sam_subject.id

response = await client.post(self.answer_url, data=data)

assert response.status_code == http.HTTPStatus.CREATED, response.json()

answers, _ = await AnswersCRUD(session).get_applet_answers(applet_id=applet_one.id, page=1, limit=5)

assert answers[0].relation == "father"

async def test_answer_activity_items_create_permanent_relation_success(
self,
tom: User,
Expand Down

0 comments on commit 62f70cd

Please sign in to comment.