Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Test for permissions using dedicated util helper

Pacu220387fbb7a91
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index ef3ed38321..b801fe59bf 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -28,16 +28,14 @@ def test_voucher_permissions(
"""
# Query to ensure user with no permissions can't see vouchers
response = staff_api_client.post(reverse('api'), {'query': query})
- content = get_graphql_content(response)
- message = 'You do not have permission to perform this action'
- assert content['errors'][0]['message'] == message
+ assert_no_permission(response)
# Give staff user proper permissions
staff_user.user_permissions.add(permission_manage_discounts)
# Query again
response = staff_api_client.post(reverse('api'), {'query': query})
- content = get_graphql_content(response)
+ get_graphql_content(response)
def test_voucher_query(
diff --git a/tests/api/utils.py b/tests/api/utils.py
index 3009f6431e..9ea6e1b69d 100644
--- a/tests/api/utils.py
+++ b/tests/api/utils.py
@@ -3,23 +3,25 @@ import json
from saleor.graphql.core.utils import snake_to_camel_case
-def get_graphql_content(response, check_for_errors=True):
+def _get_graphql_content_from_response(response):
+ return json.loads(response.content.decode('utf8'))
+
+
+def get_graphql_content(response):
"""Get's GraphQL content from the response, and optionally checks if it
contains any operating-related errors, eg. schema errors or lack of
permissions.
"""
- content = json.loads(response.content.decode('utf8'))
- # We should be always checking for errors, unless one want to test lack
- # of permissions
- if check_for_errors:
- assert 'errors' not in content, content['errors']
+ content = _get_graphql_content_from_response(response)
+ assert 'errors' not in content, content['errors']
return content
def assert_no_permission(response):
- content = get_graphql_content(response)
+ content = _get_graphql_content_from_response(response)
assert 'errors' in content
- assert content['errors'][0]['message'] == 'You do not have permission to perform this action'
+ assert content['errors'][0]['message'] == (
+ 'You do not have permission to perform this action')
def get_multipart_request_body(query, variables, file, file_name):