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

Set permissions for merged dataset asynchronously #2595

Merged
merged 4 commits into from
May 20, 2024
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
13 changes: 9 additions & 4 deletions onadata/apps/logger/models/merged_xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
MergedXForm model - stores info on forms to merge.
"""
from django.db import models
from django.db import models, transaction
from django.db.models.signals import post_save

from onadata.apps.logger.models.xform import XForm
Expand Down Expand Up @@ -39,10 +39,15 @@ def set_object_permissions(sender, instance=None, created=False, **kwargs):
OwnerRole.add(instance.created_by, instance)
OwnerRole.add(instance.created_by, instance.xform_ptr)

from onadata.libs.utils.project_utils import set_project_perms_to_xform
from onadata.libs.utils.project_utils import (
set_project_perms_to_xform_async,
)

set_project_perms_to_xform(instance, instance.project)
set_project_perms_to_xform(instance.xform_ptr, instance.project)
transaction.on_commit(
lambda: set_project_perms_to_xform_async.delay(
instance.pk, instance.project.pk
)
)


post_save.connect(
Expand Down
Empty file.
51 changes: 51 additions & 0 deletions onadata/apps/logger/models/tests/test_merged_xform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Tests for module onadata.apps.logger.models.merged_xform"""

from pyxform.builder import create_survey_element_from_dict
from unittest.mock import call, patch

from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.logger.models.merged_xform import MergedXForm
from onadata.apps.logger.models.xform import XForm


class MergedXFormTestCase(TestBase):
@patch("onadata.libs.utils.project_utils.set_project_perms_to_xform_async.delay")
def test_perms_applied_async_on_create(self, mock_set_perms):
"""Permissions are applied asynchronously on create"""
md = """
| survey |
| | type | name | label |
| | select one fruits | fruit | Fruit |

| choices |
| | list name | name | label |
| | fruits | orange | Orange |
| | fruits | mango | Mango |
"""
self._publish_markdown(md, self.user, id_string="a")
self._publish_markdown(md, self.user, id_string="b")
xf1 = XForm.objects.get(id_string="a")
xf2 = XForm.objects.get(id_string="b")
survey = create_survey_element_from_dict(xf1.json_dict())
survey["id_string"] = "c"
survey["sms_keyword"] = survey["id_string"]
survey["title"] = "Merged XForm"
merged_xf = MergedXForm.objects.create(
id_string=survey["id_string"],
sms_id_string=survey["id_string"],
title=survey["title"],
user=self.user,
created_by=self.user,
is_merged_dataset=True,
project=self.project,
xml=survey.to_xml(),
json=survey.to_json(),
)
merged_xf.xforms.add(xf1)
merged_xf.xforms.add(xf2)
calls = [
call(xf1.pk, self.project.pk),
call(xf2.pk, self.project.pk),
call(merged_xf.pk, self.project.pk),
]
mock_set_perms.assert_has_calls(calls, any_order=True)
47 changes: 44 additions & 3 deletions onadata/libs/tests/utils/test_project_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"""
Test onadata.libs.utils.project_utils
"""
from unittest.mock import MagicMock, patch
from unittest.mock import call, MagicMock, patch

from django.test.utils import override_settings

from pyxform.builder import create_survey_element_from_dict
from kombu.exceptions import OperationalError
from requests import Response

from onadata.apps.logger.models import Project
from onadata.apps.logger.models import MergedXForm, Project, XForm
from onadata.apps.main.tests.test_base import TestBase
from onadata.libs.permissions import DataEntryRole
from onadata.libs.utils.project_utils import (
Expand Down Expand Up @@ -73,6 +73,47 @@ def test_set_project_perms_to_xform_async(self, mock):
self.assertEqual(args[0], self.xform)
self.assertEqual(args[1], self.project)

@patch("onadata.libs.utils.project_utils.set_project_perms_to_xform")
def test_set_project_perms_to_xform_async_mergedxform(self, mock):
"""Permissions for a MergedXForm are set"""
md = """
| survey |
| | type | name | label |
| | select one fruits | fruit | Fruit |

| choices |
| | list name | name | label |
| | fruits | orange | Orange |
| | fruits | mango | Mango |
"""
self._publish_markdown(md, self.user, id_string="a")
self._publish_markdown(md, self.user, id_string="b")
xf1 = XForm.objects.get(id_string="a")
xf2 = XForm.objects.get(id_string="b")
survey = create_survey_element_from_dict(xf1.json_dict())
survey["id_string"] = "c"
survey["sms_keyword"] = survey["id_string"]
survey["title"] = "Merged XForm"
merged_xf = MergedXForm.objects.create(
id_string=survey["id_string"],
sms_id_string=survey["id_string"],
title=survey["title"],
user=self.user,
created_by=self.user,
is_merged_dataset=True,
project=self.project,
xml=survey.to_xml(),
json=survey.to_json(),
)
merged_xf.xforms.add(xf1)
merged_xf.xforms.add(xf2)
set_project_perms_to_xform_async.delay(merged_xf.pk, self.project.pk)
expected_calls = [
call(merged_xf.xform_ptr, self.project),
call(merged_xf, self.project),
]
mock.assert_has_calls(expected_calls, any_order=True)

def test_assign_change_asset_permission(self):
"""
Test that the `assign_change_asset_permission` function calls
Expand Down
22 changes: 16 additions & 6 deletions onadata/libs/utils/project_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@
from onadata.apps.logger.models.project import Project
from onadata.apps.logger.models.xform import XForm
from onadata.celeryapp import app
from onadata.libs.permissions import (ROLES, OwnerRole,
get_object_users_with_permissions,
get_role, is_organization)
from onadata.libs.utils.common_tags import (API_TOKEN,
ONADATA_KOBOCAT_AUTH_HEADER,
OWNER_TEAM_NAME)
from onadata.libs.permissions import (
ROLES,
OwnerRole,
get_object_users_with_permissions,
get_role,
is_organization,
)
from onadata.libs.utils.common_tags import (
API_TOKEN,
ONADATA_KOBOCAT_AUTH_HEADER,
OWNER_TEAM_NAME,
)
from onadata.libs.utils.common_tools import report_exception


Expand Down Expand Up @@ -124,6 +130,10 @@ def _set_project_perms():
else:
set_project_perms_to_xform(xform, project)

# Set MergedXForm permissions if XForm is also a MergedXForm
if hasattr(xform, "mergedxform"):
set_project_perms_to_xform(xform.mergedxform, project)

try:
if getattr(settings, "SLAVE_DATABASES", []):

Expand Down
Loading