mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
test: use real firecrawl auth responses
This commit is contained in:
1 parent
41fb94deb1
commit
a104170aa9
1 file changed
+13
-23
@@ -1,5 +1,4 @@
|
||||
import json
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
@@ -65,10 +64,9 @@ class TestFirecrawlAuth:
|
||||
FirecrawlAuth(_credentials(api_key=""))
|
||||
|
||||
@patch("services.data_source.auth.firecrawl.firecrawl.httpx.post", autospec=True)
|
||||
def test_should_validate_valid_credentials_successfully(self, mock_post: MagicMock, auth_instance: FirecrawlAuth):
|
||||
def test_should_validate_valid_credentials_successfully(self, mock_post, auth_instance: FirecrawlAuth):
|
||||
"""Test successful credential validation"""
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_response = httpx.Response(200)
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
result = auth_instance.validate_credentials()
|
||||
@@ -96,13 +94,9 @@ class TestFirecrawlAuth:
|
||||
],
|
||||
)
|
||||
@patch("services.data_source.auth.firecrawl.firecrawl.httpx.post", autospec=True)
|
||||
def test_should_handle_http_errors(
|
||||
self, mock_post: MagicMock, status_code, error_message, auth_instance: FirecrawlAuth
|
||||
):
|
||||
def test_should_handle_http_errors(self, mock_post, status_code, error_message, auth_instance: FirecrawlAuth):
|
||||
"""Test handling of various HTTP error codes"""
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = status_code
|
||||
mock_response.json.return_value = {"error": error_message}
|
||||
mock_response = httpx.Response(status_code, json={"error": error_message})
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
|
||||
@@ -113,11 +107,11 @@ class TestFirecrawlAuth:
|
||||
@patch("services.data_source.auth.firecrawl.firecrawl.httpx.post", autospec=True)
|
||||
def test_should_map_upstream_failure_to_provider_unavailable(
|
||||
self,
|
||||
mock_post: MagicMock,
|
||||
mock_post,
|
||||
status_code: int,
|
||||
auth_instance: FirecrawlAuth,
|
||||
):
|
||||
mock_response = MagicMock(status_code=status_code)
|
||||
mock_response = httpx.Response(status_code)
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
with pytest.raises(DataSourceApiKeyAuthProviderUnavailableError) as exc_info:
|
||||
@@ -139,7 +133,7 @@ class TestFirecrawlAuth:
|
||||
@patch("services.data_source.auth.firecrawl.firecrawl.httpx.post", autospec=True)
|
||||
def test_should_handle_unexpected_errors(
|
||||
self,
|
||||
mock_post: MagicMock,
|
||||
mock_post,
|
||||
status_code,
|
||||
response_text,
|
||||
has_json_error,
|
||||
@@ -147,13 +141,10 @@ class TestFirecrawlAuth:
|
||||
auth_instance: FirecrawlAuth,
|
||||
):
|
||||
"""Test handling of unexpected errors with various response formats"""
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = status_code
|
||||
mock_response.text = response_text
|
||||
if has_json_error:
|
||||
mock_response.json.side_effect = json.JSONDecodeError("Not JSON", "", 0)
|
||||
mock_response = httpx.Response(status_code, text=response_text)
|
||||
else:
|
||||
mock_response.json.return_value = {"error": "Forbidden"}
|
||||
mock_response = httpx.Response(status_code, json={"error": "Forbidden"})
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
with pytest.raises(DataSourceApiKeyAuthCredentialValidationError) as exc_info:
|
||||
@@ -171,7 +162,7 @@ class TestFirecrawlAuth:
|
||||
)
|
||||
@patch("services.data_source.auth.firecrawl.firecrawl.httpx.post", autospec=True)
|
||||
def test_should_handle_network_errors(
|
||||
self, mock_post: MagicMock, exception_type, exception_message, auth_instance: FirecrawlAuth
|
||||
self, mock_post, exception_type, exception_message, auth_instance: FirecrawlAuth
|
||||
):
|
||||
"""Test handling of various network-related errors including timeouts"""
|
||||
mock_post.side_effect = exception_type(exception_message)
|
||||
@@ -196,8 +187,7 @@ class TestFirecrawlAuth:
|
||||
@patch("services.data_source.auth.firecrawl.firecrawl.httpx.post", autospec=True)
|
||||
def test_should_use_custom_base_url_in_validation(self, mock_post):
|
||||
"""Test that custom base URL is used in validation and normalized"""
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_response = httpx.Response(200)
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
for base in ("https://custom.firecrawl.dev", "https://custom.firecrawl.dev/"):
|
||||
@@ -209,7 +199,7 @@ class TestFirecrawlAuth:
|
||||
assert mock_post.call_args[0][0] == "https://custom.firecrawl.dev/v1/crawl"
|
||||
|
||||
@patch("services.data_source.auth.firecrawl.firecrawl.httpx.post", autospec=True)
|
||||
def test_should_handle_timeout_with_retry_suggestion(self, mock_post: MagicMock, auth_instance: FirecrawlAuth):
|
||||
def test_should_handle_timeout_with_retry_suggestion(self, mock_post, auth_instance: FirecrawlAuth):
|
||||
"""Test that timeout errors are handled gracefully with appropriate error message"""
|
||||
mock_post.side_effect = httpx.TimeoutException("The request timed out after 30 seconds")
|
||||
|
||||
|
||||
Reference in new issue
Block a user