test_api.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. from django.urls import reverse
  2. from dcim.models import Site
  3. from tenancy.choices import *
  4. from tenancy.models import *
  5. from utilities.testing import APITestCase, APIViewTestCases
  6. class AppTest(APITestCase):
  7. def test_root(self):
  8. url = reverse('tenancy-api:api-root')
  9. response = self.client.get('{}?format=api'.format(url), **self.header)
  10. self.assertEqual(response.status_code, 200)
  11. class TenantGroupTest(APIViewTestCases.APIViewTestCase):
  12. model = TenantGroup
  13. brief_fields = ['_depth', 'description', 'display', 'id', 'name', 'slug', 'tenant_count', 'url']
  14. bulk_update_data = {
  15. 'description': 'New description',
  16. }
  17. @classmethod
  18. def setUpTestData(cls):
  19. parent_tenant_groups = (
  20. TenantGroup.objects.create(name='Parent Tenant Group 1', slug='parent-tenant-group-1'),
  21. TenantGroup.objects.create(name='Parent Tenant Group 2', slug='parent-tenant-group-2'),
  22. )
  23. TenantGroup.objects.create(name='Tenant Group 1', slug='tenant-group-1', parent=parent_tenant_groups[0])
  24. TenantGroup.objects.create(name='Tenant Group 2', slug='tenant-group-2', parent=parent_tenant_groups[0])
  25. TenantGroup.objects.create(name='Tenant Group 3', slug='tenant-group-3', parent=parent_tenant_groups[0])
  26. cls.create_data = [
  27. {
  28. 'name': 'Tenant Group 4',
  29. 'slug': 'tenant-group-4',
  30. 'parent': parent_tenant_groups[1].pk,
  31. },
  32. {
  33. 'name': 'Tenant Group 5',
  34. 'slug': 'tenant-group-5',
  35. 'parent': parent_tenant_groups[1].pk,
  36. },
  37. {
  38. 'name': 'Tenant Group 6',
  39. 'slug': 'tenant-group-6',
  40. 'parent': parent_tenant_groups[1].pk,
  41. },
  42. ]
  43. class TenantTest(APIViewTestCases.APIViewTestCase):
  44. model = Tenant
  45. brief_fields = ['description', 'display', 'id', 'name', 'slug', 'url']
  46. bulk_update_data = {
  47. 'group': None,
  48. 'description': 'New description',
  49. }
  50. @classmethod
  51. def setUpTestData(cls):
  52. tenant_groups = (
  53. TenantGroup.objects.create(name='Tenant Group 1', slug='tenant-group-1'),
  54. TenantGroup.objects.create(name='Tenant Group 2', slug='tenant-group-2'),
  55. )
  56. tenants = (
  57. Tenant(name='Tenant 1', slug='tenant-1', group=tenant_groups[0]),
  58. Tenant(name='Tenant 2', slug='tenant-2', group=tenant_groups[0]),
  59. Tenant(name='Tenant 3', slug='tenant-3', group=tenant_groups[0]),
  60. )
  61. Tenant.objects.bulk_create(tenants)
  62. cls.create_data = [
  63. {
  64. 'name': 'Tenant 4',
  65. 'slug': 'tenant-4',
  66. 'group': tenant_groups[1].pk,
  67. },
  68. {
  69. 'name': 'Tenant 5',
  70. 'slug': 'tenant-5',
  71. 'group': tenant_groups[1].pk,
  72. },
  73. {
  74. 'name': 'Tenant 6',
  75. 'slug': 'tenant-6',
  76. 'group': tenant_groups[1].pk,
  77. },
  78. ]
  79. class ContactGroupTest(APIViewTestCases.APIViewTestCase):
  80. model = ContactGroup
  81. brief_fields = ['_depth', 'contact_count', 'description', 'display', 'id', 'name', 'slug', 'url']
  82. bulk_update_data = {
  83. 'description': 'New description',
  84. }
  85. @classmethod
  86. def setUpTestData(cls):
  87. parent_contact_groups = (
  88. ContactGroup.objects.create(name='Parent Contact Group 1', slug='parent-contact-group-1'),
  89. ContactGroup.objects.create(name='Parent Contact Group 2', slug='parent-contact-group-2'),
  90. )
  91. ContactGroup.objects.create(name='Contact Group 1', slug='contact-group-1', parent=parent_contact_groups[0])
  92. ContactGroup.objects.create(name='Contact Group 2', slug='contact-group-2', parent=parent_contact_groups[0])
  93. ContactGroup.objects.create(name='Contact Group 3', slug='contact-group-3', parent=parent_contact_groups[0])
  94. cls.create_data = [
  95. {
  96. 'name': 'Contact Group 4',
  97. 'slug': 'contact-group-4',
  98. 'parent': parent_contact_groups[1].pk,
  99. },
  100. {
  101. 'name': 'Contact Group 5',
  102. 'slug': 'contact-group-5',
  103. 'parent': parent_contact_groups[1].pk,
  104. },
  105. {
  106. 'name': 'Contact Group 6',
  107. 'slug': 'contact-group-6',
  108. 'parent': parent_contact_groups[1].pk,
  109. },
  110. ]
  111. class ContactRoleTest(APIViewTestCases.APIViewTestCase):
  112. model = ContactRole
  113. brief_fields = ['description', 'display', 'id', 'name', 'slug', 'url']
  114. create_data = [
  115. {
  116. 'name': 'Contact Role 4',
  117. 'slug': 'contact-role-4',
  118. },
  119. {
  120. 'name': 'Contact Role 5',
  121. 'slug': 'contact-role-5',
  122. },
  123. {
  124. 'name': 'Contact Role 6',
  125. 'slug': 'contact-role-6',
  126. },
  127. ]
  128. bulk_update_data = {
  129. 'description': 'New description',
  130. }
  131. @classmethod
  132. def setUpTestData(cls):
  133. contact_roles = (
  134. ContactRole(name='Contact Role 1', slug='contact-role-1'),
  135. ContactRole(name='Contact Role 2', slug='contact-role-2'),
  136. ContactRole(name='Contact Role 3', slug='contact-role-3'),
  137. )
  138. ContactRole.objects.bulk_create(contact_roles)
  139. class ContactTest(APIViewTestCases.APIViewTestCase):
  140. model = Contact
  141. brief_fields = ['description', 'display', 'id', 'name', 'url']
  142. bulk_update_data = {
  143. 'groups': [],
  144. 'comments': 'New comments',
  145. }
  146. @classmethod
  147. def setUpTestData(cls):
  148. contact_groups = (
  149. ContactGroup.objects.create(name='Contact Group 1', slug='contact-group-1'),
  150. ContactGroup.objects.create(name='Contact Group 2', slug='contact-group-2'),
  151. )
  152. contacts = (
  153. Contact(name='Contact 1'),
  154. Contact(name='Contact 2'),
  155. Contact(name='Contact 3'),
  156. )
  157. Contact.objects.bulk_create(contacts)
  158. contacts[0].groups.add(contact_groups[0])
  159. contacts[1].groups.add(contact_groups[0])
  160. contacts[2].groups.add(contact_groups[0])
  161. cls.create_data = [
  162. {
  163. 'name': 'Contact 4',
  164. 'groups': [contact_groups[1].pk],
  165. },
  166. {
  167. 'name': 'Contact 5',
  168. },
  169. {
  170. 'name': 'Contact 6',
  171. },
  172. ]
  173. class ContactAssignmentTest(APIViewTestCases.APIViewTestCase):
  174. model = ContactAssignment
  175. brief_fields = ['contact', 'display', 'id', 'priority', 'role', 'url']
  176. bulk_update_data = {
  177. 'priority': ContactPriorityChoices.PRIORITY_INACTIVE,
  178. }
  179. user_permissions = ('tenancy.view_contact', )
  180. @classmethod
  181. def setUpTestData(cls):
  182. sites = (
  183. Site(name='Site 1', slug='site-1'),
  184. Site(name='Site 2', slug='site-2'),
  185. )
  186. Site.objects.bulk_create(sites)
  187. contacts = (
  188. Contact(name='Contact 1'),
  189. Contact(name='Contact 2'),
  190. Contact(name='Contact 3'),
  191. Contact(name='Contact 4'),
  192. Contact(name='Contact 5'),
  193. Contact(name='Contact 6'),
  194. )
  195. Contact.objects.bulk_create(contacts)
  196. contact_roles = (
  197. ContactRole(name='Contact Role 1', slug='contact-role-1'),
  198. ContactRole(name='Contact Role 2', slug='contact-role-2'),
  199. ContactRole(name='Contact Role 3', slug='contact-role-3'),
  200. )
  201. ContactRole.objects.bulk_create(contact_roles)
  202. contact_assignments = (
  203. ContactAssignment(
  204. object=sites[0],
  205. contact=contacts[0],
  206. role=contact_roles[0],
  207. priority=ContactPriorityChoices.PRIORITY_PRIMARY,
  208. ),
  209. ContactAssignment(
  210. object=sites[0],
  211. contact=contacts[1],
  212. role=contact_roles[1],
  213. priority=ContactPriorityChoices.PRIORITY_SECONDARY,
  214. ),
  215. ContactAssignment(
  216. object=sites[0],
  217. contact=contacts[2],
  218. role=contact_roles[2],
  219. priority=ContactPriorityChoices.PRIORITY_TERTIARY,
  220. ),
  221. )
  222. ContactAssignment.objects.bulk_create(contact_assignments)
  223. cls.create_data = [
  224. {
  225. 'object_type': 'dcim.site',
  226. 'object_id': sites[1].pk,
  227. 'contact': contacts[3].pk,
  228. 'role': contact_roles[0].pk,
  229. 'priority': ContactPriorityChoices.PRIORITY_PRIMARY,
  230. },
  231. {
  232. 'object_type': 'dcim.site',
  233. 'object_id': sites[1].pk,
  234. 'contact': contacts[4].pk,
  235. 'role': contact_roles[1].pk,
  236. 'priority': ContactPriorityChoices.PRIORITY_SECONDARY,
  237. },
  238. {
  239. 'object_type': 'dcim.site',
  240. 'object_id': sites[1].pk,
  241. 'contact': contacts[5].pk,
  242. 'role': contact_roles[2].pk,
  243. 'priority': ContactPriorityChoices.PRIORITY_TERTIARY,
  244. },
  245. ]