From 36ad075d2be6ea8fb3fadfabccb891cc7b06d236 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Sun, 23 Mar 2014 22:25:09 -0300 Subject: [PATCH] update groups --- luncho/blueprints/groups.py | 26 ++++++++++++++++ tests/group_tests.py | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/luncho/blueprints/groups.py b/luncho/blueprints/groups.py index cde105c..1a5a3a9 100644 --- a/luncho/blueprints/groups.py +++ b/luncho/blueprints/groups.py @@ -69,3 +69,29 @@ def create_group(token): return jsonify(status='OK', id=new_group.id) + + +@groups.route('//', methods=['POST']) +@ForceJSON() +def update_group(token, groupId): + """Update group information.""" + (user, error) = user_or_error(token) + if error: + return error + + group = Group.query.get(groupId) + if not group: + return JSONError(404, 'Group not found') + + json = request.get_json(force=True) + if 'name' in json: + group.name = json['name'] + + if 'maintainer' in json: + new_maintainer = User.query.get(json['maintainer']) + if not new_maintainer: + return JSONError(401, 'New maintainer not found') + group.owner = new_maintainer.username + + db.session.commit() + return jsonify(status='OK') diff --git a/tests/group_tests.py b/tests/group_tests.py index d253f56..694849b 100644 --- a/tests/group_tests.py +++ b/tests/group_tests.py @@ -6,6 +6,7 @@ import unittest from luncho import server from luncho.server import User +from luncho.server import Group from base import LunchoTests @@ -70,5 +71,65 @@ class TestGroups(LunchoTests): self.assertStatusCode(rv, 200) self.assertJson(expected, rv.data) + +class TestExistingGroups(LunchoTests): + """Test for existing groups.""" + def setUp(self): + super(TestExistingGroups, self).setUp() + # create a user to have a token + self.user = User(username='test', + fullname='Test User', + passhash='hash') + self.user.verified = True + server.db.session.add(self.user) + + # create a group for the user + self.group = Group(name='Test group', + owner=self.user.username) + server.db.session.add(self.group) + server.db.session.commit() + self.user.get_token() + + def tearDown(self): + super(TestExistingGroups, self).tearDown() + + def test_update_name(self): + """Change the group name.""" + groupId = self.group.id + request = {'name': 'New test group'} + rv = self.post('/group/{token}/{groupId}/'.format(token=self.user.token, + groupId=self.group.id), + request) + expected = {'status': 'OK'} + self.assertStatusCode(rv, 200) + self.assertJson(expected, rv.data) + + # check the database + group = Group.query.get(groupId) + self.assertEqual(group.name, request['name']) + + def test_update_owner(self): + """Change the group owner.""" + new_user = User(username='another_user', + fullname='Another user', + passhash='hash') + server.db.session.add(new_user) + server.db.session.commit() + + groupId = self.group.id + new_username = new_user.username + + request = {'maintainer': new_user.username} + rv = self.post('/group/{token}/{groupId}/'.format(token=self.user.token, + groupId=self.group.id), + request) + expected = {'status': 'OK'} + self.assertStatusCode(rv, 200) + self.assertJson(expected, rv.data) + + # check the database + group = Group.query.get(groupId) + self.assertEqual(group.owner, new_username) + if __name__ == '__main__': unittest.main()