Browse Source

update groups

master
Julio Biason 11 years ago
parent
commit
36ad075d2b
  1. 26
      luncho/blueprints/groups.py
  2. 61
      tests/group_tests.py

26
luncho/blueprints/groups.py

@ -69,3 +69,29 @@ def create_group(token):
return jsonify(status='OK', return jsonify(status='OK',
id=new_group.id) id=new_group.id)
@groups.route('<token>/<groupId>/', 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')

61
tests/group_tests.py

@ -6,6 +6,7 @@ import unittest
from luncho import server from luncho import server
from luncho.server import User from luncho.server import User
from luncho.server import Group
from base import LunchoTests from base import LunchoTests
@ -70,5 +71,65 @@ class TestGroups(LunchoTests):
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data) 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__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save