Skip to content

Commit

Permalink
Update tests to use deployment xform
Browse files Browse the repository at this point in the history
  • Loading branch information
rajpatel24 committed Nov 5, 2024
1 parent ad19fdc commit 98694f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
11 changes: 5 additions & 6 deletions kobo/apps/project_ownership/tests/api/v2/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from rest_framework import status
from rest_framework.reverse import reverse

from kobo.apps.openrosa.apps.logger.models import XForm
from kobo.apps.project_ownership.models import Invite, InviteStatusChoices, Transfer
from kobo.apps.trackers.utils import update_nlp_counter
from kpi.constants import PERM_VIEW_ASSET
Expand Down Expand Up @@ -502,8 +501,8 @@ def test_data_accessible_to_new_user(self):
assert attachment['filename'].startswith('anotheruser/')

# Get the mongo_uuid for the transferred asset (XForm)
xform = XForm.objects.get(kpi_asset_uid=self.asset.uid)
mongo_uuid = xform.mongo_uuid
self.asset.deployment.xform.refresh_from_db()
mongo_uuid = self.asset.deployment.xform.mongo_uuid

assert (
settings.MONGO_DB.instances.count_documents(
Expand Down Expand Up @@ -556,9 +555,9 @@ def test_mongo_uuid_after_transfer(self):
response = self.client.post(self.invite_url, data=payload, format='json')
assert response.status_code == status.HTTP_201_CREATED

# Retrieve the mongo_uuid for the transferred asset (XForm)
xform = XForm.objects.get(kpi_asset_uid=self.asset.uid)
mongo_uuid = xform.mongo_uuid
# Get the mongo_uuid for the transferred asset (XForm)
self.asset.deployment.xform.refresh_from_db()
mongo_uuid = self.asset.deployment.xform.mongo_uuid

# Verify MongoDB now uses mongo_uuid as the identifier
assert (
Expand Down
25 changes: 9 additions & 16 deletions kpi/deployment_backends/openrosa_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
except ImportError:
from backports.zoneinfo import ZoneInfo

import secrets
import string
import redis.exceptions
import requests
from defusedxml import ElementTree as DET
Expand Down Expand Up @@ -63,6 +61,7 @@
SubmissionNotFoundException,
XPathNotFoundException,
)
from kpi.fields import KpiUidField
from kpi.interfaces.sync_backend_media import SyncBackendMediaInterface
from kpi.models.asset_file import AssetFile
from kpi.models.object_permission import ObjectPermission
Expand Down Expand Up @@ -967,6 +966,14 @@ def set_enketo_open_rosa_server(self, require_auth: bool, enketo_id: str = None)
server_url,
)

def set_mongo_uuid(self):
"""
Set the `mongo_uuid` for the associated XForm if it's not already set.
"""
if not self.xform.mongo_uuid:
self.xform.mongo_uuid = KpiUidField.generate_unique_id()
self.xform.save(update_fields=['mongo_uuid'])

def set_validation_status(
self,
submission_id: int,
Expand Down Expand Up @@ -1243,20 +1250,6 @@ def xform(self):
self._xform = xform
return self._xform

def generate_unique_key(self, length=20):

characters = string.ascii_letters + string.digits
# Generate a secure key with the specified length
return ''.join(secrets.choice(characters) for _ in range(length))

def set_mongo_uuid(self):
"""
Set the `mongo_uuid` for the associated XForm if it's not already set.
"""
if not self.xform.mongo_uuid:
self.xform.mongo_uuid = self.generate_unique_key()
self.xform.save(update_fields=['mongo_uuid'])

@property
def xform_id(self):
return self.xform.pk
Expand Down

0 comments on commit 98694f2

Please sign in to comment.