Test AuthenticationContext
This commit is contained in:
parent
720859efb6
commit
30cd77ff98
@ -1,7 +1,7 @@
|
|||||||
from atst.domain.exceptions import UnauthenticatedError, NotFoundError
|
from atst.domain.exceptions import UnauthenticatedError, NotFoundError
|
||||||
from atst.domain.users import Users
|
from atst.domain.users import Users
|
||||||
from .utils import parse_sdn, email_from_certificate
|
from .utils import parse_sdn, email_from_certificate
|
||||||
from .crl import CRLRevocationException
|
from .crl import CRLRevocationException, CRLInvalidException
|
||||||
|
|
||||||
|
|
||||||
class AuthenticationContext:
|
class AuthenticationContext:
|
||||||
@ -47,6 +47,8 @@ class AuthenticationContext:
|
|||||||
def _crl_check(self):
|
def _crl_check(self):
|
||||||
try:
|
try:
|
||||||
self.crl_cache.crl_check(self.cert)
|
self.crl_cache.crl_check(self.cert)
|
||||||
|
except CRLInvalidException as exc:
|
||||||
|
raise UnauthenticatedError("CRL expired. " + str(exc))
|
||||||
except CRLRevocationException as exc:
|
except CRLRevocationException as exc:
|
||||||
raise UnauthenticatedError("CRL check failed. " + str(exc))
|
raise UnauthenticatedError("CRL check failed. " + str(exc))
|
||||||
|
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from atst.domain.authnid import AuthenticationContext
|
from atst.domain.authnid import AuthenticationContext
|
||||||
from atst.domain.authnid.crl import CRLCache, CRLRevocationException
|
from atst.domain.authnid.crl import (
|
||||||
|
CRLCache,
|
||||||
|
CRLRevocationException,
|
||||||
|
CRLInvalidException,
|
||||||
|
)
|
||||||
from atst.domain.exceptions import UnauthenticatedError, NotFoundError
|
from atst.domain.exceptions import UnauthenticatedError, NotFoundError
|
||||||
from atst.domain.users import Users
|
from atst.domain.users import Users
|
||||||
|
|
||||||
@ -12,12 +16,15 @@ CERT = open("tests/fixtures/{}.crt".format(FIXTURE_EMAIL_ADDRESS)).read()
|
|||||||
|
|
||||||
|
|
||||||
class MockCRLCache:
|
class MockCRLCache:
|
||||||
def __init__(self, valid=True):
|
def __init__(self, valid=True, expired=False):
|
||||||
self.valid = valid
|
self.valid = valid
|
||||||
|
self.expired = expired
|
||||||
|
|
||||||
def crl_check(self, cert):
|
def crl_check(self, cert):
|
||||||
if self.valid:
|
if self.valid:
|
||||||
return True
|
return True
|
||||||
|
elif self.expired == True:
|
||||||
|
raise CRLInvalidException()
|
||||||
|
|
||||||
raise CRLRevocationException()
|
raise CRLRevocationException()
|
||||||
|
|
||||||
@ -45,6 +52,17 @@ def test_crl_check_fails():
|
|||||||
assert "CRL check" in message
|
assert "CRL check" in message
|
||||||
|
|
||||||
|
|
||||||
|
def test_expired_crl_check_fails():
|
||||||
|
auth_context = AuthenticationContext(
|
||||||
|
MockCRLCache(valid=False, expired=True), "SUCCESS", DOD_SDN, CERT
|
||||||
|
)
|
||||||
|
with pytest.raises(UnauthenticatedError) as excinfo:
|
||||||
|
assert auth_context.authenticate()
|
||||||
|
|
||||||
|
(message,) = excinfo.value.args
|
||||||
|
assert "CRL expired" in message
|
||||||
|
|
||||||
|
|
||||||
def test_bad_sdn():
|
def test_bad_sdn():
|
||||||
auth_context = AuthenticationContext(MockCRLCache(), "SUCCESS", "abc123", CERT)
|
auth_context = AuthenticationContext(MockCRLCache(), "SUCCESS", "abc123", CERT)
|
||||||
with pytest.raises(UnauthenticatedError) as excinfo:
|
with pytest.raises(UnauthenticatedError) as excinfo:
|
||||||
|
@ -188,7 +188,7 @@ def test_can_dynamically_update_crls(tmpdir):
|
|||||||
assert cache.crl_check(cert)
|
assert cache.crl_check(cert)
|
||||||
# override the original CRL with one that revokes atat.mil.crt
|
# override the original CRL with one that revokes atat.mil.crt
|
||||||
shutil.copyfile("tests/fixtures/test.der.crl", crl_file)
|
shutil.copyfile("tests/fixtures/test.der.crl", crl_file)
|
||||||
with pytest.raises(CRLRevocationException):
|
with pytest.raises(CRLInvalidException):
|
||||||
assert cache.crl_check(cert)
|
assert cache.crl_check(cert)
|
||||||
|
|
||||||
|
|
||||||
@ -197,7 +197,7 @@ def test_throws_error_for_missing_issuer():
|
|||||||
# this cert is self-signed, and so the application does not have a
|
# this cert is self-signed, and so the application does not have a
|
||||||
# corresponding CRL for it
|
# corresponding CRL for it
|
||||||
cert = open("tests/fixtures/{}.crt".format(FIXTURE_EMAIL_ADDRESS), "rb").read()
|
cert = open("tests/fixtures/{}.crt".format(FIXTURE_EMAIL_ADDRESS), "rb").read()
|
||||||
with pytest.raises(CRLRevocationException) as exc:
|
with pytest.raises(CRLInvalidException) as exc:
|
||||||
assert cache.crl_check(cert)
|
assert cache.crl_check(cert)
|
||||||
(message,) = exc.value.args
|
(message,) = exc.value.args
|
||||||
# objects that the issuer is missing
|
# objects that the issuer is missing
|
||||||
|
Loading…
x
Reference in New Issue
Block a user