summaryrefslogtreecommitdiff
path: root/tests/test_stanza_roster.py
blob: cd3e607cbb208ecbbc9f125af92defea52f5e770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from sleekxmpp.test import *
from sleekxmpp.stanza.roster import Roster


class TestRosterStanzas(SleekTest):

    def testAddItems(self):
        """Test adding items to a roster stanza."""
        iq = self.Iq()
        iq['roster'].setItems({
            'user@example.com': {
                'name': 'User',
                'subscription': 'both',
                'groups': ['Friends', 'Coworkers']},
            'otheruser@example.com': {
                'name': 'Other User',
                'subscription': 'both',
                'groups': []}})
        self.check(iq, """
          <iq>
            <query xmlns="jabber:iq:roster">
              <item jid="user@example.com" name="User" subscription="both">
                <group>Friends</group>
                <group>Coworkers</group>
              </item>
              <item jid="otheruser@example.com" name="Other User"
                    subscription="both" />
            </query>
          </iq>
        """)

    def testGetItems(self):
        """Test retrieving items from a roster stanza."""
        xml_string = """
          <iq>
            <query xmlns="jabber:iq:roster">
              <item jid="user@example.com" name="User" subscription="both">
                <group>Friends</group>
                <group>Coworkers</group>
              </item>
              <item jid="otheruser@example.com" name="Other User"
                    subscription="both" />
            </query>
          </iq>
        """
        iq = self.Iq(ET.fromstring(xml_string))
        expected = {
            'user@example.com': {
                'name': 'User',
                'subscription': 'both',
                'groups': ['Friends', 'Coworkers']},
            'otheruser@example.com': {
                'name': 'Other User',
                'subscription': 'both',
                'groups': []}}
        debug = "Roster items don't match after retrieval."
        debug += "\nReturned: %s" % str(iq['roster']['items'])
        debug += "\nExpected: %s" % str(expected)
        self.failUnless(iq['roster']['items'] == expected, debug)

    def testDelItems(self):
        """Test clearing items from a roster stanza."""
        xml_string = """
          <iq>
            <query xmlns="jabber:iq:roster">
              <item jid="user@example.com" name="User" subscription="both">
                <group>Friends</group>
                <group>Coworkers</group>
              </item>
              <item jid="otheruser@example.com" name="Other User"
                    subscription="both" />
            </query>
          </iq>
        """
        iq = self.Iq(ET.fromstring(xml_string))
        del iq['roster']['items']
        self.check(iq, """
          <iq>
            <query xmlns="jabber:iq:roster" />
          </iq>
        """)


suite = unittest.TestLoader().loadTestsFromTestCase(TestRosterStanzas)