changeset 191:48245777acea

Return proper HTTP status codes on failed un-/subscription.
author Ralph Meijer <ralphm@ik.nu>
date Sat, 17 May 2008 18:31:31 +0000
parents 6e6c89eca9db
children a219fe70a762
files NEWS idavoll/gateway.py idavoll/test/test_gateway.py
diffstat 3 files changed, 39 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Sat May 17 18:30:39 2008 +0000
+++ b/NEWS	Sat May 17 18:31:31 2008 +0000
@@ -1,3 +1,9 @@
+x.x.x (yyyy-mm-dd)
+==================
+
+ - Return appropriate HTTP status codes on failed un-/subscription.
+
+
 0.7.2 (2008-05-16)
 ==================
 
--- a/idavoll/gateway.py	Sat May 17 18:30:39 2008 +0000
+++ b/idavoll/gateway.py	Sat May 17 18:31:31 2008 +0000
@@ -491,6 +491,12 @@
                          request to this resource.
     """
     serviceMethod = None
+    errorMap = {
+            error.NodeNotFound:
+                (responsecode.FORBIDDEN, "Node not found"),
+            error.NotSubscribed:
+                (responsecode.FORBIDDEN, "No such subscription found"),
+    }
 
     def __init__(self, service):
         self.service = service
@@ -502,9 +508,9 @@
 
     def http_POST(self, request):
         def trapNotFound(failure):
-            failure.trap(error.NodeNotFound)
-            return http.StatusResponse(responsecode.NOT_FOUND,
-                                       "Node not found")
+            err = failure.trap(*self.errorMap.keys())
+            code, msg = self.errorMap[err]
+            return http.StatusResponse(code, msg)
 
         def respond(result):
             return http.Response(responsecode.NO_CONTENT)
@@ -734,6 +740,18 @@
         return f.deferred
 
 
+    def unsubscribe(self, xmppURI):
+        params = {'uri': xmppURI,
+                  'callback': 'http://%s:%s/callback' % (self.callbackHost,
+                                                         self.callbackPort)}
+        f = getPageWithFactory(self._makeURI('unsubscribe'),
+                    method='POST',
+                    postdata=simplejson.dumps(params),
+                    headers={'Content-Type': MIME_JSON},
+                    agent=self.agent)
+        return f.deferred
+
+
     def items(self, xmppURI, maxItems=None):
         query = {'uri': xmppURI}
         if maxItems:
--- a/idavoll/test/test_gateway.py	Sat May 17 18:30:39 2008 +0000
+++ b/idavoll/test/test_gateway.py	Sat May 17 18:31:31 2008 +0000
@@ -24,9 +24,8 @@
 entry.addElement("author").addElement("name", content="John Doe")
 entry.addElement("content", content="Some text.")
 
-#baseURI = "http://pubsub-test.ik.nu/"
 baseURI = "http://localhost:8086/"
-componentJID = "test.ik.nu"
+componentJID = "pubsub.localhost"
 
 class GatewayTest(unittest.TestCase):
     timeout = 2
@@ -191,7 +190,7 @@
 
     def test_subscribeNonExisting(self):
         def cb(err):
-            self.assertEqual('404', err.status)
+            self.assertEqual('403', err.status)
 
         d = self.client.subscribe('xmpp:%s?node=test' % componentJID)
         self.assertFailure(d, error.Error)
@@ -199,6 +198,16 @@
         return d
 
 
+    def test_unsubscribeNonExisting(self):
+        def cb(err):
+            self.assertEqual('403', err.status)
+
+        d = self.client.unsubscribe('xmpp:%s?node=test' % componentJID)
+        self.assertFailure(d, error.Error)
+        d.addCallback(cb)
+        return d
+
+
     def test_items(self):
         def cb(response):
             xmppURI = response['uri']