Skip to content

Commit

Permalink
212038_RDI_pull_kobo_submissions_task_Error_on_KOBO_import (#4145)
Browse files Browse the repository at this point in the history
Co-authored-by: marekbiczysko <[email protected]>
  • Loading branch information
MarekBiczysko and marekbiczysko authored Aug 19, 2024
1 parent 54ea1f9 commit c3d9e38
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
3 changes: 2 additions & 1 deletion backend/hct_mis_api/apps/registration_datahub/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ def mutate(
pull_pictures: bool,
) -> "SaveKoboProjectImportDataAsync":
cls.has_permission(info, Permissions.RDI_IMPORT_DATA, business_area_slug)
program_id: str = decode_id_string_required(info.context.headers.get("Program"))

import_data = KoboImportData.objects.create(
data_type=ImportData.JSON,
Expand All @@ -586,7 +587,7 @@ def mutate(
created_by_id=info.context.user.id,
pull_pictures=pull_pictures,
)
pull_kobo_submissions_task.delay(import_data.id)
pull_kobo_submissions_task.delay(import_data.id, program_id)
return SaveKoboProjectImportDataAsync(import_data=import_data)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,37 @@
}
]
}

snapshots['TestRegistrationDataImportDatahubMutations::test_save_kobo_project_import_data_async_0_with_permission 1'] = {
'data': {
'saveKoboImportDataAsync': {
'importData': {
'koboAssetId': '123',
'koboValidationErrors': [
],
'onlyActiveSubmissions': True,
'pullPictures': False
}
}
}
}

snapshots['TestRegistrationDataImportDatahubMutations::test_save_kobo_project_import_data_async_1_without_permission 1'] = {
'data': {
'saveKoboImportDataAsync': None
},
'errors': [
{
'locations': [
{
'column': 7,
'line': 8
}
],
'message': 'Permission Denied: User does not have correct permission.',
'path': [
'saveKoboImportDataAsync'
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
from typing import Any, List
from unittest import mock

from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile, SimpleUploadedFile
Expand All @@ -14,7 +15,7 @@
from hct_mis_api.apps.core.models import BusinessArea
from hct_mis_api.apps.program.fixtures import ProgramFactory
from hct_mis_api.apps.program.models import Program
from hct_mis_api.apps.registration_data.models import ImportData
from hct_mis_api.apps.registration_data.models import ImportData, KoboImportData


class TestRegistrationDataImportDatahubMutations(APITestCase):
Expand Down Expand Up @@ -92,6 +93,32 @@ class TestRegistrationDataImportDatahubMutations(APITestCase):
}
"""

SAVE_KOBO_PROJECT_IMPORT_DATA_ASYNC = """
mutation SaveKoboProjectImportData(
$uid: Upload!,
$businessAreaSlug: String!,
$onlyActiveSubmissions: Boolean!,
$pullPictures: Boolean!
) {
saveKoboImportDataAsync(
uid: $uid,
businessAreaSlug: $businessAreaSlug,
onlyActiveSubmissions: $onlyActiveSubmissions,
pullPictures: $pullPictures
) {
importData {
koboAssetId
onlyActiveSubmissions
pullPictures
koboValidationErrors {
header
message
}
}
}
}
"""

@classmethod
def setUpTestData(cls) -> None:
partner = PartnerFactory(name="Partner")
Expand Down Expand Up @@ -216,3 +243,36 @@ def test_registration_data_import_create_validate_import_data(self) -> None:
}
},
)

@parameterized.expand(
[
("with_permission", [Permissions.RDI_IMPORT_DATA]),
("without_permission", []),
]
)
def test_save_kobo_project_import_data_async(self, _: Any, permissions: List[Permissions]) -> None:
self.create_user_role_with_permissions(self.user, permissions, self.business_area)

with mock.patch(
"hct_mis_api.apps.registration_datahub.mutations.pull_kobo_submissions_task.delay"
) as pull_kobo_submissions_task_mock:
self.snapshot_graphql_request(
request_string=self.SAVE_KOBO_PROJECT_IMPORT_DATA_ASYNC,
context={
"user": self.user,
"headers": {
"Program": self.id_to_base64(self.program.id, "ProgramNode"),
"Business-Area": self.business_area.slug,
},
},
variables={
"uid": 123,
"businessAreaSlug": self.business_area_slug,
"onlyActiveSubmissions": True,
"pullPictures": False,
},
)
if permissions:
assert KoboImportData.objects.count() == 1
kobo_import_data = KoboImportData.objects.first()
pull_kobo_submissions_task_mock.assert_called_once_with(kobo_import_data.id, str(self.program.id))

0 comments on commit c3d9e38

Please sign in to comment.