Julio Biason
11 years ago
6 changed files with 101 additions and 5 deletions
@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python |
||||
# -*- encoding: utf-8 -*- |
||||
|
||||
"""Group management.""" |
||||
|
||||
from flask import Blueprint |
||||
# from flask import request |
||||
from flask import jsonify |
||||
|
||||
# from luncho.helpers import ForceJSON |
||||
from luncho.helpers import JSONError |
||||
|
||||
from luncho.server import User |
||||
# from luncho.server import Group |
||||
|
||||
groups = Blueprint('groups', __name__) |
||||
|
||||
|
||||
@groups.route('<token>/', methods=['GET']) |
||||
def user_groups(token): |
||||
"""Return a list of the groups the user belongs or it's the owner.""" |
||||
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') |
||||
|
||||
groups = {} |
||||
for group in user.groups: |
||||
groups[group.id] = {'id': group.id, |
||||
'name': group.name, |
||||
'admin': group.owner.username == user.username} |
||||
|
||||
return jsonify(status='OK', |
||||
groups=groups.values()) |
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python |
||||
# -*- encoding: utf-8 -*- |
||||
|
||||
import unittest |
||||
|
||||
from luncho import server |
||||
|
||||
from luncho.server import User |
||||
|
||||
from base import LunchoTests |
||||
|
||||
|
||||
class TestGroups(LunchoTests): |
||||
"""Test groups requests.""" |
||||
|
||||
def setUp(self): |
||||
super(TestGroups, self).setUp() |
||||
# create a user to have a token |
||||
self.user = User(username='test', |
||||
fullname='Test User', |
||||
passhash='hash') |
||||
server.db.session.add(self.user) |
||||
server.db.session.commit() |
||||
self.user.get_token() |
||||
return |
||||
|
||||
def test_empty_list(self): |
||||
"""Get an empty list from a user without groups.""" |
||||
rv = self.get('/group/{token}/'.format(token=self.user.token)) |
||||
expected = {'status': 'OK', |
||||
'groups': []} |
||||
self.assertStatusCode(rv, 200) |
||||
self.assertJson(expected, rv.data) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
Loading…
Reference in new issue