Skip to content

Commit

Permalink
test: backend - user serializer
Browse files Browse the repository at this point in the history
user serializer
  • Loading branch information
rkshaon committed Aug 3, 2024
1 parent 5dae30c commit 05bffca
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions backend/user_api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from user_api.models import User

from user_api.serializers import UserSerializer

from user_api.views import v1 as v1_views


Expand Down Expand Up @@ -48,3 +50,39 @@ def test_user_registration_url_resolves(self):
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())
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 05bffca

Please sign in to comment.