tests.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import json
  2. from django.contrib.auth.models import User
  3. from django.test import TestCase, Client
  4. from contribmap.models import Contrib
  5. class APITestClient(Client):
  6. def json_get(self, *args, **kwargs):
  7. """ Annotate the response with a .data containing parsed JSON
  8. """
  9. response = super().get(*args, **kwargs)
  10. response.data = json.loads(response.content.decode('utf-8'))
  11. return response
  12. class APITestCase(TestCase):
  13. def setUp(self):
  14. super().setUp()
  15. self.client = APITestClient()
  16. class TestContrib(TestCase):
  17. def test_comma_separatedcharfield(self):
  18. co = Contrib(name='foo', orientations=['SO', 'NE'])
  19. co.save()
  20. self.assertEqual(
  21. Contrib.objects.get(name='foo').orientations,
  22. ['SO', 'NE'])
  23. co.orientations = ['S']
  24. co.save()
  25. class TestContribPrivacy(TestCase):
  26. def test_always_private_field(self):
  27. c = Contrib.objects.create(
  28. name='John',
  29. phone='010101010101',
  30. contrib_type=Contrib.CONTRIB_CONNECT,
  31. )
  32. self.assertEqual(c.get_public_field('phone'), None)
  33. def test_public_field(self):
  34. c = Contrib.objects.create(
  35. name='John',
  36. phone='010101010101',
  37. contrib_type=Contrib.CONTRIB_CONNECT,
  38. privacy_name=True,
  39. )
  40. self.assertEqual(c.get_public_field('name'), 'John')
  41. def test_public_callable_field(self):
  42. c = Contrib.objects.create(
  43. name='John',
  44. phone='010101010101',
  45. orientations=['N'],
  46. contrib_type=Contrib.CONTRIB_CONNECT,
  47. privacy_name=True,
  48. )
  49. self.assertEqual(c.get_public_field('angles'), [[-23, 22]])
  50. def test_private_field(self):
  51. c = Contrib.objects.create(
  52. name='John',
  53. phone='010101010101',
  54. contrib_type=Contrib.CONTRIB_CONNECT,
  55. )
  56. self.assertEqual(c.privacy_name, False)
  57. self.assertEqual(c.get_public_field('name'), None)
  58. class TestViews(APITestCase):
  59. def test_public_json(self):
  60. response = self.client.json_get('/map/public.json')
  61. self.assertEqual(response.status_code, 200)
  62. self.assertEqual(len(response.data['features']), 0)
  63. Contrib.objects.create(
  64. name='John',
  65. phone='010101010101',
  66. contrib_type=Contrib.CONTRIB_CONNECT,
  67. privacy_coordinates=False,
  68. )
  69. response = self.client.json_get('/map/public.json')
  70. self.assertEqual(response.status_code, 200)
  71. self.assertEqual(len(response.data['features']), 1)
  72. def test_private_json(self):
  73. self.client.force_login(
  74. User.objects.create(username='foo', is_staff=False))
  75. response = self.client.get('/map/private.json')
  76. self.assertEqual(response.status_code, 403)
  77. def test_private_json_staff(self):
  78. self.client.force_login(
  79. User.objects.create(username='foo', is_staff=True))
  80. response = self.client.get('/map/private.json')
  81. self.assertEqual(response.status_code, 200)
  82. class TestDataImport(TestCase):
  83. fixtures = ['bottle_data.yaml']
  84. def test_re_save(self):
  85. for contrib in Contrib.objects.all():
  86. contrib.full_clean()
  87. contrib.save()