improvements to router, y tests

This commit is contained in:
Sam
2021-01-22 12:33:59 +00:00
parent 6c3ff8f1fa
commit 2b46d01369
8 changed files with 118 additions and 285 deletions

View File

@@ -10,12 +10,14 @@ from rest_framework import status
from companies.factories import CompanyFactory
from companies.models import Company
from core.utils import get_auth_token, create_active_user
from core.factories import CustomUserFactory
from core.utils import get_auth_token, create_active_user, get_tokens_for_user
# Create your tests here.
class BuildingViewSetTest(APITestCase):
"""Building viewset tests
class CompanyViewSetTest(APITestCase):
"""CompanyViewset tests
"""
def setUp(self):
@@ -25,10 +27,8 @@ class BuildingViewSetTest(APITestCase):
self.factory = CompanyFactory
self.model = Company
# create user
self.rand = ''.join(random.choices(string.ascii_uppercase, k = 5))
self.password = ''.join(random.choices(string.ascii_uppercase, k = 10))
self.email = f"{self.rand}@mail.com"
self.user = create_active_user(email=self.email, password=self.password)
self.user = CustomUserFactory(password=self.password)
# user not authenticated
def test_not_logged_user_cannot_create_instance(self):
@@ -85,8 +85,8 @@ class BuildingViewSetTest(APITestCase):
instances = [self.factory() for n in range(random.randint(1,5))]
# Authenticate user
token = get_auth_token(self.client, self.email, self.password)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
token = get_tokens_for_user(self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
# Request list
response = self.client.get(self.endpoint)
@@ -125,8 +125,8 @@ class BuildingViewSetTest(APITestCase):
}
# Authenticate user
token = get_auth_token(self.client, self.email, self.password)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
token = get_tokens_for_user(self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
# Query endpoint
response = self.client.post(self.endpoint, data=data, format='json')
@@ -171,8 +171,8 @@ class BuildingViewSetTest(APITestCase):
}
# Authenticate user
token = get_auth_token(self.client, self.email, self.password)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
token = get_tokens_for_user(self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
# Query endpoint
url = self.endpoint + f'{instance.pk}/'
@@ -192,8 +192,8 @@ class BuildingViewSetTest(APITestCase):
instance = self.factory()
# Authenticate user
token = get_auth_token(self.client, self.email, self.password)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
token = get_tokens_for_user(self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
# Query endpoint
url = self.endpoint + f'{instance.pk}/'
@@ -209,8 +209,8 @@ class BuildingViewSetTest(APITestCase):
instance = self.factory()
# Authenticate user
token = get_auth_token(self.client, self.email, self.password)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
token = get_tokens_for_user(self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
# Query endpoint
url = self.endpoint + f'{instance.pk}/'
@@ -229,8 +229,8 @@ class BuildingViewSetTest(APITestCase):
instance.save()
# Authenticate user
token = get_auth_token(self.client, self.email, self.password)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
token = get_tokens_for_user(self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
# Query endpoint
url = self.endpoint + f'{instance.pk}/'