tests.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import json
  2. from django.core import mail
  3. from django.contrib.auth.models import User
  4. from django.test import TestCase, Client, override_settings
  5. from contribmap.models import Contrib
  6. from contribmap.forms import PublicContribForm
  7. class APITestClient(Client):
  8. def json_get(self, *args, **kwargs):
  9. """ Annotate the response with a .data containing parsed JSON
  10. """
  11. response = super().get(*args, **kwargs)
  12. response.data = json.loads(response.content.decode('utf-8'))
  13. return response
  14. class APITestCase(TestCase):
  15. def setUp(self):
  16. super().setUp()
  17. self.client = APITestClient()
  18. class TestContrib(TestCase):
  19. def test_comma_separatedcharfield(self):
  20. co = Contrib(name='foo', orientations=['SO', 'NE'],
  21. contrib_type=Contrib.CONTRIB_CONNECT,
  22. latitude=0.5, longitude=0.5,
  23. )
  24. co.save()
  25. self.assertEqual(
  26. Contrib.objects.get(name='foo').orientations,
  27. ['SO', 'NE'])
  28. co.orientations = ['S']
  29. co.save()
  30. class TestContribPrivacy(TestCase):
  31. def test_always_private_field(self):
  32. c = Contrib.objects.create(
  33. name='John',
  34. phone='010101010101',
  35. contrib_type=Contrib.CONTRIB_CONNECT,
  36. latitude=0.5,
  37. longitude=0.5,
  38. )
  39. self.assertEqual(c.get_public_field('phone'), None)
  40. def test_public_field(self):
  41. c = Contrib.objects.create(
  42. name='John',
  43. phone='010101010101',
  44. contrib_type=Contrib.CONTRIB_CONNECT,
  45. privacy_name=True,
  46. latitude=0.5,
  47. longitude=0.5,
  48. )
  49. self.assertEqual(c.get_public_field('name'), 'John')
  50. def test_public_callable_field(self):
  51. c = Contrib.objects.create(
  52. name='John',
  53. phone='010101010101',
  54. orientations=['N'],
  55. contrib_type=Contrib.CONTRIB_CONNECT,
  56. privacy_name=True,
  57. latitude=0.5,
  58. longitude=0.5,
  59. )
  60. self.assertEqual(c.get_public_field('angles'), [[-23, 22]])
  61. def test_private_field(self):
  62. c = Contrib.objects.create(
  63. name='John',
  64. phone='010101010101',
  65. contrib_type=Contrib.CONTRIB_CONNECT,
  66. latitude=0.5,
  67. longitude=0.5,
  68. )
  69. self.assertEqual(c.privacy_name, False)
  70. self.assertEqual(c.get_public_field('name'), None)
  71. class TestViews(APITestCase):
  72. def test_public_json(self):
  73. response = self.client.json_get('/map/public.json')
  74. self.assertEqual(response.status_code, 200)
  75. self.assertEqual(len(response.data['features']), 0)
  76. Contrib.objects.create(
  77. name='John',
  78. phone='010101010101',
  79. contrib_type=Contrib.CONTRIB_CONNECT,
  80. privacy_coordinates=True,
  81. latitude=0.5,
  82. longitude=0.5,
  83. )
  84. response = self.client.json_get('/map/public.json')
  85. self.assertEqual(response.status_code, 200)
  86. self.assertEqual(len(response.data['features']), 1)
  87. def test_private_json(self):
  88. self.client.force_login(
  89. User.objects.create(username='foo', is_staff=False))
  90. response = self.client.get('/map/private.json')
  91. self.assertEqual(response.status_code, 403)
  92. def test_private_json_staff(self):
  93. self.client.force_login(
  94. User.objects.create(username='foo', is_staff=True))
  95. response = self.client.get('/map/private.json')
  96. self.assertEqual(response.status_code, 200)
  97. @override_settings(NOTIFICATION_EMAILS=['foo@example.com'])
  98. def test_add_contrib_sends_email(self):
  99. response = self.client.post('/map/contribute', {
  100. 'roof': True,
  101. 'privacy_place_details': True,
  102. 'privacy_coordinates': True,
  103. 'phone': '0202020202',
  104. 'orientations': 'N',
  105. 'orientations': 'NO',
  106. 'orientations': 'O',
  107. 'orientations': 'SO',
  108. 'orientations': 'S',
  109. 'orientations': 'SE',
  110. 'orientations': 'E',
  111. 'orientations': 'NE',
  112. 'orientation': 'all',
  113. 'name': 'JohnCleese',
  114. 'longitude': -1.553621,
  115. 'latitude': 47.218371,
  116. 'floor_total': '2',
  117. 'floor': 1,
  118. 'email': 'coucou@example.com',
  119. 'contrib_type': 'connect',
  120. 'connect_local': 'on',
  121. })
  122. self.assertEqual(response.status_code, 302)
  123. self.assertEqual(len(mail.outbox), 1)
  124. self.assertIn('JohnCleese', mail.outbox[0].subject)
  125. self.assertIn('JohnCleese', mail.outbox[0].body)
  126. <<<<<<< HEAD
  127. class TestForms(TestCase):
  128. valid_data = {
  129. 'roof': True,
  130. 'privacy_place_details': True,
  131. 'privacy_coordinates': True,
  132. 'orientations': ['N'],
  133. 'orientation': 'all',
  134. 'name': 'JohnCleese',
  135. 'longitude': -1.553621,
  136. 'email': 'foo@example.com',
  137. 'phone': '0202020202',
  138. 'latitude': 47.218371,
  139. 'floor_total': '2',
  140. 'floor': 1,
  141. 'contrib_type': 'connect',
  142. 'connect_local': 'on',
  143. }
  144. def test_contact_validation(self):
  145. no_contact, phone_contact, email_contact, both_contact = [
  146. self.valid_data.copy() for i in range(4)]
  147. del phone_contact['email']
  148. del email_contact['phone']
  149. del no_contact['phone']
  150. del no_contact['email']
  151. both_contact.update(phone_contact)
  152. both_contact.update(email_contact)
  153. self.assertFalse(PublicContribForm(no_contact).is_valid())
  154. self.assertTrue(PublicContribForm(phone_contact).is_valid())
  155. self.assertTrue(PublicContribForm(email_contact).is_valid())
  156. self.assertTrue(PublicContribForm(both_contact).is_valid())
  157. def test_floors_validation(self):
  158. invalid_floors = self.valid_data.copy()
  159. invalid_floors['floor'] = 2
  160. invalid_floors['floor_total'] = 1
  161. self.assertFalse(PublicContribForm(invalid_floors).is_valid())
  162. self.assertTrue(PublicContribForm(self.valid_data).is_valid())
  163. invalid_floors['floor'] = None
  164. invalid_floors['floor_total'] = None
  165. self.assertTrue(PublicContribForm(invalid_floors).is_valid())
  166. def test_share_fields_validation(self):
  167. data = self.valid_data.copy()
  168. data['contrib_type'] = 'share'
  169. self.assertFalse(PublicContribForm(data).is_valid())
  170. data['access_type'] = 'cable'
  171. self.assertTrue(PublicContribForm(data).is_valid())
  172. @override_settings(NOTIFICATION_EMAILS=['foo@example.com'])
  173. def test_add_contrib_like_a_robot(self):
  174. response = self.client.post('/map/contribute', {
  175. 'roof': True,
  176. 'human_field': 'should not have no value',
  177. 'privacy_place_details': True,
  178. 'privacy_coordinates': True,
  179. 'phone': '0202020202',
  180. 'orientations': 'N',
  181. 'orientations': 'NO',
  182. 'orientations': 'O',
  183. 'orientations': 'SO',
  184. 'orientations': 'S',
  185. 'orientations': 'SE',
  186. 'orientations': 'E',
  187. 'orientations': 'NE',
  188. 'orientation': 'all',
  189. 'name': 'JohnCleese',
  190. 'longitude': -1.553621,
  191. 'latitude': 47.218371,
  192. 'floor_total': '2',
  193. 'floor': 1,
  194. 'email': 'coucou@example.com',
  195. 'contrib_type': 'connect',
  196. 'connect_local': 'on',
  197. })
  198. self.assertEqual(response.status_code, 403)
  199. self.assertEqual(len(mail.outbox), 0)
  200. class TestDataImport(TestCase):
  201. fixtures = ['bottle_data.yaml']
  202. def test_re_save(self):
  203. for contrib in Contrib.objects.all():
  204. contrib.full_clean()
  205. contrib.save()