Debian Codegraph @

tests/test_file.py

raw symbols SCIP
1 import datetime as dt
2 import math
3 from pathlib import PurePosixPath
4
5 import pytest
6
7 import aioftp
8
9
10 @pytest.mark.asyncio
11 async def test_remove_single_file(pair_factory):
12 async with pair_factory() as pair:
13 await pair.make_server_files("foo.txt")
14 assert await pair.server_paths_exists("foo.txt")
15 await pair.client.remove_file("foo.txt")
16 assert await pair.server_paths_exists("foo.txt") is False
17
18
19 @pytest.mark.asyncio
20 async def test_recursive_remove(pair_factory):
21 async with pair_factory() as pair:
22 paths = ["foo/bar.txt", "foo/baz.txt", "foo/bar_dir/foo.baz"]
23 await pair.make_server_files(*paths)
24 await pair.client.remove("foo")
25 assert await pair.server_paths_exists(*paths) is False
26
27
28 @pytest.mark.asyncio
29 async def test_mlsd_file(pair_factory):
30 async with pair_factory() as pair:
31 await pair.make_server_files("foo/bar.txt")
32 result = await pair.client.list("foo/bar.txt")
33 assert len(result) == 0
34
35
36 @pytest.mark.asyncio
37 async def test_file_download(pair_factory):
38 async with pair_factory() as pair:
39 await pair.make_server_files("foo", size=1, atom=b"foobar")
40 async with pair.client.download_stream("foo") as stream:
41 data = await stream.read()
42 assert data == b"foobar"
43
44
45 @pytest.mark.asyncio
46 async def test_file_download_exactly(pair_factory):
47 async with pair_factory() as pair:
48 await pair.make_server_files("foo", size=1, atom=b"foobar")
49 async with pair.client.download_stream("foo") as stream:
50 data1 = await stream.readexactly(3)
51 data2 = await stream.readexactly(3)
52 assert (data1, data2) == (b"foo", b"bar")
53
54
55 @pytest.mark.asyncio
56 async def test_file_download_enhanced_passive(pair_factory):
57 async with pair_factory() as pair:
58 pair.client._passive_commands = ["epsv"]
59 await pair.make_server_files("foo", size=1, atom=b"foobar")
60 async with pair.client.download_stream("foo") as stream:
61 data = await stream.read()
62 assert data == b"foobar"
63
64
65 @pytest.mark.asyncio
66 async def test_file_upload(pair_factory):
67 async with pair_factory() as pair:
68 async with pair.client.upload_stream("foo") as stream:
69 await stream.write(b"foobar")
70 async with pair.client.download_stream("foo") as stream:
71 data = await stream.read()
72 assert data == b"foobar"
73
74
75 @pytest.mark.asyncio
76 async def test_file_append(pair_factory):
77 async with pair_factory() as pair:
78 await pair.make_server_files("foo", size=1, atom=b"foobar")
79 async with pair.client.append_stream("foo") as stream:
80 await stream.write(b"foobar")
81 async with pair.client.download_stream("foo") as stream:
82 data = await stream.read()
83 assert data == b"foobar" * 2
84
85
86 @pytest.mark.asyncio
87 async def test_upload_folder(pair_factory):
88 async with pair_factory() as pair:
89 paths = ["foo/bar", "foo/baz"]
90 await pair.make_client_files(*paths)
91 assert await pair.server_paths_exists(*paths) is False
92 await pair.client.upload("foo")
93 assert await pair.server_paths_exists(*paths)
94
95
96 @pytest.mark.asyncio
97 async def test_upload_folder_into(pair_factory):
98 async with pair_factory() as pair:
99 paths = ["foo/bar", "foo/baz"]
100 await pair.make_client_files(*paths)
101 assert await pair.server_paths_exists("bar", "baz") is False
102 await pair.client.upload("foo", write_into=True)
103 assert await pair.server_paths_exists("bar", "baz")
104
105
106 @pytest.mark.asyncio
107 async def test_upload_folder_into_another(pair_factory):
108 async with pair_factory() as pair:
109 paths = ["foo/bar", "foo/baz"]
110 await pair.make_client_files(*paths)
111 assert await pair.server_paths_exists("bar/bar", "bar/baz") is False
112 await pair.client.upload("foo", "bar", write_into=True)
113 assert await pair.server_paths_exists("bar/bar", "bar/baz")
114
115
116 @pytest.mark.asyncio
117 async def test_download_folder(pair_factory):
118 async with pair_factory() as pair:
119 paths = ["foo/bar", "foo/baz"]
120 await pair.make_server_files(*paths)
121 assert await pair.client_paths_exists(*paths) is False
122 await pair.client.download("foo")
123 assert await pair.client_paths_exists(*paths)
124
125
126 @pytest.mark.asyncio
127 async def test_download_folder_into(pair_factory):
128 async with pair_factory() as pair:
129 paths = ["foo/bar", "foo/baz"]
130 await pair.make_server_files(*paths)
131 assert await pair.client_paths_exists("bar", "baz") is False
132 await pair.client.download("foo", write_into=True)
133 assert await pair.client_paths_exists("bar", "baz")
134
135
136 @pytest.mark.asyncio
137 async def test_download_folder_into_another(pair_factory):
138 async with pair_factory() as pair:
139 paths = ["foo/bar", "foo/baz"]
140 await pair.make_server_files(*paths)
141 assert await pair.client_paths_exists("bar/bar", "bar/baz") is False
142 await pair.client.download("foo", "bar", write_into=True)
143 assert await pair.client_paths_exists("bar/bar", "bar/baz")
144
145
146 @pytest.mark.asyncio
147 async def test_upload_file_over(pair_factory):
148 async with pair_factory() as pair:
149 await pair.make_client_files("foo", size=1, atom=b"client")
150 await pair.make_server_files("foo", size=1, atom=b"server")
151 async with pair.client.download_stream("foo") as stream:
152 assert await stream.read() == b"server"
153 await pair.client.upload("foo")
154 async with pair.client.download_stream("foo") as stream:
155 assert await stream.read() == b"client"
156
157
158 @pytest.mark.asyncio
159 async def test_download_file_over(pair_factory):
160 async with pair_factory() as pair:
161 await pair.make_client_files("foo", size=1, atom=b"client")
162 await pair.make_server_files("foo", size=1, atom=b"server")
163 async with pair.client.path_io.open(PurePosixPath("foo")) as f:
164 assert await f.read() == b"client"
165 await pair.client.download("foo")
166 async with pair.client.path_io.open(PurePosixPath("foo")) as f:
167 assert await f.read() == b"server"
168
169
170 @pytest.mark.asyncio
171 async def test_upload_file_write_into(pair_factory):
172 async with pair_factory() as pair:
173 await pair.make_client_files("foo", size=1, atom=b"client")
174 await pair.make_server_files("bar", size=1, atom=b"server")
175 async with pair.client.download_stream("bar") as stream:
176 assert await stream.read() == b"server"
177 await pair.client.upload("foo", "bar", write_into=True)
178 async with pair.client.download_stream("bar") as stream:
179 assert await stream.read() == b"client"
180
181
182 @pytest.mark.asyncio
183 async def test_upload_tree(pair_factory):
184 async with pair_factory() as pair:
185 await pair.make_client_files("foo/bar/baz", size=1, atom=b"client")
186 await pair.client.upload("foo", "bar", write_into=True)
187 files = await pair.client.list(recursive=True)
188 assert len(files) == 3
189
190
191 @pytest.mark.asyncio
192 async def test_download_file_write_into(pair_factory):
193 async with pair_factory() as pair:
194 await pair.make_client_files("foo", size=1, atom=b"client")
195 await pair.make_server_files("bar", size=1, atom=b"server")
196 async with pair.client.path_io.open(PurePosixPath("foo")) as f:
197 assert await f.read() == b"client"
198 await pair.client.download("bar", "foo", write_into=True)
199 async with pair.client.path_io.open(PurePosixPath("foo")) as f:
200 assert await f.read() == b"server"
201
202
203 @pytest.mark.asyncio
204 async def test_upload_file_os_error(
205 pair_factory,
206 Server,
207 expect_codes_in_exception,
208 ):
209 class OsErrorPathIO(aioftp.MemoryPathIO):
210 @aioftp.pathio.universal_exception
211 async def write(self, fout, data):
212 raise OSError("test os error")
213
214 s = Server(path_io_factory=OsErrorPathIO)
215 async with pair_factory(None, s) as pair:
216 with expect_codes_in_exception("451"):
217 async with pair.client.upload_stream("foo") as stream:
218 await stream.write(b"foobar")
219
220
221 @pytest.mark.asyncio
222 async def test_upload_path_unreachable(
223 pair_factory,
224 expect_codes_in_exception,
225 ):
226 async with pair_factory() as pair:
227 with expect_codes_in_exception("550"):
228 async with pair.client.upload_stream("foo/bar/foo") as stream:
229 await stream.write(b"foobar")
230
231
232 @pytest.mark.asyncio
233 async def test_stat_when_no_mlst(pair_factory):
234 async with pair_factory() as pair:
235 pair.server.commands_mapping.pop("mlst")
236 await pair.make_server_files("foo")
237 info = await pair.client.stat("foo")
238 assert info["type"] == "file"
239
240
241 @pytest.mark.asyncio
242 async def test_stat_mlst(pair_factory):
243 async with pair_factory() as pair:
244 now = dt.datetime.now(tz=dt.timezone.utc).replace(tzinfo=None)
245 await pair.make_server_files("foo")
246 info = await pair.client.stat("foo")
247 assert info["type"] == "file"
248 for fact in ("modify", "create"):
249 received = dt.datetime.strptime(info[fact], "%Y%m%d%H%M%S")
250 assert math.isclose(
251 now.timestamp(),
252 received.timestamp(),
253 abs_tol=10,
254 )