Browse Source

100% coverage

master
Julio Biason 11 years ago
parent
commit
f9c043d18b
  1. 9
      luncho/blueprints/places.py
  2. 4
      luncho/exceptions.py
  3. 22
      tests/general_tests.py
  4. 60
      tests/place_tests.py

9
luncho/blueprints/places.py

@ -90,17 +90,16 @@ def get_places():
:statuscode 412: Authorization required :statuscode 412: Authorization required
(:py:class:`AuthorizationRequiredException`) (:py:class:`AuthorizationRequiredException`)
""" """
user = request.user
places = {} places = {}
for group in user.groups: for group in request.user.groups:
for place in group.places: for place in group.places:
maintainer = place.owner == user.username maintainer = place.owner == request.user.username
places[place.id] = {'id': place.id, places[place.id] = {'id': place.id,
'name': place.name, 'name': place.name,
'maintainer': maintainer} 'maintainer': maintainer}
for place in Place.query.filter_by(owner=user.username): for place in Place.query.filter_by(owner=request.user.username):
maintainer = place.owner == user.username maintainer = place.owner == request.user.username
places[place.id] = {'id': place.id, places[place.id] = {'id': place.id,
'name': place.name, 'name': place.name,
'maintainer': maintainer} 'maintainer': maintainer}

4
luncho/exceptions.py

@ -116,12 +116,12 @@ class AuthorizationRequiredException(LunchoException):
HTTP/1.1 401 Unauthorized HTTP/1.1 401 Unauthorized
Content-Type: text/json Content-Type: text/json
{ "status": "ERROR": "message": "Request requires authorization" } { "status": "ERROR": "message": "Request requires authentication" }
""" """
def __init__(self): def __init__(self):
super(AuthorizationRequiredException, self).__init__() super(AuthorizationRequiredException, self).__init__()
self.status = 401 self.status = 401
self.message = 'Request requires authorization' self.message = 'Request requires authentication'
class AccountNotVerifiedException(LunchoException): class AccountNotVerifiedException(LunchoException):

22
tests/general_tests.py

@ -0,0 +1,22 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from base import LunchoTests
class TestLuncho(LunchoTests):
"""Test things that are in the base system, not the blueprints
(although the blueprints are required to create the URLs to be used)."""
def setUp(self):
super(TestLuncho, self).setUp()
return
def tearDown(self):
super(TestLuncho, self).tearDown()
return
def test_auth(self):
"""Try to request an authenticated request without authentication."""
rv = self.app.get('/place/') # GET /place/ is authenticated
self.assertJsonError(rv, 401, 'Request requires authentication')

60
tests/place_tests.py

