Pytest/Unittest SFTP Client Connection

I’ve written a function that establishes and returns an SFTP client connection. Here is the code:

import paramiko
from io import StringIO
from fastapi import HTTPException

def get_sftp_client(
    username: str,
    password: str = None,
    key_filename: str = None,
    private_key: str = None,
    host: str = "hostexample.com",
):
    try:
        if not username or (not password and not key_filename and not private_key):
            raise HTTPException(status_code=500, detail="SFTP credentials are not set")

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        if private_key:
            private_key_file = StringIO(private_key)
            pkey = paramiko.RSAKey.from_private_key(private_key_file)
            ssh.connect(
                hostname=host,
                port=22,
                username=username,
                pkey=pkey,
            )
        elif key_filename:
            ssh.connect(
                hostname=host,
                port=22,
                username=username,
                key_filename=key_filename,
            )
        else:
            ssh.connect(
                hostname=host,
                port=22,
                username=username,
                password=password,
            )

        sftp = ssh.open_sftp()
        return sftp
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to connect to SFTP server: {str(e)}")

I would like to write unit tests for this function, especially to understand how it behaves when both a password and a private key are provided. I’m struggling to mock the dependencies properly and I’m not sure how to test this without a local SFTP server.

Here is what I have tried so far using pytest and unittest.mock:

import pytest
from unittest.mock import patch, MagicMock
from io import StringIO

def test_get_sftp_client_password_and_private_key():
    from my_module import get_sftp_client, HTTPException 

    # Mocking paramiko
    with patch('my_module.paramiko.SSHClient') as MockSSHClient:
        mock_ssh_client = MockSSHClient.return_value
        mock_ssh_client.open_sftp.return_value = MagicMock()

        # Test with password and private_key
        username = "testuser"
        password = "testpassword"
        private_key = "test_private_key"

        # Simulate private key behavior
        mock_pkey = MagicMock()
        with patch('my_module.paramiko.RSAKey.from_private_key', return_value=mock_pkey):
            sftp_client = get_sftp_client(
                username=username,
                password=password,
                private_key=private_key
            )

        # Assertions
        MockSSHClient.assert_called_once()
        mock_ssh_client.set_missing_host_key_policy.assert_called_once()
        mock_ssh_client.connect.assert_called_once_with(
            hostname="hostexample.com",
            port=22,
            username=username,
            pkey=mock_pkey,
        )
        mock_ssh_client.open_sftp.assert_called_once()
        assert sftp_client is not None

def test_get_sftp_client_raises_exception():
    from my_module import get_sftp_client, HTTPException 

    # Mocking paramiko
    with patch('my_module.paramiko.SSHClient') as MockSSHClient:
        mock_ssh_client = MockSSHClient.return_value
        mock_ssh_client.connect.side_effect = Exception("Connection failed")

        # Test with missing credentials
        username = "testuser"

        with pytest.raises(HTTPException) as excinfo:
            get_sftp_client(username=username)

        assert excinfo.value.status_code == 500
        assert "Failed to connect to SFTP server" in excinfo.value.detail

I’m looking for advice on how to properly mock the paramiko library to test this function effectively. Any tips on how to test this without a local SFTP server would be greatly appreciated as well.

Thank you in advance for your help!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật