|
| 1 | +# Copyright 2025 IBM Corp. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +End2end tests for SSO server definitions (on CPCs in DPM mode). |
| 17 | +
|
| 18 | +These tests do not change any existing SSO server definitions, but create, |
| 19 | +modify and delete test SSO server definitions. |
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +import warnings |
| 24 | +import pytest |
| 25 | +from requests.packages import urllib3 |
| 26 | + |
| 27 | +import zhmcclient |
| 28 | + |
| 29 | +from .utils import ( |
| 30 | + skip_warn, |
| 31 | + pick_test_resources, |
| 32 | + TEST_PREFIX, |
| 33 | + runtest_find_list, |
| 34 | + runtest_get_properties, |
| 35 | +) |
| 36 | + |
| 37 | +urllib3.disable_warnings() |
| 38 | + |
| 39 | +# Properties in minimalistic SSOServerDefinition objects (e.g. find_by_name()) |
| 40 | +SSOSRVDEF_MINIMAL_PROPS = ["element-uri", "name"] |
| 41 | + |
| 42 | +# Properties in SSOServerDefinition objects returned by list() without full |
| 43 | +# props |
| 44 | +SSOSRVDEF_LIST_PROPS = ["element-uri", "name", "type"] |
| 45 | + |
| 46 | +# Properties whose values can change between retrievals of SSOServerDefinition |
| 47 | +# objects |
| 48 | +SSOSRVDEF_VOLATILE_PROPS = [] |
| 49 | + |
| 50 | + |
| 51 | +def test_ssosrvdef_find_list(hmc_session): |
| 52 | + """ |
| 53 | + Test list(), find(), findall(). |
| 54 | + """ |
| 55 | + client = zhmcclient.Client(hmc_session) |
| 56 | + console = client.consoles.console |
| 57 | + hd = hmc_session.hmc_definition |
| 58 | + |
| 59 | + api_version = client.query_api_version() |
| 60 | + hmc_version = api_version["hmc-version"] |
| 61 | + hmc_version_info = tuple(map(int, hmc_version.split("."))) |
| 62 | + if hmc_version_info < (2, 17, 0): |
| 63 | + skip_warn( |
| 64 | + f"HMC {hd.host} of version {hmc_version} does not yet " |
| 65 | + "support SSO server definitions" |
| 66 | + ) |
| 67 | + |
| 68 | + # Pick the SSO server definitions to test with |
| 69 | + ssosrvdef_list = console.sso_server_definitions.list() |
| 70 | + if not ssosrvdef_list: |
| 71 | + skip_warn(f"No SSO server definitions defined on HMC {hd.host}") |
| 72 | + ssosrvdef_list = pick_test_resources(ssosrvdef_list) |
| 73 | + |
| 74 | + for ssosrvdef in ssosrvdef_list: |
| 75 | + print(f"Testing with SSO server definition {ssosrvdef.name!r}") |
| 76 | + runtest_find_list( |
| 77 | + hmc_session, |
| 78 | + console.sso_server_definitions, |
| 79 | + ssosrvdef.name, |
| 80 | + "name", |
| 81 | + "element-uri", |
| 82 | + SSOSRVDEF_VOLATILE_PROPS, |
| 83 | + SSOSRVDEF_MINIMAL_PROPS, |
| 84 | + SSOSRVDEF_LIST_PROPS, |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def test_ssosrvdef_property(hmc_session): |
| 89 | + """ |
| 90 | + Test property related methods |
| 91 | + """ |
| 92 | + client = zhmcclient.Client(hmc_session) |
| 93 | + console = client.consoles.console |
| 94 | + hd = hmc_session.hmc_definition |
| 95 | + |
| 96 | + api_version = client.query_api_version() |
| 97 | + hmc_version = api_version["hmc-version"] |
| 98 | + hmc_version_info = tuple(map(int, hmc_version.split("."))) |
| 99 | + if hmc_version_info < (2, 17, 0): |
| 100 | + skip_warn( |
| 101 | + f"HMC {hd.host} of version {hmc_version} does not yet " |
| 102 | + "support SSO server definitions" |
| 103 | + ) |
| 104 | + |
| 105 | + # Pick the SSO server definitions to test with |
| 106 | + ssosrvdef_list = console.sso_server_definitions.list() |
| 107 | + if not ssosrvdef_list: |
| 108 | + skip_warn(f"No SSO server definitions defined on HMC {hd.host}") |
| 109 | + ssosrvdef_list = pick_test_resources(ssosrvdef_list) |
| 110 | + |
| 111 | + for ssosrvdef in ssosrvdef_list: |
| 112 | + print(f"Testing with SSO server definition {ssosrvdef.name!r}") |
| 113 | + |
| 114 | + # Select a property that is not returned by list() |
| 115 | + non_list_prop = "description" |
| 116 | + |
| 117 | + runtest_get_properties(ssosrvdef.manager, non_list_prop) |
| 118 | + |
| 119 | + |
| 120 | +def test_ssosrvdef_crud(hmc_session): |
| 121 | + """ |
| 122 | + Test create, read, update and delete a SSO server definition. |
| 123 | + """ |
| 124 | + client = zhmcclient.Client(hmc_session) |
| 125 | + console = client.consoles.console |
| 126 | + hd = hmc_session.hmc_definition |
| 127 | + |
| 128 | + api_version = client.query_api_version() |
| 129 | + hmc_version = api_version["hmc-version"] |
| 130 | + hmc_version_info = tuple(map(int, hmc_version.split("."))) |
| 131 | + if hmc_version_info < (2, 17, 0): |
| 132 | + skip_warn( |
| 133 | + f"HMC {hd.host} of version {hmc_version} does not yet " |
| 134 | + "support SSO server definitions" |
| 135 | + ) |
| 136 | + |
| 137 | + ssosrvdef_name = TEST_PREFIX + " test_ssosrvdef_crud ssosrvdef1" |
| 138 | + ssosrvdef_name_new = ssosrvdef_name + " new" |
| 139 | + |
| 140 | + # Ensure a clean starting point for this test |
| 141 | + try: |
| 142 | + ssosrvdef = console.sso_server_definitions.find(name=ssosrvdef_name) |
| 143 | + except zhmcclient.NotFound: |
| 144 | + pass |
| 145 | + else: |
| 146 | + warnings.warn( |
| 147 | + "Deleting test SSO server definition from previous run: " |
| 148 | + f"{ssosrvdef_name!r}", |
| 149 | + UserWarning, |
| 150 | + ) |
| 151 | + ssosrvdef.delete() |
| 152 | + |
| 153 | + # Test creating the SSO server definition |
| 154 | + |
| 155 | + ssosrvdef_input_props = { |
| 156 | + "authentication-page-servers": [ |
| 157 | + {"hostname-ipaddr": "images1.example.com", "port": 443}, |
| 158 | + {"hostname-ipaddr": "images2.example.com", "port": 80}, |
| 159 | + ], |
| 160 | + "authentication-url": "https://sso1.example.com/auth", |
| 161 | + "client-id": "sso1-123456", |
| 162 | + "client-secret": "sso1-client-secret", |
| 163 | + "description": "Primary SSO server", |
| 164 | + "issuer-url": "https://sso1.example.com/issuer", |
| 165 | + "jwks-url": "https://sso1.example.com/jwks", |
| 166 | + "logout-sso-session-on-reauthentication-failure": True, |
| 167 | + "logout-url": "https://sso1.example.com/logout", |
| 168 | + "name": "SSO Server 1", |
| 169 | + "token-url": "https://sso1.example.com/token", |
| 170 | + "type": "oidc", |
| 171 | + } |
| 172 | + ssosrvdef_auto_props = { |
| 173 | + "logout-url": None, |
| 174 | + "logout-sso-session-on-reauthentication-failure": False, |
| 175 | + } |
| 176 | + |
| 177 | + # The code to be tested |
| 178 | + try: |
| 179 | + ssosrvdef = console.sso_server_definitions.create(ssosrvdef_input_props) |
| 180 | + except zhmcclient.HTTPError as exc: |
| 181 | + if exc.http_status == 403 and exc.reason == 1: |
| 182 | + skip_warn( |
| 183 | + f"HMC userid {hd.userid!r} is not authorized for task " |
| 184 | + f"'Manage Single Sign-On Servers' on HMC {hd.host}" |
| 185 | + ) |
| 186 | + else: |
| 187 | + raise |
| 188 | + |
| 189 | + for pn, exp_value in ssosrvdef_input_props.items(): |
| 190 | + assert ( |
| 191 | + ssosrvdef.properties[pn] == exp_value |
| 192 | + ), f"Unexpected value for property {pn!r}" |
| 193 | + ssosrvdef.pull_full_properties() |
| 194 | + for pn, exp_value in ssosrvdef_input_props.items(): |
| 195 | + assert ( |
| 196 | + ssosrvdef.properties[pn] == exp_value |
| 197 | + ), f"Unexpected value for property {pn!r}" |
| 198 | + for pn, exp_value in ssosrvdef_auto_props.items(): |
| 199 | + assert ( |
| 200 | + ssosrvdef.properties[pn] == exp_value |
| 201 | + ), f"Unexpected value for property {pn!r}" |
| 202 | + |
| 203 | + # Test updating a property of the SSO server definition |
| 204 | + |
| 205 | + new_desc = "Updated SSO server definition description." |
| 206 | + |
| 207 | + # The code to be tested |
| 208 | + ssosrvdef.update_properties(dict(description=new_desc)) |
| 209 | + |
| 210 | + assert ssosrvdef.properties["description"] == new_desc |
| 211 | + ssosrvdef.pull_full_properties() |
| 212 | + assert ssosrvdef.properties["description"] == new_desc |
| 213 | + |
| 214 | + # Test that SSO server definitions cannot be renamed |
| 215 | + |
| 216 | + with pytest.raises(zhmcclient.HTTPError) as exc_info: |
| 217 | + |
| 218 | + # The code to be tested |
| 219 | + ssosrvdef.update_properties(dict(name=ssosrvdef_name_new)) |
| 220 | + |
| 221 | + exc = exc_info.value |
| 222 | + assert exc.http_status == 400 |
| 223 | + assert exc.reason == 6 |
| 224 | + with pytest.raises(zhmcclient.NotFound): |
| 225 | + console.sso_server_definitions.find(name=ssosrvdef_name_new) |
| 226 | + |
| 227 | + # Test deleting the SSO server definition |
| 228 | + |
| 229 | + # The code to be tested |
| 230 | + ssosrvdef.delete() |
| 231 | + |
| 232 | + with pytest.raises(zhmcclient.NotFound): |
| 233 | + console.sso_server_definitions.find(name=ssosrvdef_name) |
0 commit comments