@ -8,6 +8,7 @@ from json import loads
from luncho import server from luncho import server
from luncho.server import Place from luncho.server import Place
from luncho.server import Group
from base import LunchoTests from base import LunchoTests
@ -33,6 +34,7 @@ class TestPlaces(LunchoTests):
self.assertJsonOk(rv) self.assertJsonOk(rv)
json = loads(rv.data) json = loads(rv.data)
self.assertTrue('id' in json) self.assertTrue('id' in json)
return
def test_get_places(self): def test_get_places(self):
"""Try to get the user places.""" """Try to get the user places."""
@ -44,6 +46,7 @@ class TestPlaces(LunchoTests):
json = loads(rv.data) json = loads(rv.data)
self.assertTrue('places' in json) self.assertTrue('places' in json)
self.assertEqual(len(json['places']), 1) # just the new place self.assertEqual(len(json['places']), 1) # just the new place
return
def test_create_place_not_verified(self): def test_create_place_not_verified(self):
"""Try to create a place with an unverified account.""" """Try to create a place with an unverified account."""
@ -57,6 +60,7 @@ class TestPlaces(LunchoTests):
request, request,
token=new_user.token) token=new_user.token)
self.assertJsonError(rv, 412, 'Account not verified') self.assertJsonError(rv, 412, 'Account not verified')
return
class TestExistingPlaces(LunchoTests): class TestExistingPlaces(LunchoTests):
@ -68,6 +72,7 @@ class TestExistingPlaces(LunchoTests):
owner=self.user) owner=self.user)
server.db.session.add(self.place) server.db.session.add(self.place)
server.db.session.commit() server.db.session.commit()
return
def test_update_name(self): def test_update_name(self):
"""Try to update a place.""" """Try to update a place."""
@ -81,6 +86,7 @@ class TestExistingPlaces(LunchoTests):
# check the database # check the database
place = Place.query.get(placeId) place = Place.query.get(placeId)
self.assertEqual(place.name, request['name']) self.assertEqual(place.name, request['name'])
return
def test_update_owner(self): def test_update_owner(self):
"""Update the owner of the group.""" """Update the owner of the group."""
@ -98,6 +104,7 @@ class TestExistingPlaces(LunchoTests):
# check the database # check the database
place = Place.query.get(placeId) place = Place.query.get(placeId)
self.assertEqual(place.owner, 'newUser') self.assertEqual(place.owner, 'newUser')
return
def test_update_unknown_place(self): def test_update_unknown_place(self):
"""Try to update a place that doesn't exist.""" """Try to update a place that doesn't exist."""
@ -107,6 +114,7 @@ class TestExistingPlaces(LunchoTests):
request, request,
token=self.user.token) token=self.user.token)
self.assertJsonError(rv, 404, 'Place not found') self.assertJsonError(rv, 404, 'Place not found')
return
def test_update_non_admin(self): def test_update_non_admin(self):
"""A non-admin user tries to update the place.""" """A non-admin user tries to update the place."""
@ -121,6 +129,7 @@ class TestExistingPlaces(LunchoTests):
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')
return
def test_change_non_existent_admin(self): def test_change_non_existent_admin(self):
"""Try to transfer the admin to a user that doesn't exist.""" """Try to transfer the admin to a user that doesn't exist."""
@ -129,12 +138,14 @@ class TestExistingPlaces(LunchoTests):
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')
return
def test_delete_place(self): def test_delete_place(self):
"""Delete a place.""" """Delete a place."""
rv = self.delete('/place/{placeId}/'.format(placeId=self.place.id), rv = self.delete('/place/{placeId}/'.format(placeId=self.place.id),
token=self.user.token) token=self.user.token)
self.assertJsonOk(rv) self.assertJsonOk(rv)
return
def test_delete_non_existent_place(self): def test_delete_non_existent_place(self):
"""Try to delete a place that doesn't exist.""" """Try to delete a place that doesn't exist."""
@ -142,6 +153,7 @@ class TestExistingPlaces(LunchoTests):
rv = self.delete('/place/{placeId}/'.format(placeId=placeId), rv = self.delete('/place/{placeId}/'.format(placeId=placeId),
token=self.user.token) token=self.user.token)
self.assertJsonError(rv, 404, 'Place not found') self.assertJsonError(rv, 404, 'Place not found')
return
def test_delete_non_admin(self): def test_delete_non_admin(self):
"""Try to delete the place by a non-admin user.""" """Try to delete the place by a non-admin user."""
@ -153,7 +165,55 @@ class TestExistingPlaces(LunchoTests):
rv = self.delete('/place/{placeId}/'.format(placeId=self.place.id), rv = self.delete('/place/{placeId}/'.format(placeId=self.place.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
class TestGroupPlaces(LunchoTests):
"""Test the group+places integration, but from places perspective."""
def setUp(self):
super(TestGroupPlaces, self).setUp()
self.default_user()
self.place = Place(name='Place',
owner=self.user)
server.db.session.add(self.place)
server.db.session.commit()
def tearDown(self):
super(TestGroupPlaces, self).tearDown()
def test_get_places_from_groups(self):
"""Test getting places linked to the user groups."""
# user1 owns a place and the group.
user1 = self.create_user(name='testUser',
fullname='Test User',
verified=True,
create_token=True)
place = Place(name='Place', owner=user1)
group = Group(name='Test group', owner=user1)
user1.groups.append(group)
group.places.append(place) # the group now uses that place
server.db.session.add(user1)
server.db.session.add(place)
server.db.session.add(group)
# user2 is just a member of the group
user2 = self.create_user(name='anotherUser',
fullname='Another user',
verified=True,
create_token=True)
user2.groups.append(group)
server.db.session.add(user2)
server.db.session.commit()
# now user2 should get the place in their results.
rv = self.get('/place/',
token=user2.token)
self.assertJsonOk(rv)
json = loads(rv.data)
self.assertTrue('places' in json)
self.assertEqual(len(json['places']), 1) # just the new place
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save