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.
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"""
|
|
|
|
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())
|