Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test - backend #22

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 0 additions & 88 deletions backend/user_api/tests.py

This file was deleted.

Empty file.
35 changes: 35 additions & 0 deletions backend/user_api/tests/tests_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.test import TestCase

from user_api.models import User


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)
41 changes: 41 additions & 0 deletions backend/user_api/tests/tests_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.test import TestCase

from user_api.models import User

from user_api.serializers import UserSerializer


class UserSerializerTestCase(TestCase):
def setUp(self):
self.user_attributes = {
'email': 'testuser1@example.com',
'username': 'testuser1',
'name': 'Test User1',
'password': 'testpassword123'
}
self.serializer_data = {
'email': 'testuser2@example.com',
'username': 'testuser2',
'name': 'Test User2',
'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']))
20 changes: 20 additions & 0 deletions backend/user_api/tests/tests_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.test import SimpleTestCase
from django.urls import resolve, reverse

from user_api.views import v1 as v1_views


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
)
Loading