Skip to content

Commit

Permalink
chore: Add pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
timo-reymann committed Jul 20, 2024
1 parent 053500c commit b896528
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ on:
permissions:
contents: write
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.12
- run: |
pip install tests/
pytest tests/
main:
runs-on: ubuntu-latest
needs:
- test
steps:
- uses: timo-reymann/docker-semantic-release-gh-action@v1
with:
Expand Down
19 changes: 19 additions & 0 deletions tests/conftest.py
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
8 changes: 8 additions & 0 deletions tests/pyproject.toml
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.*"
]
81 changes: 81 additions & 0 deletions tests/test_user_creation_and_login.py
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)

0 comments on commit b896528

Please sign in to comment.