2020-12-09 20:57:11 +01:00
|
|
|
"""
|
|
|
|
@author: p.dolata
|
|
|
|
"""
|
|
|
|
|
2020-12-01 19:25:25 +01:00
|
|
|
from django.test import TestCase
|
2020-12-01 19:02:07 +01:00
|
|
|
from django.urls import reverse
|
2020-12-05 19:07:25 +01:00
|
|
|
from rest_framework import serializers
|
2020-12-01 19:02:07 +01:00
|
|
|
from rest_framework import status
|
2020-12-01 19:25:25 +01:00
|
|
|
from rest_framework.test import APITestCase, APIClient
|
|
|
|
|
2020-12-09 20:57:11 +01:00
|
|
|
from smartpicasso.app.user.models import User
|
2020-12-05 19:07:25 +01:00
|
|
|
from smartpicasso.app.user.serializers import UserLoginSerializer, UserRegistrationSerializer
|
2020-12-09 20:57:11 +01:00
|
|
|
from smartpicasso.app.user_profile.models import UserProfile
|
2020-11-30 22:10:22 +01:00
|
|
|
|
2020-12-01 19:02:07 +01:00
|
|
|
|
|
|
|
class UserApiTest(APITestCase):
|
|
|
|
client = APIClient()
|
2020-12-09 20:57:11 +01:00
|
|
|
authenticate_url = reverse('authenticate')
|
|
|
|
register_url = reverse('register')
|
|
|
|
profile = {"username": 'test', "first_name": "test", "last_name": "test"}
|
2020-12-01 19:02:07 +01:00
|
|
|
|
|
|
|
def test_login_when_user_non_exist(self):
|
2020-12-09 20:57:11 +01:00
|
|
|
response = self.client.post(self.authenticate_url, {'email': 'non-exist', 'password': '123'}, format='json')
|
2020-12-01 19:02:07 +01:00
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
def test_login_when_user_exist(self):
|
|
|
|
User.objects.create_user(email='test@test.com', password='test')
|
2020-12-09 20:57:11 +01:00
|
|
|
response = self.client.post(self.authenticate_url, {'email': 'test@test.com', 'password': 'test'},
|
|
|
|
format='json')
|
2020-12-01 19:02:07 +01:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(response.data['success'], 'True')
|
|
|
|
self.assertIn('token', response.data)
|
2020-12-01 19:25:25 +01:00
|
|
|
|
2020-12-09 20:57:11 +01:00
|
|
|
def test_register_when_user_non_exist(self):
|
|
|
|
response = self.client.post(self.register_url, {'email': 'test@test.com', 'password': 'test',
|
|
|
|
'profile': self.profile}, format='json')
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
|
|
|
self.assertEqual(response.data['success'], 'True')
|
|
|
|
|
|
|
|
def test_register_when_email_exist(self):
|
|
|
|
User.objects.create_user(email='test@test.com', password='test')
|
|
|
|
response = self.client.post(self.register_url, {'email': 'test@test.com', 'password': 'test',
|
|
|
|
'profile': self.profile}, format='json')
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
|
|
self.assertIn('email', response.data)
|
|
|
|
|
|
|
|
def test_register_when_username_exist(self):
|
|
|
|
user = User.objects.create_user(email='test2@test.com', password='test')
|
|
|
|
UserProfile.objects.create(
|
|
|
|
user=user,
|
|
|
|
username=self.profile['username'],
|
|
|
|
first_name=self.profile['first_name'],
|
|
|
|
last_name=self.profile['last_name']
|
|
|
|
)
|
|
|
|
response = self.client.post(self.register_url, {'email': 'test@test.com', 'password': 'test',
|
|
|
|
'profile': self.profile}, format='json')
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
|
|
self.assertIn('profile', response.data)
|
|
|
|
self.assertIn('username', response.data['profile'])
|
|
|
|
|
2020-12-01 19:25:25 +01:00
|
|
|
|
|
|
|
class UserTest(TestCase):
|
|
|
|
|
|
|
|
def test_user_str(self):
|
|
|
|
email = 'test@test.com'
|
|
|
|
user = User.objects.create_user(email=email, password='test')
|
|
|
|
self.assertEqual(str(user), email)
|
2020-12-01 23:25:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserLoginSerializerTest(TestCase):
|
|
|
|
serializer = UserLoginSerializer()
|
|
|
|
|
|
|
|
def test_validate_wrong_credentials(self):
|
|
|
|
data = {'email': 'test@test.com', 'password': '123'}
|
|
|
|
self.assertRaises(serializers.ValidationError, self.serializer.validate, data)
|
|
|
|
|
|
|
|
def test_validate_success(self):
|
|
|
|
User.objects.create_user(email='test@test.com', password='test')
|
|
|
|
data = {'email': 'test@test.com', 'password': 'test'}
|
|
|
|
result = self.serializer.validate(data)
|
|
|
|
self.assertEqual(result['email'], 'test@test.com')
|
|
|
|
self.assertIn('token', result)
|
2020-12-05 19:07:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserRegistrationSerializerTest(TestCase):
|
|
|
|
serializer = UserRegistrationSerializer()
|
|
|
|
|
|
|
|
def test_create(self):
|
2020-12-09 20:57:11 +01:00
|
|
|
profile = {"username": 'test', "first_name": "test", "last_name": "test"}
|
2020-12-05 19:07:25 +01:00
|
|
|
user = self.serializer.create({"email": "test@test.com", "password": "test", "profile": profile})
|
|
|
|
|
|
|
|
self.assertNotEqual(user, None)
|
|
|
|
self.assertEqual(user.email, "test@test.com")
|
|
|
|
|
|
|
|
|
|
|
|
class UserManagerTest(TestCase):
|
|
|
|
manager = User.objects
|
|
|
|
|
|
|
|
def test_create_user_none_email(self):
|
|
|
|
email = None
|
|
|
|
self.assertRaises(ValueError, self.manager.create_user, email)
|
|
|
|
|
|
|
|
def test_create_user(self):
|
|
|
|
user = self.manager.create_user("test@test.pl", "test")
|
|
|
|
self.assertNotEqual(user, None)
|
|
|
|
self.assertEqual(user.email, "test@test.pl")
|
|
|
|
self.assertEqual(user.is_active, True)
|
|
|
|
self.assertEqual(user.is_superuser, False)
|
|
|
|
self.assertEqual(user.is_staff, False)
|
|
|
|
|
|
|
|
def test_create_superuser_none_password(self):
|
|
|
|
password = None
|
|
|
|
self.assertRaises(TypeError, self.manager.create_superuser, "super@test.pl", password)
|
|
|
|
|
|
|
|
def test_create_superuser(self):
|
|
|
|
user = self.manager.create_superuser("super@test.pl", "test")
|
|
|
|
self.assertNotEqual(user, None)
|
|
|
|
self.assertEqual(user.email, "super@test.pl")
|
|
|
|
self.assertEqual(user.is_active, True)
|
|
|
|
self.assertEqual(user.is_superuser, True)
|
|
|
|
self.assertEqual(user.is_staff, True)
|