diff --git a/backend/user_api/tests.py b/backend/user_api/tests.py index ba45ff1..b80dfde 100644 --- a/backend/user_api/tests.py +++ b/backend/user_api/tests.py @@ -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 @@ -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']))