Skip to content

Commit aa2ea6b

Browse files
committed
Enhance URL generation tests for Harness microclients to verify inclusion of optional parameters (orgIdentifier, projectIdentifier) in API requests. Add comprehensive test cases for list and get methods across various microclients, ensuring correct URL formation based on provided identifiers.
1 parent cfdd389 commit aa2ea6b

10 files changed

+1325
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Shared pytest fixtures and helpers for Harness microclient tests.
3+
"""
4+
from __future__ import absolute_import, division, print_function, unicode_literals
5+
6+
import pytest
7+
8+
9+
class FakeResponse:
10+
"""
11+
Simple class to mock Response objects from the requests module.
12+
Used for testing URL generation without making actual HTTP calls.
13+
"""
14+
def __init__(self, status, text):
15+
self.status_code = status
16+
self.text = text
17+
18+
19+
@pytest.fixture
20+
def fake_response():
21+
"""Factory fixture for creating FakeResponse objects."""
22+
def _create_response(status=200, text='{}'):
23+
return FakeResponse(status, text)
24+
return _create_response
25+
26+
27+
@pytest.fixture
28+
def mock_requests_get(mocker):
29+
"""Mock requests.get and return the mock for assertions."""
30+
mock = mocker.patch('splitapiclient.http_clients.sync_client.requests.get')
31+
mock.return_value = FakeResponse(200, '{"data": []}')
32+
return mock
33+
34+
35+
@pytest.fixture
36+
def mock_requests_post(mocker):
37+
"""Mock requests.post and return the mock for assertions."""
38+
mock = mocker.patch('splitapiclient.http_clients.sync_client.requests.post')
39+
mock.return_value = FakeResponse(200, '{"data": {}}')
40+
return mock
41+
42+
43+
@pytest.fixture
44+
def mock_requests_put(mocker):
45+
"""Mock requests.put and return the mock for assertions."""
46+
mock = mocker.patch('splitapiclient.http_clients.sync_client.requests.put')
47+
mock.return_value = FakeResponse(200, '{"data": {}}')
48+
return mock
49+
50+
51+
@pytest.fixture
52+
def mock_requests_delete(mocker):
53+
"""Mock requests.delete and return the mock for assertions."""
54+
mock = mocker.patch('splitapiclient.http_clients.sync_client.requests.delete')
55+
mock.return_value = FakeResponse(200, '{}')
56+
return mock
57+

splitapiclient/tests/microclients/harness/harness_apikey_microclient_test.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from __future__ import absolute_import, division, print_function, \
22
unicode_literals
33

4+
import json
45
from splitapiclient.microclients.harness import HarnessApiKeyMicroClient
56
from splitapiclient.http_clients.sync_client import SyncHttpClient
7+
from splitapiclient.http_clients.harness_client import HarnessHttpClient
68
from splitapiclient.resources.harness import HarnessApiKey
9+
from splitapiclient.tests.microclients.harness.conftest import FakeResponse
710

811

912
class TestHarnessApiKeyMicroClient:
@@ -304,3 +307,149 @@ def test_delete(self, mocker):
304307

305308
# Verify the result
306309
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

Comments
 (0)