Browse Source

delete users

master
Julio Biason 10 years ago
parent
commit
ebb94f73d6
  1. 15
      luncho/blueprints/users.py
  2. 4
      tests/base.py
  3. 39
      tests/users_tests.py

15
luncho/blueprints/users.py

@ -68,3 +68,18 @@ def update_user(token):
db.session.commit()
return jsonify(status='OK')
@users.route('<token>/', methods=['DELETE'])
def delete_user(token):
"""Delete a user. No confirmation is send."""
user = User.query.filter_by(token=token).first()
if not user:
return JSONError(404, 'User not found (via token)')
if not user.valid_token(token):
return JSONError(400, 'Invalid token')
db.session.delete(user)
db.session.commit()
return jsonify(status='OK')

4
tests/base.py

@ -64,3 +64,7 @@ class LunchoTests(unittest.TestCase):
return self.app.put(url,
data=json.dumps(data),
content_type='application/json')
def delete(self, url):
"""Send a DELETE request to the URL. There is no data to be send."""
return self.app.delete(url)

39
tests/users_tests.py

@ -28,8 +28,7 @@ class TestUsers(LunchoTests):
self.assertIsNotNone(User.query.filter_by(username='username').first())
def test_duplicate_user(self):
"""Check the status for trying to create a user that it is already
in the database."""
"""Create a user that it is already in the database."""
self.test_create_user() # create the first user
# now duplicate
@ -44,7 +43,7 @@ class TestUsers(LunchoTests):
self.assertJson(expected, rv.data)
def test_no_json(self):
"""Check the status when doing a request that it's not JSON."""
"""Do a request that it's not JSON."""
rv = self.put('/user/', '')
expected = {"error": "Request MUST be in JSON format",
@ -122,6 +121,40 @@ class TestExistingUsers(LunchoTests):
self.assertStatusCode(rv, 400)
self.assertJson(expected, rv.data)
def test_delete_user(self):
"""Delete a user."""
rv = self.delete('/user/{token}/'.format(token=self.user.token))
expected = {'status': 'OK'}
self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data)
# check the database
user = User.query.filter_by(username='test').first()
self.assertIsNone(user)
def test_delete_wrong_token(self):
"""Send a delete to a non-existing token."""
rv = self.delete('/user/{token}/'.format(token='no-token'))
expected = {'status': 'ERROR',
'error': 'User not found (via token)'}
self.assertStatusCode(rv, 404)
self.assertJson(expected, rv.data)
def test_delete_expired_token(self):
"""Send a delete to a token for yesterday."""
# see note on `test_expired_token`
self.user.token = 'expired'
server.db.session.commit()
rv = self.delete('/user/{token}/'.format(token=self.user.token))
expected = {'status': 'ERROR',
'error': 'Invalid token'}
self.assertStatusCode(rv, 400)
self.assertJson(expected, rv.data)
if __name__ == '__main__':
unittest.main()

Loading…
Cancel
Save