Skip to content

Commit

Permalink
Merge branch 'feature-delete-users'
Browse files Browse the repository at this point in the history
  • Loading branch information
kalsmic committed Jun 21, 2022
2 parents 37028a0 + 5f2be3a commit 0bd8327
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,25 @@ def update_user(user_id):
return jsonify({'message': 'User not found'}), 404
return jsonify(user=user.serialize,
message="User Updated Successfully"), 200


@users_bp.route('/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
"""
Delete a user.
___
parameters:
user_id: The id of the user.
returns:
- 200: OK
The user object.
- 404: Not Found
message:The user with the given id was not found.
"""
try:
user = User.query.get_or_404(user_id)
user.delete()
except NotFound:
return jsonify({'message': 'User not found'}), 404
return jsonify(message=f"User with id {user_id} deleted"), 200
23 changes: 23 additions & 0 deletions tests/test_delete_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from model import User
from tests.test_base import BaseTestCase


class DeleteTestCase(BaseTestCase):
def test_delete_user_with_invalid_user_id_returns_404(self):
"""
Should return 404 if no user exists with that id
"""
response = self.client.delete('/users/1')
self.assertEqual(response.status_code, 404)
self.assertEqual(response.json['message'], 'User not found')

def test_delete_user_with_valid_user_id_returns_200_and_message(self):
"""
Should return 200 and message if user is deleted
"""
user = self.create_dummy_user()
user_id=user.id
response = self.client.delete(f'/users/{user_id}')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json['message'], f'User with id {user.id} deleted')
self.assertIsNone(User.query.get(user_id))

0 comments on commit 0bd8327

Please sign in to comment.