2020-07-10 18:57:15 +00:00
|
|
|
"""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
|
2020-08-19 08:32:44 +00:00
|
|
|
from passbook.providers.oauth2.generators import generate_client_secret
|
2020-07-10 18:57:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestHIBPPolicy(TestCase):
|
|
|
|
"""Test HIBP Policy"""
|
|
|
|
|
|
|
|
def test_false(self):
|
|
|
|
"""Failing password case"""
|
2020-09-30 17:34:22 +00:00
|
|
|
policy = HaveIBeenPwendPolicy.objects.create(
|
|
|
|
name="test_false",
|
|
|
|
)
|
2020-07-10 18:57:15 +00:00
|
|
|
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"""
|
2020-09-30 17:34:22 +00:00
|
|
|
policy = HaveIBeenPwendPolicy.objects.create(
|
|
|
|
name="test_true",
|
|
|
|
)
|
2020-07-10 18:57:15 +00:00
|
|
|
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())
|