|
| 1 | +import pytest |
| 2 | + |
| 3 | +from nypl_py_utils.classes.sftp_client import SftpClient, SftpClientError |
| 4 | + |
| 5 | +_TEST_PUBLIC_KEY = ( |
| 6 | + 'AAAAB3NzaC1yc2EAAAADAQABAAAAgQCHc5r1z7bCxJ+dwR4r65CKB4KBF6mB+VZNYPc/1kmyT' |
| 7 | + 'vRh+P89asNvGDwATw7FZkz+g/0Z/Arak2ae454AHW7gBRO+TJ6YoAIrH2H5O3vQ4GGOepcTz3' |
| 8 | + '0ckuLoXtoaRMYzDTM1juvnITFq9fE5RMeFIM+Qc7BhOub/nDPLQI7/sw==' |
| 9 | +) |
| 10 | + |
| 11 | +_TEST_PRIVATE_KEY = ( |
| 12 | + '-----BEGIN RSA PRIVATE KEY-----\nMIICWwIBAAKBgQCItzqS6yQYBq+923wf4pQ6M2u0' |
| 13 | + 'pNMknrO4itBBQiDO6uDktZn2\nONnF1L9bYCtsucBGmRes6gdn+qFGTFRa+mWBHBO5CtOhbxA' |
| 14 | + 'bH9K4MWi9B6fF6Riw\nUkhOIsXHQFPtPg23kF+0MV953CrhZMMdWmYh4EVaRFfRmQchsjJkP0' |
| 15 | + 'eqBQIDAQAB\nAoGAEC+ZOLGsGUgZYGHu5Rt/LxDNbJqjAM/lOTD+DOvWVIkMTSeO7c63Qau5a' |
| 16 | + 'AkP\nuxSWxgTz/53JeK78jwUUa5z/jUbD+4D0NbfjmFOXGlnVxs/kbx4z4tPwwArN6gMS\n7T' |
| 17 | + 'fuEDgx4RF4a5kl5hOwDV1RUUCJ2TBO9wbm533ca7TvcCECQQDy3pKOB1ae9HM/\nYgtR6z1k0' |
| 18 | + 'd734ujmDXpViESfvJpm+fd/o0MEh193cO9qGFDWiOU23axF/n5fIaaf\nhHt/8C/dAkEAkBtw' |
| 19 | + 'bdQGDN9eZKH4XX1pRvB2PzUmrpgzZl3Zst8svKPDjeD9nm0Z\n+pGFLcVCIFT8ddUH1LSbt96' |
| 20 | + 'a4wn5/dPUSQJAUs2fmdzWo4skX8/FnEBfxifnpQwv\n639c3hx/iRZ8be97eoDnMHwXCFnwxn' |
| 21 | + 'NT3FEAFRyux45k93o5nNlGYfA54QJAKIwP\n7lch/K082gPY5jVLUfKG0vIZmDaq/7qYboPtC' |
| 22 | + 'obplxofQlxgWuhnGKHQIVjIUD9I\nnMjUp7+yxP8hoBHiQQJAZsNUg/q1JNCEoa4Gqb89yygr' |
| 23 | + 'x2fFOC/6eNp0ruWMRr5P\n8x1L+ugdXeUfI5vH7qI9wU+A7oADke63JBEHavv0UQ==\n-----' |
| 24 | + 'END RSA PRIVATE KEY-----' |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class TestSftpClient: |
| 29 | + |
| 30 | + @pytest.fixture |
| 31 | + def test_instance(self, mocker): |
| 32 | + mocker.patch('paramiko.SSHClient.connect') |
| 33 | + mocker.patch('paramiko.SSHClient.open_sftp') |
| 34 | + return SftpClient('test_host', 'test_user') |
| 35 | + |
| 36 | + def test_add_host_key(self, test_instance): |
| 37 | + assert len(test_instance.ssh_client.get_host_keys().keys()) == 0 |
| 38 | + |
| 39 | + test_instance.add_host_key('ssh-rsa', _TEST_PUBLIC_KEY) |
| 40 | + |
| 41 | + assert len(test_instance.ssh_client.get_host_keys().keys()) == 1 |
| 42 | + assert test_instance.ssh_client.get_host_keys().lookup( |
| 43 | + 'test_host') is not None |
| 44 | + |
| 45 | + def test_connect_password(self, test_instance): |
| 46 | + test_instance.password = 'test_password' |
| 47 | + |
| 48 | + test_instance.connect() |
| 49 | + |
| 50 | + test_instance.ssh_client.connect.assert_called_once_with( |
| 51 | + 'test_host', username='test_user', password='test_password', |
| 52 | + pkey=None) |
| 53 | + test_instance.ssh_client.open_sftp.assert_called_once() |
| 54 | + assert test_instance.sftp_conn is not None |
| 55 | + |
| 56 | + def test_connect_pkey(self, test_instance, mocker): |
| 57 | + mock_rsa_key = mocker.MagicMock() |
| 58 | + mock_pkey_method = mocker.patch('paramiko.RSAKey.from_private_key', |
| 59 | + return_value=mock_rsa_key) |
| 60 | + test_instance.private_key_str = _TEST_PRIVATE_KEY |
| 61 | + |
| 62 | + test_instance.connect() |
| 63 | + |
| 64 | + assert mock_pkey_method.call_args[0][0].read() == _TEST_PRIVATE_KEY |
| 65 | + test_instance.ssh_client.connect.assert_called_once_with( |
| 66 | + 'test_host', username='test_user', password=None, |
| 67 | + pkey=mock_rsa_key) |
| 68 | + test_instance.ssh_client.open_sftp.assert_called_once() |
| 69 | + assert test_instance.sftp_conn is not None |
| 70 | + |
| 71 | + def test_download(self, test_instance, mocker): |
| 72 | + test_instance.sftp_conn = mocker.MagicMock() |
| 73 | + |
| 74 | + test_instance.download('remote/path', 'local/path') |
| 75 | + |
| 76 | + test_instance.sftp_conn.get.assert_called_once_with( |
| 77 | + 'remote/path', 'local/path') |
| 78 | + |
| 79 | + def test_download_error(self, test_instance, mocker): |
| 80 | + test_instance.ssh_client = mocker.MagicMock() |
| 81 | + test_instance.sftp_conn = mocker.MagicMock() |
| 82 | + test_instance.sftp_conn.get.side_effect = IOError('test error') |
| 83 | + |
| 84 | + with pytest.raises(SftpClientError): |
| 85 | + test_instance.download('remote/path', 'local/path') |
| 86 | + |
| 87 | + test_instance.sftp_conn.get.assert_called_once_with( |
| 88 | + 'remote/path', 'local/path') |
| 89 | + test_instance.sftp_conn.close.assert_called_once() |
| 90 | + test_instance.ssh_client.close.assert_called_once() |
| 91 | + |
| 92 | + def test_close_connection(self, test_instance, mocker): |
| 93 | + test_instance.sftp_conn = mocker.MagicMock() |
| 94 | + test_instance.ssh_client = mocker.MagicMock() |
| 95 | + |
| 96 | + test_instance.close_connection() |
| 97 | + |
| 98 | + test_instance.sftp_conn.close.assert_called_once() |
| 99 | + test_instance.ssh_client.close.assert_called_once() |
0 commit comments