Browse Source

simplified the tests by creating common asserts

master
Julio Biason 10 years ago
parent
commit
ed838624c0
  1. 14
      luncho/helpers.py
  2. 19
      tests/base.py
  3. 50
      tests/group_tests.py
  4. 51
      tests/users_tests.py

14
luncho/helpers.py

@ -20,10 +20,7 @@ class ForceJSON(object):
def check_json(*args, **kwargs):
json = request.get_json(force=True, silent=True)
if not json:
resp = jsonify(status='ERROR',
error='Request MUST be in JSON format')
resp.status_code = 400
return resp
return JSONError(400, 'Request MUST be in JSON format')
# now we have the JSON, let's check if all the fields are here.
missing = []
@ -32,12 +29,7 @@ class ForceJSON(object):
missing.append(field)
if missing:
fields = ', '.join(missing)
error = 'Missing fields: {fields}'.format(fields=fields)
resp = jsonify(status='ERROR',
error=error)
resp.status_code = 400
return resp
return JSONError(400, 'Missing fields', fields=missing)
return func(*args, **kwargs)
return check_json
@ -56,7 +48,7 @@ def JSONError(status, message, **kwargs):
:return: A response with the JSON and the status code."""
resp = jsonify(status='ERROR',
error=message,
message=message,
**kwargs)
resp.status_code = status
return resp

19
tests/base.py

@ -52,6 +52,25 @@ class LunchoTests(unittest.TestCase):
"""Check the status code of the response."""
self.assertEqual(response.status_code, status)
def assertJsonOk(self, response, **extras):
"""Assert the the response is an OK. Extra fields can be expected
in the `extras` parameter."""
expected = {'status': 'OK'}
if extras:
expected.update(extras)
self.assertStatusCode(response, 200)
self.assertJson(response, expected)
def assertJsonError(self, response, status, message, **extras):
"""Assert that the response is an error. Extra fields returned in
the JSON can be expected in the `extras` parameter."""
expected = {'status': 'ERROR', 'message': message}
if extras:
expected.update(extras)
self.assertStatusCode(response, status)
self.assertJson(response, expected)
# ------------------------------------------------------------
# Easy way to convert the data to JSON and do requests
# ------------------------------------------------------------

50
tests/group_tests.py

@ -29,21 +29,14 @@ class TestGroups(LunchoTests):
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(rv, expected)
self.assertJsonOk(rv, groups=[])
def test_create_group(self):
"""Test creating a group."""
request = {'name': 'Test group'}
rv = self.put('/group/{token}/'.format(token=self.user.token),
request)
expected = {'status': 'OK', 'id': 1} # always 1 'cause the database
# is erased on every test
self.assertStatusCode(rv, 200)
self.assertJson(rv, expected)
self.assertJsonOk(rv, id=1)
def test_create_group_unverified_account(self):
"""Try creating a group with an account that's not verified yet."""
@ -53,23 +46,16 @@ class TestGroups(LunchoTests):
request = {'name': 'Test group'}
rv = self.put('/group/{token}/'.format(token=self.user.token),
request)
expected = {'status': 'ERROR',
'error': 'Account not verified'}
self.assertStatusCode(rv, 412)
self.assertJson(rv, expected)
self.assertJsonError(rv, 412, 'Account not verified')
def test_user_in_own_group(self):
"""The user must belong to a group it owns."""
token = self.user.token
self.test_create_group()
rv = self.get('/group/{token}/'.format(token=token))
expected = {'status': 'OK',
'groups': [{'id': 1,
'name': 'Test group',
'admin': True}]}
self.assertStatusCode(rv, 200)
self.assertJson(rv, expected)
self.assertJsonOk(rv, groups=[{'id': 1,
'name': 'Test group',
'admin': True}])
class TestExistingGroups(LunchoTests):
@ -100,9 +86,7 @@ class TestExistingGroups(LunchoTests):
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(rv, expected)
self.assertJsonOk(rv)
# check the database
group = Group.query.get(groupId)
@ -124,9 +108,7 @@ class TestExistingGroups(LunchoTests):
token=self.user.token,
groupId=self.group.id),
request)
expected = {'status': 'OK'}
self.assertStatusCode(rv, 200)
self.assertJson(rv, expected)
self.assertJsonOk(rv)
# check the database
group = Group.query.get(groupId)
@ -140,9 +122,7 @@ class TestExistingGroups(LunchoTests):
token=self.user.token,
groupId=groupId),
request)
expected = {'status': 'ERROR', 'error': 'Group not found'}
self.assertStatusCode(rv, 404)
self.assertJson(rv, expected)
self.assertJsonError(rv, 404, 'Group not found')
def test_delete_group(self):
"""Delete a group."""
@ -150,9 +130,7 @@ class TestExistingGroups(LunchoTests):
rv = self.delete('/group/{token}/{groupId}/'.format(
token=self.user.token,
groupId=groupId))
expected = {'status': 'OK'}
self.assertStatusCode(rv, 200)
self.assertJson(rv, expected)
self.assertJsonOk(rv)
def test_delete_unknown_group(self):
"""Delete a group that doesn't exist."""
@ -160,9 +138,7 @@ class TestExistingGroups(LunchoTests):
rv = self.delete('/group/{token}/{groupId}/'.format(
token=self.user.token,
groupId=groupId))
expected = {'status': 'ERROR', 'error': 'Group not found'}
self.assertStatusCode(rv, 404)
self.assertJson(rv, expected)
self.assertJsonError(rv, 404, 'Group not found')
def test_delete_not_admin(self):
"""Try to delete a group when the user is not the admin."""
@ -176,9 +152,7 @@ class TestExistingGroups(LunchoTests):
rv = self.delete('/group/{token}/{groupId}/'.format(
token=new_user.token,
groupId=self.group.id))
expected = {'status': 'ERROR', 'error': 'User is not admin'}
self.assertStatusCode(rv, 401)
self.assertJson(rv, expected)
self.assertJsonError(rv, 401, 'User is not admin')
if __name__ == '__main__':
unittest.main()

