Skip to content

Commit

Permalink
Add tests for sending messages upon:
Browse files Browse the repository at this point in the history
1. Submission Review
2. Bulk Submission Review
3. CSV upload submission creation
4. CSV Upload submission edit
5. Form update
6. Submission Delete
7. Bulk Submission Delete

Signed-off-by: lincmba <[email protected]>
  • Loading branch information
lincmba committed Jul 20, 2020
1 parent d10b748 commit 4799710
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
25 changes: 20 additions & 5 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,8 @@ def test_data_w_attachment(self):
self.assertIsInstance(response.data, dict)
self.assertDictContainsSubset(data, response.data)

def test_delete_submission(self):
@patch('onadata.apps.api.viewsets.data_viewset.send_message')
def test_delete_submission(self, send_message_mock):
self._make_submissions()
formid = self.xform.pk
dataid = self.xform.instances.all().order_by('id')[0].pk
Expand All @@ -1384,6 +1385,11 @@ def test_delete_submission(self):
self.assertEqual(response.status_code, 204)
first_xform_instance = self.xform.instances.filter(pk=dataid)
self.assertEqual(first_xform_instance[0].deleted_by, request.user)
# message sent upon delete
self.assertTrue(send_message_mock.called)
send_message_mock.called_with(
[dataid], formid, XFORM,
request.user, SUBMISSION_DELETED)

# second delete of same submission should return 404
request = self.factory.delete('/', **self.extra)
Expand Down Expand Up @@ -1412,8 +1418,10 @@ def test_delete_submission(self):
response = view(request, pk=formid)
self.assertEqual(len(response.data), 2)

@patch('onadata.apps.api.viewsets.data_viewset.send_message')
@patch('onadata.apps.viewer.signals._post_process_submissions')
def test_post_save_signal_on_submission_deletion(self, mock):
def test_post_save_signal_on_submission_deletion(self, mock,
send_message_mock):
# test that post_save_submission signal is sent
# create form
xls_file_path = os.path.join(
Expand Down Expand Up @@ -1450,6 +1458,7 @@ def test_post_save_signal_on_submission_deletion(self, mock):
# check that call_count is still one after saving instance again
instance.save()
self.assertEqual(mock.call_count, 1)
self.assertTrue(send_message_mock.called)

@patch(
'onadata.apps.api.viewsets.data_viewset.send_message')
Expand Down Expand Up @@ -1503,7 +1512,8 @@ def test_deletion_of_bulk_submissions(self, send_message_mock):
self.assertEqual(current_count, 2)
self.assertEqual(self.xform.num_of_submissions, 2)

def test_delete_submission_inactive_form(self):
@patch('onadata.apps.api.viewsets.data_viewset.send_message')
def test_delete_submission_inactive_form(self, send_message_mock):
self._make_submissions()
formid = self.xform.pk
dataid = self.xform.instances.all().order_by('id')[0].pk
Expand All @@ -1527,8 +1537,10 @@ def test_delete_submission_inactive_form(self):
response = view(request, pk=formid, dataid=dataid)

self.assertEqual(response.status_code, 400)
self.assertTrue(send_message_mock.called)

def test_delete_submission_by_editor(self):
@patch('onadata.apps.api.viewsets.data_viewset.send_message')
def test_delete_submission_by_editor(self, send_message_mock):
self._make_submissions()
formid = self.xform.pk
dataid = self.xform.instances.all().order_by('id')[0].pk
Expand Down Expand Up @@ -1559,8 +1571,10 @@ def test_delete_submission_by_editor(self):
request = self.factory.get('/', **self.extra)
response = view(request, pk=formid)
self.assertEqual(len(response.data), 3)
self.assertTrue(send_message_mock.called)

def test_delete_submission_by_owner(self):
@patch('onadata.apps.api.viewsets.data_viewset.send_message')
def test_delete_submission_by_owner(self, send_message_mock):
self._make_submissions()
formid = self.xform.pk
dataid = self.xform.instances.all().order_by('id')[0].pk
Expand Down Expand Up @@ -1591,6 +1605,7 @@ def test_delete_submission_by_owner(self):
request = self.factory.get('/', **self.extra)
response = view(request, pk=formid)
self.assertEqual(len(response.data), 3)
self.assertTrue(send_message_mock.called)

def test_geojson_format(self):
self._publish_submit_geojson()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def test_submission_review_create(self, mock_send_message):
submission_review.id, submission_review.instance.xform.id, XFORM,
submission_review.created_by, SUBMISSION_REVIEWED)

