test_api.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. from django.urls import reverse
  2. from circuits.choices import *
  3. from circuits.models import *
  4. from dcim.models import Site
  5. from ipam.models import ASN, RIR
  6. from utilities.testing import APITestCase, APIViewTestCases
  7. class AppTest(APITestCase):
  8. def test_root(self):
  9. url = reverse('circuits-api:api-root')
  10. response = self.client.get('{}?format=api'.format(url), **self.header)
  11. self.assertEqual(response.status_code, 200)
  12. class ProviderTest(APIViewTestCases.APIViewTestCase):
  13. model = Provider
  14. brief_fields = ['circuit_count', 'description', 'display', 'id', 'name', 'slug', 'url']
  15. bulk_update_data = {
  16. 'comments': 'New comments',
  17. }
  18. @classmethod
  19. def setUpTestData(cls):
  20. rir = RIR.objects.create(name='RFC 6996', is_private=True)
  21. asns = [
  22. ASN(asn=65000 + i, rir=rir) for i in range(8)
  23. ]
  24. ASN.objects.bulk_create(asns)
  25. providers = (
  26. Provider(name='Provider 1', slug='provider-1'),
  27. Provider(name='Provider 2', slug='provider-2'),
  28. Provider(name='Provider 3', slug='provider-3'),
  29. )
  30. Provider.objects.bulk_create(providers)
  31. cls.create_data = [
  32. {
  33. 'name': 'Provider 4',
  34. 'slug': 'provider-4',
  35. 'asns': [asns[0].pk, asns[1].pk],
  36. },
  37. {
  38. 'name': 'Provider 5',
  39. 'slug': 'provider-5',
  40. 'asns': [asns[2].pk, asns[3].pk],
  41. },
  42. {
  43. 'name': 'Provider 6',
  44. 'slug': 'provider-6',
  45. 'asns': [asns[4].pk, asns[5].pk],
  46. },
  47. ]
  48. class CircuitTypeTest(APIViewTestCases.APIViewTestCase):
  49. model = CircuitType
  50. brief_fields = ['circuit_count', 'description', 'display', 'id', 'name', 'slug', 'url']
  51. create_data = (
  52. {
  53. 'name': 'Circuit Type 4',
  54. 'slug': 'circuit-type-4',
  55. },
  56. {
  57. 'name': 'Circuit Type 5',
  58. 'slug': 'circuit-type-5',
  59. },
  60. {
  61. 'name': 'Circuit Type 6',
  62. 'slug': 'circuit-type-6',
  63. },
  64. )
  65. bulk_update_data = {
  66. 'description': 'New description',
  67. }
  68. @classmethod
  69. def setUpTestData(cls):
  70. circuit_types = (
  71. CircuitType(name='Circuit Type 1', slug='circuit-type-1'),
  72. CircuitType(name='Circuit Type 2', slug='circuit-type-2'),
  73. CircuitType(name='Circuit Type 3', slug='circuit-type-3'),
  74. )
  75. CircuitType.objects.bulk_create(circuit_types)
  76. class CircuitTest(APIViewTestCases.APIViewTestCase):
  77. model = Circuit
  78. brief_fields = ['cid', 'description', 'display', 'id', 'url']
  79. bulk_update_data = {
  80. 'status': 'planned',
  81. }
  82. @classmethod
  83. def setUpTestData(cls):
  84. providers = (
  85. Provider(name='Provider 1', slug='provider-1'),
  86. Provider(name='Provider 2', slug='provider-2'),
  87. )
  88. Provider.objects.bulk_create(providers)
  89. provider_accounts = (
  90. ProviderAccount(name='Provider Account 1', provider=providers[0], account='1234'),
  91. ProviderAccount(name='Provider Account 2', provider=providers[1], account='2345'),
  92. )
  93. ProviderAccount.objects.bulk_create(provider_accounts)
  94. circuit_types = (
  95. CircuitType(name='Circuit Type 1', slug='circuit-type-1'),
  96. CircuitType(name='Circuit Type 2', slug='circuit-type-2'),
  97. )
  98. CircuitType.objects.bulk_create(circuit_types)
  99. circuits = (
  100. Circuit(cid='Circuit 1', provider=providers[0], provider_account=provider_accounts[0], type=circuit_types[0]),
  101. Circuit(cid='Circuit 2', provider=providers[0], provider_account=provider_accounts[0], type=circuit_types[0]),
  102. Circuit(cid='Circuit 3', provider=providers[0], provider_account=provider_accounts[0], type=circuit_types[0]),
  103. )
  104. Circuit.objects.bulk_create(circuits)
  105. cls.create_data = [
  106. {
  107. 'cid': 'Circuit 4',
  108. 'provider': providers[1].pk,
  109. 'provider_account': provider_accounts[1].pk,
  110. 'type': circuit_types[1].pk,
  111. },
  112. {
  113. 'cid': 'Circuit 5',
  114. 'provider': providers[1].pk,
  115. 'provider_account': provider_accounts[1].pk,
  116. 'type': circuit_types[1].pk,
  117. },
  118. {
  119. 'cid': 'Circuit 6',
  120. 'provider': providers[1].pk,
  121. # Omit provider account to test uniqueness constraint
  122. 'type': circuit_types[1].pk,
  123. },
  124. ]
  125. class CircuitTerminationTest(APIViewTestCases.APIViewTestCase):
  126. model = CircuitTermination
  127. brief_fields = ['_occupied', 'cable', 'circuit', 'description', 'display', 'id', 'term_side', 'url']
  128. @classmethod
  129. def setUpTestData(cls):
  130. SIDE_A = CircuitTerminationSideChoices.SIDE_A
  131. SIDE_Z = CircuitTerminationSideChoices.SIDE_Z
  132. provider = Provider.objects.create(name='Provider 1', slug='provider-1')
  133. circuit_type = CircuitType.objects.create(name='Circuit Type 1', slug='circuit-type-1')
  134. sites = (
  135. Site(name='Site 1', slug='site-1'),
  136. Site(name='Site 2', slug='site-2'),
  137. )
  138. Site.objects.bulk_create(sites)
  139. provider_networks = (
  140. ProviderNetwork(provider=provider, name='Provider Network 1'),
  141. ProviderNetwork(provider=provider, name='Provider Network 2'),
  142. )
  143. ProviderNetwork.objects.bulk_create(provider_networks)
  144. circuits = (
  145. Circuit(cid='Circuit 1', provider=provider, type=circuit_type),
  146. Circuit(cid='Circuit 2', provider=provider, type=circuit_type),
  147. Circuit(cid='Circuit 3', provider=provider, type=circuit_type),
  148. )
  149. Circuit.objects.bulk_create(circuits)
  150. circuit_terminations = (
  151. CircuitTermination(circuit=circuits[0], term_side=SIDE_A, site=sites[0]),
  152. CircuitTermination(circuit=circuits[0], term_side=SIDE_Z, provider_network=provider_networks[0]),
  153. CircuitTermination(circuit=circuits[1], term_side=SIDE_A, site=sites[1]),
  154. CircuitTermination(circuit=circuits[1], term_side=SIDE_Z, provider_network=provider_networks[1]),
  155. )
  156. CircuitTermination.objects.bulk_create(circuit_terminations)
  157. cls.create_data = [
  158. {
  159. 'circuit': circuits[2].pk,
  160. 'term_side': SIDE_A,
  161. 'site': sites[0].pk,
  162. 'port_speed': 200000,
  163. },
  164. {
  165. 'circuit': circuits[2].pk,
  166. 'term_side': SIDE_Z,
  167. 'provider_network': provider_networks[0].pk,
  168. 'port_speed': 200000,
  169. },
  170. ]
  171. cls.bulk_update_data = {
  172. 'port_speed': 123456
  173. }
  174. class ProviderAccountTest(APIViewTestCases.APIViewTestCase):
  175. model = ProviderAccount
  176. brief_fields = ['account', 'description', 'display', 'id', 'name', 'url']
  177. @classmethod
  178. def setUpTestData(cls):
  179. providers = (
  180. Provider(name='Provider 1', slug='provider-1'),
  181. Provider(name='Provider 2', slug='provider-2'),
  182. )
  183. Provider.objects.bulk_create(providers)
  184. provider_accounts = (
  185. ProviderAccount(name='Provider Account 1', provider=providers[0], account='1234'),
  186. ProviderAccount(name='Provider Account 2', provider=providers[0], account='2345'),
  187. ProviderAccount(name='Provider Account 3', provider=providers[0], account='3456'),
  188. )
  189. ProviderAccount.objects.bulk_create(provider_accounts)
  190. cls.create_data = [
  191. {
  192. 'name': 'Provider Account 4',
  193. 'provider': providers[0].pk,
  194. 'account': '4567',
  195. },
  196. {
  197. 'name': 'Provider Account 5',
  198. 'provider': providers[0].pk,
  199. 'account': '5678',
  200. },
  201. {
  202. # Omit name to test uniqueness constraint
  203. 'provider': providers[0].pk,
  204. 'account': '6789',
  205. },
  206. ]
  207. cls.bulk_update_data = {
  208. 'provider': providers[1].pk,
  209. 'description': 'New description',
  210. }
  211. class ProviderNetworkTest(APIViewTestCases.APIViewTestCase):
  212. model = ProviderNetwork
  213. brief_fields = ['description', 'display', 'id', 'name', 'url']
  214. @classmethod
  215. def setUpTestData(cls):
  216. providers = (
  217. Provider(name='Provider 1', slug='provider-1'),
  218. Provider(name='Provider 2', slug='provider-2'),
  219. )
  220. Provider.objects.bulk_create(providers)
  221. provider_networks = (
  222. ProviderNetwork(name='Provider Network 1', provider=providers[0]),
  223. ProviderNetwork(name='Provider Network 2', provider=providers[0]),
  224. ProviderNetwork(name='Provider Network 3', provider=providers[0]),
  225. )
  226. ProviderNetwork.objects.bulk_create(provider_networks)
  227. cls.create_data = [
  228. {
  229. 'name': 'Provider Network 4',
  230. 'provider': providers[0].pk,
  231. },
  232. {
  233. 'name': 'Provider Network 5',
  234. 'provider': providers[0].pk,
  235. },
  236. {
  237. 'name': 'Provider Network 6',
  238. 'provider': providers[0].pk,
  239. },
  240. ]
  241. cls.bulk_update_data = {
  242. 'provider': providers[1].pk,
  243. 'description': 'New description',
  244. }