|
| 1 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from scanner.crawler import AsyncCrawler |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def mock_page(): |
| 10 | + page = AsyncMock() |
| 11 | + # Mock locator for forms |
| 12 | + page.locator.return_value.all.return_value = [] |
| 13 | + # Mock eval_on_selector_all for links |
| 14 | + page.eval_on_selector_all.return_value = [] |
| 15 | + page.url = "http://test-site.com" |
| 16 | + return page |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_context(mock_page): |
| 21 | + context = AsyncMock() |
| 22 | + context.new_page.return_value = mock_page |
| 23 | + return context |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def mock_browser(mock_context): |
| 28 | + browser = AsyncMock() |
| 29 | + browser.new_context.return_value = mock_context |
| 30 | + return browser |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def mock_playwright(mock_browser): |
| 35 | + pw = AsyncMock() |
| 36 | + pw.chromium.launch.return_value = mock_browser |
| 37 | + return pw |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_crawler_initialization(): |
| 42 | + crawler = AsyncCrawler("http://test.com", max_depth=3) |
| 43 | + assert crawler.start_url == "http://test.com" |
| 44 | + assert crawler.max_depth == 3 |
| 45 | + assert crawler.domain == "test.com" |
| 46 | + assert "http://test.com" in [q[0] for q in crawler.queue] |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_is_valid_url(): |
| 51 | + crawler = AsyncCrawler("http://test.com") |
| 52 | + |
| 53 | + assert crawler.is_valid_url("http://test.com/path") |
| 54 | + assert not crawler.is_valid_url("http://google.com") # External |
| 55 | + assert not crawler.is_valid_url("http://test.com/image.png") # Static |
| 56 | + |
| 57 | + crawler.visited.add("http://test.com/visited") |
| 58 | + assert not crawler.is_valid_url("http://test.com/visited") # Visited |
| 59 | + |
| 60 | + |
| 61 | +@patch("scanner.crawler.async_playwright") |
| 62 | +@pytest.mark.asyncio |
| 63 | +async def test_crawl_flow(mock_pw_func, mock_playwright, mock_page): |
| 64 | + # Setup mock |
| 65 | + mock_pw_context = AsyncMock() |
| 66 | + mock_pw_context.__aenter__.return_value = mock_playwright |
| 67 | + mock_pw_context.__aexit__.return_value = None |
| 68 | + mock_pw_func.return_value = mock_pw_context |
| 69 | + |
| 70 | + # Specifics for this test |
| 71 | + # 1. First visit http://test.com -> found link http://test.com/page2 |
| 72 | + # 2. Visit http://test.com/page2 -> found no links |
| 73 | + |
| 74 | + async def side_effect_eval(selector, fn): |
| 75 | + if selector == "a": |
| 76 | + if mock_page.url == "http://test.com": |
| 77 | + return ["http://test.com/page2", "http://google.com", "http://test.com/image.png"] |
| 78 | + elif mock_page.url == "http://test.com/page2": |
| 79 | + return [] |
| 80 | + return [] |
| 81 | + |
| 82 | + mock_page.eval_on_selector_all.side_effect = side_effect_eval |
| 83 | + |
| 84 | + # We also need to update mock_page.url when goto is called to simulate navigation |
| 85 | + async def side_effect_goto(url, **kwargs): |
| 86 | + mock_page.url = url |
| 87 | + |
| 88 | + mock_page.goto.side_effect = side_effect_goto |
| 89 | + |
| 90 | + crawler = AsyncCrawler("http://test.com", max_depth=2, output_har="test.har") |
| 91 | + await crawler.crawl() |
| 92 | + |
| 93 | + # Verify visited |
| 94 | + assert "http://test.com" in crawler.visited |
| 95 | + assert "http://test.com/page2" in crawler.visited |
| 96 | + |
| 97 | + # Verify ignored |
| 98 | + assert "http://google.com" not in crawler.visited |
| 99 | + |
| 100 | + # Verify calls |
| 101 | + assert mock_page.goto.call_count == 2 |
| 102 | + |
| 103 | + |
| 104 | +@patch("scanner.crawler.async_playwright") |
| 105 | +@pytest.mark.asyncio |
| 106 | +async def test_crawl_form_detection(mock_pw_func, mock_playwright, mock_page): |
| 107 | + # Setup mock |
| 108 | + mock_pw_context = AsyncMock() |
| 109 | + mock_pw_context.__aenter__.return_value = mock_playwright |
| 110 | + mock_pw_context.__aexit__.return_value = None |
| 111 | + mock_pw_func.return_value = mock_pw_context |
| 112 | + |
| 113 | + mock_page.url = "http://test.com/login" |
| 114 | + |
| 115 | + # Define mock form and input |
| 116 | + mock_form = AsyncMock() |
| 117 | + mock_input = AsyncMock() |
| 118 | + mock_input.is_visible.return_value = True |
| 119 | + |
| 120 | + # Side effects for attributes |
| 121 | + async def attr_side_effect(attr): |
| 122 | + if attr == "type": |
| 123 | + return "text" |
| 124 | + if attr == "name": |
| 125 | + return "username" |
| 126 | + return "" |
| 127 | + |
| 128 | + mock_input.get_attribute.side_effect = attr_side_effect |
| 129 | + |
| 130 | + # Mock submit button |
| 131 | + mock_submit = AsyncMock() |
| 132 | + mock_submit.count.return_value = 1 |
| 133 | + mock_submit.is_visible.return_value = True |
| 134 | + |
| 135 | + # Let's construct a Mock Locator for input |
| 136 | + # Locator.all() IS async |
| 137 | + mock_input_locator = AsyncMock() |
| 138 | + mock_input_locator.all.return_value = [mock_input] |
| 139 | + |
| 140 | + # Mock Locator for submit |
| 141 | + mock_submit_locator = AsyncMock() |
| 142 | + mock_submit_locator.first = mock_submit |
| 143 | + mock_submit_locator.count.return_value = 1 |
| 144 | + |
| 145 | + # form.locator("...") is SYNCHRONOUS, returns a locator |
| 146 | + # We use MagicMock for synchronous callable |
| 147 | + # form.locator("...") should return the appropriate locator |
| 148 | + mock_form.locator = MagicMock() |
| 149 | + |
| 150 | + def form_locator_side_effect(selector): |
| 151 | + # The submit selector contains 'submit' |
| 152 | + if "submit" in selector or "Login" in selector: |
| 153 | + return mock_submit_locator |
| 154 | + # The input selector is mostly implicit or generic 'input' |
| 155 | + elif "input" in selector or "textarea" in selector: |
| 156 | + return mock_input_locator |
| 157 | + else: |
| 158 | + # Fallback |
| 159 | + return MagicMock() |
| 160 | + |
| 161 | + mock_form.locator.side_effect = form_locator_side_effect |
| 162 | + |
| 163 | + # page.locator("form") is SYNCHRONOUS |
| 164 | + mock_form_locator = AsyncMock() |
| 165 | + mock_form_locator.all.return_value = [mock_form] |
| 166 | + |
| 167 | + mock_page.locator = MagicMock() |
| 168 | + mock_page.locator.return_value = mock_form_locator |
| 169 | + |
| 170 | + crawler = AsyncCrawler("http://test.com/login", max_depth=1) |
| 171 | + await crawler.process_page(mock_page, "http://test.com/login", 0) |
| 172 | + |
| 173 | + # Verify input was filled |
| 174 | + mock_input.fill.assert_called_with("testuser") |
| 175 | + |
| 176 | + # Verify submit clicked |
| 177 | + mock_submit.click.assert_called() |
0 commit comments