Browse Source

added places works (as tested)

master
Julio Biason 11 years ago
parent
commit
657850a381
  1. 4
      luncho/blueprints/groups.py
  2. 2
      luncho/server.py
  3. 109
      tests/group_tests.py

4
luncho/blueprints/groups.py

@ -445,9 +445,11 @@ def group_add_places(group_id):
""" """
group = Group.query.get(group_id) group = Group.query.get(group_id)
if not group: if not group:
LOG.debug('Cant find group with id {group_id}'.format(
group_id=group_id))
raise ElementNotFoundException('Group') raise ElementNotFoundException('Group')
if not group.owner == request.user: if not group.owner == request.user.username:
raise UserIsNotAdminException() raise UserIsNotAdminException()
not_found = [] not_found = []

2
luncho/server.py

@ -128,12 +128,14 @@ from blueprints.users import users
from blueprints.token import token from blueprints.token import token
from blueprints.groups import groups from blueprints.groups import groups
from blueprints.groups import group_users from blueprints.groups import group_users
from blueprints.groups import group_places
from blueprints.places import places from blueprints.places import places
app.register_blueprint(token, url_prefix='/token/') app.register_blueprint(token, url_prefix='/token/')
app.register_blueprint(users, url_prefix='/user/') app.register_blueprint(users, url_prefix='/user/')
app.register_blueprint(groups, url_prefix='/group/') app.register_blueprint(groups, url_prefix='/group/')
app.register_blueprint(group_users, url_prefix='/group/') app.register_blueprint(group_users, url_prefix='/group/')
app.register_blueprint(group_places, url_prefix='/group/')
app.register_blueprint(places, url_prefix='/place/') app.register_blueprint(places, url_prefix='/place/')

109
tests/group_tests.py

