From d58f9031d1ae2e2692fa87f621003d2dae1b0164 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 16 Mar 2017 13:29:55 -0400 Subject: [PATCH] Wrote tests for tenancy API --- netbox/tenancy/tests/__init__.py | 0 netbox/tenancy/tests/test_api.py | 134 +++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 netbox/tenancy/tests/__init__.py create mode 100644 netbox/tenancy/tests/test_api.py diff --git a/netbox/tenancy/tests/__init__.py b/netbox/tenancy/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/tenancy/tests/test_api.py b/netbox/tenancy/tests/test_api.py new file mode 100644 index 000000000..fb630caee --- /dev/null +++ b/netbox/tenancy/tests/test_api.py @@ -0,0 +1,134 @@ +from rest_framework import status +from rest_framework.test import APITestCase + +from django.contrib.auth.models import User +from django.urls import reverse + +from tenancy.models import Tenant, TenantGroup +from users.models import Token + + +class TenantGroupTest(APITestCase): + + def setUp(self): + + user = User.objects.create(username='testuser', is_superuser=True) + token = Token.objects.create(user=user) + self.header = {'HTTP_AUTHORIZATION': 'Token {}'.format(token.key)} + + self.tenantgroup1 = TenantGroup.objects.create(name='Test Tenant Group 1', slug='test-tenant-group-1') + self.tenantgroup2 = TenantGroup.objects.create(name='Test Tenant Group 2', slug='test-tenant-group-2') + self.tenantgroup3 = TenantGroup.objects.create(name='Test Tenant Group 3', slug='test-tenant-group-3') + + def test_get_tenantgroup(self): + + url = reverse('tenancy-api:tenantgroup-detail', kwargs={'pk': self.tenantgroup1.pk}) + response = self.client.get(url, **self.header) + + self.assertEqual(response.data['name'], self.tenantgroup1.name) + + def test_list_tenantgroups(self): + + url = reverse('tenancy-api:tenantgroup-list') + response = self.client.get(url, **self.header) + + self.assertEqual(response.data['count'], 3) + + def test_create_tenantgroup(self): + + data = { + 'name': 'Test Tenant Group 4', + 'slug': 'test-tenant-group-4', + } + + url = reverse('tenancy-api:tenantgroup-list') + response = self.client.post(url, data, **self.header) + + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(TenantGroup.objects.count(), 4) + + def test_update_tenantgroup(self): + + data = { + 'name': 'Test Tenant Group X', + 'slug': 'test-tenant-group-x', + } + + url = reverse('tenancy-api:tenantgroup-detail', kwargs={'pk': self.tenantgroup1.pk}) + response = self.client.put(url, data, **self.header) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(TenantGroup.objects.count(), 3) + self.assertEqual(TenantGroup.objects.get(pk=self.tenantgroup1.pk).name, data['name']) + self.assertEqual(TenantGroup.objects.get(pk=self.tenantgroup1.pk).slug, data['slug']) + + def test_delete_tenantgroup(self): + + url = reverse('tenancy-api:tenantgroup-detail', kwargs={'pk': self.tenantgroup1.pk}) + response = self.client.delete(url, **self.header) + + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertEqual(TenantGroup.objects.count(), 2) + + +class TenantTest(APITestCase): + + def setUp(self): + + user = User.objects.create(username='testuser', is_superuser=True) + token = Token.objects.create(user=user) + self.header = {'HTTP_AUTHORIZATION': 'Token {}'.format(token.key)} + + self.tenant1 = Tenant.objects.create(name='Test Tenant 1', slug='test-tenant-1') + self.tenant2 = Tenant.objects.create(name='Test Tenant 2', slug='test-tenant-2') + self.tenant3 = Tenant.objects.create(name='Test Tenant 3', slug='test-tenant-3') + + def test_get_tenant(self): + + url = reverse('tenancy-api:tenant-detail', kwargs={'pk': self.tenant1.pk}) + response = self.client.get(url, **self.header) + + self.assertEqual(response.data['name'], self.tenant1.name) + + def test_list_tenants(self): + + url = reverse('tenancy-api:tenant-list') + response = self.client.get(url, **self.header) + + self.assertEqual(response.data['count'], 3) + + def test_create_tenant(self): + + data = { + 'name': 'Test Tenant 4', + 'slug': 'test-tenant-4', + } + + url = reverse('tenancy-api:tenant-list') + response = self.client.post(url, data, **self.header) + + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(Tenant.objects.count(), 4) + + def test_update_tenant(self): + + data = { + 'name': 'Test Tenant X', + 'slug': 'test-tenant-x', + } + + url = reverse('tenancy-api:tenant-detail', kwargs={'pk': self.tenant1.pk}) + response = self.client.put(url, data, **self.header) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(Tenant.objects.count(), 3) + self.assertEqual(Tenant.objects.get(pk=self.tenant1.pk).name, data['name']) + self.assertEqual(Tenant.objects.get(pk=self.tenant1.pk).slug, data['slug']) + + def test_delete_tenant(self): + + url = reverse('tenancy-api:tenant-detail', kwargs={'pk': self.tenant1.pk}) + response = self.client.delete(url, **self.header) + + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertEqual(Tenant.objects.count(), 2)