Commits

Alexander Shorin committed 39bf96b

Fix presence routing for SleekXMPP - I've forgot to define XMPP_JID globaly.

Comments (0)

Files changed (2)

tests/lib/test_xmppflask.py

         environ = {'XMPP_JID': 'k.bx@ya.ru'}
 
         environ['MESSAGE'] = 'ping'
-        self.assertRaises(TestException,
-                          lambda: app(environ))
+        self.assertRaises(TestException, lambda: app(environ))
+
+    def test_handle_presences(self):
+        app = xmppflask.XmppFlask(__name__)
+
+        @app.route_presence()
+        def presence():
+            return 'got it'
+
+        environ = {'XMPP_JID': 'k.bx@ya.ru', 'XMPP_EVENT': 'presence'}
+
+        rv = app(environ)
+        self.assertEquals(u''.join(rv), 'got it')
+
+    def test_handle_presences_from_specific_domain(self):
+        app = xmppflask.XmppFlask(__name__)
+
+        @app.route_presence(from_jid='.*@ya.ru', type='available')
+        def presence():
+            return 'welcome back, %s!' % environ['XMPP_JID']
+
+        environ = {'XMPP_JID': 'k.bx@ya.ru',
+                   'XMPP_EVENT': 'presence',
+                   'type': 'available'}
+
+        rv = app(environ)
+        self.assertEquals(u''.join(rv),
+                          'welcome back, %s!' % environ['XMPP_JID'])

xmppflask/server.py

         environ['to'] = maybe_unicode(event['to'])
         environ['type'] = maybe_unicode(event['type'])
 
+        jid = JID(
+            event['from'].user,
+            event['from'].domain,
+            event['from'].resource
+        )
+        environ['XMPP_JID'] = u'%s@%s' % (jid.user, jid.domain)
+
         if isinstance(event, self.message_class):
-            jid = JID(
-                event['from'].user,
-                event['from'].domain,
-                event['from'].resource
-            )
-            environ['XMPP_JID'] = u'%s@%s' % (jid.user, jid.domain)
             environ['body'] = environ['MESSAGE'] = maybe_unicode(event['body'])
             environ['subject'] = maybe_unicode(event['subject'])
             environ['mucroom'] = maybe_unicode(event['mucroom'])