tests.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. class APITestClient(Client):
  7. def json_get(self, *args, **kwargs):
  8. """ Annotate the response with a .data containing parsed JSON
  9. """
  10. response = super().get(*args, **kwargs)
  11. response.data = json.loads(response.content.decode('utf-8'))
  12. return response
  13. class APITestCase(TestCase):
  14. def setUp(self):
  15. super().setUp()
  16. self.client = APITestClient()
  17. class TestContrib(TestCase):
  18. def test_comma_separatedcharfield(self):
  19. co = Contrib(name='foo', orientations=['SO', 'NE'],
  20. contrib_type=Contrib.CONTRIB_CONNECT)
  21. co.save()
  22. self.assertEqual(
  23. Contrib.objects.get(name='foo').orientations,
  24. ['SO', 'NE'])
  25. co.orientations = ['S']
  26. co.save()
  27. class TestContribPrivacy(TestCase):
  28. def test_always_private_field(self):
  29. c = Contrib.objects.create(
  30. name='John',
  31. phone='010101010101',
  32. contrib_type=Contrib.CONTRIB_CONNECT,
  33. )
  34. self.assertEqual(c.get_public_field('phone'), None)
  35. def test_public_field(self):
  36. c = Contrib.objects.create(
  37. name='John',
  38. phone='010101010101',
  39. contrib_type=Contrib.CONTRIB_CONNECT,
  40. privacy_name=True,
  41. )
  42. self.assertEqual(c.get_public_field('name'), 'John')
  43. def test_public_callable_field(self):
  44. c = Contrib.objects.create(
  45. name='John',
  46. phone='010101010101',
  47. orientations=['N'],
  48. contrib_type=Contrib.CONTRIB_CONNECT,
  49. privacy_name=True,
  50. )
  51. self.assertEqual(c.get_public_field('angles'), [[-23, 22]])
  52. def test_private_field(self):
  53. c = Contrib.objects.create(
  54. name='John',
  55. phone='010101010101',
  56. contrib_type=Contrib.CONTRIB_CONNECT,
  57. )
  58. self.assertEqual(c.privacy_name, False)
  59. self.assertEqual(c.get_public_field('name'), None)
  60. class TestViews(APITestCase):
  61. def test_public_json(self):
  62. response = self.client.json_get('/map/public.json')
  63. self.assertEqual(response.status_code, 200)
  64. self.assertEqual(len(response.data['features']), 0)
  65. Contrib.objects.create(
  66. name='John',
  67. phone='010101010101',
  68. contrib_type=Contrib.CONTRIB_CONNECT,
  69. privacy_coordinates=False,
  70. )
  71. response = self.client.json_get('/map/public.json')
  72. self.assertEqual(response.status_code, 200)
  73. self.assertEqual(len(response.data['features']), 1)
  74. def test_private_json(self):
  75. self.client.force_login(
  76. User.objects.create(username='foo', is_staff=False))
  77. response = self.client.get('/map/private.json')
  78. self.assertEqual(response.status_code, 403)
  79. def test_private_json_staff(self):
  80. self.client.force_login(
  81. User.objects.create(username='foo', is_staff=True))
  82. response = self.client.get('/map/private.json')
  83. self.assertEqual(response.status_code, 200)
  84. @override_settings(NOTIFICATION_EMAILS=['foo@example.com'])
  85. def test_add_contrib_sends_email(self):
  86. response = self.client.post('/map/contribute', {
  87. 'roof': True,
  88. 'privacy_place_details': True,
  89. 'privacy_coordinates': True,
  90. 'phone': '0202020202',
  91. 'orientations': 'N',
  92. 'orientations': 'NO',
  93. 'orientations': 'O',
  94. 'orientations': 'SO',
  95. 'orientations': 'S',
  96. 'orientations': 'SE',
  97. 'orientations': 'E',
  98. 'orientations': 'NE',
  99. 'orientation': 'all',
  100. 'name': 'JohnCleese',
  101. 'longitude': -1.553621,
  102. 'latitude': 47.218371,
  103. 'floor_total': '2',
  104. 'floor': 1,
  105. 'email': 'coucou@example.com',
  106. 'contrib_type': 'connect',
  107. 'connect_local': 'on',
  108. })
  109. self.assertEqual(response.status_code, 302)
  110. self.assertEqual(len(mail.outbox), 1)
  111. self.assertIn('JohnCleese', mail.outbox[0].subject)
  112. self.assertIn('JohnCleese', mail.outbox[0].body)
  113. class TestDataImport(TestCase):
  114. fixtures = ['bottle_data.yaml']
  115. def test_re_save(self):
  116. for contrib in Contrib.objects.all():
  117. contrib.full_clean()
  118. contrib.save()