diff --git a/luncho/blueprints/groups.py b/luncho/blueprints/groups.py index e15f645..221fb3a 100644 --- a/luncho/blueprints/groups.py +++ b/luncho/blueprints/groups.py @@ -55,17 +55,14 @@ def create_group(token): return JSONError(412, 'Account not verified') json = request.get_json(force=True) - try: - new_group = Group(name=json['name'], - owner=user.username) + new_group = Group(name=json['name'], + owner=user.username) - LOG.debug('Current user groups: {groups}'.format(groups=user.groups)) - user.groups.append(new_group) + LOG.debug('Current user groups: {groups}'.format(groups=user.groups)) + user.groups.append(new_group) - db.session.add(new_group) - db.session.commit() - except IntegrityError: - return JSONError(500, 'Unknown error') + db.session.add(new_group) + db.session.commit() return jsonify(status='OK', id=new_group.id) diff --git a/luncho/server.py b/luncho/server.py index 635d6bf..aba66e7 100644 --- a/luncho/server.py +++ b/luncho/server.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -import sys import logging import json import hmac @@ -106,11 +105,3 @@ app.register_blueprint(index, url_prefix='/') app.register_blueprint(token, url_prefix='/token/') app.register_blueprint(users, url_prefix='/user/') app.register_blueprint(groups, url_prefix='/group/') - - -# ---------------------------------------------------------------------- -# Main -# ---------------------------------------------------------------------- -if __name__ == '__main__': - log.warning('Use manage.py to run the server.') - sys.exit(1) diff --git a/tests/group_tests.py b/tests/group_tests.py index 8f2284b..2ec20a9 100644 --- a/tests/group_tests.py +++ b/tests/group_tests.py @@ -57,6 +57,35 @@ class TestGroups(LunchoTests): 'name': 'Test group', 'admin': True}]) + def test_get_groups_unknown_token(self): + """Request groups with an unknown token.""" + rv = self.get('/group/{token}/'.format(token='invalid')) + self.assertJsonError(rv, 404, 'User not found (via token)') + + def test_get_groups_expired_token(self): + """Request groups with an expired token.""" + self.user.token = 'expired' + server.db.session.commit() + + rv = self.get('/group/{token}/'.format(token=self.user.token)) + self.assertJsonError(rv, 400, 'Invalid token') + + def test_create_group_unknown_token(self): + """Try to create a group with an invalid token.""" + request = {'name': 'Test group'} + rv = self.put('/group/{token}/'.format(token='invalid'), + request) + self.assertJsonError(rv, 404, 'User not found (via token)') + + def test_create_group_expired_token(self): + self.user.token = 'expired' + server.db.session.commit() + + request = {'name': 'Test group'} + rv = self.put('/group/{token}/'.format(token=self.user.token), + request) + self.assertJsonError(rv, 400, 'Invalid token') + class TestExistingGroups(LunchoTests): """Test for existing groups.""" @@ -92,6 +121,26 @@ class TestExistingGroups(LunchoTests): group = Group.query.get(groupId) self.assertEqual(group.name, request['name']) + def test_update_name_invalid_token(self): + """Try to change the name with an unknown token.""" + groupId = self.group.id + request = {'name': 'New test group'} + rv = self.post('/group/{token}/{groupId}/'.format(token='invalid', + groupId=self.group.id), + request) + self.assertJsonError(rv, 404, 'User not found (via token)') + + def test_update_name_expired_token(self): + """Try to change the name with an expired token.""" + self.user.token = 'expired' + server.db.session.commit() + + request = {'name': 'New test group'} + rv = self.post('/group/{token}/{groupId}/'.format(token=self.user.token, + groupId=self.group.id), + request) + self.assertJsonError(rv, 400, 'Invalid token') + def test_update_owner(self): """Change the group owner.""" new_user = User(username='another_user', @@ -114,6 +163,18 @@ class TestExistingGroups(LunchoTests): group = Group.query.get(groupId) self.assertEqual(group.owner, new_username) + def test_update_owner_invalid(self): + """Try to change the owner to a user that doesn't exist.""" + groupId = self.group.id + current_owner = self.group.owner + + request = {'maintainer': 'unknown'} + rv = self.post('/group/{token}/{groupId}/'.format( + token=self.user.token, + groupId=self.group.id + ), request) + self.assertJsonError(rv, 401, 'New maintainer not found') + def test_update_unknown_group(self): """Try to update a group that doesn't exist.""" groupId = self.group.id + 10 @@ -154,5 +215,12 @@ class TestExistingGroups(LunchoTests): groupId=self.group.id)) self.assertJsonError(rv, 401, 'User is not admin') + def test_delete_invalid_token(self): + """Try to delete a group with an unknown token.""" + rv = self.delete('/group/{token}/{groupId}/'.format( + token='invalid', + groupId=self.group.id)) + self.assertJsonError(rv, 404, 'User not found (via token)') + if __name__ == '__main__': unittest.main()