From faf01bcdaad186db98abacd9de0769c2abf61448 Mon Sep 17 00:00:00 2001 From: joetsoi Date: Thu, 24 Oct 2013 11:36:11 +0100 Subject: [PATCH] [#1224] change tests to check for error type instead of message --- .../test_group_and_organization_purge.py | 3 +-- .../functional/api/model/test_vocabulary.py | 22 ++++++++--------- ckan/tests/functional/api/test_follow.py | 24 +++++++------------ ckan/tests/functional/test_related.py | 3 +-- ckan/tests/logic/test_action.py | 11 ++------- ckan/tests/logic/test_tag.py | 3 +-- .../tests/test_example_iauthfunctions.py | 12 ++++------ 7 files changed, 28 insertions(+), 50 deletions(-) diff --git a/ckan/tests/functional/api/model/test_group_and_organization_purge.py b/ckan/tests/functional/api/model/test_group_and_organization_purge.py index 0770642dbe3..88a44e494f0 100644 --- a/ckan/tests/functional/api/model/test_group_and_organization_purge.py +++ b/ckan/tests/functional/api/model/test_group_and_organization_purge.py @@ -266,8 +266,7 @@ def _test_visitors_cannot_purge_groups_or_orgs(self, is_org): result = tests.call_action_api(self.app, action, id=group_or_org['id'], status=403, ) - assert result == {'__type': 'Authorization Error', - 'message': 'Access denied'} + assert result['__type'] == 'Authorization Error' def test_visitors_cannot_purge_organizations(self): '''Visitors (who aren't logged in) should not be authorized to purge diff --git a/ckan/tests/functional/api/model/test_vocabulary.py b/ckan/tests/functional/api/model/test_vocabulary.py index 5330fb09822..83bd8a57676 100644 --- a/ckan/tests/functional/api/model/test_vocabulary.py +++ b/ckan/tests/functional/api/model/test_vocabulary.py @@ -399,7 +399,7 @@ def test_vocabulary_create_not_logged_in(self): params=param_string, status=403) assert response.json['success'] is False - assert response.json['error']['message'] == 'Access denied' + assert response.json['error']['__type'] == 'Authorization Error' def test_vocabulary_create_not_authorized(self): '''Test that users who are not authorized cannot create vocabs.''' @@ -412,7 +412,7 @@ def test_vocabulary_create_not_authorized(self): str(self.normal_user.apikey)}, status=403) assert response.json['success'] is False - assert response.json['error']['message'] == 'Access denied' + assert response.json['error']['__type'] == 'Authorization Error' def test_vocabulary_update_id_only(self): self._update_vocabulary({'id': self.genre_vocab['id']}, @@ -494,7 +494,7 @@ def test_vocabulary_update_not_logged_in(self): params=param_string, status=403) assert response.json['success'] is False - assert response.json['error']['message'] == 'Access denied' + assert response.json['error']['__type'] == 'Authorization Error' def test_vocabulary_update_with_tags(self): tags = [ @@ -630,7 +630,7 @@ def test_vocabulary_delete_not_logged_in(self): params=param_string, status=403) assert response.json['success'] is False - assert response.json['error']['message'] == 'Access denied' + assert response.json['error']['__type'] == 'Authorization Error' def test_vocabulary_delete_not_authorized(self): '''Test that users who are not authorized cannot delete vocabs.''' @@ -642,7 +642,7 @@ def test_vocabulary_delete_not_authorized(self): str(self.normal_user.apikey)}, status=403) assert response.json['success'] is False - assert response.json['error']['message'] == 'Access denied' + assert response.json['error']['__type'] == 'Authorization Error' def test_add_tag_to_vocab(self): '''Test that a tag can be added to and then retrieved from a vocab.''' @@ -781,7 +781,7 @@ def test_add_tag_not_logged_in(self): params=tag_string, status=403) assert response.json['success'] is False - assert response.json['error']['message'] == 'Access denied' + assert response.json['error']['__type'] == 'Authorization Error' def test_add_tag_not_authorized(self): tag_dict = { @@ -795,7 +795,7 @@ def test_add_tag_not_authorized(self): str(self.normal_user.apikey)}, status=403) assert response.json['success'] is False - assert response.json['error']['message'] == 'Access denied' + assert response.json['error']['__type'] == 'Authorization Error' def test_add_vocab_tag_to_dataset(self): '''Test that a tag belonging to a vocab can be added to a dataset, @@ -1116,8 +1116,8 @@ def test_delete_tag_not_logged_in(self): params=helpers.json.dumps(params), status=403) assert response.json['success'] is False - msg = response.json['error']['message'] - assert msg == u"Access denied", msg + error = response.json['error']['__type'] + assert error == u"Authorization Error", error def test_delete_tag_not_authorized(self): vocab = self.genre_vocab @@ -1131,5 +1131,5 @@ def test_delete_tag_not_authorized(self): str(self.normal_user.apikey)}, status=403) assert response.json['success'] is False - msg = response.json['error']['message'] - assert msg == u"Access denied" + msg = response.json['error']['__type'] + assert msg == u"Authorization Error" diff --git a/ckan/tests/functional/api/test_follow.py b/ckan/tests/functional/api/test_follow.py index d23ead94087..74d8cc75b88 100644 --- a/ckan/tests/functional/api/test_follow.py +++ b/ckan/tests/functional/api/test_follow.py @@ -424,42 +424,36 @@ def test_01_user_follow_user_bad_apikey(self): error = ckan.tests.call_action_api(self.app, 'follow_user', id=self.russianfan['id'], apikey=apikey, status=403) - assert error['message'] == ('Access denied: ' - 'You must be logged in to follow users') + assert error['__type'] == 'Authorization Error' def test_01_user_follow_dataset_bad_apikey(self): for apikey in ('bad api key', '', ' ', 'None', '3', '35.7', 'xxx'): error = ckan.tests.call_action_api(self.app, 'follow_dataset', id=self.warandpeace['id'], apikey=apikey, status=403) - assert error['message'] == ('Access denied: ' - 'You must be logged in to follow a dataset.') + assert error['__type'] == 'Authorization Error' def test_01_user_follow_group_bad_apikey(self): for apikey in ('bad api key', '', ' ', 'None', '3', '35.7', 'xxx'): error = ckan.tests.call_action_api(self.app, 'follow_group', id=self.rogers_group['id'], apikey=apikey, status=403) - assert error['message'] == ('Access denied: ' - 'You must be logged in to follow a group.') + assert error['__type'] == 'Authorization Error' def test_01_user_follow_user_missing_apikey(self): error = ckan.tests.call_action_api(self.app, 'follow_user', id=self.russianfan['id'], status=403) - assert error['message'] == ('Access denied: ' - 'You must be logged in to follow users') + assert error['__type'] == 'Authorization Error' def test_01_user_follow_dataset_missing_apikey(self): error = ckan.tests.call_action_api(self.app, 'follow_dataset', id=self.warandpeace['id'], status=403) - assert error['message'] == ('Access denied: ' - 'You must be logged in to follow a dataset.') + assert error['__type'] == 'Authorization Error' def test_01_user_follow_group_missing_apikey(self): error = ckan.tests.call_action_api(self.app, 'follow_group', id=self.rogers_group['id'], status=403) - assert error['message'] == ('Access denied: ' - 'You must be logged in to follow a group.') + assert error['__type'] == 'Authorization Error' def test_01_follow_bad_object_id(self): for action in ('follow_user', 'follow_dataset', 'follow_group'): @@ -884,16 +878,14 @@ def test_01_unfollow_bad_apikey(self): 'xxx'): error = ckan.tests.call_action_api(self.app, action, apikey=apikey, status=403, id=self.joeadmin['id']) - assert error['message'] == ('Access denied: ' - 'You must be logged in to unfollow something.') + assert error['__type'] == 'Authorization Error' def test_01_unfollow_missing_apikey(self): '''Test error response when calling unfollow_* without api key.''' for action in ('unfollow_user', 'unfollow_dataset', 'unfollow_group'): error = ckan.tests.call_action_api(self.app, action, status=403, id=self.joeadmin['id']) - assert error['message'] == ('Access denied: ' - 'You must be logged in to unfollow something.') + assert error['__type'] == 'Authorization Error' def test_01_unfollow_bad_object_id(self): '''Test error response when calling unfollow_* with bad object id.''' diff --git a/ckan/tests/functional/test_related.py b/ckan/tests/functional/test_related.py index 5c82e8a8dba..6fa2fed4266 100644 --- a/ckan/tests/functional/test_related.py +++ b/ckan/tests/functional/test_related.py @@ -498,5 +498,4 @@ def test_api_delete_fail(self): extra_environ=extra) r = json.loads(res.body) assert r['success'] == False, r - error_msg = u'Access denied: Only the owner can delete a related item' - assert r[u'error'][u'message'] == error_msg, r + assert r[u'error'][u'__type'] == "Authorization Error", r diff --git a/ckan/tests/logic/test_action.py b/ckan/tests/logic/test_action.py index bfc9c02f22d..d7c37b98aa7 100644 --- a/ckan/tests/logic/test_action.py +++ b/ckan/tests/logic/test_action.py @@ -528,12 +528,7 @@ def test_12_user_update(self): res_obj = json.loads(res.body) assert res_obj['help'].startswith("Update a user account.") - error_msg = u'Access denied: User annafan not authorized to edit user {0}'.format( - sysadmin_user_dict['id']) - assert res_obj['error'] == { - '__type': 'Authorization Error', - 'message': error_msg, - } + assert res_obj['error']['__type'] == 'Authorization Error' assert res_obj['success'] is False def test_12_user_update_errors(self): @@ -895,9 +890,7 @@ def test_22_task_status_normal_user_not_authorized(self): res_obj = json.loads(res.body) assert res_obj['help'].startswith("Update a task status.") assert res_obj['success'] is False - error_msg = (u'Access denied: User annafan not authorized to update ' - 'task_status table') - assert res_obj['error'] == {'message': error_msg, '__type': 'Authorization Error'} + assert res_obj['error']['__type'] == 'Authorization Error' def test_23_task_status_validation(self): task_status = {} diff --git a/ckan/tests/logic/test_tag.py b/ckan/tests/logic/test_tag.py index 7426fde827d..4844fa055ca 100644 --- a/ckan/tests/logic/test_tag.py +++ b/ckan/tests/logic/test_tag.py @@ -151,8 +151,7 @@ def test_08_user_create_not_authorized(self): res_obj = json.loads(res.body) assert res_obj['help'].startswith("Create a new user.") assert res_obj['success'] is False - error_msg = 'Access denied: User not authorized to create users' - assert res_obj['error'] == {'message': error_msg, '__type': 'Authorization Error'} + assert res_obj['error']['__type'] == 'Authorization Error' def test_09_user_create(self): user_dict = {'name':'test_create_from_action_api', diff --git a/ckanext/example_iauthfunctions/tests/test_example_iauthfunctions.py b/ckanext/example_iauthfunctions/tests/test_example_iauthfunctions.py index 49b250c62b0..7031867df27 100644 --- a/ckanext/example_iauthfunctions/tests/test_example_iauthfunctions.py +++ b/ckanext/example_iauthfunctions/tests/test_example_iauthfunctions.py @@ -103,8 +103,7 @@ def test_group_create_with_visitor(self): result = tests.call_action_api(self.app, 'group_create', name='this_group_should_not_be_created', status=403) - assert result == {'__type': 'Authorization Error', - 'message': 'Access denied'} + assert result['__type'] == 'Authorization Error' def test_group_create_with_non_curator(self): '''A user who isn't a member of the curators group should not be able @@ -116,8 +115,7 @@ def test_group_create_with_non_curator(self): name='this_group_should_not_be_created', apikey=noncurator['apikey'], status=403) - assert result == {'__type': 'Authorization Error', - 'message': 'Access denied'} + assert result['__type'] == 'Authorization Error' def test_group_create_with_curator(self): '''A member of the curators group should be able to create a group. @@ -174,8 +172,7 @@ def test_group_create_with_visitor(self): response = tests.call_action_api(self.app, 'group_create', name='this_group_shouldnt_be_created', status=403) - assert response == {'__type': 'Authorization Error', - 'message': 'Access denied'} + assert response['__type'] == 'Authorization Error' class TestExampleIAuthFunctionsPluginV2(TestExampleIAuthFunctionsPlugin): @@ -203,5 +200,4 @@ def test_group_create_with_curator(self): name='this_group_should_not_be_created', apikey=curator['apikey'], status=403) - assert result == {'__type': 'Authorization Error', - 'message': 'Access denied'} + assert result['__type'] == 'Authorization Error'