From b11971d50b944fd9ee663e17d9b22a71843af96c Mon Sep 17 00:00:00 2001 From: dandds Date: Tue, 31 Jul 2018 09:52:51 -0400 Subject: [PATCH] import authnid sdn utils --- atst/domain/authnid/utils.py | 13 +++++++++++++ tests/domain/authnid/test_utils.py | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 atst/domain/authnid/utils.py create mode 100644 tests/domain/authnid/test_utils.py diff --git a/atst/domain/authnid/utils.py b/atst/domain/authnid/utils.py new file mode 100644 index 00000000..763820ce --- /dev/null +++ b/atst/domain/authnid/utils.py @@ -0,0 +1,13 @@ +import re + + +# TODO: our sample SDN does not have an email address +def parse_sdn(sdn): + try: + parts = sdn.split(",") + cn_string = [piece for piece in parts if re.match("^CN=", piece)][0] + cn = cn_string.split("=")[-1] + info = cn.split(".") + return {"last_name": info[0], "first_name": info[1], "dod_id": info[-1]} + except (IndexError, AttributeError): + raise ValueError("'{}' is not a valid SDN".format(sdn)) diff --git a/tests/domain/authnid/test_utils.py b/tests/domain/authnid/test_utils.py new file mode 100644 index 00000000..310f5ba5 --- /dev/null +++ b/tests/domain/authnid/test_utils.py @@ -0,0 +1,21 @@ +import pytest +import atst.domain.authnid.utils as utils + +DOD_SDN_INFO = { + 'first_name': 'ART', + 'last_name': 'GARFUNKEL', + 'dod_id': '5892460358' + } +DOD_SDN = f"CN={DOD_SDN_INFO['last_name']}.{DOD_SDN_INFO['first_name']}.G.{DOD_SDN_INFO['dod_id']},OU=OTHER,OU=PKI,OU=DoD,O=U.S. Government,C=US" + +def test_parse_sdn(): + parsed = utils.parse_sdn(DOD_SDN) + assert parsed.get('first_name') == 'ART' + assert parsed.get('last_name') == 'GARFUNKEL' + assert parsed.get('dod_id') == '5892460358' + +def test_parse_bad_sdn(): + with pytest.raises(ValueError): + utils.parse_sdn('this has nothing to do with anything') + with pytest.raises(ValueError): + utils.parse_sdn(None)