Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
laramies
GitHub Repository: laramies/theHarvester
Path: blob/master/tests/discovery/test_githubcode_additions.py
609 views
1
from unittest.mock import MagicMock, AsyncMock
2
import asyncio
3
import pytest
4
from _pytest.mark.structures import MarkDecorator
5
from theHarvester.discovery import githubcode
6
from theHarvester.lib.core import Core
7
8
pytestmark: MarkDecorator = pytest.mark.asyncio
9
10
11
class TestSearchGithubCodeProcess:
12
async def test_process_stops_after_max_retries(self, monkeypatch):
13
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
14
inst = githubcode.SearchGithubCode(word="test", limit=10)
15
16
# Speed up by avoiding actual sleeps
17
monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)
18
monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))
19
20
# Force RetryResult every time
21
monkeypatch.setattr(
22
inst,
23
"handle_response",
24
AsyncMock(return_value=githubcode.RetryResult(0)),
25
)
26
monkeypatch.setattr(
27
inst,
28
"do_search",
29
AsyncMock(return_value=("", {}, 403, {})),
30
)
31
32
inst.max_retries = 2
33
await inst.process()
34
assert inst.page == 0, "Process should stop after exceeding max retries"
35
assert inst.retry_count == 3, "Retry count should exceed max_retries before stopping"
36
37
async def test_process_stops_on_error_result(self, monkeypatch):
38
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
39
inst = githubcode.SearchGithubCode(word="test", limit=10)
40
41
monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)
42
monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))
43
44
# Force ErrorResult
45
monkeypatch.setattr(
46
inst,
47
"handle_response",
48
AsyncMock(return_value=githubcode.ErrorResult(500, "err")),
49
)
50
monkeypatch.setattr(
51
inst,
52
"do_search",
53
AsyncMock(return_value=("", {}, 500, {})),
54
)
55
56
await inst.process()
57
assert inst.page == 0, "Process should stop on error result to avoid infinite loop"
58
59
async def test_process_breaks_on_same_page_pagination(self, monkeypatch):
60
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
61
inst = githubcode.SearchGithubCode(word="test", limit=10)
62
63
monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)
64
monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))
65
66
# Force SuccessResult that does not advance the page
67
monkeypatch.setattr(
68
inst,
69
"handle_response",
70
AsyncMock(return_value=githubcode.SuccessResult([], next_page=1, last_page=0)),
71
)
72
monkeypatch.setattr(
73
inst,
74
"do_search",
75
AsyncMock(return_value=("", {"items": []}, 200, {})),
76
)
77
78
await inst.process()
79
assert inst.page == 0, "Process should stop when pagination does not advance"
80
81