source: ralphm-patches/roster_item.patch @ 37:03cd0cb8548c

Last change on this file since 37:03cd0cb8548c was 36:f2953cadca90, checked in by Ralph Meijer <ralphm@…>, 13 years ago

Work on roster item representation.

File size: 6.0 KB
RevLine 
[36]1diff -r c9d1bf0b783d wokkel/test/test_xmppim.py
2--- a/wokkel/test/test_xmppim.py        Sun Jan 10 15:01:41 2010 +0100
3+++ b/wokkel/test/test_xmppim.py        Fri Feb 12 19:49:36 2010 +0100
4@@ -449,6 +449,96 @@
5 
6 
7 
8+class RosterItemTest(unittest.TestCase):
9+    """
10+    Tests for L{xmppim.RosterItem}.
11+    """
12+
13+    def test_toElement(self):
14+        item = xmppim.RosterItem(JID('user@example.org'))
15+        element = item.toElement()
16+        self.assertEquals('item', element.name)
17+        self.assertEquals(NS_ROSTER, element.uri)
18+        self.assertTrue(element.hasAttribute('subscription'))
19+        self.assertFalse(element.hasAttribute('ask'))
20+        self.assertFalse(element.hasAttribute('name'))
21+        self.assertEquals(0, len(list(element.elements())))
22+
23+
24+    def test_toElementJID(self):
25+        item = xmppim.RosterItem(JID('user@example.org'))
26+        element = item.toElement()
27+        self.assertEquals(u'user@example.org', element.getAttribute('jid'))
28+
29+
30+    def test_toElementSubscriptionNone(self):
31+        item = xmppim.RosterItem(JID('user@example.org'),
32+                                 subscriptionTo=False,
33+                                 subscriptionFrom=False)
34+        element = item.toElement()
35+        self.assertEquals('none', element.getAttribute('subscription'))
36+
37+
38+    def test_toElementSubscriptionTo(self):
39+        item = xmppim.RosterItem(JID('user@example.org'),
40+                                 subscriptionTo=True,
41+                                 subscriptionFrom=False)
42+        element = item.toElement()
43+        self.assertEquals('to', element.getAttribute('subscription'))
44+
45+
46+    def test_toElementSubscriptionFrom(self):
47+        item = xmppim.RosterItem(JID('user@example.org'),
48+                                 subscriptionTo=False,
49+                                 subscriptionFrom=True)
50+        element = item.toElement()
51+        self.assertEquals('from', element.getAttribute('subscription'))
52+
53+
54+    def test_toElementSubscriptionBoth(self):
55+        item = xmppim.RosterItem(JID('user@example.org'),
56+                                 subscriptionTo=True,
57+                                 subscriptionFrom=True)
58+        element = item.toElement()
59+        self.assertEquals('both', element.getAttribute('subscription'))
60+
61+
62+    def test_toElementRemove(self):
63+        item = xmppim.RosterItem(JID('user@example.org'),
64+                                 remove=True)
65+        element = item.toElement()
66+        self.assertEquals('remove', element.getAttribute('subscription'))
67+
68+
69+    def test_toElementAsk(self):
70+        item = xmppim.RosterItem(JID('user@example.org'),
71+                                 ask=True)
72+        element = item.toElement()
73+        self.assertEquals('subscribe', element.getAttribute('ask'))
74+
75+
76+    def test_toElementName(self):
77+        item = xmppim.RosterItem(JID('user@example.org'),
78+                                 name='Joe User')
79+        element = item.toElement()
80+        self.assertEquals(u'Joe User', element.getAttribute('name'))
81+
82+
83+    def test_toElementGroups(self):
84+        groups = set(['Friends', 'Jabber'])
85+        item = xmppim.RosterItem(JID('user@example.org'),
86+                                 groups=groups)
87+
88+        element = item.toElement()
89+        foundGroups = set()
90+        for child in element.elements():
91+            if child.uri == NS_ROSTER and child.name == 'group':
92+                foundGroups.add(unicode(child))
93+
94+        self.assertEqual(groups, foundGroups)
95+
96+
97+
98 class RosterClientProtocolTest(unittest.TestCase):
99     """
100     Tests for L{xmppim.RosterClientProtocol}.
101diff -r c9d1bf0b783d wokkel/xmppim.py
102--- a/wokkel/xmppim.py  Sun Jan 10 15:01:41 2010 +0100
103+++ b/wokkel/xmppim.py  Fri Feb 12 19:49:36 2010 +0100
104@@ -588,7 +588,7 @@
105     @ivar subscriptionFrom: Contact's subscription state. If C{True}, the
106                             contact is subscribed to the presence information
107                             of the roster owner.
108-    @type subscriptionTo: C{bool}
109+    @type subscriptionFrom: C{bool}
110     @ivar ask: Whether subscription is pending.
111     @type ask: C{bool}
112     @ivar groups: Set of groups this contact is categorized in. Groups are
113@@ -596,13 +596,44 @@
114     @type groups: C{set}
115     """
116 
117-    def __init__(self, jid):
118+    subscriptionStates = {(False, False): 'none',
119+                          (True, False): 'to',
120+                          (False, True): 'from',
121+                          (True, True): 'both'}
122+
123+    def __init__(self, jid, subscriptionTo=False, subscriptionFrom=False,
124+                       ask=None, name=False, groups=None, remove=False):
125         self.jid = jid
126-        self.name = None
127-        self.subscriptionTo = False
128-        self.subscriptionFrom = False
129-        self.ask = None
130-        self.groups = set()
131+        self.subscriptionTo = subscriptionTo
132+        self.subscriptionFrom = subscriptionFrom
133+        self.ask = ask
134+        self.name = name
135+        self.groups = groups or set()
136+        self.remove = remove
137+
138+
139+    def toElement(self):
140+        element = domish.Element((NS_ROSTER, 'item'))
141+        element['jid'] = self.jid.full()
142+
143+        if self.remove:
144+            subscription = 'remove'
145+        else:
146+            subscription = self.subscriptionStates[self.subscriptionTo,
147+                                                   self.subscriptionFrom]
148+        element['subscription'] = subscription
149+
150+        if self.ask:
151+            element['ask'] = 'subscribe'
152+        if self.name:
153+            element['name'] = self.name
154+
155+        if self.groups:
156+            for group in self.groups:
157+                element.addElement('group', content=group)
158+
159+        return element
160+
161 
162 
163 class RosterClientProtocol(XMPPHandler):
164@@ -662,9 +693,8 @@
165         """
166         iq = IQ(self.xmlstream, 'set')
167         iq.addElement((NS_ROSTER, 'query'))
168-        item = iq.query.addElement('item')
169-        item['jid'] = entity.full()
170-        item['subscription'] = 'remove'
171+        item = RosterItem(entity, remove=True)
172+        iq.query.addChild(item.toElement())
173         return iq.send()
174 
175 
Note: See TracBrowser for help on using the repository browser.