Skip to content

Commit

Permalink
Fix minor bug from PR #325 (#330)
Browse files Browse the repository at this point in the history
* Fix issue #118

* Fix error when self._known_host_file is None and None is passed to load_host_keys

* Update test_sftp.py
  • Loading branch information
sallyruthstruik authored and jschneier committed Jun 7, 2017
1 parent 1755392 commit 153aa40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion storages/backends/sftpstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def _connect(self):
)

if os.path.exists(known_host_file):
self._ssh.load_host_keys(self._known_host_file)
self._ssh.load_host_keys(known_host_file)


# and automatically add new host keys for hosts we haven't seen before.
self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
Expand Down
10 changes: 10 additions & 0 deletions tests/test_sftp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import stat
from datetime import datetime

try:
from unittest.mock import patch, MagicMock
except ImportError: # Python 3.2 and below
from mock import patch, MagicMock

from django.test import TestCase
from django.core.files.base import File
from django.utils.six import BytesIO
Expand All @@ -23,6 +26,13 @@ def test_no_known_hosts_file(self, mock_ssh):
self.storage._connect()
self.assertEqual('foo', mock_ssh.return_value.connect.call_args[0][0])

@patch.object(os.path, "expanduser", return_value="/path/to/known_hosts")
@patch.object(os.path, "exists", return_value=True)
@patch('paramiko.SSHClient')
def test_error_when_known_hosts_file_not_defined(self, mock_ssh, *a):
self.storage._connect()
self.storage._ssh.load_host_keys.assert_called_once_with("/path/to/known_hosts")

@patch('paramiko.SSHClient')
def test_connect(self, mock_ssh):
self.storage._connect()
Expand Down

0 comments on commit 153aa40

Please sign in to comment.