2

I have created this functionality where System regularly connects to remote location, checks specified directory and if it finds specific files in there, it imports data to system.

Transfer functionality is implemented using paramiko and ftplib. So I wonder if it is possible to somehow create some "fake" SSH connection into one of my module's directories, so it would act like a remote location where there would be test file to import (at least with paramiko behavior)?

So unittest could be run without actual real connection to some remote location? Any ideas for that?

Update to have better question, here is a sample of method that might have unittest (unittest would be to test if I could get sftp object):

import socket
from paramiko import AuthenticationException, Transport, SFTPClient

def get_sftp_connection(self, log_error=True):
    self.ensure_one()
    login = self.login_id
    LogCustom = self.env['log.custom'] # Custom thing, no need for unittest here
    ref = self._get_rec_description()
    try:
        transport = Transport((login.host, login.port))
        transport.connect(username=login.user, password=login.passwd)
        sftp = SFTPClient.from_transport(transport)            
    except socket.gaierror:
        msg = _("Name or service '%s' not known") % (login.host)
        if log_error:
            LogCustom.log_error_event(operation='read', ref=ref, name=msg)
            return None
        else:
            raise ValidationError(msg)
    except AuthenticationException:
        msg = _("Bad username or password")
        if log_error: # No need for unittest
            LogCustom.log_error_event(operation='read', ref=ref, name=msg)
            return None
        else:
            raise ValidationError(msg)

    return sftp
Andrius
  • 19,658
  • 37
  • 143
  • 243
  • 1
    Change your function opening SSH connection so that it open a directory on your computer (or create another with the same interface which you will use for testing). – Psytho Nov 05 '15 at 14:26
  • If you can post an initial version of your test (that doesn't solve your problem) it should be possible to provide a version of that test that does solve your problem. – Sebastian Nov 05 '15 at 14:28
  • Have you seen MockSSH yet by any chance? It might help: https://github.com/ncouture/MockSSH . – idjaw Nov 05 '15 at 14:32
  • @Sebastian I haven't started this unittesting. First I wanted to understand how can you write one for such functionality. – Andrius Nov 05 '15 at 14:42
  • @idjaw thanks, that one looks promising or at least a start to writing such unittests. – Andrius Nov 05 '15 at 14:42
  • @Andrius if you're looking for examples try to look up how to use mock with mockSSH. There should be some good examples out there to help with an initial structure. – idjaw Nov 05 '15 at 14:45
  • @idjaw yeah I will do that. Thanks – Andrius Nov 05 '15 at 14:46

0 Answers0