Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
laramies
GitHub Repository: laramies/theHarvester
Path: blob/master/tests/discovery/test_githubcode.py
609 views
1
from unittest.mock import MagicMock
2
import pytest
3
from _pytest.mark.structures import MarkDecorator
4
from httpx import Response
5
from theHarvester.discovery import githubcode
6
from theHarvester.discovery.constants import MissingKey
7
from theHarvester.lib.core import Core
8
9
pytestmark: MarkDecorator = pytest.mark.asyncio
10
11
12
class TestSearchGithubCode:
13
class OkResponse:
14
response = Response(status_code=200)
15
16
# Mocking the json method properly
17
def __init__(self):
18
self.response = Response(status_code=200)
19
object.__setattr__(
20
self.response,
21
"json",
22
MagicMock(
23
return_value={
24
"items": [
25
{"text_matches": [{"fragment": "test1"}]},
26
{"text_matches": [{"fragment": "test2"}]},
27
]
28
}
29
),
30
)
31
32
class FailureResponse:
33
def __init__(self):
34
self.response = Response(status_code=401)
35
object.__setattr__(self.response, "json", MagicMock(return_value={}))
36
37
class RetryResponse:
38
def __init__(self):
39
self.response = Response(status_code=403)
40
object.__setattr__(self.response, "json", MagicMock(return_value={}))
41
42
class MalformedResponse:
43
def __init__(self):
44
self.response = Response(status_code=200)
45
object.__setattr__(
46
self.response,
47
"json",
48
MagicMock(
49
return_value={
50
"items": [
51
{"fail": True},
52
{"text_matches": []},
53
{"text_matches": [{"weird": "result"}]},
54
]
55
}
56
),
57
)
58
59
async def test_missing_key(self):
60
with pytest.raises(MissingKey):
61
Core.github_key = MagicMock(return_value=None) # type: ignore[method-assign]
62
githubcode.SearchGithubCode(word="test", limit=500)
63
64
async def test_fragments_from_response(self):
65
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
66
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
67
test_result = await test_class_instance.fragments_from_response(
68
self.OkResponse().response.json()
69
)
70
print("test_result: ", test_result)
71
assert test_result == ["test1", "test2"]
72
73
async def test_invalid_fragments_from_response(self):
74
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
75
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
76
test_result = await test_class_instance.fragments_from_response(
77
self.MalformedResponse().response.json()
78
)
79
assert test_result == []
80
81
async def test_next_page(self):
82
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
83
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
84
test_result = githubcode.SuccessResult(list(), next_page=2, last_page=4)
85
assert 2 == await test_class_instance.next_page_or_end(test_result)
86
87
async def test_last_page(self):
88
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
89
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
90
test_result = githubcode.SuccessResult(list(), 0, 0)
91
assert await test_class_instance.next_page_or_end(test_result) is 0
92
93
async def test_infinite_loop_fix_page_zero(self):
94
"""Test that the loop condition properly exits when page becomes 0"""
95
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
96
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
97
98
# Test the fixed condition: page != 0
99
page = 0
100
counter = 0
101
limit = 10
102
103
# The condition should be False when page is 0, preventing infinite loop
104
condition_result = counter <= limit and page != 0
105
assert condition_result is False, "Loop should exit when page is 0"
106
107
async def test_infinite_loop_fix_page_nonzero(self):
108
"""Test that the loop condition continues when page is non-zero"""
109
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
110
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
111
112
# Test with non-zero page values
113
for page in [1, 2, 3, 10]:
114
counter = 0
115
limit = 10
116
117
# The condition should be True when page is non-zero
118
condition_result = counter <= limit and page != 0
119
assert condition_result is True, f"Loop should continue when page is {page}"
120
121
async def test_infinite_loop_fix_old_vs_new_condition(self):
122
"""Test that demonstrates the difference between old and new conditions"""
123
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
124
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
125
126
page = 0
127
counter = 0
128
limit = 10
129
130
# Old problematic condition (would cause infinite loop)
131
old_condition = counter <= limit and page is not None
132
133
# New fixed condition (properly exits)
134
new_condition = counter <= limit and page != 0
135
136
# Old condition would be True (causing infinite loop)
137
assert old_condition is True, "Old condition would cause infinite loop when page=0"
138
139
# New condition is False (properly exits)
140
assert new_condition is False, "New condition properly exits when page=0"
141
142
143
if __name__ == "__main__":
144
pytest.main()
145
146