forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sftp.py
More file actions
159 lines (140 loc) · 5.49 KB
/
Copy pathtest_sftp.py
File metadata and controls
159 lines (140 loc) · 5.49 KB
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
import tempfile
from pathlib import Path
import paramiko
import pytest
from testcontainers.sftp import SFTPContainer, SFTPUser
def test_sftp_login_with_default_basic_auth():
with SFTPContainer() as sftp_container:
sftp_container.start()
host_ip = sftp_container.get_container_host_ip()
host_port = sftp_container.get_exposed_sftp_port()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=host_ip,
port=host_port,
username=sftp_container.users[0].name,
password=sftp_container.users[0].password,
)
def test_sftp_login_with_default_keypair_auth():
with SFTPContainer() as sftp_container:
sftp_container.start()
host_ip = sftp_container.get_container_host_ip()
host_port = sftp_container.get_exposed_sftp_port()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=host_ip,
port=host_port,
username=sftp_container.users[1].name,
key_filename=sftp_container.users[1].private_key_file,
)
def test_sftp_login_with_custom_user_basic_auth():
user = SFTPUser(name="custom", password="custom_password")
with SFTPContainer(users=[user]) as sftp_container:
sftp_container.start()
host_ip = sftp_container.get_container_host_ip()
host_port = sftp_container.get_exposed_sftp_port()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=host_ip,
port=host_port,
username=user.name,
password=user.password,
)
def test_sftp_login_with_custom_user_keypair_auth():
user = SFTPUser.with_keypair(name="custom")
with SFTPContainer(users=[user]) as sftp_container:
sftp_container.start()
host_ip = sftp_container.get_container_host_ip()
host_port = sftp_container.get_exposed_sftp_port()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=host_ip,
port=host_port,
username=user.name,
key_filename=user.private_key_file,
)
def test_sftp_login_with_custom_user_password_and_keypair_auth():
user = SFTPUser.with_keypair(name="custom", password="custom_password")
with SFTPContainer(users=[user]) as sftp_container:
sftp_container.start()
host_ip = sftp_container.get_container_host_ip()
host_port = sftp_container.get_exposed_sftp_port()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=host_ip,
port=host_port,
username=user.name,
password=user.password,
key_filename=user.private_key_file,
)
def test_sftp_user_can_upload():
with SFTPContainer() as sftp_container:
sftp_container.start()
host_ip = sftp_container.get_container_host_ip()
host_port = sftp_container.get_exposed_sftp_port()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=host_ip,
port=host_port,
username=sftp_container.users[0].name,
password=sftp_container.users[0].password,
)
sftp = ssh.open_sftp()
sftp.chdir("upload")
with tempfile.NamedTemporaryFile() as f:
f.write(b"test")
f.seek(0)
sftp.put(f.name, "test.txt")
with tempfile.NamedTemporaryFile() as f:
sftp.get("test.txt", f.name)
f.seek(0)
assert f.read() == b"test"
def test_sftp_user_can_download_from_mounted(tmp_path: Path):
temp_dir = tmp_path / "sub"
temp_dir.mkdir()
temp_file = temp_dir / "test.txt"
temp_file.write_text("test")
user = SFTPUser.with_keypair(name="custom", mount_dir=temp_dir.as_posix())
with SFTPContainer(users=[user]) as sftp_container:
sftp_container.start()
host_ip = sftp_container.get_container_host_ip()
host_port = sftp_container.get_exposed_sftp_port()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=host_ip,
port=host_port,
username=user.name,
key_filename=user.private_key_file,
)
sftp = ssh.open_sftp()
with tempfile.NamedTemporaryFile() as f:
sftp.get(temp_file.name, f.name)
f.seek(0)
assert f.read() == b"test"
def test_sftp_user_cant_upload_to_root(tmp_path: Path):
temp_dir = tmp_path / "sub"
temp_dir.mkdir()
temp_file = temp_dir / "test.txt"
temp_file.write_text("test")
with SFTPContainer() as sftp_container:
sftp_container.start()
host_ip = sftp_container.get_container_host_ip()
host_port = sftp_container.get_exposed_sftp_port()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=host_ip,
port=host_port,
username=sftp_container.users[0].name,
password=sftp_container.users[0].password,
)
sftp = ssh.open_sftp()
with pytest.raises(PermissionError):
sftp.put(temp_file.as_posix(), temp_file.name)