An other cleanup of xmlstream.py
[slixmpp.git] / tests / test_stanza_gmail.py
bloba17efca27102b047f03d0d9f3dc101682f7c026d
1 import unittest
2 from slixmpp import Iq
3 from slixmpp.test import SlixTest
4 import slixmpp.plugins.gmail_notify as gmail
5 from slixmpp.xmlstream import register_stanza_plugin, ET
8 class TestGmail(SlixTest):
10 def setUp(self):
11 register_stanza_plugin(Iq, gmail.GmailQuery)
12 register_stanza_plugin(Iq, gmail.MailBox)
13 register_stanza_plugin(Iq, gmail.NewMail)
15 def testCreateQuery(self):
16 """Testing querying Gmail for emails."""
18 iq = self.Iq()
19 iq['type'] = 'get'
20 iq['gmail']['search'] = 'is:starred'
21 iq['gmail']['newer-than-time'] = '1140638252542'
22 iq['gmail']['newer-than-tid'] = '11134623426430234'
24 self.check(iq, """
25 <iq type="get">
26 <query xmlns="google:mail:notify"
27 newer-than-time="1140638252542"
28 newer-than-tid="11134623426430234"
29 q="is:starred" />
30 </iq>
31 """, use_values=False)
33 def testMailBox(self):
34 """Testing reading from Gmail mailbox result"""
36 # Use the example from Google's documentation at
37 # http://code.google.com/apis/talk/jep_extensions/gmail.html#notifications
38 xml = ET.fromstring("""
39 <iq type="result">
40 <mailbox xmlns="google:mail:notify"
41 result-time='1118012394209'
42 url='http://mail.google.com/mail'
43 total-matched='95'
44 total-estimate='0'>
45 <mail-thread-info tid='1172320964060972012'
46 participation='1'
47 messages='28'
48 date='1118012394209'
49 url='http://mail.google.com/mail?view=cv'>
50 <senders>
51 <sender name='Me' address='romeo@gmail.com' originator='1' />
52 <sender name='Benvolio' address='benvolio@gmail.com' />
53 <sender name='Mercutio' address='mercutio@gmail.com' unread='1'/>
54 </senders>
55 <labels>act1scene3</labels>
56 <subject>Put thy rapier up.</subject>
57 <snippet>Ay, ay, a scratch, a scratch; marry, 'tis enough.</snippet>
58 </mail-thread-info>
59 </mailbox>
60 </iq>
61 """)
63 iq = self.Iq(xml=xml)
64 mailbox = iq['mailbox']
65 self.failUnless(mailbox['result-time'] == '1118012394209', "result-time doesn't match")
66 self.failUnless(mailbox['url'] == 'http://mail.google.com/mail', "url doesn't match")
67 self.failUnless(mailbox['matched'] == '95', "total-matched incorrect")
68 self.failUnless(mailbox['estimate'] == False, "total-estimate incorrect")
69 self.failUnless(len(mailbox['threads']) == 1, "could not extract message threads")
71 thread = mailbox['threads'][0]
72 self.failUnless(thread['tid'] == '1172320964060972012', "thread tid doesn't match")
73 self.failUnless(thread['participation'] == '1', "thread participation incorrect")
74 self.failUnless(thread['messages'] == '28', "thread message count incorrect")
75 self.failUnless(thread['date'] == '1118012394209', "thread date doesn't match")
76 self.failUnless(thread['url'] == 'http://mail.google.com/mail?view=cv', "thread url doesn't match")
77 self.failUnless(thread['labels'] == 'act1scene3', "thread labels incorrect")
78 self.failUnless(thread['subject'] == 'Put thy rapier up.', "thread subject doesn't match")
79 self.failUnless(thread['snippet'] == "Ay, ay, a scratch, a scratch; marry, 'tis enough.", "snippet doesn't match")
80 self.failUnless(len(thread['senders']) == 3, "could not extract senders")
82 sender1 = thread['senders'][0]
83 self.failUnless(sender1['name'] == 'Me', "sender name doesn't match")
84 self.failUnless(sender1['address'] == 'romeo@gmail.com', "sender address doesn't match")
85 self.failUnless(sender1['originator'] == True, "sender originator incorrect")
86 self.failUnless(sender1['unread'] == False, "sender unread incorrectly True")
88 sender2 = thread['senders'][2]
89 self.failUnless(sender2['unread'] == True, "sender unread incorrectly False")
91 suite = unittest.TestLoader().loadTestsFromTestCase(TestGmail)