【发布时间】:2021-01-20 14:28:26
【问题描述】:
问题
我正在尝试测试一个 graphql 查询。该查询要求与请求关联的用户 (info.context.user) 是超级用户 (.is_superuser)。否则会抛出异常。我正在尝试测试查询是否正常工作,但不确定如何确保 info.context.user.is_superuser 在测试中解析为 true。请帮忙!
schema.py
class Query(graphene.ObjectType):
gardens = graphene.List(GardenType)
def resolve_gardens(self, info):
user = info.context.user
if not (user.is_superuser or user.is_staff):
raise Exception("You must be staff or a superuser to view all gardens")
return Garden.objects.all()
tests.py
from graphene_django.utils.testing import GraphQLTestCase, graphql_query
from users.models import CustomUser
class TestGraphQLQueries(GraphQLTestCase):
"""
Test that GraphQL queries related to gardens work and throw errors appropriately
"""
def test_gardens_query(self):
response = self.query(
"""
query {
gardens {
id
name
owner {
id
email
}
}
}
"""
)
self.assertResponseNoErrors(response)
抛出错误
gardens/tests.py:93:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../env/lib/python3.6/site-packages/graphene_django/utils/testing.py:114: in assertResponseNoErrors
self.assertNotIn("errors", list(content.keys()))
E AssertionError: 'errors' unexpectedly found in ['errors', 'data']
-------------------------- Captured log call ---------------------------
ERROR graphql.execution.utils:utils.py:155 Traceback (most recent call last):
File "/home/dthompson/Code/personal/gardenbuilder-backend/env/lib/python3.6/site-packages/promise/promise.py", line 489, in _resolve_from_executor
executor(resolve, reject)
File "/home/dthompson/Code/personal/gardenbuilder-backend/env/lib/python3.6/site-packages/promise/promise.py", line 756, in executor
return resolve(f(*args, **kwargs))
File "/home/dthompson/Code/personal/gardenbuilder-backend/env/lib/python3.6/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
return next(*args, **kwargs)
File "/home/dthompson/Code/personal/gardenbuilder-backend/src/gardens/schema.py", line 43, in resolve_gardens
raise Exception("You must be a superuser to view all gardens")
graphql.error.located_error.GraphQLLocatedError: You must be a superuser to view all gardens
======================= short test summary info ========================
FAILED gardens/tests.py::TestGraphQLQueries::test_gardens_query - Ass...
【问题讨论】:
-
不会以超级用户通过测试发送请求吗?
-
是的,以超级用户身份发送请求会通过测试,但我如何模拟它以进行测试?
标签: django graphql pytest graphene-python graphene-django