From e12ee1d89cc673753942e4b3af7fd5a04e1dd65a Mon Sep 17 00:00:00 2001 From: Mathias Petermann Date: Wed, 11 Nov 2020 11:23:24 +0100 Subject: [PATCH] Implement delete file and delete token tests --- lemur/plugins/lemur_sftp/tests/test_sftp.py | 68 ++++++++++++++++----- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/lemur/plugins/lemur_sftp/tests/test_sftp.py b/lemur/plugins/lemur_sftp/tests/test_sftp.py index 09ad4e80..52fa6a14 100644 --- a/lemur/plugins/lemur_sftp/tests/test_sftp.py +++ b/lemur/plugins/lemur_sftp/tests/test_sftp.py @@ -20,9 +20,9 @@ class TestSftp(unittest.TestCase): @patch("lemur.plugins.lemur_sftp.plugin.paramiko") def test_upload_file_single_with_password(self, mock_paramiko): - dst_path = '/tmp/non-existant' + dst_path = '/var/non-existent' files = {'first-file': 'data'} - options = [{'name': 'host', 'value': 'non-existant'}, {'name': 'port', 'value': '22'}, + options = [{'name': 'host', 'value': 'non-existent'}, {'name': 'port', 'value': '22'}, {'name': 'user', 'value': 'test_acme'}, {'name': 'password', 'value': 'test_password'}] mock_sftp = Mock() @@ -34,19 +34,19 @@ class TestSftp(unittest.TestCase): self.sftp_destination.upload_file(dst_path, files, options) - mock_sftp.open.assert_called_once() + mock_sftp.open.assert_called_once_with('/var/non-existent/first-file', 'w') handle = mock_sftp.open() handle.write.assert_called_once_with('data') mock_ssh.close.assert_called_once() - mock_ssh.connect.assert_called_with('non-existant', username='test_acme', port='22', + mock_ssh.connect.assert_called_with('non-existent', username='test_acme', port='22', password='test_password') @patch("lemur.plugins.lemur_sftp.plugin.paramiko") def test_upload_file_multiple_with_key(self, mock_paramiko): - dst_path = '/tmp/non-existant' + dst_path = '/var/non-existent' files = {'first-file': 'data', 'second-file': 'data2'} - options = [{'name': 'host', 'value': 'non-existant'}, {'name': 'port', 'value': '22'}, - {'name': 'user', 'value': 'test_acme'}, {'name': 'privateKeyPath', 'value': '/tmp/id_rsa'}, + options = [{'name': 'host', 'value': 'non-existent'}, {'name': 'port', 'value': '22'}, + {'name': 'user', 'value': 'test_acme'}, {'name': 'privateKeyPath', 'value': '/var/id_rsa'}, {'name': 'privateKeyPass', 'value': 'ssh-key-password'}] mock_sftp = Mock() @@ -60,22 +60,22 @@ class TestSftp(unittest.TestCase): self.sftp_destination.upload_file(dst_path, files, options) - mock_sftp.open.assert_called() + mock_sftp.open.assert_called_with('/var/non-existent/second-file', 'w') handle = mock_sftp.open() handle.write.assert_called_with('data2') mock_ssh.close.assert_called_once() - mock_paramiko.RSAKey.from_private_key_file.assert_called_with('/tmp/id_rsa', 'ssh-key-password') - mock_ssh.connect.assert_called_with('non-existant', username='test_acme', port='22', + mock_paramiko.RSAKey.from_private_key_file.assert_called_with('/var/id_rsa', 'ssh-key-password') + mock_ssh.connect.assert_called_with('non-existent', username='test_acme', port='22', pkey='ssh-rsa test-key') @patch("lemur.plugins.lemur_sftp.plugin.paramiko") def test_upload_acme_token(self, mock_paramiko): token_path = './well-known/acme-challenge/some-token-path' token = 'token-data' - options = [{'name': 'host', 'value': 'non-existant'}, {'name': 'port', 'value': '22'}, + options = [{'name': 'host', 'value': 'non-existent'}, {'name': 'port', 'value': '22'}, {'name': 'user', 'value': 'test_acme'}, {'name': 'password', 'value': 'test_password'}, - {'name': 'destinationPath', 'value': '/tmp/destination-path'}] + {'name': 'destinationPath', 'value': '/var/destination-path'}] mock_sftp = Mock() mock_sftp.open = mock_open() @@ -86,9 +86,49 @@ class TestSftp(unittest.TestCase): self.sftp_destination.upload_acme_token(token_path, token, options) - mock_sftp.open.assert_called_once() + mock_sftp.open.assert_called_once_with('/var/destination-path/some-token-path', 'w') handle = mock_sftp.open() handle.write.assert_called_once_with('token-data') mock_ssh.close.assert_called_once() - mock_ssh.connect.assert_called_with('non-existant', username='test_acme', port='22', + mock_ssh.connect.assert_called_with('non-existent', username='test_acme', port='22', + password='test_password') + + @patch("lemur.plugins.lemur_sftp.plugin.paramiko") + def test_delete_file_with_password(self, mock_paramiko): + dst_path = '/var/non-existent' + files = {'first-file': None} + options = [{'name': 'host', 'value': 'non-existent'}, {'name': 'port', 'value': '22'}, + {'name': 'user', 'value': 'test_acme'}, {'name': 'password', 'value': 'test_password'}] + + mock_sftp = Mock() + + mock_ssh = mock_paramiko.SSHClient.return_value + mock_ssh.connect = MagicMock() + mock_ssh.open_sftp.return_value = mock_sftp + + self.sftp_destination.delete_file(dst_path, files, options) + + mock_sftp.remove.assert_called_once_with('/var/non-existent/first-file') + mock_ssh.close.assert_called_once() + mock_ssh.connect.assert_called_with('non-existent', username='test_acme', port='22', + password='test_password') + + @patch("lemur.plugins.lemur_sftp.plugin.paramiko") + def test_delete_acme_token(self, mock_paramiko): + token_path = './well-known/acme-challenge/some-token-path' + options = [{'name': 'host', 'value': 'non-existent'}, {'name': 'port', 'value': '22'}, + {'name': 'user', 'value': 'test_acme'}, {'name': 'password', 'value': 'test_password'}, + {'name': 'destinationPath', 'value': '/var/destination-path'}] + + mock_sftp = Mock() + + mock_ssh = mock_paramiko.SSHClient.return_value + mock_ssh.connect = MagicMock() + mock_ssh.open_sftp.return_value = mock_sftp + + self.sftp_destination.delete_acme_token(token_path, options) + + mock_sftp.remove.assert_called_once_with('/var/destination-path/some-token-path') + mock_ssh.close.assert_called_once() + mock_ssh.connect.assert_called_with('non-existent', username='test_acme', port='22', password='test_password') \ No newline at end of file