You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_watercrawl_auth.py 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. from unittest.mock import MagicMock, patch
  2. import pytest
  3. import requests
  4. from services.auth.watercrawl.watercrawl import WatercrawlAuth
  5. class TestWatercrawlAuth:
  6. @pytest.fixture
  7. def valid_credentials(self):
  8. """Fixture for valid x-api-key credentials"""
  9. return {"auth_type": "x-api-key", "config": {"api_key": "test_api_key_123"}}
  10. @pytest.fixture
  11. def auth_instance(self, valid_credentials):
  12. """Fixture for WatercrawlAuth instance with valid credentials"""
  13. return WatercrawlAuth(valid_credentials)
  14. def test_should_initialize_with_valid_x_api_key_credentials(self, valid_credentials):
  15. """Test successful initialization with valid x-api-key credentials"""
  16. auth = WatercrawlAuth(valid_credentials)
  17. assert auth.api_key == "test_api_key_123"
  18. assert auth.base_url == "https://app.watercrawl.dev"
  19. assert auth.credentials == valid_credentials
  20. def test_should_initialize_with_custom_base_url(self):
  21. """Test initialization with custom base URL"""
  22. credentials = {
  23. "auth_type": "x-api-key",
  24. "config": {"api_key": "test_api_key_123", "base_url": "https://custom.watercrawl.dev"},
  25. }
  26. auth = WatercrawlAuth(credentials)
  27. assert auth.api_key == "test_api_key_123"
  28. assert auth.base_url == "https://custom.watercrawl.dev"
  29. @pytest.mark.parametrize(
  30. ("auth_type", "expected_error"),
  31. [
  32. ("bearer", "Invalid auth type, WaterCrawl auth type must be x-api-key"),
  33. ("basic", "Invalid auth type, WaterCrawl auth type must be x-api-key"),
  34. ("", "Invalid auth type, WaterCrawl auth type must be x-api-key"),
  35. ],
  36. )
  37. def test_should_raise_error_for_invalid_auth_type(self, auth_type, expected_error):
  38. """Test that non-x-api-key auth types raise ValueError"""
  39. credentials = {"auth_type": auth_type, "config": {"api_key": "test_api_key_123"}}
  40. with pytest.raises(ValueError) as exc_info:
  41. WatercrawlAuth(credentials)
  42. assert str(exc_info.value) == expected_error
  43. @pytest.mark.parametrize(
  44. ("credentials", "expected_error"),
  45. [
  46. ({"auth_type": "x-api-key", "config": {}}, "No API key provided"),
  47. ({"auth_type": "x-api-key"}, "No API key provided"),
  48. ({"auth_type": "x-api-key", "config": {"api_key": ""}}, "No API key provided"),
  49. ({"auth_type": "x-api-key", "config": {"api_key": None}}, "No API key provided"),
  50. ],
  51. )
  52. def test_should_raise_error_for_missing_api_key(self, credentials, expected_error):
  53. """Test that missing or empty API key raises ValueError"""
  54. with pytest.raises(ValueError) as exc_info:
  55. WatercrawlAuth(credentials)
  56. assert str(exc_info.value) == expected_error
  57. @patch("services.auth.watercrawl.watercrawl.requests.get")
  58. def test_should_validate_valid_credentials_successfully(self, mock_get, auth_instance):
  59. """Test successful credential validation"""
  60. mock_response = MagicMock()
  61. mock_response.status_code = 200
  62. mock_get.return_value = mock_response
  63. result = auth_instance.validate_credentials()
  64. assert result is True
  65. mock_get.assert_called_once_with(
  66. "https://app.watercrawl.dev/api/v1/core/crawl-requests/",
  67. headers={"Content-Type": "application/json", "X-API-KEY": "test_api_key_123"},
  68. )
  69. @pytest.mark.parametrize(
  70. ("status_code", "error_message"),
  71. [
  72. (402, "Payment required"),
  73. (409, "Conflict error"),
  74. (500, "Internal server error"),
  75. ],
  76. )
  77. @patch("services.auth.watercrawl.watercrawl.requests.get")
  78. def test_should_handle_http_errors(self, mock_get, status_code, error_message, auth_instance):
  79. """Test handling of various HTTP error codes"""
  80. mock_response = MagicMock()
  81. mock_response.status_code = status_code
  82. mock_response.json.return_value = {"error": error_message}
  83. mock_get.return_value = mock_response
  84. with pytest.raises(Exception) as exc_info:
  85. auth_instance.validate_credentials()
  86. assert str(exc_info.value) == f"Failed to authorize. Status code: {status_code}. Error: {error_message}"
  87. @pytest.mark.parametrize(
  88. ("status_code", "response_text", "has_json_error", "expected_error_contains"),
  89. [
  90. (403, '{"error": "Forbidden"}', True, "Failed to authorize. Status code: 403. Error: Forbidden"),
  91. (404, "", True, "Unexpected error occurred while trying to authorize. Status code: 404"),
  92. (401, "Not JSON", True, "Expecting value"), # JSON decode error
  93. ],
  94. )
  95. @patch("services.auth.watercrawl.watercrawl.requests.get")
  96. def test_should_handle_unexpected_errors(
  97. self, mock_get, status_code, response_text, has_json_error, expected_error_contains, auth_instance
  98. ):
  99. """Test handling of unexpected errors with various response formats"""
  100. mock_response = MagicMock()
  101. mock_response.status_code = status_code
  102. mock_response.text = response_text
  103. if has_json_error:
  104. mock_response.json.side_effect = Exception("Not JSON")
  105. mock_get.return_value = mock_response
  106. with pytest.raises(Exception) as exc_info:
  107. auth_instance.validate_credentials()
  108. assert expected_error_contains in str(exc_info.value)
  109. @pytest.mark.parametrize(
  110. ("exception_type", "exception_message"),
  111. [
  112. (requests.ConnectionError, "Network error"),
  113. (requests.Timeout, "Request timeout"),
  114. (requests.ReadTimeout, "Read timeout"),
  115. (requests.ConnectTimeout, "Connection timeout"),
  116. ],
  117. )
  118. @patch("services.auth.watercrawl.watercrawl.requests.get")
  119. def test_should_handle_network_errors(self, mock_get, exception_type, exception_message, auth_instance):
  120. """Test handling of various network-related errors including timeouts"""
  121. mock_get.side_effect = exception_type(exception_message)
  122. with pytest.raises(exception_type) as exc_info:
  123. auth_instance.validate_credentials()
  124. assert exception_message in str(exc_info.value)
  125. def test_should_not_expose_api_key_in_error_messages(self):
  126. """Test that API key is not exposed in error messages"""
  127. credentials = {"auth_type": "x-api-key", "config": {"api_key": "super_secret_key_12345"}}
  128. auth = WatercrawlAuth(credentials)
  129. # Verify API key is stored but not in any error message
  130. assert auth.api_key == "super_secret_key_12345"
  131. # Test various error scenarios don't expose the key
  132. with pytest.raises(ValueError) as exc_info:
  133. WatercrawlAuth({"auth_type": "bearer", "config": {"api_key": "super_secret_key_12345"}})
  134. assert "super_secret_key_12345" not in str(exc_info.value)
  135. @patch("services.auth.watercrawl.watercrawl.requests.get")
  136. def test_should_use_custom_base_url_in_validation(self, mock_get):
  137. """Test that custom base URL is used in validation"""
  138. mock_response = MagicMock()
  139. mock_response.status_code = 200
  140. mock_get.return_value = mock_response
  141. credentials = {
  142. "auth_type": "x-api-key",
  143. "config": {"api_key": "test_api_key_123", "base_url": "https://custom.watercrawl.dev"},
  144. }
  145. auth = WatercrawlAuth(credentials)
  146. result = auth.validate_credentials()
  147. assert result is True
  148. assert mock_get.call_args[0][0] == "https://custom.watercrawl.dev/api/v1/core/crawl-requests/"
  149. @pytest.mark.parametrize(
  150. ("base_url", "expected_url"),
  151. [
  152. ("https://app.watercrawl.dev", "https://app.watercrawl.dev/api/v1/core/crawl-requests/"),
  153. ("https://app.watercrawl.dev/", "https://app.watercrawl.dev/api/v1/core/crawl-requests/"),
  154. ("https://app.watercrawl.dev//", "https://app.watercrawl.dev/api/v1/core/crawl-requests/"),
  155. ],
  156. )
  157. @patch("services.auth.watercrawl.watercrawl.requests.get")
  158. def test_should_use_urljoin_for_url_construction(self, mock_get, base_url, expected_url):
  159. """Test that urljoin is used correctly for URL construction with various base URLs"""
  160. mock_response = MagicMock()
  161. mock_response.status_code = 200
  162. mock_get.return_value = mock_response
  163. credentials = {"auth_type": "x-api-key", "config": {"api_key": "test_api_key_123", "base_url": base_url}}
  164. auth = WatercrawlAuth(credentials)
  165. auth.validate_credentials()
  166. # Verify the correct URL was called
  167. assert mock_get.call_args[0][0] == expected_url
  168. @patch("services.auth.watercrawl.watercrawl.requests.get")
  169. def test_should_handle_timeout_with_retry_suggestion(self, mock_get, auth_instance):
  170. """Test that timeout errors are handled gracefully with appropriate error message"""
  171. mock_get.side_effect = requests.Timeout("The request timed out after 30 seconds")
  172. with pytest.raises(requests.Timeout) as exc_info:
  173. auth_instance.validate_credentials()
  174. # Verify the timeout exception is raised with original message
  175. assert "timed out" in str(exc_info.value)