|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +from copy import deepcopy |
| 5 | +from django.core.exceptions import ImproperlyConfigured |
| 6 | +from django.core.urlresolvers import reverse |
| 7 | +from django.conf import settings |
| 8 | +from django.test import TestCase, override_settings |
| 9 | + |
| 10 | +from rest_framework.test import APITestCase |
| 11 | + |
| 12 | +from formidable.models import Formidable |
| 13 | +from formidable.views import check_callback_configuration |
| 14 | + |
| 15 | +from . import form_data, form_data_items |
| 16 | + |
| 17 | +import six |
| 18 | +if six.PY3: |
| 19 | + from unittest.mock import patch |
| 20 | +else: |
| 21 | + from mock import patch |
| 22 | + |
| 23 | +CALLBACK = 'demo.callback_save' |
| 24 | +CALLBACK_EXCEPTION = 'demo.callback_exception' |
| 25 | + |
| 26 | + |
| 27 | +class CreateFormTestCase(APITestCase): |
| 28 | + |
| 29 | + @override_settings( |
| 30 | + FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS=CALLBACK, |
| 31 | + FORMIDABLE_POST_CREATE_CALLBACK_FAIL=CALLBACK |
| 32 | + ) |
| 33 | + def test_do_no_call_on_get(self): |
| 34 | + with patch(CALLBACK) as patched_callback: |
| 35 | + res = self.client.get( |
| 36 | + reverse('formidable:form_create') |
| 37 | + ) |
| 38 | + self.assertEqual(res.status_code, 405) |
| 39 | + # No call on GET |
| 40 | + self.assertEqual(patched_callback.call_count, 0) |
| 41 | + |
| 42 | + @override_settings(FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS=CALLBACK) |
| 43 | + def test_create_no_error_post(self): |
| 44 | + with patch(CALLBACK) as patched_callback: |
| 45 | + res = self.client.post( |
| 46 | + reverse('formidable:form_create'), form_data, format='json' |
| 47 | + ) |
| 48 | + self.assertEqual(res.status_code, 201) |
| 49 | + self.assertEqual(patched_callback.call_count, 1) |
| 50 | + |
| 51 | + @override_settings(FORMIDABLE_POST_CREATE_CALLBACK_FAIL=CALLBACK) |
| 52 | + def test_create_error_post(self): |
| 53 | + with patch(CALLBACK) as patched_callback: |
| 54 | + form_data_without_items = deepcopy(form_data_items) |
| 55 | + form_data_without_items['fields'][0].pop('items') |
| 56 | + |
| 57 | + res = self.client.post( |
| 58 | + reverse('formidable:form_create'), form_data_without_items, |
| 59 | + format='json' |
| 60 | + ) |
| 61 | + self.assertEquals(res.status_code, 400) |
| 62 | + self.assertEqual(patched_callback.call_count, 1) |
| 63 | + |
| 64 | + @override_settings( |
| 65 | + FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS=CALLBACK_EXCEPTION |
| 66 | + ) |
| 67 | + def test_create_exception(self): |
| 68 | + # The called function raises an error, but the treatment proceeds |
| 69 | + # as if nothing has happened |
| 70 | + res = self.client.post( |
| 71 | + reverse('formidable:form_create'), form_data, format='json' |
| 72 | + ) |
| 73 | + self.assertEqual(res.status_code, 201) |
| 74 | + |
| 75 | + @override_settings( |
| 76 | + FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS=CALLBACK_EXCEPTION |
| 77 | + ) |
| 78 | + def test_create_exception_logger(self): |
| 79 | + # The called function raises an error, but the treatment proceeds |
| 80 | + # as if nothing has happened |
| 81 | + with patch('formidable.views.logger.error') as logger_error: |
| 82 | + res = self.client.post( |
| 83 | + reverse('formidable:form_create'), form_data, format='json' |
| 84 | + ) |
| 85 | + self.assertEqual(res.status_code, 201) |
| 86 | + self.assertEqual(logger_error.call_count, 1) |
| 87 | + |
| 88 | + @override_settings(FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS='non.existent') |
| 89 | + def test_create_callback_is_non_existent(self): |
| 90 | + # A non-existing module is treated separately. |
| 91 | + with patch('formidable.views.logger.error') as logger_error: |
| 92 | + res = self.client.post( |
| 93 | + reverse('formidable:form_create'), form_data, format='json' |
| 94 | + ) |
| 95 | + self.assertEqual(res.status_code, 201) |
| 96 | + self.assertEqual(logger_error.call_count, 1) |
| 97 | + |
| 98 | + |
| 99 | +class UpdateFormTestCase(APITestCase): |
| 100 | + |
| 101 | + def setUp(self): |
| 102 | + super(UpdateFormTestCase, self).setUp() |
| 103 | + self.form = Formidable.objects.create( |
| 104 | + label='test', description='test' |
| 105 | + ) |
| 106 | + |
| 107 | + @override_settings( |
| 108 | + FORMIDABLE_POST_UPDATE_CALLBACK_SUCCESS=CALLBACK, |
| 109 | + FORMIDABLE_POST_UPDATE_CALLBACK_FAIL=CALLBACK |
| 110 | + ) |
| 111 | + def test_do_no_call_on_get(self): |
| 112 | + with patch(CALLBACK) as patched_callback: |
| 113 | + res = self.client.get( |
| 114 | + reverse('formidable:form_detail', args=[self.form.id]) |
| 115 | + ) |
| 116 | + self.assertEqual(res.status_code, 200) |
| 117 | + # No call on GET |
| 118 | + self.assertEqual(patched_callback.call_count, 0) |
| 119 | + |
| 120 | + @override_settings(FORMIDABLE_POST_UPDATE_CALLBACK_SUCCESS=CALLBACK) |
| 121 | + def test_update_no_error_post(self): |
| 122 | + with patch(CALLBACK) as patched_callback: |
| 123 | + res = self.client.put( |
| 124 | + reverse('formidable:form_detail', args=[self.form.id]), |
| 125 | + form_data, format='json' |
| 126 | + ) |
| 127 | + self.assertEqual(res.status_code, 200) |
| 128 | + self.assertEqual(patched_callback.call_count, 1) |
| 129 | + |
| 130 | + @override_settings(FORMIDABLE_POST_UPDATE_CALLBACK_FAIL=CALLBACK) |
| 131 | + def test_update_error_post(self): |
| 132 | + with patch(CALLBACK) as patched_callback: |
| 133 | + form_data_without_items = deepcopy(form_data_items) |
| 134 | + form_data_without_items['fields'][0].pop('items') |
| 135 | + |
| 136 | + res = self.client.put( |
| 137 | + reverse('formidable:form_detail', args=[self.form.id]), |
| 138 | + form_data_without_items, format='json' |
| 139 | + ) |
| 140 | + self.assertEquals(res.status_code, 400) |
| 141 | + self.assertEqual(patched_callback.call_count, 1) |
| 142 | + |
| 143 | + @override_settings( |
| 144 | + FORMIDABLE_POST_UPDATE_CALLBACK_SUCCESS=CALLBACK_EXCEPTION |
| 145 | + ) |
| 146 | + def test_update_exception(self): |
| 147 | + # The called function raises an error, but the treatment proceeds |
| 148 | + # as if nothing has happened |
| 149 | + res = self.client.put( |
| 150 | + reverse('formidable:form_detail', args=[self.form.id]), |
| 151 | + form_data, format='json' |
| 152 | + ) |
| 153 | + self.assertEqual(res.status_code, 200) |
| 154 | + |
| 155 | + @override_settings( |
| 156 | + FORMIDABLE_POST_UPDATE_CALLBACK_SUCCESS=CALLBACK_EXCEPTION |
| 157 | + ) |
| 158 | + def test_update_exception_logger(self): |
| 159 | + # The called function raises an error, but the treatment proceeds |
| 160 | + # as if nothing has happened |
| 161 | + with patch('formidable.views.logger.error') as logger_error: |
| 162 | + res = self.client.put( |
| 163 | + reverse('formidable:form_detail', args=[self.form.id]), |
| 164 | + form_data, format='json' |
| 165 | + ) |
| 166 | + self.assertEqual(res.status_code, 200) |
| 167 | + self.assertEqual(logger_error.call_count, 1) |
| 168 | + |
| 169 | + @override_settings(FORMIDABLE_POST_UPDATE_CALLBACK_SUCCESS='non.existent') |
| 170 | + def test_update_callback_is_non_existent(self): |
| 171 | + # A non-existing module is treated separately. |
| 172 | + with patch('formidable.views.logger.error') as logger_error: |
| 173 | + res = self.client.put( |
| 174 | + reverse('formidable:form_detail', args=[self.form.id]), |
| 175 | + form_data, format='json' |
| 176 | + ) |
| 177 | + self.assertEqual(res.status_code, 200) |
| 178 | + self.assertEqual(logger_error.call_count, 1) |
| 179 | + |
| 180 | + |
| 181 | +class ConfigurationLoadingTestCases(TestCase): |
| 182 | + |
| 183 | + @override_settings() |
| 184 | + def test_all_deleted(self): |
| 185 | + del settings.FORMIDABLE_POST_UPDATE_CALLBACK_SUCCESS |
| 186 | + del settings.FORMIDABLE_POST_UPDATE_CALLBACK_FAIL |
| 187 | + del settings.FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS |
| 188 | + del settings.FORMIDABLE_POST_CREATE_CALLBACK_FAIL |
| 189 | + self.assertTrue(check_callback_configuration()) |
| 190 | + |
| 191 | + @override_settings( |
| 192 | + FORMIDABLE_POST_UPDATE_CALLBACK_SUCCESS=None, |
| 193 | + FORMIDABLE_POST_UPDATE_CALLBACK_FAIL=None, |
| 194 | + FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS=None, |
| 195 | + FORMIDABLE_POST_CREATE_CALLBACK_FAIL=None |
| 196 | + ) |
| 197 | + def test_all_none(self): |
| 198 | + self.assertTrue(check_callback_configuration()) |
| 199 | + |
| 200 | + @override_settings( |
| 201 | + FORMIDABLE_POST_UPDATE_CALLBACK_SUCCESS='', |
| 202 | + FORMIDABLE_POST_UPDATE_CALLBACK_FAIL='', |
| 203 | + FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS='', |
| 204 | + FORMIDABLE_POST_CREATE_CALLBACK_FAIL='' |
| 205 | + ) |
| 206 | + def test_all_empty(self): |
| 207 | + self.assertTrue(check_callback_configuration()) |
| 208 | + |
| 209 | + @override_settings( |
| 210 | + FORMIDABLE_POST_UPDATE_CALLBACK_SUCCESS='non.existing', |
| 211 | + ) |
| 212 | + def test_update_success_unknown(self): |
| 213 | + with self.assertRaises(ImproperlyConfigured): |
| 214 | + check_callback_configuration() |
| 215 | + |
| 216 | + @override_settings( |
| 217 | + FORMIDABLE_POST_UPDATE_CALLBACK_FAIL='non.existing', |
| 218 | + ) |
| 219 | + def test_update_fail_unknown(self): |
| 220 | + with self.assertRaises(ImproperlyConfigured): |
| 221 | + check_callback_configuration() |
| 222 | + |
| 223 | + @override_settings( |
| 224 | + FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS='non.existing', |
| 225 | + ) |
| 226 | + def test_create_success_unknown(self): |
| 227 | + with self.assertRaises(ImproperlyConfigured): |
| 228 | + check_callback_configuration() |
| 229 | + |
| 230 | + @override_settings( |
| 231 | + FORMIDABLE_POST_CREATE_CALLBACK_FAIL='non.existing', |
| 232 | + ) |
| 233 | + def test_create_fail_unknown(self): |
| 234 | + with self.assertRaises(ImproperlyConfigured): |
| 235 | + check_callback_configuration() |
0 commit comments