Skip to content

Commit

Permalink
fixed bug on testing due to indentation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bcg0006 committed Apr 24, 2024
1 parent 636fb28 commit c20de57
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
Empty file removed app/user/migrations/__init__.py
Empty file.
32 changes: 16 additions & 16 deletions app/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''



Expand Down
10 changes: 7 additions & 3 deletions app/user/tests/test_user_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/user/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
URL mapping for the user app
"""

from django.urls import path

from user import views

app_name = 'user'
Expand Down

0 comments on commit c20de57

Please sign in to comment.