首页 > 解决方案 > 在单元测试中添加不记名令牌 Django - ValueError: int() 的无效文字,基数为 10

问题描述

我正在尝试通过在单元测试中发布来添加不记名令牌,access_token 生成良好,但是当我通过它时,我发布请求并引发错误,如下所示:

ValueError: invalid literal for int() with base 10: '<bound method TestCase.id of <customers.tests.CustomerProfileTestCase testMethod=test_validate_founder_cant_add_owp>>'

下面是我的测试:

def setUp():
    user = User.objects.create_user(username='john', email='js@js.com', password='js.sj')
    client = APIClient()

    
    @property
    def bearer_token(self):
        """Generate token with bearer"""
        refresh = RefreshToken.for_user(self)
        return str(refresh.access_token)

    def test_create_customer_profile(self, data):
        """
        Create a new customer profile.

        :param self: Reference to the current instance of the class.
        :param data: customer profile data.
        """
        token = self.bearer_token
        self.client.credentials(
            HTTP_AUTHORIZATION='Bearer ' + token)
        response = self.client.post(
            reverse('api:customers:customer-profile-list'), data)
        assert(response.status, 200)

完整追溯:

Traceback (most recent call last):
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/captiq/common/utils_tests.py", line 70, in login
    func(self, *args, **kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/captiq/customers/tests.py", line 889, in test_validate_founder_cant_add_owp
    response = self.create_customer_profile(self.customer_data)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/captiq/customers/utils_tests.py", line 176, in create_customer_profile
    response = self.client.post(
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/test.py", line 293, in post
    response = super().post(
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/test.py", line 207, in post
    return self.generic('POST', path, data, content_type, **extra)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/test.py", line 231, in generic
    return super().generic(
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/test/client.py", line 422, in generic
    return self.request(**r)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/test.py", line 283, in request
    return super().request(**kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/test.py", line 235, in request
    request = super().request(**kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/test/client.py", line 503, in request
    raise exc_value
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/viewsets.py", line 114, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/views.py", line 493, in dispatch
    self.initial(request, *args, **kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/views.py", line 410, in initial
    self.perform_authentication(request)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/views.py", line 324, in perform_authentication
    request.user
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/request.py", line 220, in user
    self._authenticate()
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework/request.py", line 373, in _authenticate
    user_auth_tuple = authenticator.authenticate(self)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework_simplejwt/authentication.py", line 42, in authenticate
    return self.get_user(validated_token), validated_token
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/rest_framework_simplejwt/authentication.py", line 115, in get_user
    user = self.user_model.objects.get(**{api_settings.USER_ID_FIELD: user_id})
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/query.py", line 399, in get
    clone = self.filter(*args, **kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/query.py", line 892, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1290, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1315, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1251, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1116, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/lookups.py", line 20, in __init__
    self.rhs = self.get_prep_lookup()
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/lookups.py", line 70, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "/Users/lutaayaidris/Documents/workspace_two/captiq/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 972, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: '<bound method TestCase.id of <customers.tests.CustomerProfileTestCase testMethod=test_validate_founder_cant_add_owp>>'

标签: djangounit-testingdjango-rest-frameworkjwt

解决方案


推荐阅读