Browse Source

inverted the expected/response to make more sense with assertStatusCode

master
Julio Biason 11 years ago
parent
commit
a4cbd9bc71
  1. 11
      tests/base.py
  2. 20
      tests/group_tests.py
  3. 20
      tests/users_tests.py

11
tests/base.py

@ -28,10 +28,13 @@ class LunchoTests(unittest.TestCase):
# Common assertions for lunch-o # Common assertions for lunch-o
# ------------------------------------------------------------ # ------------------------------------------------------------
def assertJson(self, expected, response): def assertJson(self, response, expected):
"""Compare JSONs.""" """Compare JSONs.
if not isinstance(response, dict):
response = json.loads(response) :param response: a test_client response
:param expected: expected response
:type expected: dict"""
response = json.loads(response.data)
for key in expected: for key in expected:
if not key in response: if not key in response:

20
tests/group_tests.py

@ -32,7 +32,7 @@ class TestGroups(LunchoTests):
expected = {'status': 'OK', expected = {'status': 'OK',
'groups': []} 'groups': []}
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_create_group(self): def test_create_group(self):
"""Test creating a group.""" """Test creating a group."""
@ -43,7 +43,7 @@ class TestGroups(LunchoTests):
expected = {'status': 'OK', 'id': 1} # always 1 'cause the database expected = {'status': 'OK', 'id': 1} # always 1 'cause the database
# is erased on every test # is erased on every test
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_create_group_unverified_account(self): def test_create_group_unverified_account(self):
"""Try creating a group with an account that's not verified yet.""" """Try creating a group with an account that's not verified yet."""
@ -57,7 +57,7 @@ class TestGroups(LunchoTests):
expected = {'status': 'ERROR', expected = {'status': 'ERROR',
'error': 'Account not verified'} 'error': 'Account not verified'}
self.assertStatusCode(rv, 412) self.assertStatusCode(rv, 412)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_user_in_own_group(self): def test_user_in_own_group(self):
"""The user must belong to a group it owns.""" """The user must belong to a group it owns."""
@ -69,7 +69,7 @@ class TestGroups(LunchoTests):
'name': 'Test group', 'name': 'Test group',
'admin': True}]} 'admin': True}]}
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
class TestExistingGroups(LunchoTests): class TestExistingGroups(LunchoTests):
@ -102,7 +102,7 @@ class TestExistingGroups(LunchoTests):
request) request)
expected = {'status': 'OK'} expected = {'status': 'OK'}
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
# check the database # check the database
group = Group.query.get(groupId) group = Group.query.get(groupId)
@ -126,7 +126,7 @@ class TestExistingGroups(LunchoTests):
request) request)
expected = {'status': 'OK'} expected = {'status': 'OK'}
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
# check the database # check the database
group = Group.query.get(groupId) group = Group.query.get(groupId)
@ -142,7 +142,7 @@ class TestExistingGroups(LunchoTests):
request) request)
expected = {'status': 'ERROR', 'error': 'Group not found'} expected = {'status': 'ERROR', 'error': 'Group not found'}
self.assertStatusCode(rv, 404) self.assertStatusCode(rv, 404)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_delete_group(self): def test_delete_group(self):
"""Delete a group.""" """Delete a group."""
@ -152,7 +152,7 @@ class TestExistingGroups(LunchoTests):
groupId=groupId)) groupId=groupId))
expected = {'status': 'OK'} expected = {'status': 'OK'}
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
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."""
@ -162,7 +162,7 @@ class TestExistingGroups(LunchoTests):
groupId=groupId)) groupId=groupId))
expected = {'status': 'ERROR', 'error': 'Group not found'} expected = {'status': 'ERROR', 'error': 'Group not found'}
self.assertStatusCode(rv, 404) self.assertStatusCode(rv, 404)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_delete_not_admin(self): def test_delete_not_admin(self):
"""Try to delete a group when the user is not the admin.""" """Try to delete a group when the user is not the admin."""
@ -178,7 +178,7 @@ class TestExistingGroups(LunchoTests):
groupId=self.group.id)) groupId=self.group.id))
expected = {'status': 'ERROR', 'error': 'User is not admin'} expected = {'status': 'ERROR', 'error': 'User is not admin'}
self.assertStatusCode(rv, 401) self.assertStatusCode(rv, 401)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

20
tests/users_tests.py

@ -22,7 +22,7 @@ class TestUsers(LunchoTests):
rv = self.put('/user/', request) rv = self.put('/user/', request)
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson({'status': 'OK'}, rv.data) self.assertJson(rv, {'status': 'OK'})
# db check # db check
self.assertIsNotNone(User.query.filter_by(username='username').first()) self.assertIsNotNone(User.query.filter_by(username='username').first())
@ -40,7 +40,7 @@ class TestUsers(LunchoTests):
expected = {"status": "ERROR", expected = {"status": "ERROR",
"error": "Username already exists"} "error": "Username already exists"}
self.assertStatusCode(rv, 409) self.assertStatusCode(rv, 409)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_no_json(self): def test_no_json(self):
"""Do a request that it's not JSON.""" """Do a request that it's not JSON."""
@ -49,7 +49,7 @@ class TestUsers(LunchoTests):
expected = {"error": "Request MUST be in JSON format", expected = {"error": "Request MUST be in JSON format",
"status": "ERROR"} "status": "ERROR"}
self.assertStatusCode(rv, 400) self.assertStatusCode(rv, 400)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_missing_fields(self): def test_missing_fields(self):
"""Send a request with missing fields.""" """Send a request with missing fields."""
@ -59,7 +59,7 @@ class TestUsers(LunchoTests):
expected = {'error': 'Missing fields: username, full_name', expected = {'error': 'Missing fields: username, full_name',
'status': 'ERROR'} 'status': 'ERROR'}
self.assertStatusCode(rv, 400) self.assertStatusCode(rv, 400)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
class TestExistingUsers(LunchoTests): class TestExistingUsers(LunchoTests):
@ -85,7 +85,7 @@ class TestExistingUsers(LunchoTests):
expected = {'status': 'OK'} expected = {'status': 'OK'}
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
# check in the database # check in the database
user = User.query.filter_by(username='test').first() user = User.query.filter_by(username='test').first()
@ -102,7 +102,7 @@ class TestExistingUsers(LunchoTests):
expected = {'status': 'ERROR', expected = {'status': 'ERROR',
'error': 'User not found (via token)'} 'error': 'User not found (via token)'}
self.assertStatusCode(rv, 404) self.assertStatusCode(rv, 404)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_expired_token(self): def test_expired_token(self):
"""Send a token that exists but it's not valid for today.""" """Send a token that exists but it's not valid for today."""
@ -119,7 +119,7 @@ class TestExistingUsers(LunchoTests):
expected = {'status': 'ERROR', expected = {'status': 'ERROR',
'error': 'Invalid token'} 'error': 'Invalid token'}
self.assertStatusCode(rv, 400) self.assertStatusCode(rv, 400)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_delete_user(self): def test_delete_user(self):
"""Delete a user.""" """Delete a user."""
@ -127,7 +127,7 @@ class TestExistingUsers(LunchoTests):
expected = {'status': 'OK'} expected = {'status': 'OK'}
self.assertStatusCode(rv, 200) self.assertStatusCode(rv, 200)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
# check the database # check the database
user = User.query.filter_by(username='test').first() user = User.query.filter_by(username='test').first()
@ -140,7 +140,7 @@ class TestExistingUsers(LunchoTests):
expected = {'status': 'ERROR', expected = {'status': 'ERROR',
'error': 'User not found (via token)'} 'error': 'User not found (via token)'}
self.assertStatusCode(rv, 404) self.assertStatusCode(rv, 404)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
def test_delete_expired_token(self): def test_delete_expired_token(self):
"""Send a delete to a token for yesterday.""" """Send a delete to a token for yesterday."""
@ -153,7 +153,7 @@ class TestExistingUsers(LunchoTests):
expected = {'status': 'ERROR', expected = {'status': 'ERROR',
'error': 'Invalid token'} 'error': 'Invalid token'}
self.assertStatusCode(rv, 400) self.assertStatusCode(rv, 400)
self.assertJson(expected, rv.data) self.assertJson(rv, expected)
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save