diff --git a/tests/base.py b/tests/base.py index 2879a85..4558cc2 100644 --- a/tests/base.py +++ b/tests/base.py @@ -28,10 +28,13 @@ class LunchoTests(unittest.TestCase): # Common assertions for lunch-o # ------------------------------------------------------------ - def assertJson(self, expected, response): - """Compare JSONs.""" - if not isinstance(response, dict): - response = json.loads(response) + def assertJson(self, response, expected): + """Compare JSONs. + + :param response: a test_client response + :param expected: expected response + :type expected: dict""" + response = json.loads(response.data) for key in expected: if not key in response: diff --git a/tests/group_tests.py b/tests/group_tests.py index 3f0399b..68778fd 100644 --- a/tests/group_tests.py +++ b/tests/group_tests.py @@ -32,7 +32,7 @@ class TestGroups(LunchoTests): expected = {'status': 'OK', 'groups': []} self.assertStatusCode(rv, 200) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_create_group(self): """Test creating a group.""" @@ -43,7 +43,7 @@ class TestGroups(LunchoTests): expected = {'status': 'OK', 'id': 1} # always 1 'cause the database # is erased on every test self.assertStatusCode(rv, 200) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_create_group_unverified_account(self): """Try creating a group with an account that's not verified yet.""" @@ -57,7 +57,7 @@ class TestGroups(LunchoTests): expected = {'status': 'ERROR', 'error': 'Account not verified'} self.assertStatusCode(rv, 412) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_user_in_own_group(self): """The user must belong to a group it owns.""" @@ -69,7 +69,7 @@ class TestGroups(LunchoTests): 'name': 'Test group', 'admin': True}]} self.assertStatusCode(rv, 200) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) class TestExistingGroups(LunchoTests): @@ -102,7 +102,7 @@ class TestExistingGroups(LunchoTests): request) expected = {'status': 'OK'} self.assertStatusCode(rv, 200) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) # check the database group = Group.query.get(groupId) @@ -126,7 +126,7 @@ class TestExistingGroups(LunchoTests): request) expected = {'status': 'OK'} self.assertStatusCode(rv, 200) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) # check the database group = Group.query.get(groupId) @@ -142,7 +142,7 @@ class TestExistingGroups(LunchoTests): request) expected = {'status': 'ERROR', 'error': 'Group not found'} self.assertStatusCode(rv, 404) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_delete_group(self): """Delete a group.""" @@ -152,7 +152,7 @@ class TestExistingGroups(LunchoTests): groupId=groupId)) expected = {'status': 'OK'} self.assertStatusCode(rv, 200) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_delete_unknown_group(self): """Delete a group that doesn't exist.""" @@ -162,7 +162,7 @@ class TestExistingGroups(LunchoTests): groupId=groupId)) expected = {'status': 'ERROR', 'error': 'Group not found'} self.assertStatusCode(rv, 404) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_delete_not_admin(self): """Try to delete a group when the user is not the admin.""" @@ -178,7 +178,7 @@ class TestExistingGroups(LunchoTests): groupId=self.group.id)) expected = {'status': 'ERROR', 'error': 'User is not admin'} self.assertStatusCode(rv, 401) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) if __name__ == '__main__': unittest.main() diff --git a/tests/users_tests.py b/tests/users_tests.py index 0ac0cf1..7696a1a 100644 --- a/tests/users_tests.py +++ b/tests/users_tests.py @@ -22,7 +22,7 @@ class TestUsers(LunchoTests): rv = self.put('/user/', request) self.assertStatusCode(rv, 200) - self.assertJson({'status': 'OK'}, rv.data) + self.assertJson(rv, {'status': 'OK'}) # db check self.assertIsNotNone(User.query.filter_by(username='username').first()) @@ -40,7 +40,7 @@ class TestUsers(LunchoTests): expected = {"status": "ERROR", "error": "Username already exists"} self.assertStatusCode(rv, 409) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_no_json(self): """Do a request that it's not JSON.""" @@ -49,7 +49,7 @@ class TestUsers(LunchoTests): expected = {"error": "Request MUST be in JSON format", "status": "ERROR"} self.assertStatusCode(rv, 400) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_missing_fields(self): """Send a request with missing fields.""" @@ -59,7 +59,7 @@ class TestUsers(LunchoTests): expected = {'error': 'Missing fields: username, full_name', 'status': 'ERROR'} self.assertStatusCode(rv, 400) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) class TestExistingUsers(LunchoTests): @@ -85,7 +85,7 @@ class TestExistingUsers(LunchoTests): expected = {'status': 'OK'} self.assertStatusCode(rv, 200) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) # check in the database user = User.query.filter_by(username='test').first() @@ -102,7 +102,7 @@ class TestExistingUsers(LunchoTests): expected = {'status': 'ERROR', 'error': 'User not found (via token)'} self.assertStatusCode(rv, 404) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_expired_token(self): """Send a token that exists but it's not valid for today.""" @@ -119,7 +119,7 @@ class TestExistingUsers(LunchoTests): expected = {'status': 'ERROR', 'error': 'Invalid token'} self.assertStatusCode(rv, 400) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_delete_user(self): """Delete a user.""" @@ -127,7 +127,7 @@ class TestExistingUsers(LunchoTests): expected = {'status': 'OK'} self.assertStatusCode(rv, 200) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) # check the database user = User.query.filter_by(username='test').first() @@ -140,7 +140,7 @@ class TestExistingUsers(LunchoTests): expected = {'status': 'ERROR', 'error': 'User not found (via token)'} self.assertStatusCode(rv, 404) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) def test_delete_expired_token(self): """Send a delete to a token for yesterday.""" @@ -153,7 +153,7 @@ class TestExistingUsers(LunchoTests): expected = {'status': 'ERROR', 'error': 'Invalid token'} self.assertStatusCode(rv, 400) - self.assertJson(expected, rv.data) + self.assertJson(rv, expected) if __name__ == '__main__':