tests.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. co.save()
  23. self.assertEqual(
  24. Contrib.objects.get(name='foo').orientations,
  25. ['SO', 'NE'])
  26. co.orientations = ['S']
  27. co.save()
  28. class TestContribPrivacy(TestCase):
  29. def test_always_private_field(self):
  30. c = Contrib.objects.create(
  31. name='John',
  32. phone='010101010101',
  33. contrib_type=Contrib.CONTRIB_CONNECT,
  34. )
  35. self.assertEqual(c.get_public_field('phone'), None)
  36. def test_public_field(self):
  37. c = Contrib.objects.create(
  38. name='John',
  39. phone='010101010101',
  40. contrib_type=Contrib.CONTRIB_CONNECT,
  41. privacy_name=True,
  42. )
  43. self.assertEqual(c.get_public_field('name'), 'John')
  44. def test_public_callable_field(self):
  45. c = Contrib.objects.create(
  46. name='John',
  47. phone='010101010101',
  48. orientations=['N'],
  49. contrib_type=Contrib.CONTRIB_CONNECT,
  50. privacy_name=True,
  51. )
  52. self.assertEqual(c.get_public_field('angles'), [[-23, 22]])
  53. def test_private_field(self):
  54. c = Contrib.objects.create(
  55. name='John',
  56. phone='010101010101',
  57. contrib_type=Contrib.CONTRIB_CONNECT,
  58. )
  59. self.assertEqual(c.privacy_name, False)
  60. self.assertEqual(c.get_public_field('name'), None)
  61. class TestViews(APITestCase):
  62. def test_public_json(self):
  63. response = self.client.json_get('/map/public.json')
  64. self.assertEqual(response.status_code, 200)
  65. self.assertEqual(len(response.data['features']), 0)
  66. Contrib.objects.create(
  67. name='John',
  68. phone='010101010101',
  69. contrib_type=Contrib.CONTRIB_CONNECT,
  70. privacy_coordinates=False,
  71. )
  72. response = self.client.json_get('/map/public.json')
  73. self.assertEqual(response.status_code, 200)
  74. self.assertEqual(len(response.data['features']), 1)
  75. def test_private_json(self):
  76. self.client.force_login(
  77. User.objects.create(username='foo', is_staff=False))
  78. response = self.client.get('/map/private.json')
  79. self.assertEqual(response.status_code, 403)
  80. def test_private_json_staff(self):
  81. self.client.force_login(
  82. User.objects.create(username='foo', is_staff=True))
  83. response = self.client.get('/map/private.json')
  84. self.assertEqual(response.status_code, 200)
  85. @override_settings(NOTIFICATION_EMAILS=['foo@example.com'])
  86. def test_add_contrib_sends_email(self):
  87. response = self.client.post('/map/contribute', {
  88. 'roof': True,
  89. 'privacy_place_details': True,
  90. 'privacy_coordinates': True,
  91. 'phone': '0202020202',
  92. 'orientations': 'N',
  93. 'orientations': 'NO',
  94. 'orientations': 'O',
  95. 'orientations': 'SO',
  96. 'orientations': 'S',
  97. 'orientations': 'SE',
  98. 'orientations': 'E',
  99. 'orientations': 'NE',
  100. 'orientation': 'all',
  101. 'name': 'JohnCleese',
  102. 'longitude': -1.553621,
  103. 'latitude': 47.218371,
  104. 'floor_total': '2',
  105. 'floor': 1,
  106. 'email': 'coucou@example.com',
  107. 'contrib_type': 'connect',
  108. 'connect_local': 'on',
  109. })
  110. self.assertEqual(response.status_code, 302)
  111. self.assertEqual(len(mail.outbox), 1)
  112. self.assertIn('JohnCleese', mail.outbox[0].subject)
  113. self.assertIn('JohnCleese', mail.outbox[0].body)
  114. class TestForms(TestCase):
  115. valid_data = {
  116. 'roof': True,
  117. 'privacy_place_details': True,
  118. 'privacy_coordinates': True,
  119. 'orientations': ['N'],
  120. 'orientation': 'all',
  121. 'name': 'JohnCleese',
  122. 'longitude': -1.553621,
  123. 'email': 'foo@example.com',
  124. 'phone': '0202020202',
  125. 'latitude': 47.218371,
  126. 'floor_total': '2',
  127. 'floor': 1,
  128. 'contrib_type': 'connect',
  129. 'connect_local': 'on',
  130. }
  131. def test_contact_validation(self):
  132. no_contact, phone_contact, email_contact, both_contact = [
  133. self.valid_data.copy() for i in range(4)]
  134. del phone_contact['email']
  135. del email_contact['phone']
  136. del no_contact['phone']
  137. del no_contact['email']
  138. both_contact.update(phone_contact)
  139. both_contact.update(email_contact)
  140. self.assertFalse(PublicContribForm(no_contact).is_valid())
  141. self.assertTrue(PublicContribForm(phone_contact).is_valid())
  142. self.assertTrue(PublicContribForm(email_contact).is_valid())
  143. self.assertTrue(PublicContribForm(both_contact).is_valid())
  144. class TestDataImport(TestCase):
  145. fixtures = ['bottle_data.yaml']
  146. def test_re_save(self):
  147. for contrib in Contrib.objects.all():
  148. contrib.full_clean()
  149. contrib.save()