test_views.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. import datetime
  2. from netaddr import IPNetwork
  3. from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
  4. from ipam.choices import *
  5. from ipam.models import *
  6. from tenancy.models import Tenant
  7. from utilities.testing import ViewTestCases, create_tags
  8. class VRFTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  9. model = VRF
  10. @classmethod
  11. def setUpTestData(cls):
  12. tenants = (
  13. Tenant(name='Tenant A', slug='tenant-a'),
  14. Tenant(name='Tenant B', slug='tenant-b'),
  15. )
  16. Tenant.objects.bulk_create(tenants)
  17. VRF.objects.bulk_create([
  18. VRF(name='VRF 1', rd='65000:1'),
  19. VRF(name='VRF 2', rd='65000:2'),
  20. VRF(name='VRF 3', rd='65000:3'),
  21. ])
  22. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  23. cls.form_data = {
  24. 'name': 'VRF X',
  25. 'rd': '65000:999',
  26. 'tenant': tenants[0].pk,
  27. 'enforce_unique': True,
  28. 'description': 'A new VRF',
  29. 'tags': [t.pk for t in tags],
  30. }
  31. cls.csv_data = (
  32. "name",
  33. "VRF 4",
  34. "VRF 5",
  35. "VRF 6",
  36. )
  37. cls.bulk_edit_data = {
  38. 'tenant': tenants[1].pk,
  39. 'enforce_unique': False,
  40. 'description': 'New description',
  41. }
  42. class RouteTargetTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  43. model = RouteTarget
  44. @classmethod
  45. def setUpTestData(cls):
  46. tenants = (
  47. Tenant(name='Tenant A', slug='tenant-a'),
  48. Tenant(name='Tenant B', slug='tenant-b'),
  49. )
  50. Tenant.objects.bulk_create(tenants)
  51. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  52. route_targets = (
  53. RouteTarget(name='65000:1001', tenant=tenants[0]),
  54. RouteTarget(name='65000:1002', tenant=tenants[1]),
  55. RouteTarget(name='65000:1003'),
  56. )
  57. RouteTarget.objects.bulk_create(route_targets)
  58. cls.form_data = {
  59. 'name': '65000:100',
  60. 'description': 'A new route target',
  61. 'tags': [t.pk for t in tags],
  62. }
  63. cls.csv_data = (
  64. "name,tenant,description",
  65. "65000:1004,Tenant A,Foo",
  66. "65000:1005,Tenant B,Bar",
  67. "65000:1006,,No tenant",
  68. )
  69. cls.bulk_edit_data = {
  70. 'tenant': tenants[1].pk,
  71. 'description': 'New description',
  72. }
  73. class RIRTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
  74. model = RIR
  75. @classmethod
  76. def setUpTestData(cls):
  77. RIR.objects.bulk_create([
  78. RIR(name='RIR 1', slug='rir-1'),
  79. RIR(name='RIR 2', slug='rir-2'),
  80. RIR(name='RIR 3', slug='rir-3'),
  81. ])
  82. cls.form_data = {
  83. 'name': 'RIR X',
  84. 'slug': 'rir-x',
  85. 'is_private': True,
  86. 'description': 'A new RIR',
  87. }
  88. cls.csv_data = (
  89. "name,slug,description",
  90. "RIR 4,rir-4,Fourth RIR",
  91. "RIR 5,rir-5,Fifth RIR",
  92. "RIR 6,rir-6,Sixth RIR",
  93. )
  94. cls.bulk_edit_data = {
  95. 'description': 'New description',
  96. }
  97. class AggregateTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  98. model = Aggregate
  99. @classmethod
  100. def setUpTestData(cls):
  101. rirs = (
  102. RIR(name='RIR 1', slug='rir-1'),
  103. RIR(name='RIR 2', slug='rir-2'),
  104. )
  105. RIR.objects.bulk_create(rirs)
  106. Aggregate.objects.bulk_create([
  107. Aggregate(prefix=IPNetwork('10.1.0.0/16'), rir=rirs[0]),
  108. Aggregate(prefix=IPNetwork('10.2.0.0/16'), rir=rirs[0]),
  109. Aggregate(prefix=IPNetwork('10.3.0.0/16'), rir=rirs[0]),
  110. ])
  111. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  112. cls.form_data = {
  113. 'prefix': IPNetwork('10.99.0.0/16'),
  114. 'rir': rirs[1].pk,
  115. 'date_added': datetime.date(2020, 1, 1),
  116. 'description': 'A new aggregate',
  117. 'tags': [t.pk for t in tags],
  118. }
  119. cls.csv_data = (
  120. "prefix,rir",
  121. "10.4.0.0/16,RIR 1",
  122. "10.5.0.0/16,RIR 1",
  123. "10.6.0.0/16,RIR 1",
  124. )
  125. cls.bulk_edit_data = {
  126. 'rir': rirs[1].pk,
  127. 'date_added': datetime.date(2020, 1, 1),
  128. 'description': 'New description',
  129. }
  130. class RoleTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
  131. model = Role
  132. @classmethod
  133. def setUpTestData(cls):
  134. Role.objects.bulk_create([
  135. Role(name='Role 1', slug='role-1'),
  136. Role(name='Role 2', slug='role-2'),
  137. Role(name='Role 3', slug='role-3'),
  138. ])
  139. cls.form_data = {
  140. 'name': 'Role X',
  141. 'slug': 'role-x',
  142. 'weight': 200,
  143. 'description': 'A new role',
  144. }
  145. cls.csv_data = (
  146. "name,slug,weight",
  147. "Role 4,role-4,1000",
  148. "Role 5,role-5,1000",
  149. "Role 6,role-6,1000",
  150. )
  151. cls.bulk_edit_data = {
  152. 'description': 'New description',
  153. }
  154. class PrefixTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  155. model = Prefix
  156. @classmethod
  157. def setUpTestData(cls):
  158. sites = (
  159. Site(name='Site 1', slug='site-1'),
  160. Site(name='Site 2', slug='site-2'),
  161. )
  162. Site.objects.bulk_create(sites)
  163. vrfs = (
  164. VRF(name='VRF 1', rd='65000:1'),
  165. VRF(name='VRF 2', rd='65000:2'),
  166. )
  167. VRF.objects.bulk_create(vrfs)
  168. roles = (
  169. Role(name='Role 1', slug='role-1'),
  170. Role(name='Role 2', slug='role-2'),
  171. )
  172. Role.objects.bulk_create(roles)
  173. Prefix.objects.bulk_create([
  174. Prefix(prefix=IPNetwork('10.1.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
  175. Prefix(prefix=IPNetwork('10.2.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
  176. Prefix(prefix=IPNetwork('10.3.0.0/16'), vrf=vrfs[0], site=sites[0], role=roles[0]),
  177. ])
  178. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  179. cls.form_data = {
  180. 'prefix': IPNetwork('192.0.2.0/24'),
  181. 'site': sites[1].pk,
  182. 'vrf': vrfs[1].pk,
  183. 'tenant': None,
  184. 'vlan': None,
  185. 'status': PrefixStatusChoices.STATUS_RESERVED,
  186. 'role': roles[1].pk,
  187. 'is_pool': True,
  188. 'description': 'A new prefix',
  189. 'tags': [t.pk for t in tags],
  190. }
  191. cls.csv_data = (
  192. "vrf,prefix,status",
  193. "VRF 1,10.4.0.0/16,active",
  194. "VRF 1,10.5.0.0/16,active",
  195. "VRF 1,10.6.0.0/16,active",
  196. )
  197. cls.bulk_edit_data = {
  198. 'site': sites[1].pk,
  199. 'vrf': vrfs[1].pk,
  200. 'tenant': None,
  201. 'status': PrefixStatusChoices.STATUS_RESERVED,
  202. 'role': roles[1].pk,
  203. 'is_pool': False,
  204. 'description': 'New description',
  205. }
  206. class IPRangeTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  207. model = IPRange
  208. @classmethod
  209. def setUpTestData(cls):
  210. vrfs = (
  211. VRF(name='VRF 1', rd='65000:1'),
  212. VRF(name='VRF 2', rd='65000:2'),
  213. )
  214. VRF.objects.bulk_create(vrfs)
  215. roles = (
  216. Role(name='Role 1', slug='role-1'),
  217. Role(name='Role 2', slug='role-2'),
  218. )
  219. Role.objects.bulk_create(roles)
  220. ip_ranges = (
  221. IPRange(start_address='192.168.0.10/24', end_address='192.168.0.100/24', size=91),
  222. IPRange(start_address='192.168.1.10/24', end_address='192.168.1.100/24', size=91),
  223. IPRange(start_address='192.168.2.10/24', end_address='192.168.2.100/24', size=91),
  224. IPRange(start_address='192.168.3.10/24', end_address='192.168.3.100/24', size=91),
  225. IPRange(start_address='192.168.4.10/24', end_address='192.168.4.100/24', size=91),
  226. )
  227. IPRange.objects.bulk_create(ip_ranges)
  228. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  229. cls.form_data = {
  230. 'start_address': IPNetwork('192.0.5.10/24'),
  231. 'end_address': IPNetwork('192.0.5.100/24'),
  232. 'vrf': vrfs[1].pk,
  233. 'tenant': None,
  234. 'vlan': None,
  235. 'status': IPRangeStatusChoices.STATUS_RESERVED,
  236. 'role': roles[1].pk,
  237. 'is_pool': True,
  238. 'description': 'A new IP range',
  239. 'tags': [t.pk for t in tags],
  240. }
  241. cls.csv_data = (
  242. "vrf,start_address,end_address,status",
  243. "VRF 1,10.1.0.1/16,10.1.9.254/16,active",
  244. "VRF 1,10.2.0.1/16,10.2.9.254/16,active",
  245. "VRF 1,10.3.0.1/16,10.3.9.254/16,active",
  246. )
  247. cls.bulk_edit_data = {
  248. 'vrf': vrfs[1].pk,
  249. 'tenant': None,
  250. 'status': IPRangeStatusChoices.STATUS_RESERVED,
  251. 'role': roles[1].pk,
  252. 'description': 'New description',
  253. }
  254. class IPAddressTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  255. model = IPAddress
  256. @classmethod
  257. def setUpTestData(cls):
  258. vrfs = (
  259. VRF(name='VRF 1', rd='65000:1'),
  260. VRF(name='VRF 2', rd='65000:2'),
  261. )
  262. VRF.objects.bulk_create(vrfs)
  263. IPAddress.objects.bulk_create([
  264. IPAddress(address=IPNetwork('192.0.2.1/24'), vrf=vrfs[0]),
  265. IPAddress(address=IPNetwork('192.0.2.2/24'), vrf=vrfs[0]),
  266. IPAddress(address=IPNetwork('192.0.2.3/24'), vrf=vrfs[0]),
  267. ])
  268. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  269. cls.form_data = {
  270. 'vrf': vrfs[1].pk,
  271. 'address': IPNetwork('192.0.2.99/24'),
  272. 'tenant': None,
  273. 'status': IPAddressStatusChoices.STATUS_RESERVED,
  274. 'role': IPAddressRoleChoices.ROLE_ANYCAST,
  275. 'nat_inside': None,
  276. 'dns_name': 'example',
  277. 'description': 'A new IP address',
  278. 'tags': [t.pk for t in tags],
  279. }
  280. cls.csv_data = (
  281. "vrf,address,status",
  282. "VRF 1,192.0.2.4/24,active",
  283. "VRF 1,192.0.2.5/24,active",
  284. "VRF 1,192.0.2.6/24,active",
  285. )
  286. cls.bulk_edit_data = {
  287. 'vrf': vrfs[1].pk,
  288. 'tenant': None,
  289. 'status': IPAddressStatusChoices.STATUS_RESERVED,
  290. 'role': IPAddressRoleChoices.ROLE_ANYCAST,
  291. 'dns_name': 'example',
  292. 'description': 'New description',
  293. }
  294. class VLANGroupTestCase(ViewTestCases.OrganizationalObjectViewTestCase):
  295. model = VLANGroup
  296. @classmethod
  297. def setUpTestData(cls):
  298. sites = (
  299. Site(name='Site 1', slug='site-1'),
  300. Site(name='Site 2', slug='site-2'),
  301. )
  302. Site.objects.bulk_create(sites)
  303. VLANGroup.objects.bulk_create([
  304. VLANGroup(name='VLAN Group 1', slug='vlan-group-1', scope=sites[0]),
  305. VLANGroup(name='VLAN Group 2', slug='vlan-group-2', scope=sites[0]),
  306. VLANGroup(name='VLAN Group 3', slug='vlan-group-3', scope=sites[0]),
  307. ])
  308. cls.form_data = {
  309. 'name': 'VLAN Group X',
  310. 'slug': 'vlan-group-x',
  311. 'description': 'A new VLAN group',
  312. }
  313. cls.csv_data = (
  314. "name,slug,description",
  315. "VLAN Group 4,vlan-group-4,Fourth VLAN group",
  316. "VLAN Group 5,vlan-group-5,Fifth VLAN group",
  317. "VLAN Group 6,vlan-group-6,Sixth VLAN group",
  318. )
  319. cls.bulk_edit_data = {
  320. 'description': 'New description',
  321. }
  322. class VLANTestCase(ViewTestCases.PrimaryObjectViewTestCase):
  323. model = VLAN
  324. @classmethod
  325. def setUpTestData(cls):
  326. sites = (
  327. Site(name='Site 1', slug='site-1'),
  328. Site(name='Site 2', slug='site-2'),
  329. )
  330. Site.objects.bulk_create(sites)
  331. vlangroups = (
  332. VLANGroup(name='VLAN Group 1', slug='vlan-group-1', scope=sites[0]),
  333. VLANGroup(name='VLAN Group 2', slug='vlan-group-2', scope=sites[1]),
  334. )
  335. VLANGroup.objects.bulk_create(vlangroups)
  336. roles = (
  337. Role(name='Role 1', slug='role-1'),
  338. Role(name='Role 2', slug='role-2'),
  339. )
  340. Role.objects.bulk_create(roles)
  341. VLAN.objects.bulk_create([
  342. VLAN(group=vlangroups[0], vid=101, name='VLAN101', site=sites[0], role=roles[0]),
  343. VLAN(group=vlangroups[0], vid=102, name='VLAN102', site=sites[0], role=roles[0]),
  344. VLAN(group=vlangroups[0], vid=103, name='VLAN103', site=sites[0], role=roles[0]),
  345. ])
  346. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  347. cls.form_data = {
  348. 'site': sites[1].pk,
  349. 'group': vlangroups[1].pk,
  350. 'vid': 999,
  351. 'name': 'VLAN999',
  352. 'tenant': None,
  353. 'status': VLANStatusChoices.STATUS_RESERVED,
  354. 'role': roles[1].pk,
  355. 'description': 'A new VLAN',
  356. 'tags': [t.pk for t in tags],
  357. }
  358. cls.csv_data = (
  359. "vid,name,status",
  360. "104,VLAN104,active",
  361. "105,VLAN105,active",
  362. "106,VLAN106,active",
  363. )
  364. cls.bulk_edit_data = {
  365. 'site': sites[1].pk,
  366. 'group': vlangroups[1].pk,
  367. 'tenant': None,
  368. 'status': VLANStatusChoices.STATUS_RESERVED,
  369. 'role': roles[1].pk,
  370. 'description': 'New description',
  371. }
  372. # TODO: Update base class to PrimaryObjectViewTestCase
  373. # Blocked by absence of standard creation view
  374. class ServiceTestCase(
  375. ViewTestCases.GetObjectViewTestCase,
  376. ViewTestCases.GetObjectChangelogViewTestCase,
  377. ViewTestCases.EditObjectViewTestCase,
  378. ViewTestCases.DeleteObjectViewTestCase,
  379. ViewTestCases.ListObjectsViewTestCase,
  380. ViewTestCases.BulkImportObjectsViewTestCase,
  381. ViewTestCases.BulkEditObjectsViewTestCase,
  382. ViewTestCases.BulkDeleteObjectsViewTestCase
  383. ):
  384. model = Service
  385. @classmethod
  386. def setUpTestData(cls):
  387. site = Site.objects.create(name='Site 1', slug='site-1')
  388. manufacturer = Manufacturer.objects.create(name='Manufacturer 1', slug='manufacturer-1')
  389. devicetype = DeviceType.objects.create(manufacturer=manufacturer, model='Device Type 1')
  390. devicerole = DeviceRole.objects.create(name='Device Role 1', slug='device-role-1')
  391. device = Device.objects.create(name='Device 1', site=site, device_type=devicetype, device_role=devicerole)
  392. Service.objects.bulk_create([
  393. Service(device=device, name='Service 1', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[101]),
  394. Service(device=device, name='Service 2', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[102]),
  395. Service(device=device, name='Service 3', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[103]),
  396. ])
  397. tags = create_tags('Alpha', 'Bravo', 'Charlie')
  398. cls.form_data = {
  399. 'device': device.pk,
  400. 'virtual_machine': None,
  401. 'name': 'Service X',
  402. 'protocol': ServiceProtocolChoices.PROTOCOL_TCP,
  403. 'ports': '104,105',
  404. 'ipaddresses': [],
  405. 'description': 'A new service',
  406. 'tags': [t.pk for t in tags],
  407. }
  408. cls.csv_data = (
  409. "device,name,protocol,ports,description",
  410. "Device 1,Service 1,tcp,1,First service",
  411. "Device 1,Service 2,tcp,2,Second service",
  412. "Device 1,Service 3,udp,3,Third service",
  413. )
  414. cls.bulk_edit_data = {
  415. 'protocol': ServiceProtocolChoices.PROTOCOL_UDP,
  416. 'ports': '106,107',
  417. 'description': 'New description',
  418. }