def test_bulk_create_submission_review(self):
@patch('onadata.apps.api.viewsets.submission_review_viewset.send_message')
def test_bulk_create_submission_review(self, mock_send_message):
"""
Test that we can bulk create submission reviews
"""
Expand All @@ -93,6 +94,11 @@ def test_bulk_create_submission_review(self):
self.assertEqual(201, response.status_code)
self.assertEqual(4, len(response.data))
already_seen = []
# sends message upon saving the submission review
self.assertTrue(mock_send_message.called)
mock_send_message.called_with(
[s.id for s in self.xform.instances.all()], self.xform.id, XFORM,
request.user, SUBMISSION_REVIEWED)
for item in response.data:
# the note should match what we provided
self.assertEqual('This is not very good, is it?', item['note'])
Expand Down
9 changes: 8 additions & 1 deletion onadata/apps/api/tests/viewsets/test_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from onadata.apps.logger.models import XForm
from onadata.apps.logger.xform_instance_parser import XLSFormError
from onadata.apps.main.models import MetaData
from onadata.apps.messaging.constants import XFORM, FORM_UPDATED
from onadata.apps.viewer.models import Export
from onadata.libs.permissions import (
ROLES_ORDERED, DataEntryMinorRole, DataEntryOnlyRole, DataEntryRole,
Expand Down Expand Up @@ -114,7 +115,8 @@ def test_form_publishing_arabic(self):
self.assertEqual(xforms + 1, XForm.objects.count())
self.assertEqual(response.status_code, 201)

def test_replace_form_with_external_choices(self):
@patch('onadata.apps.api.viewsets.xform_viewset.send_message')
def test_replace_form_with_external_choices(self, mock_send_message):
with HTTMock(enketo_mock):
xls_file_path = os.path.join(
settings.PROJECT_ROOT, "apps", "logger", "fixtures",
Expand Down Expand Up @@ -145,6 +147,11 @@ def test_replace_form_with_external_choices(self):
request = self.factory.patch('/', data=post_data, **self.extra)
response = view(request, pk=form_id)
self.assertEqual(response.status_code, 200)
# send message upon form update
self.assertTrue(mock_send_message.called)
mock_send_message.called_with(self.xform.id, self.xform.id,
XFORM,
request.user, FORM_UPDATED)

def test_form_publishing_using_invalid_text_xls_form(self):
view = ProjectViewSet.as_view({
Expand Down
14 changes: 10 additions & 4 deletions onadata/apps/api/viewsets/data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
from rest_framework.settings import api_settings
from rest_framework.viewsets import ModelViewSet

from onadata.apps.api.permissions import ConnectViewsetPermissions
from onadata.apps.api.permissions import XFormPermissions
from onadata.apps.api.tools import add_tags_to_instance
from onadata.apps.api.tools import get_baseviewset_class
from onadata.apps.logger.models import OsmData, MergedXForm
from onadata.apps.logger.models.attachment import Attachment
from onadata.apps.logger.models.instance import FormInactiveError
from onadata.apps.logger.models.instance import Instance
from onadata.apps.logger.models.xform import XForm
from onadata.apps.messaging.constants import XFORM, SUBMISSION_DELETED
Expand Down Expand Up @@ -55,11 +58,9 @@
from onadata.libs.serializers.geojson_serializer import GeoJsonSerializer
from onadata.libs.utils.api_export_tools import custom_response_handler
from onadata.libs.utils.common_tools import json_stream
from onadata.libs.utils.viewer_tools import get_enketo_edit_url
from onadata.apps.api.permissions import ConnectViewsetPermissions
from onadata.apps.api.tools import get_baseviewset_class
from onadata.apps.logger.models.instance import FormInactiveError
from onadata.libs.utils.model_tools import queryset_iterator
from onadata.libs.utils.viewer_tools import get_enketo_edit_url

SAFE_METHODS = ['GET', 'HEAD', 'OPTIONS']
BaseViewset = get_baseviewset_class()

Expand Down Expand Up @@ -325,6 +326,11 @@ def destroy(self, request, *args, **kwargs):
if request.user.has_perm(
CAN_DELETE_SUBMISSION, self.object.xform):
delete_instance(self.object, request.user)
# send message
send_message(
instance_id=self.object, target_id=self.object.xform.id,
target_type=XFORM, user=request.user,
message_verb=SUBMISSION_DELETED)
else:
raise PermissionDenied(_(u"You do not have delete "
u"permissions."))
Expand Down
2 changes: 2 additions & 0 deletions onadata/apps/messaging/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def send_message(instance_id, target_id, target_type, user, message_verb):
:return:
"""
if user:
if isinstance(instance_id, int):
instance_id = [instance_id]
request = HttpRequest()
request.user = user
message = json.dumps({'id': instance_id})
Expand Down
15 changes: 13 additions & 2 deletions onadata/libs/tests/utils/test_csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import unicodecsv as ucsv
from celery.backends.amqp import BacklogLimitExceeded
from django.conf import settings
from mock import patch

from onadata.apps.logger.models import Instance, XForm
from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.messaging.constants import \
XFORM, SUBMISSION_EDITED, SUBMISSION_CREATED
from onadata.libs.utils import csv_import
from onadata.libs.utils.csv_import import get_submission_meta_dict
from onadata.libs.utils.user_auth import get_user_default_project
Expand Down Expand Up @@ -119,7 +122,8 @@ def test_submit_csv_and_rollback(self):
self.xform.refresh_from_db()
self.assertEqual(self.xform.num_of_submissions, count + 9)

def test_submit_csv_edits(self):
@patch('onadata.libs.utils.logger_tools.send_message')
def test_submit_csv_edits(self, send_message_mock):
xls_file_path = os.path.join(settings.PROJECT_ROOT, "apps", "main",
"tests", "fixtures", "tutorial.xls")
self._publish_xls_file(xls_file_path)
Expand All @@ -141,6 +145,9 @@ def test_submit_csv_edits(self):
csv_import.submit_csv(self.user.username, self.xform, edit_csv)
self.assertEqual(Instance.objects.count(), count,
'submit_csv edits #2 test Failed!')
# message sent upon submission edit
self.assertTrue(send_message_mock.called)
send_message_mock.called_with(self.xform.id, XFORM, SUBMISSION_EDITED)

def test_import_non_utf8_csv(self):
xls_file_path = os.path.join(self.fixtures_dir, "mali_health.xls")
Expand Down Expand Up @@ -335,7 +342,8 @@ def test_submit_csv_instance_id_consistency(self, safe_create_instance):
len(instance_id), len(single_instance_id),
"Same uuid length in generated xml")

def test_data_upload(self):
@patch('onadata.libs.utils.logger_tools.send_message')
def test_data_upload(self, send_message_mock):
"""Data upload for submissions with no uuids"""
self._publish_xls_file(self.xls_file_path)
self.xform = XForm.objects.get()
Expand All @@ -345,6 +353,9 @@ def test_data_upload(self):
csv_import.submit_csv(self.user.username, self.xform, single_csv)
self.xform.refresh_from_db()
self.assertEqual(self.xform.num_of_submissions, count + 1)
# message sent upon submission creation
self.assertTrue(send_message_mock.called)
send_message_mock.called_with(self.xform.id, XFORM, SUBMISSION_CREATED)

def test_excel_date_conversion(self):
"""Convert date from 01/01/1900 to 01-01-1900"""
Expand Down

0 comments on commit 4799710

Please sign in to comment.