@ -9,6 +9,7 @@ from luncho import server
from luncho.server import User from luncho.server import User
from luncho.server import Group from luncho.server import Group
from luncho.server import Place
from base import LunchoTests from base import LunchoTests
@ -126,22 +127,22 @@ class TestExistingGroups(LunchoTests):
def test_update_name(self): def test_update_name(self):
"""Change the group name.""" """Change the group name."""
groupId = self.group.id group_id = self.group.id
request = {'name': 'New test group'} request = {'name': 'New test group'}
rv = self.put('/group/{groupId}/'.format(groupId=self.group.id), rv = self.put('/group/{group_id}/'.format(group_id=self.group.id),
request, request,
token=self.user.token) token=self.user.token)
self.assertJsonOk(rv) self.assertJsonOk(rv)
# check the database # check the database
group = Group.query.get(groupId) group = Group.query.get(group_id)
self.assertEqual(group.name, request['name']) self.assertEqual(group.name, request['name'])
return return
def test_update_name_invalid_token(self): def test_update_name_invalid_token(self):
"""Try to change the name with an unknown token.""" """Try to change the name with an unknown token."""
request = {'name': 'New test group'} request = {'name': 'New test group'}
rv = self.put('/group/{groupId}/'.format(groupId=self.group.id), rv = self.put('/group/{group_id}/'.format(group_id=self.group.id),
request, request,
token='invalid') token='invalid')
self.assertJsonError(rv, 404, 'User not found (via token)') self.assertJsonError(rv, 404, 'User not found (via token)')
@ -153,7 +154,7 @@ class TestExistingGroups(LunchoTests):
server.db.session.commit() server.db.session.commit()
request = {'name': 'New test group'} request = {'name': 'New test group'}
rv = self.put('/group/{groupId}/'.format(groupId=self.group.id), rv = self.put('/group/{group_id}/'.format(group_id=self.group.id),
request, request,
token=self.user.token) token=self.user.token)
self.assertJsonError(rv, 400, 'Invalid token') self.assertJsonError(rv, 400, 'Invalid token')
@ -167,24 +168,24 @@ class TestExistingGroups(LunchoTests):
server.db.session.add(new_user) server.db.session.add(new_user)
server.db.session.commit() server.db.session.commit()
groupId = self.group.id group_id = self.group.id
new_username = new_user.username new_username = new_user.username
request = {'admin': new_user.username} request = {'admin': new_user.username}
rv = self.put('/group/{groupId}/'.format(groupId=groupId), rv = self.put('/group/{group_id}/'.format(group_id=group_id),
request, request,
token=self.user.token) token=self.user.token)
self.assertJsonOk(rv) self.assertJsonOk(rv)
# check the database # check the database
group = Group.query.get(groupId) group = Group.query.get(group_id)
self.assertEqual(group.owner, new_username) self.assertEqual(group.owner, new_username)
return return
def test_update_owner_invalid(self): def test_update_owner_invalid(self):
"""Try to change the owner to a user that doesn't exist.""" """Try to change the owner to a user that doesn't exist."""
request = {'admin': 'unknown'} request = {'admin': 'unknown'}
rv = self.put('/group/{groupId}/'.format(groupId=self.group.id), rv = self.put('/group/{group_id}/'.format(group_id=self.group.id),
request, request,
token=self.user.token) token=self.user.token)
self.assertJsonError(rv, 404, 'New admin not found') self.assertJsonError(rv, 404, 'New admin not found')
@ -192,9 +193,9 @@ class TestExistingGroups(LunchoTests):
def test_update_unknown_group(self): def test_update_unknown_group(self):
"""Try to update a group that doesn't exist.""" """Try to update a group that doesn't exist."""
groupId = self.group.id + 10 group_id = self.group.id + 10
request = {'name': 'New test group'} request = {'name': 'New test group'}
rv = self.put('/group/{groupId}/'.format(groupId=groupId), rv = self.put('/group/{group_id}/'.format(group_id=group_id),
request, request,
token=self.user.token) token=self.user.token)
self.assertJsonError(rv, 404, 'Group not found') self.assertJsonError(rv, 404, 'Group not found')
@ -208,7 +209,7 @@ class TestExistingGroups(LunchoTests):
verified=True, verified=True,
create_token=True) create_token=True)
request = {'name': 'A new name'} request = {'name': 'A new name'}
rv = self.put('/group/{groupId}/'.format(groupId=self.group.id), rv = self.put('/group/{group_id}/'.format(group_id=self.group.id),
request, request,
token=new_user.token) token=new_user.token)
self.assertJsonError(rv, 403, 'User is not admin') self.assertJsonError(rv, 403, 'User is not admin')
@ -216,16 +217,16 @@ class TestExistingGroups(LunchoTests):
def test_delete_group(self): def test_delete_group(self):
"""Delete a group.""" """Delete a group."""
groupId = self.group.id group_id = self.group.id
rv = self.delete('/group/{groupId}/'.format(groupId=groupId), rv = self.delete('/group/{group_id}/'.format(group_id=group_id),
token=self.user.token) token=self.user.token)
self.assertJsonOk(rv) self.assertJsonOk(rv)
return return
def test_delete_unknown_group(self): def test_delete_unknown_group(self):
"""Delete a group that doesn't exist.""" """Delete a group that doesn't exist."""
groupId = self.group.id + 10 group_id = self.group.id + 10
rv = self.delete('/group/{groupId}/'.format(groupId=groupId), rv = self.delete('/group/{group_id}/'.format(group_id=group_id),
token=self.user.token) token=self.user.token)
self.assertJsonError(rv, 404, 'Group not found') self.assertJsonError(rv, 404, 'Group not found')
return return
@ -239,14 +240,14 @@ class TestExistingGroups(LunchoTests):
server.db.session.commit() server.db.session.commit()
new_user.get_token() new_user.get_token()
rv = self.delete('/group/{groupId}/'.format(groupId=self.group.id), rv = self.delete('/group/{group_id}/'.format(group_id=self.group.id),
token=new_user.token) token=new_user.token)
self.assertJsonError(rv, 403, 'User is not admin') self.assertJsonError(rv, 403, 'User is not admin')
return return
def test_delete_invalid_token(self): def test_delete_invalid_token(self):
"""Try to delete a group with an unknown token.""" """Try to delete a group with an unknown token."""
rv = self.delete('/group/{groupId}/'.format(groupId=self.group.id), rv = self.delete('/group/{group_id}/'.format(group_id=self.group.id),
token='invalid') token='invalid')
self.assertJsonError(rv, 404, 'User not found (via token)') self.assertJsonError(rv, 404, 'User not found (via token)')
return return
@ -287,7 +288,8 @@ class TestUsersInGroup(LunchoTests):
request = {'usernames': [new_user.username]} request = {'usernames': [new_user.username]}
rv = self.put('/group/{groupId}/users/'.format(groupId=self.group.id), url = '/group/{group_id}/users/'.format(group_id=self.group.id)
rv = self.put(url,
request, request,
token=self.user.token) token=self.user.token)
self.assertJsonOk(rv) self.assertJsonOk(rv)
@ -304,7 +306,8 @@ class TestUsersInGroup(LunchoTests):
request = {'usernames': [new_user.username]} request = {'usernames': [new_user.username]}
rv = self.put('/group/{groupId}/users/'.format(groupId=self.group.id), url = '/group/{group_id}/users/'.format(group_id=self.group.id)
rv = self.put(url,
request, request,
token=new_user.token) token=new_user.token)
self.assertJsonError(rv, 403, 'User is not admin') self.assertJsonError(rv, 403, 'User is not admin')
@ -313,7 +316,8 @@ class TestUsersInGroup(LunchoTests):
def test_add_no_such_user(self): def test_add_no_such_user(self):
"""Try to add an unknown user to the group.""" """Try to add an unknown user to the group."""
request = {'usernames': ['unknown']} request = {'usernames': ['unknown']}
rv = self.put('/group/{groupId}/users/'.format(groupId=self.group.id), url = '/group/{group_id}/users/'.format(group_id=self.group.id)
rv = self.put(url,
request, request,
token=self.user.token) token=self.user.token)
@ -329,8 +333,8 @@ class TestUsersInGroup(LunchoTests):
"""Try to add users to some unknown group.""" """Try to add users to some unknown group."""
# the usernames are worthless, group not found should kick first # the usernames are worthless, group not found should kick first
request = {'usernames': ['unkonwn']} request = {'usernames': ['unkonwn']}
groupId = self.group.id + 10 group_id = self.group.id + 10
rv = self.put('/group/{groupId}/users/'.format(groupId=groupId), rv = self.put('/group/{group_id}/users/'.format(group_id=group_id),
request, request,
token=self.user.token) token=self.user.token)
self.assertJsonError(rv, 404, 'Group not found') self.assertJsonError(rv, 404, 'Group not found')
@ -340,7 +344,8 @@ class TestUsersInGroup(LunchoTests):
"""Try to get a list of group members.""" """Try to get a list of group members."""
username = self.user.username username = self.user.username
fullname = self.user.fullname fullname = self.user.fullname
rv = self.get('/group/{groupId}/users/'.format(groupId=self.group.id), url = '/group/{group_id}/users/'.format(group_id=self.group.id)
rv = self.get(url,
token=self.user.token) token=self.user.token)
self.assertJsonOk(rv) self.assertJsonOk(rv)
json = loads(rv.data) json = loads(rv.data)
@ -360,7 +365,8 @@ class TestUsersInGroup(LunchoTests):
server.db.session.commit() server.db.session.commit()
new_user.get_token() new_user.get_token()
rv = self.get('/group/{groupId}/users/'.format(groupId=self.group.id), url = '/group/{group_id}/users/'.format(group_id=self.group.id)
rv = self.get(url,
token=new_user.token) token=new_user.token)
self.assertJsonOk(rv) self.assertJsonOk(rv)
@ -378,17 +384,64 @@ class TestUsersInGroup(LunchoTests):
server.db.session.commit() server.db.session.commit()
new_user.get_token() new_user.get_token()
rv = self.get('/group/{groupId}/users/'.format(groupId=self.group.id), url = '/group/{group_id}/users/'.format(group_id=self.group.id)
rv = self.get(url,
token=new_user.token) token=new_user.token)
self.assertJsonError(rv, 403, 'User is not member of this group') self.assertJsonError(rv, 403, 'User is not member of this group')
return return
def test_unknown_group(self): def test_unknown_group(self):
"""Test trying to get members of a group that doesn't exist.""" """Test trying to get members of a group that doesn't exist."""
groupId = self.group.id + 10 group_id = self.group.id + 10
rv = self.get('/group/{groupId}/users/'.format(groupId=groupId), rv = self.get('/group/{group_id}/users/'.format(group_id=group_id),
token=self.user.token) token=self.user.token)
self.assertJsonError(rv, 404, 'Group not found') self.assertJsonError(rv, 404, 'Group not found')
return
class TestPlacesInGroup(LunchoTests):
"""Test the integration between groups and places."""
def setUp(self):
super(TestPlacesInGroup, self).setUp()
self.default_user()
return
def tearDown(self):
super(TestPlacesInGroup, self).tearDown()
return
def _group(self):
"""Add a default group."""
group = Group(name='Test group',
owner=self.user)
server.db.session.add(group)
self.user.groups.append(group)
server.db.session.commit()
return group
def _place(self, user=None):
"""Add a default place, linked to the user."""
if not user:
user = self.user
place = Place(name='Place',
owner=self.user)
server.db.session.add(place)
server.db.session.commit()
return place
def test_add_place(self):
"""Add a place to the group."""
place = self._place()
group = self._group()
request = {'places': [place.id]}
group_id = group.id
rv = self.post('/group/{group_id}/places/'.format(group_id=group_id),
request,
token=self.user.token)
self.assertJsonOk(rv)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save