如何使用 FTPS/TLS 1.2 版模拟 FTP 连接? [英] How to mock FTP connection with FTPS/TLS version 1.2?

查看:86
本文介绍了如何使用 FTPS/TLS 1.2 版模拟 FTP 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 代码,可以使用 FTPS 和 TLS 1.2 版从 FPT 读取一些文件,这是函数,凭据是从 AWS 秘密管理器读取的:

I have a Python code to read some files from FPT using FTPS and TLS version 1.2, this is the function, the credentials are read from AWS secret manager:

def ftp_connection(host, username, password):

    try:
        ftp_connection = ftplib.FTP_TLS(host, username, password)

        # set TLS version 1.2
        ftp_connection.ssl_version = ssl.PROTOCOL_TLSv1_2

        # switching to secure data connection
        ftp_connection.prot_p()

    except ftplib.all_errors as err:
        print(err)

    return ftp_connection

我想知道如何为这个函数编写单元测试,我想测试:1.验证TLS v1.2连接2. 验证它使用的是安全数据连接

I wonder how I can write unit tests for this function, I'd like to test: 1.verify TLS v1.2 connection 2. verify it's using the secure data connection

我是单元测试的新手,还有什么可以添加到单元测试中的吗?非常感谢.

I'm new to unit tests, anything else that can be added to the unit tests? Many thanks.

这是我按照这个中的第一个答案所尝试的页面:

@patch('ftplib.FTP_TLS', autospec=True)
    def test_open_ftp_connection(self, mock_ftp_constructor):
        mock_ftp = mock_ftp_constructor.return_value
        read_news_ftp_read.open_ftp_connection('test_host', 'test_username', 'test_password')

        mock_ftp_constructor.assert_called_with('test_host', 'test_username', 'test_password')
        self.assertTrue(mock_ftp.login.called)

这给了我错误:

  File "C:\User\AppData\Local\Continuum\anaconda3\envs\Env-python3.7\lib\unittest\case.py", line 692, in assertTrue
    raise self.failureException(msg)
AssertionError: False is not true


Assertion failed

推荐答案

我认为您问的问题不太理想:

I think you're asking a less than ideal question:

我如何为这个函数编写单元测试,我想测试:1. 验证 TLS v1.2 连接 2. 验证它正在使用安全数据连接

how I can write unit tests for this function, I'd like to test: 1.verify TLS v1.2 connection 2. verify it's using the secure data connection

这两件事都不在您的控制范围内 - FTP 服务器可能已关闭或已更改,或者其证书可能会过期.这意味着您的单元测试将通过一次并在没有任何更改的情况下失败下一次,这使得它成为一个糟糕的单元测试,因为它没有测试单元".您的代码.

Both of those things are outside of your control - the FTP server could be down, or changed, or its certificates could expire. That means your unit test would pass one time and fail the next with no changes, and that makes it a poor unit test because it's not testing a "unit" of your code.

另一种方法是在测试期间创建一个 FTP 服务器,例如https://pytest-localftpserver.readthedocs.io 然后您可以添加虚拟文件以匹配您的期望要在服务器上,运行您的代码,然后检查它是否输出您期望的内容.这不是一个完美的解决方案,但它是一个务实的解决方案,应该让您对自己的代码充满信心,而且不需要花费大量的时间和精力来设置和使用.

A alternative is to create a FTP server for the duration of the test e.g. https://pytest-localftpserver.readthedocs.io You can then add dummy files to match what you expect to be on the server, run your code, then check that it output what you were expecting. This isn't a perfect solution, but it is a pragmatic one that should give you confidence in your code, but not take an immense amount of time and energy to setup and use.

另一种选择是进行端到端测试,您可以在其中联系外部服务器.它不会告诉您代码的特定版本是否有效,例如找出破坏内容的更改,但它会在运行端到端测试时告诉您它是否有效.

Another alternative is to do an end-to-end test, where you contact an external server. It doesn't tell you if a specific version of your code works, e.g. to find the change that broke stuff, but it will tell you if it was working when the end-to-end test was run.

这篇关于如何使用 FTPS/TLS 1.2 版模拟 FTP 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