test_customfields.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. from __future__ import unicode_literals
  2. from datetime import date
  3. from django.contrib.auth.models import User
  4. from django.contrib.contenttypes.models import ContentType
  5. from django.test import TestCase
  6. from django.urls import reverse
  7. from rest_framework import status
  8. from rest_framework.test import APITestCase
  9. from dcim.models import Site
  10. from extras.constants import CF_TYPE_TEXT, CF_TYPE_INTEGER, CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_SELECT, CF_TYPE_URL
  11. from extras.models import CustomField, CustomFieldValue, CustomFieldChoice
  12. from users.models import Token
  13. from utilities.tests import HttpStatusMixin
  14. class CustomFieldTest(TestCase):
  15. def setUp(self):
  16. Site.objects.bulk_create([
  17. Site(name='Site A', slug='site-a'),
  18. Site(name='Site B', slug='site-b'),
  19. Site(name='Site C', slug='site-c'),
  20. ])
  21. def test_simple_fields(self):
  22. DATA = (
  23. {'field_type': CF_TYPE_TEXT, 'field_value': 'Foobar!', 'empty_value': ''},
  24. {'field_type': CF_TYPE_INTEGER, 'field_value': 0, 'empty_value': None},
  25. {'field_type': CF_TYPE_INTEGER, 'field_value': 42, 'empty_value': None},
  26. {'field_type': CF_TYPE_BOOLEAN, 'field_value': True, 'empty_value': None},
  27. {'field_type': CF_TYPE_BOOLEAN, 'field_value': False, 'empty_value': None},
  28. {'field_type': CF_TYPE_DATE, 'field_value': date(2016, 6, 23), 'empty_value': None},
  29. {'field_type': CF_TYPE_URL, 'field_value': 'http://example.com/', 'empty_value': ''},
  30. )
  31. obj_type = ContentType.objects.get_for_model(Site)
  32. for data in DATA:
  33. # Create a custom field
  34. cf = CustomField(type=data['field_type'], name='my_field', required=False)
  35. cf.save()
  36. cf.obj_type.set([obj_type])
  37. cf.save()
  38. # Assign a value to the first Site
  39. site = Site.objects.first()
  40. cfv = CustomFieldValue(field=cf, obj_type=obj_type, obj_id=site.id)
  41. cfv.value = data['field_value']
  42. cfv.save()
  43. # Retrieve the stored value
  44. cfv = CustomFieldValue.objects.filter(obj_type=obj_type, obj_id=site.pk).first()
  45. self.assertEqual(cfv.value, data['field_value'])
  46. # Delete the stored value
  47. cfv.value = data['empty_value']
  48. cfv.save()
  49. self.assertEqual(CustomFieldValue.objects.filter(obj_type=obj_type, obj_id=site.pk).count(), 0)
  50. # Delete the custom field
  51. cf.delete()
  52. def test_select_field(self):
  53. obj_type = ContentType.objects.get_for_model(Site)
  54. # Create a custom field
  55. cf = CustomField(type=CF_TYPE_SELECT, name='my_field', required=False)
  56. cf.save()
  57. cf.obj_type.set([obj_type])
  58. cf.save()
  59. # Create some choices for the field
  60. CustomFieldChoice.objects.bulk_create([
  61. CustomFieldChoice(field=cf, value='Option A'),
  62. CustomFieldChoice(field=cf, value='Option B'),
  63. CustomFieldChoice(field=cf, value='Option C'),
  64. ])
  65. # Assign a value to the first Site
  66. site = Site.objects.first()
  67. cfv = CustomFieldValue(field=cf, obj_type=obj_type, obj_id=site.id)
  68. cfv.value = cf.choices.first()
  69. cfv.save()
  70. # Retrieve the stored value
  71. cfv = CustomFieldValue.objects.filter(obj_type=obj_type, obj_id=site.pk).first()
  72. self.assertEqual(str(cfv.value), 'Option A')
  73. # Delete the stored value
  74. cfv.value = None
  75. cfv.save()
  76. self.assertEqual(CustomFieldValue.objects.filter(obj_type=obj_type, obj_id=site.pk).count(), 0)
  77. # Delete the custom field
  78. cf.delete()
  79. class CustomFieldAPITest(HttpStatusMixin, APITestCase):
  80. def setUp(self):
  81. user = User.objects.create(username='testuser', is_superuser=True)
  82. token = Token.objects.create(user=user)
  83. self.header = {'HTTP_AUTHORIZATION': 'Token {}'.format(token.key)}
  84. content_type = ContentType.objects.get_for_model(Site)
  85. # Text custom field
  86. self.cf_text = CustomField(type=CF_TYPE_TEXT, name='magic_word')
  87. self.cf_text.save()
  88. self.cf_text.obj_type.set([content_type])
  89. self.cf_text.save()
  90. # Integer custom field
  91. self.cf_integer = CustomField(type=CF_TYPE_INTEGER, name='magic_number')
  92. self.cf_integer.save()
  93. self.cf_integer.obj_type.set([content_type])
  94. self.cf_integer.save()
  95. # Boolean custom field
  96. self.cf_boolean = CustomField(type=CF_TYPE_BOOLEAN, name='is_magic')
  97. self.cf_boolean.save()
  98. self.cf_boolean.obj_type.set([content_type])
  99. self.cf_boolean.save()
  100. # Date custom field
  101. self.cf_date = CustomField(type=CF_TYPE_DATE, name='magic_date')
  102. self.cf_date.save()
  103. self.cf_date.obj_type.set([content_type])
  104. self.cf_date.save()
  105. # URL custom field
  106. self.cf_url = CustomField(type=CF_TYPE_URL, name='magic_url')
  107. self.cf_url.save()
  108. self.cf_url.obj_type.set([content_type])
  109. self.cf_url.save()
  110. # Select custom field
  111. self.cf_select = CustomField(type=CF_TYPE_SELECT, name='magic_choice')
  112. self.cf_select.save()
  113. self.cf_select.obj_type.set([content_type])
  114. self.cf_select.save()
  115. self.cf_select_choice1 = CustomFieldChoice(field=self.cf_select, value='Foo')
  116. self.cf_select_choice1.save()
  117. self.cf_select_choice2 = CustomFieldChoice(field=self.cf_select, value='Bar')
  118. self.cf_select_choice2.save()
  119. self.cf_select_choice3 = CustomFieldChoice(field=self.cf_select, value='Baz')
  120. self.cf_select_choice3.save()
  121. self.site = Site.objects.create(name='Test Site 1', slug='test-site-1')
  122. def test_get_obj_without_custom_fields(self):
  123. url = reverse('dcim-api:site-detail', kwargs={'pk': self.site.pk})
  124. response = self.client.get(url, **self.header)
  125. self.assertEqual(response.data['name'], self.site.name)
  126. self.assertEqual(response.data['custom_fields'], {
  127. 'magic_word': None,
  128. 'magic_number': None,
  129. 'is_magic': None,
  130. 'magic_date': None,
  131. 'magic_url': None,
  132. 'magic_choice': None,
  133. })
  134. def test_get_obj_with_custom_fields(self):
  135. CUSTOM_FIELD_VALUES = [
  136. (self.cf_text, 'Test string'),
  137. (self.cf_integer, 1234),
  138. (self.cf_boolean, True),
  139. (self.cf_date, date(2016, 6, 23)),
  140. (self.cf_url, 'http://example.com/'),
  141. (self.cf_select, self.cf_select_choice1.pk),
  142. ]
  143. for field, value in CUSTOM_FIELD_VALUES:
  144. cfv = CustomFieldValue(field=field, obj=self.site)
  145. cfv.value = value
  146. cfv.save()
  147. url = reverse('dcim-api:site-detail', kwargs={'pk': self.site.pk})
  148. response = self.client.get(url, **self.header)
  149. self.assertEqual(response.data['name'], self.site.name)
  150. self.assertEqual(response.data['custom_fields'].get('magic_word'), CUSTOM_FIELD_VALUES[0][1])
  151. self.assertEqual(response.data['custom_fields'].get('magic_number'), CUSTOM_FIELD_VALUES[1][1])
  152. self.assertEqual(response.data['custom_fields'].get('is_magic'), CUSTOM_FIELD_VALUES[2][1])
  153. self.assertEqual(response.data['custom_fields'].get('magic_date'), CUSTOM_FIELD_VALUES[3][1])
  154. self.assertEqual(response.data['custom_fields'].get('magic_url'), CUSTOM_FIELD_VALUES[4][1])
  155. self.assertEqual(response.data['custom_fields'].get('magic_choice'), {
  156. 'value': self.cf_select_choice1.pk, 'label': 'Foo'
  157. })
  158. def test_set_custom_field_text(self):
  159. data = {
  160. 'name': 'Test Site 1',
  161. 'slug': 'test-site-1',
  162. 'custom_fields': {
  163. 'magic_word': 'Foo bar baz',
  164. }
  165. }
  166. url = reverse('dcim-api:site-detail', kwargs={'pk': self.site.pk})
  167. response = self.client.put(url, data, format='json', **self.header)
  168. self.assertHttpStatus(response, status.HTTP_200_OK)
  169. self.assertEqual(response.data['custom_fields'].get('magic_word'), data['custom_fields']['magic_word'])
  170. cfv = self.site.custom_field_values.get(field=self.cf_text)
  171. self.assertEqual(cfv.value, data['custom_fields']['magic_word'])
  172. def test_set_custom_field_integer(self):
  173. data = {
  174. 'name': 'Test Site 1',
  175. 'slug': 'test-site-1',
  176. 'custom_fields': {
  177. 'magic_number': 42,
  178. }
  179. }
  180. url = reverse('dcim-api:site-detail', kwargs={'pk': self.site.pk})
  181. response = self.client.put(url, data, format='json', **self.header)
  182. self.assertHttpStatus(response, status.HTTP_200_OK)
  183. self.assertEqual(response.data['custom_fields'].get('magic_number'), data['custom_fields']['magic_number'])
  184. cfv = self.site.custom_field_values.get(field=self.cf_integer)
  185. self.assertEqual(cfv.value, data['custom_fields']['magic_number'])
  186. def test_set_custom_field_boolean(self):
  187. data = {
  188. 'name': 'Test Site 1',
  189. 'slug': 'test-site-1',
  190. 'custom_fields': {
  191. 'is_magic': 0,
  192. }
  193. }
  194. url = reverse('dcim-api:site-detail', kwargs={'pk': self.site.pk})
  195. response = self.client.put(url, data, format='json', **self.header)
  196. self.assertHttpStatus(response, status.HTTP_200_OK)
  197. self.assertEqual(response.data['custom_fields'].get('is_magic'), data['custom_fields']['is_magic'])
  198. cfv = self.site.custom_field_values.get(field=self.cf_boolean)
  199. self.assertEqual(cfv.value, data['custom_fields']['is_magic'])
  200. def test_set_custom_field_date(self):
  201. data = {
  202. 'name': 'Test Site 1',
  203. 'slug': 'test-site-1',
  204. 'custom_fields': {
  205. 'magic_date': '2017-04-25',
  206. }
  207. }
  208. url = reverse('dcim-api:site-detail', kwargs={'pk': self.site.pk})
  209. response = self.client.put(url, data, format='json', **self.header)
  210. self.assertHttpStatus(response, status.HTTP_200_OK)
  211. self.assertEqual(response.data['custom_fields'].get('magic_date'), data['custom_fields']['magic_date'])
  212. cfv = self.site.custom_field_values.get(field=self.cf_date)
  213. self.assertEqual(cfv.value.isoformat(), data['custom_fields']['magic_date'])
  214. def test_set_custom_field_url(self):
  215. data = {
  216. 'name': 'Test Site 1',
  217. 'slug': 'test-site-1',
  218. 'custom_fields': {
  219. 'magic_url': 'http://example.com/2/',
  220. }
  221. }
  222. url = reverse('dcim-api:site-detail', kwargs={'pk': self.site.pk})
  223. response = self.client.put(url, data, format='json', **self.header)
  224. self.assertHttpStatus(response, status.HTTP_200_OK)
  225. self.assertEqual(response.data['custom_fields'].get('magic_url'), data['custom_fields']['magic_url'])
  226. cfv = self.site.custom_field_values.get(field=self.cf_url)
  227. self.assertEqual(cfv.value, data['custom_fields']['magic_url'])
  228. def test_set_custom_field_select(self):
  229. data = {
  230. 'name': 'Test Site 1',
  231. 'slug': 'test-site-1',
  232. 'custom_fields': {
  233. 'magic_choice': self.cf_select_choice2.pk,
  234. }
  235. }
  236. url = reverse('dcim-api:site-detail', kwargs={'pk': self.site.pk})
  237. response = self.client.put(url, data, format='json', **self.header)
  238. self.assertHttpStatus(response, status.HTTP_200_OK)
  239. self.assertEqual(response.data['custom_fields'].get('magic_choice'), data['custom_fields']['magic_choice'])
  240. cfv = self.site.custom_field_values.get(field=self.cf_select)
  241. self.assertEqual(cfv.value.pk, data['custom_fields']['magic_choice'])