|
| 1 | +from __future__ import absolute_import, division, print_function, \ |
| 2 | + unicode_literals |
| 3 | + |
| 4 | +import pytest |
| 5 | +from splitapiclient.http_clients.harness_client import HarnessHttpClient |
| 6 | +from splitapiclient.util.exceptions import HTTPUnauthorizedError, \ |
| 7 | + HTTPNotFoundError, HTTPIncorrectParametersError, HTTPResponseError |
| 8 | + |
| 9 | + |
| 10 | +class FakeResponse: |
| 11 | + ''' |
| 12 | + Simple class to mock returned Response objects from the requests module. |
| 13 | + ''' |
| 14 | + def __init__(self, status, text): |
| 15 | + self.status_code = status |
| 16 | + self.text = text |
| 17 | + |
| 18 | + |
| 19 | +class TestHarnessHttpClient: |
| 20 | + ''' |
| 21 | + Tests for the HarnessHttpClient class error handling |
| 22 | + ''' |
| 23 | + |
| 24 | + def test_handle_invalid_response(self): |
| 25 | + ''' |
| 26 | + Test that error messages include status codes and response object is passed |
| 27 | + ''' |
| 28 | + c1 = HarnessHttpClient('https://app.harness.io/', 'fake_harness_token') |
| 29 | + |
| 30 | + # Test 401 Unauthorized |
| 31 | + with pytest.raises(HTTPUnauthorizedError) as exc_info: |
| 32 | + c1._handle_invalid_response(FakeResponse(401, 'Unauthorized')) |
| 33 | + assert 'HTTP 401' in str(exc_info.value) |
| 34 | + assert 'Unauthorized' in str(exc_info.value) |
| 35 | + assert exc_info.value._error is not None |
| 36 | + assert exc_info.value._error.status_code == 401 |
| 37 | + |
| 38 | + # Test 400 Bad Request |
| 39 | + with pytest.raises(HTTPIncorrectParametersError) as exc_info: |
| 40 | + c1._handle_invalid_response(FakeResponse(400, 'Bad request')) |
| 41 | + assert 'HTTP 400' in str(exc_info.value) |
| 42 | + assert 'Bad request' in str(exc_info.value) |
| 43 | + assert exc_info.value._error.status_code == 400 |
| 44 | + |
| 45 | + # Test 404 Not Found |
| 46 | + with pytest.raises(HTTPNotFoundError) as exc_info: |
| 47 | + c1._handle_invalid_response(FakeResponse(404, 'Not found')) |
| 48 | + assert 'HTTP 404' in str(exc_info.value) |
| 49 | + assert 'Not found' in str(exc_info.value) |
| 50 | + assert exc_info.value._error.status_code == 404 |
| 51 | + |
| 52 | + # Test generic error (500 Internal Server Error) |
| 53 | + with pytest.raises(HTTPResponseError) as exc_info: |
| 54 | + c1._handle_invalid_response(FakeResponse(500, 'Internal server error')) |
| 55 | + assert 'HTTP 500' in str(exc_info.value) |
| 56 | + assert 'Internal server error' in str(exc_info.value) |
| 57 | + assert exc_info.value._error.status_code == 500 |
| 58 | + |
| 59 | + # Test another generic error (403 Forbidden) |
| 60 | + with pytest.raises(HTTPResponseError) as exc_info: |
| 61 | + c1._handle_invalid_response(FakeResponse(403, '{"error": "Forbidden"}')) |
| 62 | + assert 'HTTP 403' in str(exc_info.value) |
| 63 | + assert 'Forbidden' in str(exc_info.value) |
| 64 | + assert exc_info.value._error.status_code == 403 |
| 65 | + |
| 66 | + # Test JSON error response |
| 67 | + with pytest.raises(HTTPResponseError) as exc_info: |
| 68 | + c1._handle_invalid_response(FakeResponse(503, '{"message": "Service temporarily unavailable", "code": "SERVICE_UNAVAILABLE"}')) |
| 69 | + assert 'HTTP 503' in str(exc_info.value) |
| 70 | + assert 'Service temporarily unavailable' in str(exc_info.value) |
| 71 | + assert exc_info.value._error.status_code == 503 |
| 72 | + |
| 73 | + def test_is_harness_endpoint(self): |
| 74 | + ''' |
| 75 | + Test that Harness endpoints are correctly identified |
| 76 | + ''' |
| 77 | + harness_client = HarnessHttpClient('https://app.harness.io/', 'token') |
| 78 | + split_client = HarnessHttpClient('https://api.split.io/internal/api/v2', 'token') |
| 79 | + |
| 80 | + # Harness endpoint should return True |
| 81 | + assert harness_client._is_harness_endpoint({}) == True |
| 82 | + |
| 83 | + # Split endpoint should return False |
| 84 | + assert split_client._is_harness_endpoint({}) == False |
| 85 | + |
| 86 | + def test_deprecated_endpoint_detection(self): |
| 87 | + ''' |
| 88 | + Test that deprecated endpoints are correctly identified |
| 89 | + ''' |
| 90 | + client = HarnessHttpClient('https://app.harness.io/', 'token') |
| 91 | + |
| 92 | + # Test workspace POST (deprecated) |
| 93 | + workspace_endpoint = { |
| 94 | + 'url_template': 'workspaces/create', |
| 95 | + 'method': 'POST' |
| 96 | + } |
| 97 | + assert client._is_deprecated_endpoint(workspace_endpoint) == True |
| 98 | + |
| 99 | + # Test workspace GET (not deprecated) |
| 100 | + workspace_get_endpoint = { |
| 101 | + 'url_template': 'workspaces/list', |
| 102 | + 'method': 'GET' |
| 103 | + } |
| 104 | + assert client._is_deprecated_endpoint(workspace_get_endpoint) == False |
| 105 | + |
| 106 | + # Test users endpoint (all methods deprecated) |
| 107 | + users_endpoint = { |
| 108 | + 'url_template': 'users/123', |
| 109 | + 'method': 'GET' |
| 110 | + } |
| 111 | + assert client._is_deprecated_endpoint(users_endpoint) == True |
| 112 | + |
| 113 | + # Test groups endpoint (all methods deprecated) |
| 114 | + groups_endpoint = { |
| 115 | + 'url_template': 'groups/456', |
| 116 | + 'method': 'POST' |
| 117 | + } |
| 118 | + assert client._is_deprecated_endpoint(groups_endpoint) == True |
| 119 | + |
| 120 | + # Test restrictions endpoint (all methods deprecated) |
| 121 | + restrictions_endpoint = { |
| 122 | + 'url_template': 'restrictions/789', |
| 123 | + 'method': 'DELETE' |
| 124 | + } |
| 125 | + assert client._is_deprecated_endpoint(restrictions_endpoint) == True |
| 126 | + |
| 127 | + # Test apiKeys with admin type (deprecated) |
| 128 | + apikeys_endpoint = { |
| 129 | + 'url_template': 'apiKeys/create', |
| 130 | + 'method': 'POST' |
| 131 | + } |
| 132 | + body_admin = {'apiKeyType': 'admin'} |
| 133 | + assert client._is_deprecated_endpoint(apikeys_endpoint, body_admin) == True |
| 134 | + |
| 135 | + # Test apiKeys with non-admin type (not deprecated) |
| 136 | + body_non_admin = {'apiKeyType': 'user'} |
| 137 | + assert client._is_deprecated_endpoint(apikeys_endpoint, body_non_admin) == False |
| 138 | + |
| 139 | + # Test non-deprecated endpoint |
| 140 | + splits_endpoint = { |
| 141 | + 'url_template': 'splits/ws/123', |
| 142 | + 'method': 'GET' |
| 143 | + } |
| 144 | + assert client._is_deprecated_endpoint(splits_endpoint) == False |
| 145 | + |
0 commit comments