Fix test for self-signed url scrape

This commit is contained in:
Paul Gauthier 2024-07-28 16:49:32 -03:00
parent 8a9184cef9
commit 5bf9dbb226

View file

@ -1,6 +1,5 @@
import unittest import unittest
from unittest.mock import MagicMock from unittest.mock import MagicMock
import re
from aider.commands import Commands from aider.commands import Commands
from aider.io import InputOutput from aider.io import InputOutput
@ -10,17 +9,22 @@ from aider.scrape import Scraper
class TestScrape(unittest.TestCase): class TestScrape(unittest.TestCase):
def test_scrape_self_signed_ssl(self): def test_scrape_self_signed_ssl(self):
# Test with SSL verification # Test with SSL verification
scraper_verify = Scraper(print_error=MagicMock(), playwright_available=True, verify_ssl=True) scraper_verify = Scraper(
print_error=MagicMock(), playwright_available=True, verify_ssl=True
)
result_verify = scraper_verify.scrape("https://self-signed.badssl.com") result_verify = scraper_verify.scrape("https://self-signed.badssl.com")
self.assertIsNone(result_verify) self.assertIsNone(result_verify)
scraper_verify.print_error.assert_called() scraper_verify.print_error.assert_called()
# Test without SSL verification # Test without SSL verification
scraper_no_verify = Scraper(print_error=MagicMock(), playwright_available=True, verify_ssl=False) scraper_no_verify = Scraper(
print_error=MagicMock(), playwright_available=True, verify_ssl=False
)
result_no_verify = scraper_no_verify.scrape("https://self-signed.badssl.com") result_no_verify = scraper_no_verify.scrape("https://self-signed.badssl.com")
self.assertIsNotNone(result_no_verify) self.assertIsNotNone(result_no_verify)
self.assertIn("self-signed.badssl.com", result_no_verify) self.assertIn("self-signed", result_no_verify)
scraper_no_verify.print_error.assert_not_called() scraper_no_verify.print_error.assert_not_called()
def setUp(self): def setUp(self):
self.io = InputOutput(yes=True) self.io = InputOutput(yes=True)
self.commands = Commands(self.io, None) self.commands = Commands(self.io, None)
@ -88,6 +92,7 @@ class TestScrape(unittest.TestCase):
# Mock the playwright module to raise an error # Mock the playwright module to raise an error
import playwright import playwright
playwright._impl._errors.Error = Exception # Mock the Error class playwright._impl._errors.Error = Exception # Mock the Error class
def mock_content(): def mock_content():
@ -104,7 +109,9 @@ class TestScrape(unittest.TestCase):
self.assertIsNone(result) self.assertIsNone(result)
# Assert that print_error was called with the expected error message # Assert that print_error was called with the expected error message
mock_print_error.assert_called_once_with("Failed to retrieve content from https://example.com") mock_print_error.assert_called_once_with(
"Failed to retrieve content from https://example.com"
)
if __name__ == "__main__": if __name__ == "__main__":