# -*- test-case-name: mv3d.test.test_upload -*- # Copyright (C) 2009-2012 Mortal Coil Games # See LICENSE for details. """ @author: mike """ import os from zope.interface.verify import verifyClass from twisted.trial.unittest import TestCase from twisted.internet import defer from twisted.internet.defer import inlineCallbacks from mv3d.resource.upload import SFTPClient, ClientUserAuth, SFTPChannel from mv3d.resource.upload import SFTPUploader from mv3d.resource.upload import connect from mv3d.resource.iasset import IUploader class MockKey(object): """ A mock version of twisted.conch.ssh.keys.Key """ class TestClientUserAuth(TestCase): """ Tests for the ClientUserAuth class """ @inlineCallbacks def test_getPassword(self): """ Instantitate a ClientUserAuth and request the password """ cua = ClientUserAuth("mike", None, None, None) self.assertEqual(cua.getPassword(None), None) cua = ClientUserAuth("mike", None, "password", None) result = yield cua.getPassword(None) self.assertEqual(result, "password") def test_getPublicKey(self): """ Instantiate a ClientUserAuth and request the public key """ cua = ClientUserAuth("mike", None, None, None) self.assertEqual(cua.getPublicKey().result, None) pub = MockKey() private = MockKey() cua = ClientUserAuth("mike", None, None, (private, pub)) self.assertIdentical(cua.getPublicKey().result, pub) def test_getPrivateKey(self): """ Instantiate a ClientUserAuth and request the private key """ cua = ClientUserAuth("mike", None, None, None) self.assertEqual(cua.getPrivateKey().result, None) pub = MockKey() private = MockKey() cua = ClientUserAuth("mike", None, None, (private, pub)) self.assertIdentical(cua.getPrivateKey().result, private) class MockConn(object): """ A mock implementation of a connection object that a SFTPChannel may get. """ def sendRequest(self, channel, requestType, data, wantReply=0): """ Do nothing """ return defer.succeed(None) def sendData(self, data): """ Do nothing """ return len(data) class MockRemoteFile(object): """ A mock implementation of a remote file """ def __init__(self): self.data = "" self.name = "" def writeChunk(self, start, data): """ append the data """ self.data += data def close(self): """ close it """ class TestSFTPChannel(TestCase): """ Tests for the SFTPChannel class """ @inlineCallbacks def test_channelOpen(self): """ Instantiate a SFTPChannel and ensure the channelOpen method works as expected """ chan = SFTPChannel(MockConn()) chan.remoteMaxPacket = 32768 chan.channelOpen(None) yield chan.dfrd self.assertEqual(chan.dataReceived, chan.client.dataReceived) @inlineCallbacks def test_sendFile(self): """ Make a temporary file and attempt to send it """ tmpfn = os.path.join(self.mktemp(), "foo.txt") os.makedirs(os.path.dirname(tmpfn)) fil = open(tmpfn, "w") fil.write("hello") fil.close() chan = SFTPChannel(MockConn()) chan.remoteMaxPacket = 32768 chan.channelOpen(None) yield chan.dfrd dfrd = chan.sendFile(tmpfn) marf = MockRemoteFile() dfrd.callback(marf) self.assertEqual(marf.data, "hello") class MockChannel(object): """ A fake version of twisted.conch.ssh.channel.SSHChannel """ def __init__(self): self.dfrd = defer.succeed(None) def mockConnect(_host, _port, _options, _verifyHostKey, userAuthObject): """ Fake version of twisted.conch.client.connect.connect() """ userAuthObject.instance.channel = MockChannel() return defer.succeed(None) class TestSFTPClient(TestCase): """ Test various aspects of the sftp upload class """ def setUp(self): self.oldConnect = connect.connect connect.connect = mockConnect def tearDown(self): connect.connect = self.oldConnect @inlineCallbacks def test_connect(self): """ Test building an SFTPClient instance and then connecting to a mock server """ sftp = SFTPClient("nowhere.com", "noone", "password") yield sftp.connect() self.assertNotIdentical(sftp.connection, None) class MockConfigParser(object): """ Fake out a ConfigParser.ConfigParser """ def has_option(self, _section, option): if option in ["publicKey", "privateKey"]: return False return True def get(self, _section, option): return option class TestSFTPUploader(TestCase): """ Test the functionality of the SFTPUploader class """ def test_interface(self): """ Test that it adheres to the IUploader interface """ verifyClass(IUploader, SFTPUploader) def test_configure(self): """ Test configuring the uploader """ upl = SFTPUploader() upl.configure(MockConfigParser(), "Test") self.assertEqual(upl.client.host, "host") self.assertEqual(upl.client.user, "user") self.assertEqual(upl.client.password, "password") self.assertEqual(upl.client.key, None)