]>
code.delx.au - pymsnt/blob - src/tlib/msn/test_msnw.py
97579db908d546f966bae9923d7c5cabec21f9b2
1 # Copyright 2005-2006 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
5 Test cases for msnw (MSN Wrapper)
9 TIMEOUT
= 30.0 # Connection timeout in seconds
14 USER1
= "messengertest1@hotmail.com"
16 USER2
= "messengertest2@hotmail.com"
22 from twisted
. internet
. defer
import Deferred
23 from twisted
. internet
import reactor
, error
24 from twisted
. trial
import unittest
25 from twisted
. python
import log
36 log
. startLogging ( sys
. stdout
)
40 def clearAccount ( msncon
):
41 """ Clears the contact list of the given MSNConnection. Returns a
42 Deferred which fires when the task is complete.
51 if checkCount
== count
:
54 for msnContact
in msncon
. getContacts (). contacts
. values ():
55 for list in [ msn
. FORWARD_LIST
, msn
. BLOCK_LIST
, msn
. ALLOW_LIST
, msn
. PENDING_LIST
]:
56 if msnContact
. lists
& list :
57 msncon
. remContact ( list , msnContact
. userHandle
). addCallback ( cb
)
61 reactor
. callLater ( 0 , d
. callback
, None )
69 class MSNConnection ( msnw
. MSNConnection
):
70 def __init__ ( self
, username
, password
, ident
, testCase
):
71 msnw
. MSNConnection
.__ init
__ ( self
, username
, password
, ident
)
72 self
. testCase
= testCase
74 self
. contactAdded
= None
76 def listSynchronized ( self
):
77 # Now we're fully connected
78 self
. testCase
. done
= "SYNCED"
80 def gotMessage ( self
, userHandle
, text
):
81 self
. testCase
. done
= "GOTMESSAGE"
82 self
. message
= ( userHandle
, text
)
84 def contactAddedMe ( self
, userHandle
):
85 self
. contactAdded
= userHandle
104 def doLogins ( self
, both
= True ):
105 # Connect two accounts
106 self
. user1
= MSNConnection ( USER1
, PASS1
, "user1" , self
)
107 self
. loop ( "Logging in user1." , cond
= "SYNCED" )
109 self
. user2
= MSNConnection ( USER2
, PASS2
, "user2" , self
)
110 self
. loop ( "Logging in user2." , cond
= "SYNCED" )
112 def doPurgeContacts ( self
, both
= True ):
113 # Purge both contact lists
114 clearAccount ( self
. user1
). addCallback ( self
. cb
)
115 self
. loop ( "Purging user1 contact list." )
117 clearAccount ( self
. user2
). addCallback ( self
. cb
)
118 self
. loop ( "Purging user2 contact list." )
120 def doAddContacts ( self
, both
= True ):
121 # Adding users to each other's lists
122 self
. user1
. addContact ( msn
. FORWARD_LIST
, USER2
). addCallback ( self
. cb
)
123 self
. loop ( "Adding user2 to user1's forward list." )
124 self
. user1
. addContact ( msn
. ALLOW_LIST
, USER2
). addCallback ( self
. cb
)
125 self
. loop ( "Adding user2 to user1's allow list." )
127 self
. user2
. addContact ( msn
. FORWARD_LIST
, USER1
). addCallback ( self
. cb
)
128 self
. loop ( "Adding user1 to user2's forward list." )
129 self
. user2
. addContact ( msn
. ALLOW_LIST
, USER1
). addCallback ( self
. cb
)
130 self
. loop ( "Adding user1 to user2's allow list." )
132 # Check the contacts have seen each other
133 reactor
. iterate ( 0.1 ) # One last chance to notice each other
134 self
. failUnless (( self
. user1
. contactAdded
== USER2
and self
. user2
. contactAdded
== USER1
), "Contacts can't see each other." )
136 def cb ( self
, ignored
= None ):
139 def loop ( self
, failMsg
, cond
= True , timeout
= TIMEOUT
):
140 # Loops with a timeout
142 self
. timeout
= reactor
. callLater ( timeout
, self
. failed
, "Timeout: " + failMsg
)
147 while self
. done
!= cond
:
150 self
. timeout
. cancel ()
151 except ( error
. AlreadyCancelled
, error
. AlreadyCalled
):
154 self
. fail ( self
. failure
)
156 self
. failUnless (( self
. done
== cond
), "Failed: " + failMsg
)
158 def failed ( self
, why
):
164 class BasicTests ( unittest
. TestCase
, TestsUtil
):
166 TestsUtil
. setUp ( self
)
169 TestsUtil
. tearDown ( self
)
171 def testConnect ( self
):
172 self
. doLogins ( both
= False )
173 testConnect
. skip
= FTRECEIVETEST
or FTSENDTEST
175 def testPurgeContacts ( self
):
177 self
. doPurgeContacts ()
178 testPurgeContacts
. skip
= FTRECEIVETEST
or FTSENDTEST
180 def testAddContacts ( self
):
182 self
. doPurgeContacts ()
184 testAddContacts
. skip
= FTRECEIVETEST
or FTSENDTEST
186 def testMessageExchange ( self
):
188 self
. doPurgeContacts ()
190 self
. user1
. sendMessage ( USER2
, "Hi user2" )
191 self
. loop ( "Timeout exchanging message." , cond
= "GOTMESSAGE" )
192 self
. failUnless (( self
. user2
. message
== ( USER1
, "Hi user2" )), "Failed to transfer message." )
193 testMessageExchange
. skip
= FTRECEIVETEST
or FTSENDTEST
195 def testFileSend ( self
):
196 if raw_input ( " \n\n ALERT!!! \n\n Please 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. \n Type ok when you are ready: " % ( USER2
, USER1
)). lower () != "ok" :
197 raise unittest
. SkipTest ( "User didn't type 'ok'" )
199 data
= "Testing 123 \r\n " * 5000
200 def accepted (( yes
,)):
202 self
. fileSend
. write ( data
)
203 self
. fileSend
. close ()
205 self
. fail ( "File was not accepted." )
207 self
. fail ( "Transfer failed in invitation." )
208 def gotFileSend (( fileSend
, d
)):
209 self
. fileSend
= fileSend
210 d
. addCallbacks ( accepted
, failed
)
212 self
. doLogins ( both
= False )
213 self
. doPurgeContacts ( both
= False )
214 self
. doAddContacts ( both
= False )
215 d
= self
. user1
. sendFile ( USER2
, "myfile.txt" , len ( data
))
216 d
. addCallback ( gotFileSend
)
217 self
. loop ( "Sending file." , cond
= "GOTMESSAGE" , timeout
= 60 * 60 )
220 d
= self
. user1
. sendFile ( USER2
, "myfile2.txt" , len ( data
))
221 d
. addCallback ( gotFileSend
)
222 self
. loop ( "Sending file." , cond
= "GOTMESSAGE" , timeout
= 60 * 60 )
223 testFileSend
. skip
= not FTSENDTEST
225 def testFileReceive ( self
):
226 if raw_input ( " \n\n ALERT!!! \n\n Please connect to account %s and send a file transfer to %s . \n Type ok when you are ready: " % ( USER2
, USER1
)). lower () != "ok" :
227 raise unittest
. SkipTest ( "User didn't type 'ok'" )
229 def fileFinished ( data
):
230 #filename = "/tmp/msn" + str(random.randint(1000, 9999)) + ".dat"
231 filename
= "/tmp/MSNFILE_" + self
. fileReceive
. filename
232 f
= open ( filename
, "w" )
235 print "Got file!" , filename
236 # Terminate the loop in a little, let them send the BYE before
237 # we drop the connection
239 self
. done
= "GOTFILE"
240 reactor
. callLater ( 5 , wait
)
242 def gotFileReceive ( fileReceive
):
243 buffer = msn
. StringBuffer ( fileFinished
)
244 self
. fileReceive
= fileReceive
245 self
. fileReceive
. accept ( buffer )
247 self
. doLogins ( both
= False )
248 self
. user1
. gotFileReceive
= gotFileReceive
249 self
. doPurgeContacts ( both
= False )
250 self
. doAddContacts ( both
= False )
251 self
. loop ( "Receiving file." , cond
= "GOTFILE" , timeout
= 60 * 60 )
254 self
. loop ( "Receiving file." , cond
= "GOTFILE" , timeout
= 60 * 60 )
255 testFileReceive
. skip
= not FTRECEIVETEST