diff --git a/app/user/migrations/__init__.py b/app/user/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/app/user/serializers.py b/app/user/serializers.py index 74c2665..20445ff 100644 --- a/app/user/serializers.py +++ b/app/user/serializers.py @@ -11,24 +11,24 @@ class UserSerializer(serializers.ModelSerializer): class Meta: model = get_user_model() - fields = ('email', 'password', 'name') + fields = ['email', 'password', 'name'] extra_kwargs = {'password': {'write_only': True, 'min_length': 5}} - def create(self, validated_data): - """Create a new user with encrypted password and return it""" - return get_user_model().objects.create_user(**validated_data) - - ''' - def update(self, instance, validated_data): - """Update a user, setting the password correctly and return it""" - password = validated_data.pop('password', None) - user = super().update(instance, validated_data) - - if password: - user.set_password(password) - user.save() - return user - ''' + def create(self, validated_data): + """Create a new user with encrypted password and return it""" + return get_user_model().objects.create_user(**validated_data) + + ''' + def update(self, instance, validated_data): + """Update a user, setting the password correctly and return it""" + password = validated_data.pop('password', None) + user = super().update(instance, validated_data) + + if password: + user.set_password(password) + user.save() + return user + ''' diff --git a/app/user/tests/test_user_api.py b/app/user/tests/test_user_api.py index d90269e..5ef8cd0 100644 --- a/app/user/tests/test_user_api.py +++ b/app/user/tests/test_user_api.py @@ -7,6 +7,8 @@ from django.urls import reverse from rest_framework.test import APIClient from rest_framework import status +from django.core.management import call_command + CREATE_USER_URL = reverse('user:create') @@ -20,25 +22,27 @@ class PublicUserApiTests(TestCase): def setUp(self): self.client = APIClient() - def test_create_valid_user_success(self): + def test_create_user_success(self): """Test creating user with valid payload is successful""" payload = { 'email': 'test@example.com', 'password': 'testpass123', 'name': 'Test Name', } + res = self.client.post(CREATE_USER_URL, payload) self.assertEqual(res.status_code, status.HTTP_201_CREATED) user = get_user_model().objects.get(email=payload['email']) + user_manager = get_user_model().objects self.assertTrue(user.check_password(payload['password'])) self.assertNotIn('password', res.data) - def test_user_exists(self): + def test_user_with_email_exists_error(self): """Test creating user that already exists fails""" payload = { 'email': 'test@example.com', - 'password': 'pw', + 'password': 'testpass123', 'name': 'Test Name', } create_user(**payload) diff --git a/app/user/urls.py b/app/user/urls.py index 630eabc..4db748d 100644 --- a/app/user/urls.py +++ b/app/user/urls.py @@ -1,8 +1,8 @@ """ URL mapping for the user app """ - from django.urls import path + from user import views app_name = 'user'