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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
import os
import discord
from tests.conftest import MockApplicationContext, MockMessage, MockUser
@pytest.fixture
def uploader_cog(bot):
with patch('cogs.utils.temp.get_temp_dir', return_value='/tmp/test'):
from cogs.uploader import Uploader
cog = Uploader(bot)
return cog
@pytest.mark.asyncio
async def test_file_autocomplete(uploader_cog):
mock_autocomplete_ctx = MagicMock()
mock_autocomplete_ctx.value = "test"
mock_interaction = MagicMock()
mock_user = MockUser(id=12345)
mock_interaction.user = mock_user
mock_autocomplete_ctx.interaction = mock_interaction
with patch('cogs.utils.file_search.UniversalSearcher.search_copyparty_autocomplete', new_callable=AsyncMock) as mock_search:
mock_search.return_value = [
"https://copyparty.local/testfile1.mp3",
"https://copyparty.local/testfile2.mp3"
]
result = await uploader_cog._file_autocomplete(mock_autocomplete_ctx)
assert len(result) == 2
assert result[0].name == "testfile1.mp3"
@pytest.mark.asyncio
async def test_file_autocomplete_empty_query(uploader_cog):
mock_autocomplete_ctx = MagicMock()
mock_autocomplete_ctx.value = ""
mock_interaction = MagicMock()
mock_interaction.user = MockUser(id=12345)
mock_autocomplete_ctx.interaction = mock_interaction
with patch('cogs.utils.file_search.UniversalSearcher.search_copyparty_autocomplete', new_callable=AsyncMock) as mock_search:
result = await uploader_cog._file_autocomplete(mock_autocomplete_ctx)
mock_search.assert_called_once_with("", max_results=25)
@pytest.mark.asyncio
async def test_download_from_url_success(uploader_cog):
with patch('requests.get') as mock_get:
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = b"test file content"
mock_get.return_value = mock_response
mock_file = MagicMock()
mock_file.write = AsyncMock()
mock_cm = AsyncMock()
mock_cm.__aenter__ = AsyncMock(return_value=mock_file)
mock_cm.__aexit__ = AsyncMock(return_value=None)
with patch('cogs.uploader.aiofiles.open', return_value=mock_cm):
result = await uploader_cog._download_from_url("https://copyparty.local/file.txt")
assert result is not None
assert "file.txt" in result
mock_file.write.assert_called_once_with(b"test file content")
@pytest.mark.asyncio
async def test_download_from_url_failure(uploader_cog):
with patch('requests.get') as mock_get:
mock_response = MagicMock()
mock_response.status_code = 404
mock_get.return_value = mock_response
result = await uploader_cog._download_from_url("https://copyparty.local/notfound.txt")
assert result is None
@pytest.mark.asyncio
async def test_upload_to_copyparty_success(uploader_cog):
mock_file = MagicMock()
mock_file.read = AsyncMock(return_value=b"test content")
mock_cm = AsyncMock()
mock_cm.__aenter__ = AsyncMock(return_value=mock_file)
mock_cm.__aexit__ = AsyncMock(return_value=None)
with patch('cogs.uploader.aiofiles.open', return_value=mock_cm):
with patch('requests.post') as mock_post:
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"fileurl": "https://copyparty.local/uploaded.txt"}
mock_post.return_value = mock_response
with patch.dict(os.environ, {"COPYPARTY_PASSWORD": "testpass"}):
result = await uploader_cog._upload_to_copyparty("/tmp/testfile.txt")
assert result == "https://copyparty.local/uploaded.txt"
@pytest.mark.asyncio
async def test_upload_to_copyparty_failure(uploader_cog):
mock_file = MagicMock()
mock_file.read = AsyncMock(return_value=b"test content")
mock_cm = AsyncMock()
mock_cm.__aenter__ = AsyncMock(return_value=mock_file)
mock_cm.__aexit__ = AsyncMock(return_value=None)
with patch('cogs.uploader.aiofiles.open', return_value=mock_cm):
with patch('requests.post') as mock_post:
mock_response = MagicMock()
mock_response.status_code = 500
mock_post.return_value = mock_response
result = await uploader_cog._upload_to_copyparty("/tmp/testfile.txt")
assert result is None
@pytest.mark.asyncio
async def test_upload_file_no_url(uploader_cog):
uploader_cog._uploadurl = None
result = await uploader_cog._upload_file("/tmp/test.txt")
assert result is None
@pytest.mark.asyncio
async def test_message_upload_no_attachments(uploader_cog, mock_channel, mock_guild, bot):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
mock_message = MagicMock()
mock_message.attachments = []
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
await uploader_cog.message_upload.callback(uploader_cog, ctx, message=mock_message)
mock_respond.assert_called_once_with("Brak załączników w wiadomości", ephemeral=True)
@pytest.mark.asyncio
async def test_message_upload_success(uploader_cog, mock_channel, mock_guild, bot):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
mock_attachment = MagicMock()
mock_attachment.filename = "test.txt"
mock_attachment.save = AsyncMock()
mock_message = MagicMock()
mock_message.attachments = [mock_attachment]
with patch.object(uploader_cog, '_upload_file', new_callable=AsyncMock) as mock_upload:
mock_upload.return_value = "https://copyparty.local/uploaded.txt"
with patch('os.path.exists', return_value=True):
with patch('os.remove'):
with patch.object(ctx, 'defer', new_callable=AsyncMock):
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
await uploader_cog.message_upload.callback(uploader_cog, ctx, message=mock_message)
mock_respond.assert_called_once()
assert "Wysłano" in str(mock_respond.call_args)
@pytest.mark.asyncio
async def test_message_upload_all_failed(uploader_cog, mock_channel, mock_guild, bot):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
mock_attachment = MagicMock()
mock_attachment.filename = "test.txt"
mock_attachment.save = AsyncMock()
mock_message = MagicMock()
mock_message.attachments = [mock_attachment]
with patch.object(uploader_cog, '_upload_file', new_callable=AsyncMock) as mock_upload:
mock_upload.return_value = None
with patch('os.path.exists', return_value=True):
with patch('os.remove'):
with patch.object(ctx, 'defer', new_callable=AsyncMock):
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
await uploader_cog.message_upload.callback(uploader_cog, ctx, message=mock_message)
mock_respond.assert_called_once_with("Nie udało się wysłać żadnych plików", ephemeral=True)
@pytest.mark.asyncio
async def test_upload_slash_command_success(uploader_cog, mock_channel, mock_guild, bot):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
mock_attachment = MagicMock()
mock_attachment.filename = "test.txt"
mock_attachment.save = AsyncMock()
with patch.object(uploader_cog, '_upload_file', new_callable=AsyncMock) as mock_upload:
mock_upload.return_value = "https://copyparty.local/uploaded.txt"
with patch('os.path.exists', return_value=True):
with patch('os.remove'):
with patch.object(ctx, 'defer', new_callable=AsyncMock):
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
await uploader_cog.upload.callback(uploader_cog, ctx, zalacznik=mock_attachment)
mock_respond.assert_called_once_with("https://copyparty.local/uploaded.txt")
@pytest.mark.asyncio
async def test_upload_slash_command_failure(uploader_cog, mock_channel, mock_guild, bot):
ctx = MockApplicationContext(bot=bot, channel=mock_channel, guild=mock_guild)
mock_attachment = MagicMock()
mock_attachment.filename = "test.txt"
mock_attachment.save = AsyncMock()
with patch.object(uploader_cog, '_upload_file', new_callable=AsyncMock) as mock_upload:
mock_upload.return_value = None
with patch('os.path.exists', return_value=True):
with patch('os.remove'):
with patch.object(ctx, 'defer', new_callable=AsyncMock):
with patch.object(ctx, 'respond', new_callable=AsyncMock) as mock_respond:
await uploader_cog.upload.callback(uploader_cog, ctx, zalacznik=mock_attachment)
mock_respond.assert_called_once_with("Nie udało się wysłać pliku", ephemeral=True)
|