Skip to content

Commit

Permalink
test: Refactor test structure for connection creation
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-reymann committed Jul 20, 2024
1 parent 78ff6ac commit 6718c1a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
.idea/
pyvenv.cfg
.venv/
bin/
lib/
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
*.egg-info
41 changes: 40 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from ftplib import FTP
from pathlib import Path

import paramiko
import pytest
from testcontainers.core.container import DockerContainer
from testcontainers.core.image import DockerImage


@pytest.fixture(scope="session")
def docker_image():

image = DockerImage(path=Path(__file__).parent.parent, tag="timoreymann/chrooted-ftp:local-test-image")
image.build()
return image
Expand All @@ -20,3 +21,41 @@ def chrooted_ftp_test_container(docker_image) -> DockerContainer:
container.with_exposed_ports(2022)

return container


@pytest.fixture
def create_ftp_connection():
def impl(port: str | int, username: str, password: str):
ftp = FTP()
ftp.connect(host="localhost", port=int(port))
ftp.login(username, password)
return ftp

return impl


@pytest.fixture
def create_sftp_connection():
def impl(host, port, username, password):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(host, port, username, password)
return ssh_client.open_sftp()

return impl


@pytest.fixture()
def verify_login(create_ftp_connection, create_sftp_connection):
def impl(chrooted_ftp_test_container, connection_type, password, username):
if connection_type == "ftp":
ftp_port = chrooted_ftp_test_container.get_exposed_port(21)
ftp = create_ftp_connection(ftp_port, username, password)
assert ftp.getwelcome() is not None
elif connection_type == "sftp":
sftp_port = chrooted_ftp_test_container.get_exposed_port(2022)
sftp = create_sftp_connection("localhost", sftp_port, username, password)
result = sftp.listdir()
assert result is not None

return impl
42 changes: 12 additions & 30 deletions tests/test_user_creation_and_login.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,9 @@
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",
Expand All @@ -45,13 +19,17 @@ def impl(port: str | int):
)
@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,
verify_login,
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)
verify_login(chrooted_ftp_test_container, connection_type, password, username)


@pytest.mark.parametrize(
Expand All @@ -69,7 +47,11 @@ def test_login_with_env_var_user(
)
@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
chrooted_ftp_test_container,
username,
password,
connection_type,
verify_login,
):
with tempfile.NamedTemporaryFile() as f:
f.write(f"{username}:{password}\n".encode("utf8"))
Expand All @@ -78,4 +60,4 @@ def test_login_with_file_created_user(
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)
verify_login(chrooted_ftp_test_container, connection_type, password, username)

0 comments on commit 6718c1a

Please sign in to comment.