forked from hackthebox/Hackster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_other.py
190 lines (145 loc) · 6.43 KB
/
test_other.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from unittest.mock import AsyncMock, patch
import pytest
from discord import ApplicationContext
from src.bot import Bot
from src.cmds.core import other
from src.cmds.core.other import OtherCog, SpoilerModal
from src.core import settings
from src.helpers import webhook
class TestWebhookHelper:
"""Test the webhook helper functions."""
@pytest.mark.asyncio
async def test_webhook_call_success(self):
"""Test successful webhook call."""
test_url = "http://test.webhook.url"
test_data = {"key": "value"}
# Mock the aiohttp ClientSession
with patch('aiohttp.ClientSession.post') as mock_post:
mock_response = AsyncMock()
mock_response.status = 200
mock_post.return_value.__aenter__.return_value = mock_response
await webhook.webhook_call(test_url, test_data)
# Verify the post was called with correct parameters
mock_post.assert_called_once_with(test_url, json=test_data)
@pytest.mark.asyncio
async def test_webhook_call_failure(self):
"""Test failed webhook call."""
test_url = "http://test.webhook.url"
test_data = {"key": "value"}
# Mock the aiohttp ClientSession
with patch('aiohttp.ClientSession.post') as mock_post:
mock_response = AsyncMock()
mock_response.status = 500
mock_response.text = AsyncMock(return_value="Internal Server Error")
mock_post.return_value.__aenter__.return_value = mock_response
# Test should complete without raising an exception
await webhook.webhook_call(test_url, test_data)
class TestOther:
"""Test the `ChannelManage` cog."""
@pytest.mark.asyncio
async def test_no_hints(self, bot, ctx):
"""Test the response of the `no_hints` command."""
cog = OtherCog(bot)
ctx.bot = bot
# Invoke the command.
await cog.no_hints.callback(cog, ctx)
args, kwargs = ctx.respond.call_args
content = args[0]
# Command should respond with a string.
assert isinstance(content, str)
assert content.startswith("No hints are allowed")
@pytest.mark.asyncio
async def test_support_labs(self, bot, ctx):
"""Test the response of the `support` command."""
cog = other.OtherCog(bot)
ctx.bot = bot
platform = "labs"
# Invoke the command.
await cog.support.callback(cog, ctx, platform)
args, kwargs = ctx.respond.call_args
content = args[0]
# Command should respond with a string.
assert isinstance(content, str)
assert content == "https://help.hackthebox.com/en/articles/5986762-contacting-htb-support"
@pytest.mark.asyncio
async def test_support_academy(self, bot, ctx):
"""Test the response of the `support` command."""
cog = other.OtherCog(bot)
ctx.bot = bot
platform = "academy"
# Invoke the command.
await cog.support.callback(cog, ctx, platform)
args, kwargs = ctx.respond.call_args
content = args[0]
# Command should respond with a string.
assert isinstance(content, str)
assert content == "https://help.hackthebox.com/en/articles/5987511-contacting-academy-support"
@pytest.mark.asyncio
async def test_support_urls_different(self, bot, ctx):
"""Test that the URLs for 'labs' and 'academy' platforms are different."""
cog = other.OtherCog(bot)
ctx.bot = bot
# Test the 'labs' platform
await cog.support.callback(cog, ctx, "labs")
labs_url = ctx.respond.call_args[0][0]
# Test the 'academy' platform
await cog.support.callback(cog, ctx, "academy")
academy_url = ctx.respond.call_args[0][0]
# Assert that the URLs are different
assert labs_url != academy_url
@pytest.mark.asyncio
async def test_spoiler_modal_callback_with_url(self):
"""Test the spoiler modal callback with a valid URL."""
modal = SpoilerModal(title="Report Spoiler")
interaction = AsyncMock()
interaction.user.display_name = "TestUser"
modal.children[0].value = "Test description"
modal.children[1].value = "http://example.com/spoiler"
with patch('src.helpers.webhook.webhook_call', new_callable=AsyncMock) as mock_webhook:
await modal.callback(interaction)
interaction.response.send_message.assert_called_once_with(
"Thank you, the spoiler has been reported.", ephemeral=True
)
@pytest.mark.asyncio
async def test_spoiler_modal_callback_with_url(self):
modal = SpoilerModal(title="Report Spoiler")
interaction = AsyncMock()
interaction.user.display_name = "TestUser"
modal.children[0].value = "Test description"
modal.children[1].value = "http://example.com/spoiler"
with patch('aiohttp.ClientSession.post', new_callable=AsyncMock) as mock_post:
mock_post.return_value.__aenter__.return_value.status = 200
await modal.callback(interaction)
interaction.response.send_message.assert_called_once_with(
"Thank you, the spoiler has been reported.", ephemeral=True
)
@pytest.mark.asyncio
async def test_cheater_command(self, bot, ctx):
"""Test the cheater command with valid inputs."""
cog = OtherCog(bot)
ctx.bot = bot
ctx.user.display_name = "ReporterUser"
test_username = "SuspectedUser"
test_description = "Suspicious activity description"
with patch('src.helpers.webhook.webhook_call', new_callable=AsyncMock) as mock_webhook:
await cog.cheater.callback(cog, ctx, test_username, test_description)
# Verify the webhook was called with correct data
mock_webhook.assert_called_once_with(
settings.JIRA_WEBHOOK,
{
"user": "ReporterUser",
"cheater": test_username,
"description": test_description,
"type": "cheater"
}
)
# Verify the response was sent
ctx.respond.assert_called_once_with(
"Thank you for your report.",
ephemeral=True
)
def test_setup(self, bot):
"""Test the setup method of the cog."""
# Invoke the command
other.setup(bot)
bot.add_cog.assert_called_once()