Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pcmxgti committed Nov 9, 2023
1 parent 1ee8b25 commit a66a537
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
15 changes: 15 additions & 0 deletions tests/unit/test_duo.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ def test_get_mfa_response():
get_mfa_response(mfa_result)
assert err.value.code == 1

# Test no key available
with pytest.raises(SystemExit) as err:
get_mfa_response({"pytest": "FAIL"})
assert err.value.code == 1

# Test generic failure
with pytest.raises(SystemExit) as err:
get_mfa_response(Mock(return_value="FAIL"))
Expand Down Expand Up @@ -182,12 +187,22 @@ def test_parse_mfa_challenge():
parse_mfa_challenge(mfa_challenge)
assert err.value.code == 1

# Test no key in returned content
with pytest.raises(SystemExit) as err:
parse_mfa_challenge({"pyest": "OK", "badresponse": "error"})
assert err.value.code == 1

# Test no response in returned content
mfa_challenge.json = Mock(return_value={"stat": "OK", "badresponse": "error"})
with pytest.raises(SystemExit) as err:
parse_mfa_challenge(mfa_challenge)
assert err.value.code == 1

# Test failure
with pytest.raises(SystemExit) as err:
parse_mfa_challenge({"stat": "fail", "response": {"txid": "pytest_error"}})
assert err.value.code == 1

# Test API failure
mfa_challenge.json = Mock(return_value={"stat": "fail", "response": {"txid": "error"}})
with pytest.raises(SystemExit) as err:
Expand Down
9 changes: 7 additions & 2 deletions tokendito/duo.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def get_mfa_response(mfa_result):
verify_mfa = mfa_result["response"]
except KeyError as key_error:
logger.error(f"The mfa challenge response is missing a required parameter: {key_error}")
logger.debug(json.dumps(mfa_result.json()))
logger.debug(mfa_result)
sys.exit(1)
except Exception as other_error:
logger.error(f"There was an error getting the mfa challenge result: {other_error}")
Expand Down Expand Up @@ -266,11 +266,16 @@ def factor_callback(duo_info, verify_mfa):
factor_callback_url = f"https://{duo_info['host']}{verify_mfa['result_url']}"
factor_callback = api_post(factor_callback_url, payload={"sid": duo_info["sid"]})

if type(factor_callback) is not dict:
logger.error("There was an error getting your application signature. Please try again.")
logger.debug(f"Response from DUO: {factor_callback}")
sys.exit(2)

try:
sig_response = f"{factor_callback['response']['cookie']}:{duo_info['tile_sig']}"
except Exception as sig_error:
logger.error("There was an error getting your application signature. Please try again.")
logger.debug(f"from Duo: {sig_error}")
logger.debug(f"Response from DUO: {sig_error}")
sys.exit(2)

logger.debug(f"Completed callback to {factor_callback_url} with sig_response {sig_response}")
Expand Down

0 comments on commit a66a537

Please sign in to comment.