61
61
from onadata .apps .logger .xform_instance_parser import XLSFormError
62
62
from onadata .apps .main .models import MetaData
63
63
from onadata .apps .messaging .constants import (
64
- FORM_UPDATED , XFORM , PROJECT , FORM_DELETED , FORM_CREATED
64
+ FORM_UPDATED , XFORM , PROJECT , FORM_DELETED , FORM_CREATED ,
65
+ FORM_RENAMED , FORM_ACTIVE
65
66
)
66
67
from onadata .apps .viewer .models import Export
67
68
from onadata .apps .viewer .models .export import ExportTypeError
@@ -1385,7 +1386,7 @@ def test_publish_xlsform(self, mock_send_message):
1385
1386
self .assertTrue (mock_send_message .called )
1386
1387
mock_send_message .assert_called_with (
1387
1388
instance_id = xform .id ,
1388
- target_id = xform .project . pk ,
1389
+ target_id = xform .pk ,
1389
1390
target_type = XFORM ,
1390
1391
user = request .user ,
1391
1392
message_verb = FORM_CREATED
@@ -1823,7 +1824,8 @@ def test_publish_invalid_xls_form_no_choices(self):
1823
1824
)
1824
1825
self .assertEqual (response .data .get ("text" ), error_msg )
1825
1826
1826
- def test_partial_update (self ):
1827
+ @patch ("onadata.apps.api.viewsets.xform_viewset.send_message" )
1828
+ def test_partial_update (self , mock_send_message ):
1827
1829
with HTTMock (enketo_mock ):
1828
1830
self ._publish_xls_form_to_project ()
1829
1831
view = XFormViewSet .as_view ({"patch" : "partial_update" })
@@ -1847,6 +1849,38 @@ def test_partial_update(self):
1847
1849
request = self .factory .patch ("/" , data = data , ** self .extra )
1848
1850
response = view (request , pk = self .xform .id )
1849
1851
self .assertEqual (response .status_code , 200 )
1852
+
1853
+ # send messages upon form update
1854
+ self .assertTrue (mock_send_message .called )
1855
+
1856
+ # check calls to send_message triggered by patch request
1857
+ mock_calls = mock_send_message .call_args_list
1858
+ args , kwargs = mock_calls [0 ]
1859
+ # test form rename message
1860
+ message_kwargs = {
1861
+ "instance_id" : self .xform .id ,
1862
+ "target_id" : self .xform .id ,
1863
+ "target_type" : XFORM ,
1864
+ "user" : self .xform .user ,
1865
+ "message_verb" : FORM_RENAMED ,
1866
+ "custom_message" : {
1867
+ "old_title" : "transportation_2011_07_25" ,
1868
+ "new_title" : "Hello & World!"
1869
+ }
1870
+ }
1871
+ self .assertEqual (kwargs , message_kwargs )
1872
+
1873
+ # test form status message
1874
+ args , kwargs = mock_calls [1 ]
1875
+ message_kwargs = {
1876
+ "instance_id" : self .xform .id ,
1877
+ "target_id" : self .xform .id ,
1878
+ "target_type" : XFORM ,
1879
+ "user" : self .xform .user ,
1880
+ "message_verb" : FORM_ACTIVE ,
1881
+ }
1882
+ self .assertEqual (kwargs , message_kwargs )
1883
+
1850
1884
xform_old_hash = self .xform .hash
1851
1885
self .xform .refresh_from_db ()
1852
1886
self .assertTrue (self .xform .downloadable )
@@ -3541,8 +3575,9 @@ def test_survey_preview_endpoint(self):
3541
3575
self .assertEqual (response .data .get ("detail" ), error_message )
3542
3576
3543
3577
@override_settings (CELERY_TASK_ALWAYS_EAGER = True )
3578
+ @patch ("onadata.apps.api.viewsets.xform_viewset.send_message" )
3544
3579
@patch ("onadata.apps.api.tasks.get_async_status" )
3545
- def test_delete_xform_async (self , mock_get_status ):
3580
+ def test_delete_xform_async (self , mock_get_status , mock_send_msg ):
3546
3581
with HTTMock (enketo_mock ):
3547
3582
mock_get_status .return_value = {"job_status" : "PENDING" }
3548
3583
self ._publish_xls_form_to_project ()
@@ -3571,6 +3606,16 @@ def test_delete_xform_async(self, mock_get_status):
3571
3606
self .assertEqual (response .status_code , 202 )
3572
3607
self .assertEqual (response .data , {"job_status" : "PENDING" })
3573
3608
3609
+ # send message upon form deletion
3610
+ self .assertTrue (mock_send_msg .called )
3611
+ mock_send_msg .assert_called_with (
3612
+ instance_id = self .xform .id ,
3613
+ target_id = self .xform .project .pk ,
3614
+ target_type = XFORM ,
3615
+ user = request .user ,
3616
+ message_verb = FORM_DELETED
3617
+ )
3618
+
3574
3619
xform = XForm .objects .get (pk = formid )
3575
3620
3576
3621
self .assertIsNotNone (xform .deleted_at )
0 commit comments