summaryrefslogtreecommitdiff
path: root/tests/test_stanza_roster.py
blob: 762b8c4c12777527c88e6136a271def57f618de9 (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
85
86
87
88
89
import unittest
from slixmpp.test import SlixTest
from slixmpp.xmlstream import ET


class TestRosterStanzas(SlixTest):

    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',
                'ask': '',
                'approved': '',
                'groups': ['Friends', 'Coworkers']},
            'otheruser@example.com': {
                'name': 'Other User',
                'subscription': 'both',
                'ask': '',
                'approved': '',
                '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)