This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/policies/hibp/tests.py
2020-09-30 19:34:22 +02:00

34 lines
1.2 KiB
Python

"""HIBP Policy tests"""
from django.test import TestCase
from guardian.shortcuts import get_anonymous_user
from passbook.policies.hibp.models import HaveIBeenPwendPolicy
from passbook.policies.types import PolicyRequest, PolicyResult
from passbook.providers.oauth2.generators import generate_client_secret
class TestHIBPPolicy(TestCase):
"""Test HIBP Policy"""
def test_false(self):
"""Failing password case"""
policy = HaveIBeenPwendPolicy.objects.create(
name="test_false",
)
request = PolicyRequest(get_anonymous_user())
request.context["password"] = "password"
result: PolicyResult = policy.passes(request)
self.assertFalse(result.passing)
self.assertTrue(result.messages[0].startswith("Password exists on "))
def test_true(self):
"""Positive password case"""
policy = HaveIBeenPwendPolicy.objects.create(
name="test_true",
)
request = PolicyRequest(get_anonymous_user())
request.context["password"] = generate_client_secret()
result: PolicyResult = policy.passes(request)
self.assertTrue(result.passing)
self.assertEqual(result.messages, tuple())