From 2f61df2c650f4cbef67f1a81849a728154199f7a Mon Sep 17 00:00:00 2001 From: littlewolf Date: Tue, 24 Sep 2019 16:20:04 +0300 Subject: [PATCH] Add address CRUD tests --- apps/location/tests.py | 74 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/apps/location/tests.py b/apps/location/tests.py index 7ce503c2..cab871a0 100644 --- a/apps/location/tests.py +++ b/apps/location/tests.py @@ -1,3 +1,73 @@ -from django.test import TestCase +from rest_framework.test import APITestCase +from account.models import User +from rest_framework import status +from http.cookies import SimpleCookie -# Create your tests here. +from location.models import City, Region, Country + + +class BaseTestCase(APITestCase): + + def setUp(self): + self.username = 'sedragurda' + self.password = 'sedragurdaredips19' + self.email = 'sedragurda@desoz.com' + self.newsletter = True + self.user = User.objects.create_user( + username=self.username, email=self.email, password=self.password) + + # get tokens + + tokkens = User.create_jwt_tokens(self.user) + self.client.cookies = SimpleCookie( + {'access_token': tokkens.get('access_token'), + 'refresh_token': tokkens.get('refresh_token')}) + + +class AddressTests(BaseTestCase): + + def setUp(self): + super().setUp() + + self.country = Country( + name="Test country", + code="+7" + ) + + self.region = Region( + name="Test region", + code="812", + country=self.country + ) + + self.city = City( + name="Test region", + code="812", + region=self.region, + country=self.country + ) + + def test_address_CRUD(self): + response = self.client.get('/api/back/location/addresses/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + data = { + 'user': self.user.id, + 'name': 'Test name' + } + + response = self.client.post('/api/back/location/addresses/', data=data) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + response = self.client.get('/api/back/location/addresses/1/', format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + update_data = { + 'name': 'Test new name' + } + + response = self.client.patch('/api/back/location/addresses/1/', data=update_data) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + response = self.client.delete('/api/back/location/addresses/1/', format='json') + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)