51
tests/users_tests.py

@ -20,9 +20,7 @@ class TestUsers(LunchoTests):
'full_name': 'full name',
'password': 'hash'}
rv = self.put('/user/', request)
self.assertStatusCode(rv, 200)
self.assertJson(rv, {'status': 'OK'})
self.assertJsonOk(rv)
# db check
self.assertIsNotNone(User.query.filter_by(username='username').first())
@ -36,30 +34,19 @@ class TestUsers(LunchoTests):
'full_name': 'full name',
'password': 'hash'}
rv = self.put('/user/', data=request)
expected = {"status": "ERROR",
"error": "Username already exists"}
self.assertStatusCode(rv, 409)
self.assertJson(rv, expected)
self.assertJsonError(rv, 409, 'Username already exists')
def test_no_json(self):
"""Do a request that it's not JSON."""
rv = self.put('/user/', '')
expected = {"error": "Request MUST be in JSON format",
"status": "ERROR"}
self.assertStatusCode(rv, 400)
self.assertJson(rv, expected)
self.assertJsonError(rv, 400, 'Request MUST be in JSON format')
def test_missing_fields(self):
"""Send a request with missing fields."""
request = {'password': 'hash'}
rv = self.put('/user/', request)
expected = {'error': 'Missing fields: username, full_name',
'status': 'ERROR'}
self.assertStatusCode(rv, 400)
self.assertJson(rv, expected)
self.assertJsonError(rv, 400, 'Missing fields', fields=['username',
'full_name'])
class TestExistingUsers(LunchoTests):
@ -84,8 +71,7 @@ class TestExistingUsers(LunchoTests):
request)
expected = {'status': 'OK'}
self.assertStatusCode(rv, 200)
self.assertJson(rv, expected)
self.assertJsonOk(rv)
# check in the database
user = User.query.filter_by(username='test').first()
@ -99,10 +85,7 @@ class TestExistingUsers(LunchoTests):
rv = self.post('/user/{token}/'.format(token='no-token'),
request)
expected = {'status': 'ERROR',
'error': 'User not found (via token)'}
self.assertStatusCode(rv, 404)
self.assertJson(rv, expected)
self.assertJsonError(rv, 404, 'User not found (via token)')
def test_expired_token(self):
"""Send a token that exists but it's not valid for today."""
@ -116,18 +99,12 @@ class TestExistingUsers(LunchoTests):
rv = self.post('/user/{token}/'.format(token=self.user.token),
request)
expected = {'status': 'ERROR',
'error': 'Invalid token'}
self.assertStatusCode(rv, 400)
self.assertJson(rv, expected)
self.assertJsonError(rv, 400, 'Invalid token')
def test_delete_user(self):
"""Delete a user."""
rv = self.delete('/user/{token}/'.format(token=self.user.token))
expected = {'status': 'OK'}
self.assertStatusCode(rv, 200)
self.assertJson(rv, expected)
self.assertJsonOk(rv)
# check the database
user = User.query.filter_by(username='test').first()
@ -137,10 +114,7 @@ class TestExistingUsers(LunchoTests):
"""Send a delete to a non-existing token."""
rv = self.delete('/user/{token}/'.format(token='no-token'))
expected = {'status': 'ERROR',
'error': 'User not found (via token)'}
self.assertStatusCode(rv, 404)
self.assertJson(rv, expected)
self.assertJsonError(rv, 404, 'User not found (via token)')
def test_delete_expired_token(self):
"""Send a delete to a token for yesterday."""
@ -150,10 +124,7 @@ class TestExistingUsers(LunchoTests):
rv = self.delete('/user/{token}/'.format(token=self.user.token))
expected = {'status': 'ERROR',
'error': 'Invalid token'}
self.assertStatusCode(rv, 400)
self.assertJson(rv, expected)
self.assertJsonError(rv, 400, 'Invalid token')
if __name__ == '__main__':

Loading…
Cancel
Save