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
|
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
import discord
from conftest import MockApplicationContext, MockMessage
@pytest.fixture
def downloader_cog(bot):
from cogs.downloader import Downloader
cog = Downloader(bot)
return cog
@pytest.mark.asyncio
async def test_download_command_success_file(downloader_cog, mock_channel, mock_guild, bot):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
mock_file_path = "/tmp/test_video.mp4"
with patch('cogs.downloader.Downloader.download_standalone', return_value={
"status": "success",
"type": "file",
"content": mock_file_path,
"filename": "test_video.mp4"
}):
with patch('discord.File', return_value=MagicMock()) as mock_file:
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
await downloader_cog.download.callback(downloader_cog, ctx, url="https://example.com/video", catbox=False, fformat="mp4", filename_user=None)
mock_respond.assert_called_once()
call_args = mock_respond.call_args
assert 'file' in str(call_args) or mock_file.called
@pytest.mark.asyncio
async def test_download_command_success_url(downloader_cog, mock_channel, mock_guild, bot):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
with patch('cogs.downloader.Downloader.download_standalone', return_value={
"status": "success",
"type": "url",
"content": "https://catbox.2137697.xyz/sperma/test.mp4"
}):
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
await downloader_cog.download.callback(downloader_cog, ctx, url="https://example.com/video", catbox=True, fformat="mp4", filename_user=None)
mock_respond.assert_called_once()
call_args = mock_respond.call_args
assert "pobioron" in str(call_args)
@pytest.mark.asyncio
async def test_download_command_error(downloader_cog, mock_channel, mock_guild, bot):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
with patch('cogs.downloader.Downloader.download_standalone', return_value={
"status": "error",
"type": "error",
"content": "Download failed",
"details": "Connection timeout"
}):
with patch('cogs.downloader.logger') as mock_logger:
await downloader_cog.download.callback(downloader_cog, ctx, url="https://badurl.com", catbox=False, fformat="mp4", filename_user=None)
assert mock_logger.error.called
@pytest.mark.asyncio
async def test_message_download_with_url(downloader_cog, mock_channel, mock_guild, bot, mock_user):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
mock_message = MagicMock()
mock_message.content = "Check this out https://example.com/video"
with patch('cogs.downloader.Downloader.download_standalone', return_value={
"status": "success",
"type": "file",
"content": "/tmp/test.mp4"
}):
with patch.object(downloader_cog, 'download', new_callable=AsyncMock) as mock_download:
await downloader_cog.message_download.callback(downloader_cog, ctx, message=mock_message)
mock_download.assert_called_once()
@pytest.mark.asyncio
async def test_message_download_no_url(downloader_cog, mock_channel, mock_guild, bot):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
mock_message = MagicMock()
mock_message.content = "No URL here"
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
await downloader_cog.message_download.callback(downloader_cog, ctx, message=mock_message)
mock_respond.assert_called_once()
call_args = mock_respond.call_args
assert "Brak" in str(call_args)
|