]> code.delx.au - pymsnt/blobdiff - src/tlib/msn/test_msnw.py
We can now receive files too!
[pymsnt] / src / tlib / msn / test_msnw.py
index fe078546ab466580bc5ff753b1a1bbea5a099a99..e039f9caa3a5b82d095cf4283773a1511e4dba5b 100644 (file)
@@ -12,10 +12,11 @@ from twisted.trial import unittest
 from twisted.python import log
 
 # System imports
-import sys
+import sys, random
 
 # Local imports
 import msnw
+import msn
 
 
 # Settings
@@ -136,8 +137,12 @@ class TestsUtil:
                # Loops with a timeout
                self.done = False
                self.timeout = reactor.callLater(timeout, self.failed, "Timeout: " + failMsg)
-               while self.done != cond and not self.done:
-                       reactor.iterate(0.1)
+               if cond == True:
+                       while not self.done:
+                               reactor.iterate(0.1)
+               else:
+                       while self.done != cond:
+                               reactor.iterate(0.1)
                try:
                        self.timeout.cancel()
                except (error.AlreadyCancelled, error.AlreadyCalled):
@@ -184,7 +189,10 @@ class BasicTests(unittest.TestCase, TestsUtil):
                self.failUnless((self.user2.message == (USER1, "Hi user2")), "Failed to transfer message.")
 #      testMessageExchange.skip = "True"
 
-       def testFileTransfer(self):
+       def testFileSend(self):
+               if raw_input("\n\nALERT!!!\n\nPlease connect to account %s and accept the file transfer from %s. When you have received the complete file, send a message back to the client to signal success.\nType ok when you are ready: " % (USER2, USER1)).lower() != "ok":
+                       raise unittest.SkipTest("User didn't type 'ok'")
+
                data = "Testing 123\r\n" * 5000
                def accepted((yes,)):
                        if yes:
@@ -197,13 +205,41 @@ class BasicTests(unittest.TestCase, TestsUtil):
                def gotFileSend((fileSend, d)):
                        self.fileSend = fileSend
                        d.addCallbacks(accepted, failed)
-               if raw_input("\n\nALERT!!!\n\nPlease connect to account %s and accept the file transfer from %s. When you have received the complete file, send a message back to the client to signal success.\nType ok when you are ready: " % (USER2, USER1)).lower() != "ok":
-                       print "TEST SKIPPED!!!"
-                       return
+
                self.doLogins(both=False)
                self.doPurgeContacts(both=False)
                self.doAddContacts(both=False)
                d = self.user1.sendFile(USER2, "myfile.txt", len(data))
                d.addCallback(gotFileSend)
                self.loop("Sending file.", cond="GOTMESSAGE", timeout=60*60)
+#      testFileSend.skip = "True"
+
+       def testFileReceive(self):
+               if raw_input("\n\nALERT!!!\n\nPlease connect to account %s and send a file transfer to %s.\nType ok when you are ready: " % (USER2, USER1)).lower() != "ok":
+                       raise unittest.SkipTest("User didn't type 'ok'")
+
+               def fileFinished(data):
+                       #filename = "/tmp/msn" + str(random.randint(1000, 9999)) + ".dat"
+                       filename = "/tmp/MSNFILE_" + self.fileReceive.filename
+                       f = open(filename, "w")
+                       f.write(data)
+                       f.close()
+                       print "Got file!", filename
+                       # Terminate the loop in a little, let them send the BYE before
+                       # we drop the connection
+                       def wait():
+                               self.done = "GOTFILE"
+                       reactor.callLater(5, wait)
+
+               def gotFileReceive(fileReceive):
+                       buffer = msn.StringBuffer(fileFinished)
+                       self.fileReceive = fileReceive
+                       self.fileReceive.accept(buffer)
+
+               self.doLogins(both=False)
+               self.user1.gotFileReceive = gotFileReceive
+               self.doPurgeContacts(both=False)
+               self.doAddContacts(both=False)
+               self.loop("Receiving file.", cond="GOTFILE", timeout=60*60)
+#      testFileReceive.skip = "True"