|
1 | 1 | from __future__ import absolute_import, division, print_function, \ |
2 | 2 | unicode_literals |
3 | 3 |
|
| 4 | +import json |
4 | 5 | from splitapiclient.microclients.harness import HarnessApiKeyMicroClient |
5 | 6 | from splitapiclient.http_clients.sync_client import SyncHttpClient |
| 7 | +from splitapiclient.http_clients.harness_client import HarnessHttpClient |
6 | 8 | from splitapiclient.resources.harness import HarnessApiKey |
| 9 | +from splitapiclient.tests.microclients.harness.conftest import FakeResponse |
7 | 10 |
|
8 | 11 |
|
9 | 12 | class TestHarnessApiKeyMicroClient: |
@@ -304,3 +307,149 @@ def test_delete(self, mocker): |
304 | 307 |
|
305 | 308 | # Verify the result |
306 | 309 | assert result is True |
| 310 | + |
| 311 | + |
| 312 | +class TestHarnessApiKeyURLGeneration: |
| 313 | + """ |
| 314 | + Tests that verify actual URL generation by mocking at the requests level. |
| 315 | + These tests ensure that optional parameters (orgIdentifier, projectIdentifier) |
| 316 | + are correctly included or excluded from the final URL. |
| 317 | + """ |
| 318 | + |
| 319 | + # ========================================================================= |
| 320 | + # LIST method URL tests |
| 321 | + # ========================================================================= |
| 322 | + |
| 323 | + def test_list_url_without_optional_identifiers(self, mocker): |
| 324 | + """Verify list URL doesn't contain orgIdentifier/projectIdentifier when not set""" |
| 325 | + mock_get = mocker.patch('splitapiclient.http_clients.harness_client.requests.get') |
| 326 | + mock_get.return_value = FakeResponse(200, json.dumps({'data': []})) |
| 327 | + |
| 328 | + hc = HarnessHttpClient('https://app.harness.io', 'test_token') |
| 329 | + client = HarnessApiKeyMicroClient(hc, 'test_account') |
| 330 | + client.list('parent1') |
| 331 | + |
| 332 | + called_url = mock_get.call_args[0][0] |
| 333 | + assert 'accountIdentifier=test_account' in called_url |
| 334 | + assert 'parentIdentifier=parent1' in called_url |
| 335 | + assert 'orgIdentifier' not in called_url |
| 336 | + assert 'projectIdentifier' not in called_url |
| 337 | + |
| 338 | + def test_list_url_with_org_identifier_only(self, mocker): |
| 339 | + """Verify list URL contains orgIdentifier when set, but not projectIdentifier""" |
| 340 | + mock_get = mocker.patch('splitapiclient.http_clients.harness_client.requests.get') |
| 341 | + mock_get.return_value = FakeResponse(200, json.dumps({'data': []})) |
| 342 | + |
| 343 | + hc = HarnessHttpClient('https://app.harness.io', 'test_token') |
| 344 | + client = HarnessApiKeyMicroClient(hc, 'test_account', org_identifier='org1') |
| 345 | + client.list('parent1') |
| 346 | + |
| 347 | + called_url = mock_get.call_args[0][0] |
| 348 | + assert 'accountIdentifier=test_account' in called_url |
| 349 | + assert 'parentIdentifier=parent1' in called_url |
| 350 | + assert 'orgIdentifier=org1' in called_url |
| 351 | + assert 'projectIdentifier' not in called_url |
| 352 | + |
| 353 | + def test_list_url_with_project_identifier_only(self, mocker): |
| 354 | + """Verify list URL contains projectIdentifier when set, but not orgIdentifier""" |
| 355 | + mock_get = mocker.patch('splitapiclient.http_clients.harness_client.requests.get') |
| 356 | + mock_get.return_value = FakeResponse(200, json.dumps({'data': []})) |
| 357 | + |
| 358 | + hc = HarnessHttpClient('https://app.harness.io', 'test_token') |
| 359 | + client = HarnessApiKeyMicroClient(hc, 'test_account', project_identifier='proj1') |
| 360 | + client.list('parent1') |
| 361 | + |
| 362 | + called_url = mock_get.call_args[0][0] |
| 363 | + assert 'accountIdentifier=test_account' in called_url |
| 364 | + assert 'parentIdentifier=parent1' in called_url |
| 365 | + assert 'orgIdentifier' not in called_url |
| 366 | + assert 'projectIdentifier=proj1' in called_url |
| 367 | + |
| 368 | + def test_list_url_with_both_identifiers(self, mocker): |
| 369 | + """Verify list URL contains both orgIdentifier and projectIdentifier when set""" |
| 370 | + mock_get = mocker.patch('splitapiclient.http_clients.harness_client.requests.get') |
| 371 | + mock_get.return_value = FakeResponse(200, json.dumps({'data': []})) |
| 372 | + |
| 373 | + hc = HarnessHttpClient('https://app.harness.io', 'test_token') |
| 374 | + client = HarnessApiKeyMicroClient(hc, 'test_account', org_identifier='org1', project_identifier='proj1') |
| 375 | + client.list('parent1') |
| 376 | + |
| 377 | + called_url = mock_get.call_args[0][0] |
| 378 | + assert 'accountIdentifier=test_account' in called_url |
| 379 | + assert 'parentIdentifier=parent1' in called_url |
| 380 | + assert 'orgIdentifier=org1' in called_url |
| 381 | + assert 'projectIdentifier=proj1' in called_url |
| 382 | + |
| 383 | + def test_list_url_with_method_override_identifiers(self, mocker): |
| 384 | + """Verify list URL uses method parameters to override instance defaults""" |
| 385 | + mock_get = mocker.patch('splitapiclient.http_clients.harness_client.requests.get') |
| 386 | + mock_get.return_value = FakeResponse(200, json.dumps({'data': []})) |
| 387 | + |
| 388 | + hc = HarnessHttpClient('https://app.harness.io', 'test_token') |
| 389 | + client = HarnessApiKeyMicroClient(hc, 'test_account', org_identifier='default_org', project_identifier='default_proj') |
| 390 | + client.list('parent1', org_identifier='override_org', project_identifier='override_proj') |
| 391 | + |
| 392 | + called_url = mock_get.call_args[0][0] |
| 393 | + assert 'accountIdentifier=test_account' in called_url |
| 394 | + assert 'orgIdentifier=override_org' in called_url |
| 395 | + assert 'projectIdentifier=override_proj' in called_url |
| 396 | + assert 'default_org' not in called_url |
| 397 | + assert 'default_proj' not in called_url |
| 398 | + |
| 399 | + # ========================================================================= |
| 400 | + # GET method URL tests |
| 401 | + # ========================================================================= |
| 402 | + |
| 403 | + def test_get_url_without_optional_identifiers(self, mocker): |
| 404 | + """Verify get URL doesn't contain orgIdentifier/projectIdentifier when not set""" |
| 405 | + mock_get = mocker.patch('splitapiclient.http_clients.harness_client.requests.get') |
| 406 | + mock_get.return_value = FakeResponse(200, json.dumps({ |
| 407 | + 'data': {'apiKey': {'identifier': 'ak1', 'name': 'AK1', 'description': '', 'parentIdentifier': 'parent1', 'apiKeyType': 'SERVICE_ACCOUNT'}} |
| 408 | + })) |
| 409 | + |
| 410 | + hc = HarnessHttpClient('https://app.harness.io', 'test_token') |
| 411 | + client = HarnessApiKeyMicroClient(hc, 'test_account') |
| 412 | + client.get('ak1', 'parent1') |
| 413 | + |
| 414 | + called_url = mock_get.call_args[0][0] |
| 415 | + assert '/apikey/aggregate/ak1' in called_url |
| 416 | + assert 'accountIdentifier=test_account' in called_url |
| 417 | + assert 'parentIdentifier=parent1' in called_url |
| 418 | + assert 'orgIdentifier' not in called_url |
| 419 | + assert 'projectIdentifier' not in called_url |
| 420 | + |
| 421 | + def test_get_url_with_org_identifier_only(self, mocker): |
| 422 | + """Verify get URL contains orgIdentifier when set, but not projectIdentifier""" |
| 423 | + mock_get = mocker.patch('splitapiclient.http_clients.harness_client.requests.get') |
| 424 | + mock_get.return_value = FakeResponse(200, json.dumps({ |
| 425 | + 'data': {'apiKey': {'identifier': 'ak1', 'name': 'AK1', 'description': '', 'parentIdentifier': 'parent1', 'apiKeyType': 'SERVICE_ACCOUNT'}} |
| 426 | + })) |
| 427 | + |
| 428 | + hc = HarnessHttpClient('https://app.harness.io', 'test_token') |
| 429 | + client = HarnessApiKeyMicroClient(hc, 'test_account', org_identifier='org1') |
| 430 | + client.get('ak1', 'parent1') |
| 431 | + |
| 432 | + called_url = mock_get.call_args[0][0] |
| 433 | + assert '/apikey/aggregate/ak1' in called_url |
| 434 | + assert 'accountIdentifier=test_account' in called_url |
| 435 | + assert 'parentIdentifier=parent1' in called_url |
| 436 | + assert 'orgIdentifier=org1' in called_url |
| 437 | + assert 'projectIdentifier' not in called_url |
| 438 | + |
| 439 | + def test_get_url_with_both_identifiers(self, mocker): |
| 440 | + """Verify get URL contains both orgIdentifier and projectIdentifier when set""" |
| 441 | + mock_get = mocker.patch('splitapiclient.http_clients.harness_client.requests.get') |
| 442 | + mock_get.return_value = FakeResponse(200, json.dumps({ |
| 443 | + 'data': {'apiKey': {'identifier': 'ak1', 'name': 'AK1', 'description': '', 'parentIdentifier': 'parent1', 'apiKeyType': 'SERVICE_ACCOUNT'}} |
| 444 | + })) |
| 445 | + |
| 446 | + hc = HarnessHttpClient('https://app.harness.io', 'test_token') |
| 447 | + client = HarnessApiKeyMicroClient(hc, 'test_account', org_identifier='org1', project_identifier='proj1') |
| 448 | + client.get('ak1', 'parent1') |
| 449 | + |
| 450 | + called_url = mock_get.call_args[0][0] |
| 451 | + assert '/apikey/aggregate/ak1' in called_url |
| 452 | + assert 'accountIdentifier=test_account' in called_url |
| 453 | + assert 'parentIdentifier=parent1' in called_url |
| 454 | + assert 'orgIdentifier=org1' in called_url |
| 455 | + assert 'projectIdentifier=proj1' in called_url |
0 commit comments