def test_auth_verify__valid_token__returns_same_token(self):
        auth_response = call_auth_endpoint(self.client, "foobar", "foo")
        auth_token = auth_response.json()['token']

        verify_response = call_auth_verify_endpoint(self.client, auth_token)
        verify_token = verify_response.json()['token']

        self.assertEqual(verify_token, auth_token)
    def test_auth_verify__expired_token__returns_validation_error(self):

        payload = JSONWebTokenAuthentication.jwt_create_payload(
            self.active_user)
        payload['iat'] = 0  # beginning of time
        payload['exp'] = 1  # one second after beginning of time
        auth_token = JSONWebTokenAuthentication.jwt_encode_payload(payload)

        expected_output = {'non_field_errors': [_('Token has expired.')]}

        verify_response = call_auth_verify_endpoint(self.client, auth_token)
        self.assertEqual(verify_response.json(), expected_output)
    def test_auth_verify__token_with_invalid_username__returns_validation_error(
            self):
        # create token with invalid username
        payload = JSONWebTokenAuthentication.jwt_create_payload(
            self.active_user)
        payload['username'] = "******"
        auth_token = JSONWebTokenAuthentication.jwt_encode_payload(payload)

        expected_output = {'non_field_errors': [_("User doesn't exist.")]}

        verify_response = call_auth_verify_endpoint(self.client, auth_token)
        self.assertEqual(verify_response.json(), expected_output)
    def test_auth_verify__token_for_inactive_user__returns_validation_error(
            self):
        # create token with invalid username
        payload = JSONWebTokenAuthentication.jwt_create_payload(
            self.inactive_user)
        auth_token = JSONWebTokenAuthentication.jwt_encode_payload(payload)

        expected_output = {
            'non_field_errors': [_('User account is disabled.')]
        }

        verify_response = call_auth_verify_endpoint(self.client, auth_token)
        self.assertEqual(verify_response.json(), expected_output)
    def test_auth_verify__token_without_username__returns_validation_error(
            self):
        # create token without username field
        payload = JSONWebTokenAuthentication.jwt_create_payload(
            self.active_user)
        del payload['username']
        auth_token = JSONWebTokenAuthentication.jwt_encode_payload(payload)

        expected_output = {'non_field_errors': [_('Invalid token.')]}

        verify_response = call_auth_verify_endpoint(self.client, auth_token)

        self.assertEqual(verify_response.json(), expected_output)
    def test_auth_verify__invalid_token__returns_validation_error(self):
        expected_output = {'non_field_errors': [_('Error decoding token.')]}

        response = call_auth_verify_endpoint(self.client, "invalid_token")

        self.assertEqual(response.json(), expected_output)