]>
code.delx.au - pymsnt/blob - src/tlib/msn/test_msnw.py
1 # Copyright 2005 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 from twisted
. internet
. defer
import Deferred
10 from twisted
. internet
import reactor
, error
11 from twisted
. trial
import unittest
12 from twisted
. python
import log
22 TIMEOUT
= 30.0 # Connection timeout in seconds
24 USER1
= "messengertest1@hotmail.com"
26 USER2
= "messengertest2@hotmail.com"
32 log
. startLogging ( sys
. stdout
)
36 def clearAccount ( msncon
):
37 """ Clears the contact list of the given MSNConnection. Returns a
38 Deferred which fires when the task is complete.
47 if checkCount
== count
:
50 for msnContact
in msncon
. getContacts (). contacts
. values ():
51 for list in [ msnw
. FORWARD_LIST
, msnw
. BLOCK_LIST
, msnw
. ALLOW_LIST
, msnw
. PENDING_LIST
]:
52 if msnContact
. lists
& list :
53 msncon
. remContact ( list , msnContact
. userHandle
). addCallback ( cb
)
57 reactor
. callLater ( 0 , d
. callback
, None )
65 class MSNConnection ( msnw
. MSNConnection
):
66 def __init__ ( self
, username
, password
, ident
, testCase
):
67 msnw
. MSNConnection
.__ init
__ ( self
, username
, password
, ident
)
68 self
. testCase
= testCase
70 self
. contactAdded
= None
72 def listSynchronized ( self
):
73 # Now we're fully connected
74 self
. testCase
. done
= "SYNCED"
76 def gotMessage ( self
, userHandle
, text
):
77 self
. testCase
. done
= "GOTMESSAGE"
78 self
. message
= ( userHandle
, text
)
80 def contactAddedMe ( self
, userHandle
):
81 self
. contactAdded
= userHandle
100 def doLogins ( self
, both
= True ):
101 # Connect two accounts
102 self
. user1
= MSNConnection ( USER1
, PASS1
, "user1" , self
)
103 self
. loop ( "Logging in user1." , cond
= "SYNCED" )
105 self
. user2
= MSNConnection ( USER2
, PASS2
, "user2" , self
)
106 self
. loop ( "Logging in user2." , cond
= "SYNCED" )
108 def doPurgeContacts ( self
, both
= True ):
109 # Purge both contact lists
110 clearAccount ( self
. user1
). addCallback ( self
. cb
)
111 self
. loop ( "Purging user1 contact list." )
113 clearAccount ( self
. user2
). addCallback ( self
. cb
)
114 self
. loop ( "Purging user2 contact list." )
116 def doAddContacts ( self
, both
= True ):
117 # Adding users to each other's lists
118 self
. user1
. addContact ( msnw
. FORWARD_LIST
, USER2
). addCallback ( self
. cb
)
119 self
. loop ( "Adding user2 to user1's forward list." )
120 self
. user1
. addContact ( msnw
. ALLOW_LIST
, USER2
). addCallback ( self
. cb
)
121 self
. loop ( "Adding user2 to user1's allow list." )
123 self
. user2
. addContact ( msnw
. FORWARD_LIST
, USER1
). addCallback ( self
. cb
)
124 self
. loop ( "Adding user1 to user2's forward list." )
125 self
. user2
. addContact ( msnw
. ALLOW_LIST
, USER1
). addCallback ( self
. cb
)
126 self
. loop ( "Adding user1 to user2's allow list." )
128 # Check the contacts have seen each other
129 reactor
. iterate ( 0.1 ) # One last chance to notice each other
130 self
. failUnless (( self
. user1
. contactAdded
== USER2
and self
. user2
. contactAdded
== USER1
), "Contacts can't see each other." )
132 def cb ( self
, ignored
= None ):
135 def loop ( self
, failMsg
, cond
= True , timeout
= TIMEOUT
):
136 # Loops with a timeout
138 self
. timeout
= reactor
. callLater ( timeout
, self
. failed
, "Timeout: " + failMsg
)
139 while self
. done
!= cond
and not self
. done
:
142 self
. timeout
. cancel ()
143 except ( error
. AlreadyCancelled
, error
. AlreadyCalled
):
146 self
. fail ( self
. failure
)
148 self
. failUnless (( self
. done
== cond
), "Failed: " + failMsg
)
150 def failed ( self
, why
):
156 class BasicTests ( unittest
. TestCase
, TestsUtil
):
158 TestsUtil
. setUp ( self
)
161 TestsUtil
. tearDown ( self
)
163 def testConnect ( self
):
164 self
. doLogins ( both
= False )
165 # testConnect.skip = "True"
167 def testPurgeContacts ( self
):
169 self
. doPurgeContacts ()
170 # testPurgeContacts.skip = "True"
172 def testAddContacts ( self
):
174 self
. doPurgeContacts ()
176 # testAddContacts.skip = "True"
178 def testMessageExchange ( self
):
180 self
. doPurgeContacts ()
182 self
. user1
. sendMessage ( USER2
, "Hi user2" )
183 self
. loop ( "Timeout exchanging message." , cond
= "GOTMESSAGE" )
184 self
. failUnless (( self
. user2
. message
== ( USER1
, "Hi user2" )), "Failed to transfer message." )
185 # testMessageExchange.skip = "True"
187 def testFileTransfer ( self
):
188 data
= "Testing 123 \r\n " * 5000
189 def accepted (( yes
,)):
191 self
. fileSend
. write ( data
)
192 self
. fileSend
. close ()
194 self
. fail ( "File was not accepted." )
196 self
. fail ( "Transfer failed in invitation." )
197 def gotFileSend (( fileSend
, d
)):
198 self
. fileSend
= fileSend
199 d
. addCallbacks ( accepted
, failed
)
200 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" :
201 print "TEST SKIPPED!!!"
203 self
. doLogins ( both
= False )
204 self
. doPurgeContacts ( both
= False )
205 self
. doAddContacts ( both
= False )
206 d
= self
. user1
. sendFile ( USER2
, "myfile.txt" , len ( data
))
207 d
. addCallback ( gotFileSend
)
208 self
. loop ( "Sending file." , cond
= "GOTMESSAGE" , timeout
= 60 * 60 )