Skip to content

Commit

Permalink
Merge pull request #21 from rkshaon/rkshaon
Browse files Browse the repository at this point in the history
test: backend
  • Loading branch information
rkshaon authored Aug 3, 2024
2 parents f13cf27 + b15fe9f commit 1382bc5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/.flake8
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# H405: multi line docstring summary not separated with an empty line
# H501: Do not use self.__dict__ for string formatting
; ignore = F812,H101,H202,H233,H301,H306,H401,H403,H404,H405,H501
exclude = __pycache__,tests.py,migrations,env,user_api/migrations,user_api/tests.py
exclude = __pycache__,tests.py,migrations,env
87 changes: 86 additions & 1 deletion backend/user_api/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,88 @@
from django.urls import resolve, reverse
from django.test import TestCase
from django.test import SimpleTestCase

# Create your tests here.
from user_api.models import User

from user_api.serializers import UserSerializer

from user_api.views import v1 as v1_views


class UserModelTestCase(TestCase):
def setUp(self):
self.user = User.objects.create_user(
email='testuser@example.com',
username='testuser',
password='testpassword123',
name='Test User'
)

def test_user_creation(self):
self.assertEqual(self.user.email, 'testuser@example.com')
self.assertEqual(self.user.username, 'testuser')
self.assertTrue(self.user.check_password('testpassword123'))
self.assertEqual(self.user.name, 'Test User')
self.assertTrue(self.user.is_active)
self.assertFalse(self.user.is_staff)
self.assertFalse(self.user.is_superuser)

def test_superuser_creation(self):
superuser = User.objects.create_superuser(
email='superuser@example.com',
username='superuser',
password='superpassword123'
)
self.assertEqual(superuser.email, 'superuser@example.com')
self.assertEqual(superuser.username, 'superuser')
self.assertTrue(superuser.check_password('superpassword123'))
self.assertTrue(superuser.is_active)
self.assertTrue(superuser.is_staff)
self.assertTrue(superuser.is_superuser)


class UrlsTestCase(SimpleTestCase):
def test_user_registration_url_resolves(self):
url = reverse('user-registration')
self.assertEqual(resolve(url).func.view_class,
v1_views.UserRegistrationView)

def test_user_login_url_resolves(self):
url = reverse('user-login')
self.assertEqual(resolve(url).func.view_class, v1_views.UserLoginView)


class UserSerializerTestCase(TestCase):
def setUp(self):
self.user_attributes = {
'email': 'testuser@example.com',
'username': 'testuser',
'name': 'Test User',
'password': 'testpassword123'
}
self.serializer_data = {
'email': 'testuser@example.com',
'username': 'testuser',
'name': 'Test User',
'password': 'testpassword123'
}
self.user = User.objects.create(**self.user_attributes)
self.serializer = UserSerializer(instance=self.user)

def test_contains_expected_fields(self):
data = self.serializer.data
self.assertEqual(set(data.keys()), set(
['id', 'email', 'name', 'username']))

def test_email_field_content(self):
data = self.serializer.data
self.assertEqual(data['email'], self.user_attributes['email'])

def test_create_user(self):
serializer = UserSerializer(data=self.serializer_data)
self.assertTrue(serializer.is_valid(), serializer.errors)
user = serializer.save()
self.assertEqual(user.email, self.serializer_data['email'])
self.assertEqual(user.username, self.serializer_data['username'])
self.assertEqual(user.name, self.serializer_data['name'])
self.assertTrue(user.check_password(self.serializer_data['password']))

0 comments on commit 1382bc5

Please sign in to comment.