1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
from unittest import TestCase
import requests_mock
from parameterized import parameterized
from hvac.adapters import JSONAdapter
from hvac.api.auth_methods import LegacyMfa
from hvac.api.auth_methods.github import DEFAULT_MOUNT_POINT
class TestLegacyMfa(TestCase):
@parameterized.expand(
[
("default mount point", DEFAULT_MOUNT_POINT),
("custom mount point", "cathub"),
]
)
@requests_mock.Mocker()
def test_configure(self, test_label, mount_point, requests_mocker):
expected_status_code = 204
mock_url = "http://localhost:8200/v1/auth/{mount_point}/mfa_config".format(
mount_point=mount_point,
)
requests_mocker.register_uri(
method="POST",
url=mock_url,
status_code=expected_status_code,
)
mfa = LegacyMfa(adapter=JSONAdapter())
response = mfa.configure(
mount_point=mount_point,
)
self.assertEqual(
first=expected_status_code,
second=response.status_code,
)
@parameterized.expand(
[
("default mount point", DEFAULT_MOUNT_POINT),
("custom mount point", "cathub"),
]
)
@requests_mock.Mocker()
def test_read_configuration(self, test_label, mount_point, requests_mocker):
expected_status_code = 200
mock_response = {
"lease_id": "",
"warnings": None,
"wrap_info": None,
"auth": None,
"lease_duration": 0,
"request_id": "18ecf194-aba2-ba99-ebb5-1b90e5e231c7",
"data": {"type": "duo"},
"renewable": False,
}
mock_url = "http://localhost:8200/v1/auth/{mount_point}/mfa_config".format(
mount_point=mount_point,
)
requests_mocker.register_uri(
method="GET",
url=mock_url,
status_code=expected_status_code,
json=mock_response,
)
mfa = LegacyMfa(adapter=JSONAdapter())
response = mfa.read_configuration(
mount_point=mount_point,
)
self.assertEqual(
first=mock_response,
second=response,
)
@parameterized.expand(
[
("default mount point", DEFAULT_MOUNT_POINT),
("custom mount point", "cathub"),
]
)
@requests_mock.Mocker()
def test_configure_duo_access(self, test_label, mount_point, requests_mocker):
expected_status_code = 204
mock_url = "http://localhost:8200/v1/auth/{mount_point}/duo/access".format(
mount_point=mount_point,
)
requests_mocker.register_uri(
method="POST",
url=mock_url,
status_code=expected_status_code,
)
mfa = LegacyMfa(adapter=JSONAdapter())
response = mfa.configure_duo_access(
mount_point=mount_point,
host="someapisubdomain.python-hvac.org",
integration_key="ikey",
secret_key="supersecret",
)
self.assertEqual(
first=expected_status_code,
second=response.status_code,
)
@parameterized.expand(
[
("default mount point", DEFAULT_MOUNT_POINT),
("custom mount point", "cathub"),
]
)
@requests_mock.Mocker()
def test_configure_duo_behavior(self, test_label, mount_point, requests_mocker):
expected_status_code = 204
mock_url = "http://localhost:8200/v1/auth/{mount_point}/duo/config".format(
mount_point=mount_point,
)
requests_mocker.register_uri(
method="POST",
url=mock_url,
status_code=expected_status_code,
)
mfa = LegacyMfa(adapter=JSONAdapter())
response = mfa.configure_duo_behavior(
mount_point=mount_point, push_info="howdy"
)
self.assertEqual(
first=expected_status_code,
second=response.status_code,
)
@parameterized.expand(
[
("default mount point", DEFAULT_MOUNT_POINT),
("custom mount point", "cathub"),
]
)
@requests_mock.Mocker()
def test_read_duo_behavior_configuration(
self, test_label, mount_point, requests_mocker
):
expected_status_code = 200
mock_response = {
"lease_id": "",
"warnings": None,
"wrap_info": None,
"auth": None,
"lease_duration": 0,
"request_id": "7ea734e8-bbc4-e2de-2769-d052d6a320c6",
"data": {"username_format": "%s", "push_info": "", "user_agent": ""},
"renewable": False,
}
mock_url = "http://localhost:8200/v1/auth/{mount_point}/duo/config".format(
mount_point=mount_point,
)
requests_mocker.register_uri(
method="GET",
url=mock_url,
status_code=expected_status_code,
json=mock_response,
)
mfa = LegacyMfa(adapter=JSONAdapter())
response = mfa.read_duo_behavior_configuration(
mount_point=mount_point,
)
self.assertEqual(
first=mock_response,
second=response,
)
|