Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Mar 27, 2024
1 parent 6382c4b commit 133ef16
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 11 deletions.
36 changes: 27 additions & 9 deletions onadata/apps/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from onadata.libs.utils.email import ProjectInvitationEmail
from onadata.celeryapp import app

logger = logging.getLogger(__name__)


User = get_user_model()

Expand Down Expand Up @@ -146,7 +148,7 @@ def send_project_invitation_email_async(
invitation = ProjectInvitation.objects.get(id=invitation_id)

except ProjectInvitation.DoesNotExist as err:
logging.exception(err)
logger.exception(err)

else:
email = ProjectInvitationEmail(invitation, url)
Expand All @@ -162,7 +164,7 @@ def regenerate_form_instance_json(xform_id: int):
try:
xform: XForm = XForm.objects.get(pk=xform_id)
except XForm.DoesNotExist as err:
logging.exception(err)
logger.exception(err)

else:
if not xform.is_instance_json_regenerated:
Expand All @@ -187,19 +189,35 @@ def regenerate_form_instance_json(xform_id: int):

@app.task()
def add_org_user_and_share_projects_async(
org_id, user_id, role
org_id, user_id, role=None
): # pylint: disable=invalid-name
"""Add user to organization and share projects asynchronously"""
organization = OrganizationProfile.objects.get(pk=org_id)
user = User.objects.get(pk=user_id)
try:
organization = OrganizationProfile.objects.get(pk=org_id)
user = User.objects.get(pk=user_id)

tools.add_org_user_and_share_projects(organization, user, role)
except OrganizationProfile.DoesNotExist as err:
logger.exception(err)

except User.DoesNotExist as err:
logger.exception(err)

else:
tools.add_org_user_and_share_projects(organization, user, role)


@app.task()
def remove_org_user_async(org_id, user_id):
"""Remove user from organization asynchronously"""
organization = OrganizationProfile.objects.get(pk=org_id)
user = User.objects.get(pk=user_id)
try:
organization = OrganizationProfile.objects.get(pk=org_id)
user = User.objects.get(pk=user_id)

except OrganizationProfile.DoesNotExist as err:
logger.exception(err)

tools.remove_user_from_organization(organization, user)
except User.DoesNotExist as err:
logger.exception(err)

else:
tools.remove_user_from_organization(organization, user)
82 changes: 80 additions & 2 deletions onadata/apps/api/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
"""Tests for module onadata.apps.api.tasks"""

import sys

from unittest.mock import patch

from django.core.cache import cache
from django.contrib.auth import get_user_model

from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.api.tasks import (
send_project_invitation_email_async,
regenerate_form_instance_json,
add_org_user_and_share_projects_async,
remove_org_user_async,
)
from onadata.apps.api.models.organization_profile import OrganizationProfile
from onadata.apps.logger.models import ProjectInvitation, Instance
from onadata.apps.main.tests.test_base import TestBase
from onadata.libs.utils.user_auth import get_user_default_project
from onadata.libs.utils.email import ProjectInvitationEmail

User = get_user_model()


class SendProjectInivtationEmailAsyncTestCase(TestBase):
"""Tests for send_project_invitation_email_async"""
Expand Down Expand Up @@ -80,7 +87,7 @@ def mock_get_full_dict(
instance.refresh_from_db()
self.assertFalse("foo" in instance.json)

@patch("logging.exception")
@patch("onadata.apps.api.tasks.logger.exception")
def test_form_id_invalid(self, mock_log_exception):
"""An invalid xform_id is handled"""

Expand All @@ -107,3 +114,74 @@ def mock_get_full_dict(
regenerate_form_instance_json.delay(self.xform.pk)
instance.refresh_from_db()
self.assertFalse(instance.json)


@patch("onadata.apps.api.tasks.tools.add_org_user_and_share_projects")
class AddOrgUserAndShareProjectsAsyncTestCase(TestBase):
"""Tests for add_org_user_and_share_projects_async"""

def setUp(self):
super().setUp()

self.org_user = User.objects.create(username="onaorg")
alice = self._create_user("alice", "1234&&")
self.org = OrganizationProfile.objects.create(
user=self.org_user, name="Ona Org", creator=alice
)

def test_user_added_to_org(self, mock_add):
"""User is added to organization"""
add_org_user_and_share_projects_async(self.org.pk, self.user.pk, "manager")
mock_add.assert_called_once_with(self.org, self.user, "manager")

def test_role_optional(self, mock_add):
"""role param is optional"""
add_org_user_and_share_projects_async(self.org.pk, self.user.pk)
mock_add.assert_called_once_with(self.org, self.user, None)

@patch("onadata.apps.api.tasks.logger.exception")
def test_invalid_org_id(self, mock_log, mock_add):
"""Invalid org_id is handled"""
add_org_user_and_share_projects_async(sys.maxsize, self.user.pk)
mock_add.assert_not_called()
mock_log.assert_called_once()

@patch("onadata.apps.api.tasks.logger.exception")
def test_invalid_user_id(self, mock_log, mock_add):
"""Invalid org_id is handled"""
add_org_user_and_share_projects_async(self.org.pk, sys.maxsize)
mock_add.assert_not_called()
mock_log.assert_called_once()


@patch("onadata.apps.api.tasks.tools.remove_user_from_organization")
class RemoveOrgUserAsyncTestCase(TestBase):
"""Tests for remove_org_user_async"""

def setUp(self):
super().setUp()

self.org_user = User.objects.create(username="onaorg")
alice = self._create_user("alice", "1234&&")
self.org = OrganizationProfile.objects.create(
user=self.org_user, name="Ona Org", creator=alice
)

def test_user_removed_from_org(self, mock_remove):
"""User is removed from organization"""
remove_org_user_async(self.org.pk, self.user.pk)
mock_remove.assert_called_once_with(self.org, self.user)

@patch("onadata.apps.api.tasks.logger.exception")
def test_invalid_org_id(self, mock_log, mock_remove):
"""Invalid org_id is handled"""
remove_org_user_async(sys.maxsize, self.user.pk)
mock_remove.assert_not_called()
mock_log.assert_called_once()

@patch("onadata.apps.api.tasks.logger.exception")
def test_invalid_user_id(self, mock_log, mock_remove):
"""Invalid org_id is handled"""
remove_org_user_async(self.org.pk, sys.maxsize)
mock_remove.assert_not_called()
mock_log.assert_called_once()

0 comments on commit 133ef16

Please sign in to comment.