Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
laramies
GitHub Repository: laramies/theHarvester
Path: blob/master/tests/discovery/test_githubcode_additions.py
883 views
1
from unittest.mock import MagicMock, AsyncMock
2
import asyncio
3
import pytest
4
from theHarvester.discovery import githubcode
5
from theHarvester.lib.core import Core
6
7
8
class TestSearchGithubCodeProcess:
9
@pytest.mark.asyncio
10
async def test_process_stops_after_max_retries(self, monkeypatch):
11
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
12
inst = githubcode.SearchGithubCode(word="test", limit=10)
13
14
# Speed up by avoiding actual sleeps
15
monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)
16
monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))
17
18
# Force RetryResult every time
19
monkeypatch.setattr(
20
inst,
21
"handle_response",
22
AsyncMock(return_value=githubcode.RetryResult(0)),
23
)
24
monkeypatch.setattr(
25
inst,
26
"do_search",
27
AsyncMock(return_value=("", {}, 403, {})),
28
)
29
30
inst.max_retries = 2
31
await inst.process()
32
assert inst.page == 0, "Process should stop after exceeding max retries"
33
assert inst.retry_count == 3, "Retry count should exceed max_retries before stopping"
34
35
@pytest.mark.asyncio
36
async def test_process_stops_on_error_result(self, monkeypatch):
37
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
38
inst = githubcode.SearchGithubCode(word="test", limit=10)
39
40
monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)
41
monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))
42
43
# Force ErrorResult
44
monkeypatch.setattr(
45
inst,
46
"handle_response",
47
AsyncMock(return_value=githubcode.ErrorResult(500, "err")),
48
)
49
monkeypatch.setattr(
50
inst,
51
"do_search",
52
AsyncMock(return_value=("", {}, 500, {})),
53
)
54
55
await inst.process()
56
assert inst.page == 0, "Process should stop on error result to avoid infinite loop"
57
58
@pytest.mark.asyncio
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