improvements to router, y tests
This commit is contained in:
215
geo/tests.py
215
geo/tests.py
@@ -8,9 +8,7 @@ from django.utils import timezone
|
||||
from rest_framework.test import APITestCase
|
||||
from rest_framework import status
|
||||
|
||||
import jwt
|
||||
|
||||
from tenants.factories import TenantUserFactory
|
||||
from core.factories import CustomUserFactory
|
||||
|
||||
from . import models
|
||||
from . import factories
|
||||
@@ -68,6 +66,32 @@ class RegionTest(TestCase):
|
||||
self.assertEqual(getattr(instance, field), data[field])
|
||||
|
||||
|
||||
class ProvinceTest(TestCase):
|
||||
"""Province model tests
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""Tests setup
|
||||
"""
|
||||
self.model = models.Province
|
||||
|
||||
def test_content(self):
|
||||
"""Test content correctly set
|
||||
"""
|
||||
# Define instance data
|
||||
data = {
|
||||
'name': 'region name _test_ data',
|
||||
'region': factories.RegionFactory(),
|
||||
}
|
||||
|
||||
# Create instance
|
||||
instance = self.model.objects.create(**data)
|
||||
|
||||
# Assert content correctly set
|
||||
for field in data.keys():
|
||||
self.assertEqual(getattr(instance, field), data[field])
|
||||
|
||||
|
||||
class CityTest(TestCase):
|
||||
"""City model tests
|
||||
"""
|
||||
@@ -94,37 +118,11 @@ class CityTest(TestCase):
|
||||
self.assertEqual(getattr(instance, field), data[field])
|
||||
|
||||
|
||||
class JurisdictionTest(TestCase):
|
||||
"""Jurisdiction model tests
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""Tests setup
|
||||
"""
|
||||
self.model = models.Jurisdiction
|
||||
|
||||
def test_content(self):
|
||||
"""Test content correctly set
|
||||
"""
|
||||
# Define instance data
|
||||
data = {
|
||||
'name': 'jurisdiction name test data',
|
||||
'region': factories.RegionFactory(),
|
||||
}
|
||||
|
||||
# Create instance
|
||||
instance = self.model.objects.create(**data)
|
||||
|
||||
# Assert content correctly set
|
||||
for field in data.keys():
|
||||
self.assertEqual(getattr(instance, field), data[field])
|
||||
|
||||
|
||||
# ViewSet Tests
|
||||
class CountryViewSetTest(APITestCase):
|
||||
"""Country viewset tests
|
||||
"""
|
||||
databases = {'default', 'tenants'}
|
||||
|
||||
def setUp(self):
|
||||
"""Tests setup
|
||||
@@ -182,7 +180,7 @@ class CountryViewSetTest(APITestCase):
|
||||
"""Regular logged-in user can list country
|
||||
"""
|
||||
# Create instances
|
||||
user = TenantUserFactory()
|
||||
user = CustomUserFactory()
|
||||
instances = [self.factory() for n in range(random.randint(1,5))]
|
||||
|
||||
# Authenticate user
|
||||
@@ -207,8 +205,7 @@ class CountryViewSetTest(APITestCase):
|
||||
}
|
||||
|
||||
# Authenticate user
|
||||
user = TenantUserFactory()
|
||||
user.db = 'default'
|
||||
user = CustomUserFactory()
|
||||
self.client.force_authenticate(user=user)
|
||||
|
||||
# Query endpoint
|
||||
@@ -268,7 +265,6 @@ class CountryViewSetTest(APITestCase):
|
||||
class RegionViewSetTest(APITestCase):
|
||||
"""Region viewset tests
|
||||
"""
|
||||
databases = {'default', 'tenants'}
|
||||
|
||||
def setUp(self):
|
||||
"""Tests setup
|
||||
@@ -415,7 +411,6 @@ class RegionViewSetTest(APITestCase):
|
||||
class CityViewSetTest(APITestCase):
|
||||
"""City viewset tests
|
||||
"""
|
||||
databases = {'default', 'tenants'}
|
||||
|
||||
def setUp(self):
|
||||
"""Tests setup
|
||||
@@ -558,155 +553,3 @@ class CityViewSetTest(APITestCase):
|
||||
# Assert instance doesn't exists on db anymore
|
||||
self.assertFalse(self.model.objects.filter(id=instance.pk).exists())
|
||||
|
||||
|
||||
class JurisdictionViewSetTest(APITestCase):
|
||||
"""Jurisdiction viewset tests
|
||||
"""
|
||||
databases = {'default', 'tenants'}
|
||||
|
||||
def setUp(self):
|
||||
"""Tests setup
|
||||
"""
|
||||
self.endpoint = 'api-v1:jurisdiction-'
|
||||
self.factory = factories.JurisdictionFactory
|
||||
self.model = models.Jurisdiction
|
||||
|
||||
def test_not_logged_user_cannot_create_jurisdiction(self):
|
||||
"""Not logged-in user cannot create new city
|
||||
"""
|
||||
# Query endpoint
|
||||
url = reverse(self.endpoint+'list')
|
||||
response = self.client.post(url, data={})
|
||||
|
||||
# Assert access is forbidden
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
def test_not_logged_user_cannot_modify_existing_jurisdiction(self):
|
||||
"""Not logged-in user cannot modify existing city
|
||||
"""
|
||||
# Create instance
|
||||
instance = self.factory()
|
||||
|
||||
# Query endpoint
|
||||
url = reverse(self.endpoint+'detail', args=[instance.pk])
|
||||
response = self.client.put(url, {}, format='json')
|
||||
|
||||
# Assert forbidden code
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
def test_not_logged_user_cannot_delete_existing_jurisdiction(self):
|
||||
"""Not logged-in user cannot delete existing city
|
||||
"""
|
||||
# Create instances
|
||||
instance = self.factory()
|
||||
|
||||
# Query endpoint
|
||||
url = reverse(self.endpoint+'detail', args=[instance.pk])
|
||||
response = self.client.delete(url)
|
||||
|
||||
# Assert instance still exists on db
|
||||
self.assertTrue(self.model.objects.get(id=instance.pk))
|
||||
|
||||
def test_not_logged_user_cant_list_jurisdiction(self):
|
||||
"""Not logged-in user can't read city
|
||||
"""
|
||||
# Request list
|
||||
url = reverse(self.endpoint+'list')
|
||||
response = self.client.get(url)
|
||||
|
||||
# Assert access is forbidden
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
def test_logged_user_can_list_jurisdiction(self):
|
||||
"""Regular logged-in user can list city
|
||||
"""
|
||||
# Create instances
|
||||
user = TenantUserFactory()
|
||||
instances = [self.factory() for n in range(random.randint(1,5))]
|
||||
|
||||
# Authenticate user
|
||||
user.db = 'default'
|
||||
self.client.force_authenticate(user=user)
|
||||
|
||||
# Request list
|
||||
url = reverse(self.endpoint+'list')
|
||||
response = self.client.get(url)
|
||||
|
||||
# Assert access is allowed
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# Assert all instances are returned
|
||||
self.assertEqual(len(instances), len(response.data['results']))
|
||||
|
||||
def test_logged_user_can_create_jurisdiction(self):
|
||||
"""Regular logged-in user can create new city
|
||||
"""
|
||||
# Define request data
|
||||
data = {
|
||||
'name': 'jurisdiction name test data',
|
||||
'region': factories.RegionFactory().pk,
|
||||
}
|
||||
|
||||
# Authenticate user
|
||||
user = TenantUserFactory()
|
||||
user.db = 'default'
|
||||
self.client.force_authenticate(user=user)
|
||||
|
||||
# Query endpoint
|
||||
url = reverse(self.endpoint+'list')
|
||||
response = self.client.post(url, data=data)
|
||||
|
||||
# Assert endpoint returns created status
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
# Assert instance exists on db
|
||||
self.assertTrue(self.model.objects.get(id=response.data['id']))
|
||||
|
||||
def test_logged_user_can_modify_existing_jurisdiction(self):
|
||||
"""Regular logged-in user can modify existing citycity
|
||||
"""
|
||||
# Create instances
|
||||
instance = self.factory()
|
||||
|
||||
# Define request data
|
||||
data = {
|
||||
'name': 'jurisdiction name test MOD data',
|
||||
'region': factories.RegionFactory().pk,
|
||||
}
|
||||
|
||||
# Authenticate user
|
||||
user = TenantUserFactory()
|
||||
user.db = 'default'
|
||||
self.client.force_authenticate(user=user)
|
||||
|
||||
# Query endpoint
|
||||
url = reverse(self.endpoint+'detail', args=[instance.pk])
|
||||
response = self.client.put(url, data, format='json')
|
||||
|
||||
# Assert endpoint returns OK code
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
# Assert instance has been modified
|
||||
for key in data:
|
||||
self.assertEqual(data[key], response.data[key])
|
||||
|
||||
def test_logged_user_can_delete_existing_jurisdiction(self):
|
||||
"""Regular logged-in user can delete existing city
|
||||
"""
|
||||
# Create instances
|
||||
instance = self.factory()
|
||||
|
||||
# Authenticate user
|
||||
user = TenantUserFactory()
|
||||
user.db = 'default'
|
||||
self.client.force_authenticate(user=user)
|
||||
|
||||
# Query endpoint
|
||||
url = reverse(self.endpoint+'detail', args=[instance.pk])
|
||||
response = self.client.delete(url)
|
||||
|
||||
# Assert 204 no content
|
||||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
|
||||
# Assert instance not exists anymore on db
|
||||
self.assertFalse(self.model.objects.filter(id=instance.pk).exists())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user