【问题标题】:How to test GraphQLError exception in graphene_django or graphql_jwt?如何在 graphene_django 或 graphql_jwt 中测试 GraphQLError 异常?
【发布时间】:2020-02-08 13:34:44
【问题描述】:

我正在使用 graphene_django 和 graphql_jwt 在 django 中实现用户类型和身份验证。这是我的两个文件:代码和相应的测试位于名为“用户”的文件夹中,该文件夹是应用级文件夹(但不是 django 应用)

schema.py

import graphene
from graphene_django import DjangoObjectType
from django.contrib.auth import get_user_model

class UserType(DjangoObjectType):
    class Meta:
        model = get_user_model()

class Query(graphene.ObjectType):
    user = graphene.Field(UserType, id=graphene.Int(required=True))
    me = graphene.Field(UserType)

    def resolve_user(self, info, id):
        user = get_user_model().objects.get(id=id)
        return user

    def resolve_me(self, info):
        current_user = info.context.user
        if current_user.is_anonymous:
            raise GraphQLError("Not logged in !")
        return current_user

tests.py

from django.contrib.auth import get_user_model
from graphql import GraphQLError
from graphql.error.located_error import GraphQLLocatedError
from graphql_jwt.testcases import JSONWebTokenTestCase

class TestUserAuthentication(JSONWebTokenTestCase):

    def setUp(self):
        self.user = get_user_model().objects.create(
            username='Moctar', password='moctar')

    # @unittest.skip("Cannot handle raised GraphQLError")
    def test_not_autenticated_me(self):
        query = '''
        {
            me{
                id
                username
                password
            }
        }
        '''
        with self.assertRaises(GraphQLError, msg='Not logged in !'):
            self.client.execute(query)

    def test_autenticated_me(self):
        self.client.authenticate(self.user)
        query = '''
        {
            me{
                id 
                username
                password
            }
        }
        '''
        self.client.execute(query)

然后,当我通过 python manage.py test users 运行测试时,它会说:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..F.
======================================================================
FAIL: test_not_autenticated_me (tests.TestUserAuthentication)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/my_username/my_projects/server/arjangen/app/users/tests.py", line 97, in test_not_autenticated_me
    self.client.execute(query)
AssertionError: GraphQLError not raised : Not logged in !

----------------------------------------------------------------------
Ran 4 tests in 0.531s

FAILED (failures=1)
Destroying test database for alias 'default'...

我搜索过这样的stackoverflow [异常引发但未被 assertRaises 捕获 ][1]

[1]: Exception raised but not caught by assertRaises 但这仍然不能解决我的问题。那么 GraphQLError 真的可以怎么测试呢?

【问题讨论】:

    标签: unit-testing graphql graphene-python graphene-django django-graphql-jwt


    【解决方案1】:

    尝试删除test_not_autenticated_me()上的这一行:

    self.client.authenticate(self.user)
    

    因为authenticate() 所做的是登录并获取 JWT 令牌。 如果您不登录,默认用户将是 AnonymousUser 对象。

    【讨论】:

    • 实际上该行是问题中的错误。通过删除我上面的​​错误出现的那一行。感谢您指出错误!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2011-01-22
    • 2018-07-20
    • 2017-06-18
    相关资源
    最近更新 更多