| 1 | import sys |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | if sys.version_info < (3, 11): |
| 6 | pytest.skip(reason="required python 3.11+", allow_module_level=True) |
| 7 | |
| 8 | |
| 9 | def simple_response(code, message): |
| 10 | async def command(connection, rest): |
| 11 | connection.response(code, message) |
| 12 | return True |
| 13 | |
| 14 | return command |
| 15 | |
| 16 | |
| 17 | AUTH_RESPONSE = simple_response("234", ":P") |
| 18 | PBSZ_RESPONSE = simple_response("200", ":P") |
| 19 | PROT_RESPONSE = simple_response("200", ":P") |
| 20 | OK_RESPONSE = simple_response("200", ":P") |
| 21 | |
| 22 | |
| 23 | @pytest.mark.asyncio |
| 24 | async def test_upgrade_to_tls(mocker, pair_factory): |
| 25 | ssl_context = object() |
| 26 | create_default_context = mocker.patch("aioftp.client.ssl.create_default_context", return_value=ssl_context) |
| 27 | |
| 28 | async with pair_factory(logged=False, do_quit=False) as pair: |
| 29 | pair.server.commands_mapping["auth"] = AUTH_RESPONSE |
| 30 | pair.server.commands_mapping["pbsz"] = PBSZ_RESPONSE |
| 31 | pair.server.commands_mapping["prot"] = PROT_RESPONSE |
| 32 | |
| 33 | start_tls = mocker.patch.object(pair.client.stream, "start_tls") |
| 34 | command_spy = mocker.spy(pair.client, "command") |
| 35 | |
| 36 | await pair.client.upgrade_to_tls() |
| 37 | |
| 38 | create_default_context.assert_called_once_with() |
| 39 | start_tls.assert_called_once_with(sslcontext=ssl_context, server_hostname=pair.client.server_host) |
| 40 | assert command_spy.call_count == 3 |
| 41 | command_spy.assert_has_calls( |
| 42 | [ |
| 43 | mocker.call("AUTH TLS", "234"), |
| 44 | mocker.call("PBSZ 0", "200"), |
| 45 | mocker.call("PROT P", "200"), |
| 46 | ], |
| 47 | ) |
| 48 | |
| 49 | |
| 50 | @pytest.mark.asyncio |
| 51 | async def test_upgrade_to_tls_custom_ssl_context(mocker, pair_factory): |
| 52 | ssl_context = object() |
| 53 | create_default_context = mocker.patch("aioftp.client.ssl.create_default_context") |
| 54 | |
| 55 | async with pair_factory(logged=False, do_quit=False) as pair: |
| 56 | pair.server.commands_mapping["auth"] = AUTH_RESPONSE |
| 57 | pair.server.commands_mapping["pbsz"] = PBSZ_RESPONSE |
| 58 | pair.server.commands_mapping["prot"] = PROT_RESPONSE |
| 59 | |
| 60 | start_tls = mocker.patch.object(pair.client.stream, "start_tls") |
| 61 | command_spy = mocker.spy(pair.client, "command") |
| 62 | |
| 63 | await pair.client.upgrade_to_tls(sslcontext=ssl_context) |
| 64 | |
| 65 | create_default_context.assert_not_called() |
| 66 | start_tls.assert_called_once_with(sslcontext=ssl_context, server_hostname=pair.client.server_host) |
| 67 | assert command_spy.call_count == 3 |
| 68 | command_spy.assert_has_calls( |
| 69 | [ |
| 70 | mocker.call("AUTH TLS", "234"), |
| 71 | mocker.call("PBSZ 0", "200"), |
| 72 | mocker.call("PROT P", "200"), |
| 73 | ], |
| 74 | ) |
| 75 | |
| 76 | |
| 77 | @pytest.mark.asyncio |
| 78 | async def test_upgrade_to_tls_when_logged_in(mocker, pair_factory): |
| 79 | mocker.patch("aioftp.client.ssl.create_default_context") |
| 80 | |
| 81 | async with pair_factory(logged=False, do_quit=False) as pair: |
| 82 | pair.server.commands_mapping["auth"] = AUTH_RESPONSE |
| 83 | pair.server.commands_mapping["pbsz"] = OK_RESPONSE |
| 84 | pair.server.commands_mapping["prot"] = OK_RESPONSE |
| 85 | |
| 86 | mocker.patch.object(pair.client.stream, "start_tls") |
| 87 | command_spy = mocker.spy(pair.client, "command") |
| 88 | |
| 89 | await pair.client.upgrade_to_tls() |
| 90 | |
| 91 | command_spy.assert_has_calls( |
| 92 | [ |
| 93 | mocker.call("AUTH TLS", "234"), |
| 94 | mocker.call("PBSZ 0", "200"), |
| 95 | mocker.call("PROT P", "200"), |
| 96 | ], |
| 97 | ) |