tests.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. contrib_type=Contrib.CONTRIB_CONNECT)
  20. co.save()
  21. self.assertEqual(
  22. Contrib.objects.get(name='foo').orientations,
  23. ['SO', 'NE'])
  24. co.orientations = ['S']
  25. co.save()
  26. class TestContribPrivacy(TestCase):
  27. def test_always_private_field(self):
  28. c = Contrib.objects.create(
  29. name='John',
  30. phone='010101010101',
  31. contrib_type=Contrib.CONTRIB_CONNECT,
  32. )
  33. self.assertEqual(c.get_public_field('phone'), None)
  34. def test_public_field(self):
  35. c = Contrib.objects.create(
  36. name='John',
  37. phone='010101010101',
  38. contrib_type=Contrib.CONTRIB_CONNECT,
  39. privacy_name=True,
  40. )
  41. self.assertEqual(c.get_public_field('name'), 'John')
  42. def test_public_callable_field(self):
  43. c = Contrib.objects.create(
  44. name='John',
  45. phone='010101010101',
  46. orientations=['N'],
  47. contrib_type=Contrib.CONTRIB_CONNECT,
  48. privacy_name=True,
  49. )
  50. self.assertEqual(c.get_public_field('angles'), [[-23, 22]])
  51. def test_private_field(self):
  52. c = Contrib.objects.create(
  53. name='John',
  54. phone='010101010101',
  55. contrib_type=Contrib.CONTRIB_CONNECT,
  56. )
  57. self.assertEqual(c.privacy_name, False)
  58. self.assertEqual(c.get_public_field('name'), None)
  59. class TestViews(APITestCase):
  60. def test_public_json(self):
  61. response = self.client.json_get('/map/public.json')
  62. self.assertEqual(response.status_code, 200)
  63. self.assertEqual(len(response.data['features']), 0)
  64. Contrib.objects.create(
  65. name='John',
  66. phone='010101010101',
  67. contrib_type=Contrib.CONTRIB_CONNECT,
  68. privacy_coordinates=False,
  69. )
  70. response = self.client.json_get('/map/public.json')
  71. self.assertEqual(response.status_code, 200)
  72. self.assertEqual(len(response.data['features']), 1)
  73. def test_private_json(self):
  74. self.client.force_login(
  75. User.objects.create(username='foo', is_staff=False))
  76. response = self.client.get('/map/private.json')
  77. self.assertEqual(response.status_code, 403)
  78. def test_private_json_staff(self):
  79. self.client.force_login(
  80. User.objects.create(username='foo', is_staff=True))
  81. response = self.client.get('/map/private.json')
  82. self.assertEqual(response.status_code, 200)
  83. class TestDataImport(TestCase):
  84. fixtures = ['bottle_data.yaml']
  85. def test_re_save(self):
  86. for contrib in Contrib.objects.all():
  87. contrib.full_clean()
  88. contrib.save()