-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
053500c
commit b896528
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytest | ||
from testcontainers.core.container import DockerContainer | ||
from testcontainers.core.image import DockerImage | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def docker_image(): | ||
image = DockerImage(path="..", tag="timoreymann/chrooted-ftp:local-test-image") | ||
image.build() | ||
return image | ||
|
||
|
||
@pytest.fixture | ||
def chrooted_ftp_test_container(docker_image) -> DockerContainer: | ||
container = DockerContainer(docker_image.tag) | ||
container.with_exposed_ports(21) | ||
container.with_exposed_ports(2022) | ||
|
||
return container |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
name = "chrooted-ftp-tests" | ||
version = "0.0.0" | ||
dependencies = [ | ||
"pytest==8.2.*", | ||
"testcontainers==4.7.*", | ||
"paramiko==3.4.*" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import tempfile | ||
from ftplib import FTP | ||
|
||
import paramiko | ||
import pytest | ||
from testcontainers.core.waiting_utils import wait_for_logs | ||
|
||
|
||
def verify_login(chrooted_ftp_test_container, connection_type, create_ftp_connection, password, username): | ||
if connection_type == "ftp": | ||
ftp_port = chrooted_ftp_test_container.get_exposed_port(21) | ||
ftp_connection = create_ftp_connection(ftp_port) | ||
ftp_connection.login(username, password) | ||
elif connection_type == "sftp": | ||
sftp_port = chrooted_ftp_test_container.get_exposed_port(2022) | ||
ssh_client = paramiko.SSHClient() | ||
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | ||
ssh_client.connect("localhost", sftp_port, username, password) | ||
sftp = ssh_client.open_sftp() | ||
sftp.listdir() | ||
|
||
|
||
@pytest.fixture | ||
def create_ftp_connection(): | ||
def impl(port: str | int): | ||
ftp = FTP() | ||
ftp.connect(host="localhost", port=int(port)) | ||
return ftp | ||
|
||
return impl | ||
|
||
|
||
@pytest.mark.parametrize( | ||
[ | ||
"username", | ||
"password" | ||
], [ | ||
[ | ||
"user", "password", | ||
], | ||
[ | ||
"alex", "sp3ci$al^", | ||
] | ||
] | ||
) | ||
@pytest.mark.parametrize("connection_type", ["sftp", "ftp"]) | ||
def test_login_with_env_var_user( | ||
chrooted_ftp_test_container, create_ftp_connection, username, password, connection_type | ||
): | ||
chrooted_ftp_test_container.with_env(f"ACCOUNT_{username}", password) | ||
with chrooted_ftp_test_container: | ||
wait_for_logs(chrooted_ftp_test_container, "Server listening on :: port 2022") | ||
|
||
verify_login(chrooted_ftp_test_container, connection_type, create_ftp_connection, password, username) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
[ | ||
"username", | ||
"password" | ||
], [ | ||
[ | ||
"user", "password", | ||
], | ||
[ | ||
"alex", "sp3ci$al^", | ||
] | ||
] | ||
) | ||
@pytest.mark.parametrize("connection_type", ["sftp", "ftp"]) | ||
def test_login_with_file_created_user( | ||
chrooted_ftp_test_container, create_ftp_connection, username, password, connection_type | ||
): | ||
with tempfile.NamedTemporaryFile() as f: | ||
f.write(f"{username}:{password}\n".encode("utf8")) | ||
f.flush() | ||
chrooted_ftp_test_container.with_volume_mapping(f.name, "/opt/chrooted-ftp/users") | ||
with chrooted_ftp_test_container: | ||
wait_for_logs(chrooted_ftp_test_container, "Server listening on :: port 2022") | ||
|
||
verify_login(chrooted_ftp_test_container, connection_type, create_ftp_connection, password, username) |