← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:fix-service-factory-tests into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:fix-service-factory-tests into launchpad:master.

Commit message:
Fix incorrect ServiceFactory tests

Two of the ServiceFactory tests were incorrect, and only coincidentally
passed.  They were testing that using getUserBrowser to traverse to
https://launchpad.test/api/devel/+services and
https://launchpad.test/api/devel/+services/invalid raised NotFound; but
this only worked because traversing to 'api' from getUserBrowser raises
NotFound (I think it doesn't set up quite the right kind of request),
and in fact https://launchpad.net/api/devel/+services works just fine.

Bring this more into line with test_service_traversal (which is correct)
by using test_traverse instead, and rewrite the test for traversing to
the service factory to assert its actual behaviour of exporting the
service factory (even though that isn't currently very useful).

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/375033
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:fix-service-factory-tests into launchpad:master.
diff --git a/lib/lp/app/tests/test_services.py b/lib/lp/app/tests/test_services.py
index a04c7bc..75061e7 100644
--- a/lib/lp/app/tests/test_services.py
+++ b/lib/lp/app/tests/test_services.py
@@ -1,13 +1,16 @@
-# Copyright 2012-2017 Canonical Ltd.  This software is licensed under the
+# Copyright 2012-2019 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
 
 """Tests for core services infrastructure."""
 
+import json
+
 from fixtures import FakeLogger
 from lazr.restful.interfaces._rest import IHTTPResource
+from six.moves.urllib.parse import urlparse
 from zope.component import getUtility
 from zope.interface import implementer
-from zope.publisher.interfaces import NotFound
+from zope.interface.interfaces import ComponentLookupError
 
 from lp.app.interfaces.services import (
     IService,
@@ -48,16 +51,18 @@ class TestServiceFactory(TestCaseWithFactory, FakeAdapterMixin):
         self.assertEqual(getUtility(IServiceFactory), context)
         self.assertEqual(fake_service, view)
 
-    def test_invalid_traversal(self):
-        # Test that traversal to +services without a service specified fails.
-        self.useFixture(FakeLogger())
-        self.assertRaises(
-            NotFound, self.getUserBrowser,
+    def test_service_factory_traversal(self):
+        # Test that traversal to the service factory works.
+        context, view, request = test_traverse(
             'https://launchpad.test/api/devel/+services')
+        self.assertEqual(getUtility(IServiceFactory), context)
+        self.assertEqual(
+            'service_factory',
+            urlparse(json.loads(view())['resource_type_link']).fragment)
 
     def test_invalid_service(self):
-        # Test that traversal an invalid service name fails.
+        # Test that traversal to an invalid service name fails.
         self.useFixture(FakeLogger())
         self.assertRaises(
-            NotFound, self.getUserBrowser,
+            ComponentLookupError, test_traverse,
             'https://launchpad.test/api/devel/+services/invalid')