django中的例外单元测试

时间:2017-06-21 21:11:17

标签: python django django-rest-framework

我正在编写此代码的单元测试,我想知道如何测试下面代码的任何异常。谢谢。

class ExpiringTokenAuthentication(TokenAuthentication):
        """
        Extends token auth with inactivity expiration mechanism.
        """
        model = ExpiringToken

        def authenticate_credentials(self, key):

            try:
                token = self.model.objects.get(key=key)
            except self.model.DoesNotExist:
                raise exceptions.AuthenticationFailed('Invalid token')

            if not token.user.is_active:
                raise exceptions.AuthenticationFailed('Invalid user')

            if token.has_expired():
                raise exceptions.AuthenticationFailed('Token has expired')

            token.last_activity = timezone.now()
            token.save()
            return token.user, token

0 个答案:

没有答案