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
|
import logging
from unittest import TestCase
import requests_mock
from parameterized import parameterized
from hvac.adapters import JSONAdapter
from hvac.api.auth_methods import Okta
class TestOkta(TestCase):
TEST_MOUNT_POINT = "okta-test"
TEST_USERNAME = "hvac-person"
@parameterized.expand(
[
(
"success",
dict(),
None,
),
]
)
@requests_mock.Mocker()
def test_login(self, label, test_params, raises, requests_mocker):
test_policies = [
"default",
]
expected_status_code = 200
mock_url = (
"http://localhost:8200/v1/auth/{mount_point}/login/{username}".format(
mount_point=self.TEST_MOUNT_POINT,
username=self.TEST_USERNAME,
)
)
mock_response = {
"lease_id": "",
"data": None,
"warnings": None,
"auth": {
"client_token": "64d2a8f2-2a2f-5688-102b-e6088b76e344",
"accessor": "18bb8f89-826a-56ee-c65b-1736dc5ea27d",
"policies": ["default"],
"metadata": {"username": self.TEST_USERNAME, "policies": "default"},
},
"lease_duration": 7200,
"renewable": True,
}
requests_mocker.register_uri(
method="POST",
url=mock_url,
status_code=expected_status_code,
json=mock_response,
)
okta = Okta(adapter=JSONAdapter())
if raises is not None:
with self.assertRaises(raises):
okta.login(
username=self.TEST_USERNAME,
password="badpassword",
mount_point=self.TEST_MOUNT_POINT,
**test_params
)
else:
login_response = okta.login(
username=self.TEST_USERNAME,
password="badpassword",
mount_point=self.TEST_MOUNT_POINT,
**test_params
)
logging.debug("login_response: %s" % login_response)
self.assertEqual(
first=login_response["auth"]["policies"],
second=test_policies,
)
|