]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Mock oslo policy HTTPCheck instead of urllib
authorKevin Benton <blak111@gmail.com>
Wed, 7 Oct 2015 02:28:47 +0000 (19:28 -0700)
committerKevin Benton <blak111@gmail.com>
Wed, 7 Oct 2015 04:25:57 +0000 (21:25 -0700)
We were mocking internal behavior of oslo policy by
patching urllib. This will break with the upcoming oslo
release that switches to requests.

This patch changes the mock to the HTTPCheck level and we
can leave implementation details testing up to oslo_policy.

Change-Id: I07957f01307e25f1547197c720eea6e3e7f0ef5a
Closes-Bug: #1503890

neutron/tests/unit/test_policy.py

index ed230179fdf4c32e83b869efb40d07a8cab97720..bed9740c9252281c460fa628f230ff9f9f2157c2 100644 (file)
@@ -19,8 +19,6 @@ import mock
 from oslo_policy import policy as oslo_policy
 from oslo_serialization import jsonutils
 from oslo_utils import importutils
-import six
-import six.moves.urllib.request as urlrequest
 
 import neutron
 from neutron.api.v2 import attributes
@@ -105,25 +103,24 @@ class PolicyTestCase(base.BaseTestCase):
         result = policy.enforce(self.context, action, self.target)
         self.assertEqual(result, True)
 
-    @mock.patch.object(urlrequest, 'urlopen',
-                       return_value=six.StringIO("True"))
-    def test_enforce_http_true(self, mock_urlrequest):
+    #TODO(kevinbenton): replace these private method mocks with a fixture
+    @mock.patch.object(oslo_policy._checks.HttpCheck, '__call__',
+                       return_value=True)
+    def test_enforce_http_true(self, mock_httpcheck):
         action = "example:get_http"
         target = {}
         result = policy.enforce(self.context, action, target)
         self.assertEqual(result, True)
 
-    def test_enforce_http_false(self):
-
-        def fakeurlopen(url, post_data):
-            return six.StringIO("False")
-
-        with mock.patch.object(urlrequest, 'urlopen', new=fakeurlopen):
-            action = "example:get_http"
-            target = {}
-            self.assertRaises(oslo_policy.PolicyNotAuthorized,
-                              policy.enforce, self.context,
-                              action, target)
+    #TODO(kevinbenton): replace these private method mocks with a fixture
+    @mock.patch.object(oslo_policy._checks.HttpCheck, '__call__',
+                       return_value=False)
+    def test_enforce_http_false(self, mock_httpcheck):
+        action = "example:get_http"
+        target = {}
+        self.assertRaises(oslo_policy.PolicyNotAuthorized,
+                          policy.enforce, self.context,
+                          action, target)
 
     def test_templatized_enforcement(self):
         target_mine = {'tenant_id': 'fake'}