test_api.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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', 'display', 'id', 'name', 'slug', 'url']
  15. bulk_update_data = {
  16. 'asn': 1234,
  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', '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', '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. circuit_types = (
  90. CircuitType(name='Circuit Type 1', slug='circuit-type-1'),
  91. CircuitType(name='Circuit Type 2', slug='circuit-type-2'),
  92. )
  93. CircuitType.objects.bulk_create(circuit_types)
  94. circuits = (
  95. Circuit(cid='Circuit 1', provider=providers[0], type=circuit_types[0]),
  96. Circuit(cid='Circuit 2', provider=providers[0], type=circuit_types[0]),
  97. Circuit(cid='Circuit 3', provider=providers[0], type=circuit_types[0]),
  98. )
  99. Circuit.objects.bulk_create(circuits)
  100. cls.create_data = [
  101. {
  102. 'cid': 'Circuit 4',
  103. 'provider': providers[1].pk,
  104. 'type': circuit_types[1].pk,
  105. },
  106. {
  107. 'cid': 'Circuit 5',
  108. 'provider': providers[1].pk,
  109. 'type': circuit_types[1].pk,
  110. },
  111. {
  112. 'cid': 'Circuit 6',
  113. 'provider': providers[1].pk,
  114. 'type': circuit_types[1].pk,
  115. },
  116. ]
  117. class CircuitTerminationTest(APIViewTestCases.APIViewTestCase):
  118. model = CircuitTermination
  119. brief_fields = ['_occupied', 'cable', 'circuit', 'display', 'id', 'term_side', 'url']
  120. @classmethod
  121. def setUpTestData(cls):
  122. SIDE_A = CircuitTerminationSideChoices.SIDE_A
  123. SIDE_Z = CircuitTerminationSideChoices.SIDE_Z
  124. provider = Provider.objects.create(name='Provider 1', slug='provider-1')
  125. circuit_type = CircuitType.objects.create(name='Circuit Type 1', slug='circuit-type-1')
  126. sites = (
  127. Site(name='Site 1', slug='site-1'),
  128. Site(name='Site 2', slug='site-2'),
  129. )
  130. Site.objects.bulk_create(sites)
  131. provider_networks = (
  132. ProviderNetwork(provider=provider, name='Provider Network 1'),
  133. ProviderNetwork(provider=provider, name='Provider Network 2'),
  134. )
  135. ProviderNetwork.objects.bulk_create(provider_networks)
  136. circuits = (
  137. Circuit(cid='Circuit 1', provider=provider, type=circuit_type),
  138. Circuit(cid='Circuit 2', provider=provider, type=circuit_type),
  139. Circuit(cid='Circuit 3', provider=provider, type=circuit_type),
  140. )
  141. Circuit.objects.bulk_create(circuits)
  142. circuit_terminations = (
  143. CircuitTermination(circuit=circuits[0], term_side=SIDE_A, site=sites[0]),
  144. CircuitTermination(circuit=circuits[0], term_side=SIDE_Z, provider_network=provider_networks[0]),
  145. CircuitTermination(circuit=circuits[1], term_side=SIDE_A, site=sites[1]),
  146. CircuitTermination(circuit=circuits[1], term_side=SIDE_Z, provider_network=provider_networks[1]),
  147. )
  148. CircuitTermination.objects.bulk_create(circuit_terminations)
  149. cls.create_data = [
  150. {
  151. 'circuit': circuits[2].pk,
  152. 'term_side': SIDE_A,
  153. 'site': sites[0].pk,
  154. 'port_speed': 200000,
  155. },
  156. {
  157. 'circuit': circuits[2].pk,
  158. 'term_side': SIDE_Z,
  159. 'provider_network': provider_networks[0].pk,
  160. 'port_speed': 200000,
  161. },
  162. ]
  163. cls.bulk_update_data = {
  164. 'port_speed': 123456
  165. }
  166. class ProviderNetworkTest(APIViewTestCases.APIViewTestCase):
  167. model = ProviderNetwork
  168. brief_fields = ['display', 'id', 'name', 'url']
  169. @classmethod
  170. def setUpTestData(cls):
  171. providers = (
  172. Provider(name='Provider 1', slug='provider-1'),
  173. Provider(name='Provider 2', slug='provider-2'),
  174. )
  175. Provider.objects.bulk_create(providers)
  176. provider_networks = (
  177. ProviderNetwork(name='Provider Network 1', provider=providers[0]),
  178. ProviderNetwork(name='Provider Network 2', provider=providers[0]),
  179. ProviderNetwork(name='Provider Network 3', provider=providers[0]),
  180. )
  181. ProviderNetwork.objects.bulk_create(provider_networks)
  182. cls.create_data = [
  183. {
  184. 'name': 'Provider Network 4',
  185. 'provider': providers[0].pk,
  186. },
  187. {
  188. 'name': 'Provider Network 5',
  189. 'provider': providers[0].pk,
  190. },
  191. {
  192. 'name': 'Provider Network 6',
  193. 'provider': providers[0].pk,
  194. },
  195. ]
  196. cls.bulk_update_data = {
  197. 'provider': providers[1].pk,
  198. 'description': 'New description',
  199. }