|
18 | 18 | MY_CLIENT_ID = str(uuid.uuid4()) |
19 | 19 | MY_TENANT_ID = str(uuid.uuid4()) |
20 | 20 |
|
| 21 | +SECOND_OAUTH2_CONNECTION_UUID = str(uuid.uuid4()) |
| 22 | +SECOND_CLIENT_ID = str(uuid.uuid4()) |
| 23 | +SECOND_TENANT_ID = str(uuid.uuid4()) |
| 24 | + |
21 | 25 | OAUTH2_CONNECTION_CONTENT = { |
22 | 26 | MY_OAUTH2_CONNECTION_UUID: OAuth2Connection( |
23 | 27 | title="My OAuth2 connection", |
|
31 | 35 | ), |
32 | 36 | } |
33 | 37 |
|
| 38 | +TWO_OAUTH2_CONNECTIONS_CONTENT = { |
| 39 | + **OAUTH2_CONNECTION_CONTENT, |
| 40 | + SECOND_OAUTH2_CONNECTION_UUID: OAuth2Connection( |
| 41 | + title="Second OAuth2 connection", |
| 42 | + client_secret=("cmk_postprocessed", "stored_password", ("second_client_secret", "")), |
| 43 | + access_token=("cmk_postprocessed", "stored_password", ("second_access_token", "")), |
| 44 | + refresh_token=("cmk_postprocessed", "stored_password", ("second_refresh_token", "")), |
| 45 | + client_id=SECOND_CLIENT_ID, |
| 46 | + tenant_id=SECOND_TENANT_ID, |
| 47 | + authority="global", |
| 48 | + connector_type="microsoft_entra_id", |
| 49 | + ), |
| 50 | +} |
| 51 | + |
34 | 52 |
|
35 | 53 | @pytest.fixture |
36 | 54 | def mock_update_passwords_merged_file(monkeypatch: pytest.MonkeyPatch) -> Iterable[None]: |
@@ -231,3 +249,117 @@ def test_create_non_existing_oauth2_connection_without_permissions( |
231 | 249 | # THEN |
232 | 250 | assert resp.status_code == 401 |
233 | 251 | assert resp.json["title"] == "Unauthorized" |
| 252 | + |
| 253 | + |
| 254 | +@pytest.mark.usefixtures("mock_update_passwords_merged_file") |
| 255 | +def test_create_oauth2_connection_with_duplicate_title( |
| 256 | + clients: ClientRegistry, with_admin: tuple[str, str] |
| 257 | +) -> None: |
| 258 | + # GIVEN |
| 259 | + for ident, details in OAUTH2_CONNECTION_CONTENT.items(): |
| 260 | + save_oauth2_connection(ident, details, user_id=None, pprint_value=False, use_git=False) |
| 261 | + clients.ConfigurationEntity.set_credentials(with_admin[0], with_admin[1]) |
| 262 | + my_new_uuid = str(uuid.uuid4()) |
| 263 | + |
| 264 | + # WHEN |
| 265 | + resp = clients.ConfigurationEntity.create_configuration_entity( |
| 266 | + { |
| 267 | + "entity_type": ConfigEntityType.oauth2_connection.value, |
| 268 | + "entity_type_specifier": "microsoft_entra_id", |
| 269 | + "data": { |
| 270 | + "ident": my_new_uuid, |
| 271 | + "title": "My OAuth2 connection", |
| 272 | + "editable_by": ("administrators", None), |
| 273 | + "shared_with": [], |
| 274 | + "client_secret": ("explicit_password", "", "my_client_secret", False), |
| 275 | + "access_token": ("explicit_password", "", "my_access_token", False), |
| 276 | + "refresh_token": ("explicit_password", "", "my_refresh_token", False), |
| 277 | + "client_id": MY_CLIENT_ID, |
| 278 | + "tenant_id": MY_TENANT_ID, |
| 279 | + "authority": option_id("global"), |
| 280 | + }, |
| 281 | + }, |
| 282 | + expect_ok=False, |
| 283 | + ) |
| 284 | + |
| 285 | + # THEN |
| 286 | + assert resp.status_code == 422, resp.json |
| 287 | + validation_errors = resp.json["ext"]["validation_errors"] |
| 288 | + assert len(validation_errors) == 1 |
| 289 | + assert validation_errors[0]["location"] == ["title"] |
| 290 | + assert "The title must be unique" in validation_errors[0]["message"] |
| 291 | + |
| 292 | + |
| 293 | +@pytest.mark.usefixtures("mock_update_passwords_merged_file") |
| 294 | +def test_update_oauth2_connection_keeping_same_title( |
| 295 | + clients: ClientRegistry, with_admin: tuple[str, str] |
| 296 | +) -> None: |
| 297 | + # GIVEN |
| 298 | + for ident, details in OAUTH2_CONNECTION_CONTENT.items(): |
| 299 | + save_oauth2_connection(ident, details, user_id=None, pprint_value=False, use_git=False) |
| 300 | + clients.ConfigurationEntity.set_credentials(with_admin[0], with_admin[1]) |
| 301 | + |
| 302 | + # WHEN |
| 303 | + resp = clients.ConfigurationEntity.update_configuration_entity( |
| 304 | + { |
| 305 | + "entity_id": MY_OAUTH2_CONNECTION_UUID, |
| 306 | + "entity_type": ConfigEntityType.oauth2_connection.value, |
| 307 | + "entity_type_specifier": "microsoft_entra_id", |
| 308 | + "data": { |
| 309 | + "ident": MY_OAUTH2_CONNECTION_UUID, |
| 310 | + "title": "My OAuth2 connection", |
| 311 | + "editable_by": ("administrators", None), |
| 312 | + "shared_with": [], |
| 313 | + "client_secret": ("explicit_password", "", "my_client_secret", False), |
| 314 | + "access_token": ("explicit_password", "", "my_access_token", False), |
| 315 | + "refresh_token": ("explicit_password", "", "my_refresh_token", False), |
| 316 | + "client_id": MY_CLIENT_ID, |
| 317 | + "tenant_id": MY_TENANT_ID, |
| 318 | + "authority": option_id("global"), |
| 319 | + }, |
| 320 | + }, |
| 321 | + expect_ok=True, |
| 322 | + ) |
| 323 | + |
| 324 | + # THEN |
| 325 | + assert resp.status_code == 200, resp.json |
| 326 | + assert resp.json["id"] == MY_OAUTH2_CONNECTION_UUID, resp.json |
| 327 | + |
| 328 | + |
| 329 | +@pytest.mark.usefixtures("mock_update_passwords_merged_file") |
| 330 | +def test_update_oauth2_connection_to_existing_title( |
| 331 | + clients: ClientRegistry, with_admin: tuple[str, str] |
| 332 | +) -> None: |
| 333 | + # GIVEN |
| 334 | + for ident, details in TWO_OAUTH2_CONNECTIONS_CONTENT.items(): |
| 335 | + save_oauth2_connection(ident, details, user_id=None, pprint_value=False, use_git=False) |
| 336 | + clients.ConfigurationEntity.set_credentials(with_admin[0], with_admin[1]) |
| 337 | + |
| 338 | + # WHEN |
| 339 | + resp = clients.ConfigurationEntity.update_configuration_entity( |
| 340 | + { |
| 341 | + "entity_id": MY_OAUTH2_CONNECTION_UUID, |
| 342 | + "entity_type": ConfigEntityType.oauth2_connection.value, |
| 343 | + "entity_type_specifier": "microsoft_entra_id", |
| 344 | + "data": { |
| 345 | + "ident": MY_OAUTH2_CONNECTION_UUID, |
| 346 | + "title": "Second OAuth2 connection", |
| 347 | + "editable_by": ("administrators", None), |
| 348 | + "shared_with": [], |
| 349 | + "client_secret": ("explicit_password", "", "my_client_secret", False), |
| 350 | + "access_token": ("explicit_password", "", "my_access_token", False), |
| 351 | + "refresh_token": ("explicit_password", "", "my_refresh_token", False), |
| 352 | + "client_id": MY_CLIENT_ID, |
| 353 | + "tenant_id": MY_TENANT_ID, |
| 354 | + "authority": option_id("global"), |
| 355 | + }, |
| 356 | + }, |
| 357 | + expect_ok=False, |
| 358 | + ) |
| 359 | + |
| 360 | + # THEN |
| 361 | + assert resp.status_code == 422, resp.json |
| 362 | + validation_errors = resp.json["ext"]["validation_errors"] |
| 363 | + assert len(validation_errors) == 1 |
| 364 | + assert validation_errors[0]["location"] == ["title"] |
| 365 | + assert "The title must be unique" in validation_errors[0]["message"] |
0 commit comments