From ebb94f73d6dcdb104e1a7c5b54e13ddde7cbda87 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Sun, 16 Mar 2014 21:29:20 -0300 Subject: [PATCH] delete users --- luncho/blueprints/users.py | 15 +++++++++++++++ tests/base.py | 4 ++++ tests/users_tests.py | 39 +++++++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/luncho/blueprints/users.py b/luncho/blueprints/users.py index b11405f..79035ce 100644 --- a/luncho/blueprints/users.py +++ b/luncho/blueprints/users.py @@ -68,3 +68,18 @@ def update_user(token): db.session.commit() return jsonify(status='OK') + + +@users.route('/', 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') diff --git a/tests/base.py b/tests/base.py index e91430b..f5b6f31 100644 --- a/tests/base.py +++ b/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) diff --git a/tests/users_tests.py b/tests/users_tests.py index 3c881dc..0ac0cf1 100644 --- a/tests/users_tests.py +++ b/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()