| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 |
# you may not use this file except in compliance with the License. |
| 3 |
# You may obtain a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 10 |
# implied. |
| 11 |
# See the License for the specific language governing permissions and |
| 12 |
# limitations under the License. |
| 13 |
|
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
from neutron_lib import context |
| 17 |
from neutron_lib.policy import _engine as policy_engine |
| 18 |
|
| 19 |
from neutron_lib.tests import _base as base |
| 20 |
|
| 21 |
|
| 22 |
class TestPolicyEnforcer(base.BaseTestCase): |
| 23 |
def setUp(self): |
| 24 |
super().setUp() |
| 25 |
# Isolate one _ROLE_ENFORCER per test case |
| 26 |
mock.patch.object(policy_engine, '_ROLE_ENFORCER', None).start() |
| 27 |
|
| 28 |
def test_init_reset(self): |
| 29 |
self.assertIsNone(policy_engine._ROLE_ENFORCER) |
| 30 |
policy_engine.init() |
| 31 |
self.assertIsNotNone(policy_engine._ROLE_ENFORCER) |
| 32 |
|
| 33 |
def test_check_user_is_not_admin(self): |
| 34 |
ctx = context.Context('me', 'my_project') |
| 35 |
self.assertFalse(policy_engine.check_is_admin(ctx)) |
| 36 |
|
| 37 |
def test_check_user_elevated_is_admin(self): |
| 38 |
ctx = context.Context('me', 'my_project', roles=['user']).elevated() |
| 39 |
self.assertTrue(policy_engine.check_is_admin(ctx)) |
| 40 |
|
| 41 |
def test_check_is_admin_no_roles_no_admin(self): |
| 42 |
policy_engine.init(policy_file='dummy_policy.yaml') |
| 43 |
ctx = context.Context('me', 'my_project', roles=['user']).elevated() |
| 44 |
# With no admin role, elevated() should not work. |
| 45 |
self.assertFalse(policy_engine.check_is_admin(ctx)) |
| 46 |
|
| 47 |
def test_check_user_elevated_is_admin_with_default_policy(self): |
| 48 |
policy_engine.init(policy_file='no_policy.yaml') |
| 49 |
ctx = context.Context('me', 'my_project', roles=['user']).elevated() |
| 50 |
self.assertTrue(policy_engine.check_is_admin(ctx)) |
| 51 |
|
| 52 |
def test_check_is_advsvc_role(self): |
| 53 |
ctx = context.Context('me', 'my_project', roles=['advsvc']) |
| 54 |
self.assertTrue(policy_engine.check_is_advsvc(ctx)) |
| 55 |
|
| 56 |
def test_check_is_not_advsvc_user(self): |
| 57 |
ctx = context.Context('me', 'my_project', roles=['user']) |
| 58 |
self.assertFalse(policy_engine.check_is_advsvc(ctx)) |
| 59 |
|
| 60 |
def test_check_is_not_advsvc_admin(self): |
| 61 |
ctx = context.Context('me', 'my_project').elevated() |
| 62 |
self.assertTrue(policy_engine.check_is_admin(ctx)) |
| 63 |
self.assertFalse(policy_engine.check_is_advsvc(ctx)) |
| 64 |
|
| 65 |
def test_check_is_advsvc_no_roles_no_advsvc(self): |
| 66 |
policy_engine.init(policy_file='dummy_policy.yaml') |
| 67 |
ctx = context.Context('me', 'my_project', roles=['advsvc']) |
| 68 |
# No advsvc role in the policy file, so cannot assume the role. |
| 69 |
self.assertFalse(policy_engine.check_is_advsvc(ctx)) |
| 70 |
|
| 71 |
def test_check_is_advsvc_role_with_default_policy(self): |
| 72 |
policy_engine.init(policy_file='no_policy.yaml') |
| 73 |
ctx = context.Context('me', 'my_project', roles=['advsvc']) |
| 74 |
self.assertTrue(policy_engine.check_is_advsvc(ctx)) |
| 75 |
|
| 76 |
def test_check_is_service_role(self): |
| 77 |
ctx = context.Context('me', 'my_project', roles=['service']) |
| 78 |
self.assertTrue(policy_engine.check_is_service_role(ctx)) |
| 79 |
|
| 80 |
def test_check_is_not_service_role_user(self): |
| 81 |
ctx = context.Context('me', 'my_project', roles=['member']) |
| 82 |
self.assertFalse(policy_engine.check_is_service_role(ctx)) |
| 83 |
|
| 84 |
def test_check_is_not_service_role_admin(self): |
| 85 |
ctx = context.Context('me', 'my_project').elevated() |
| 86 |
self.assertTrue(policy_engine.check_is_admin(ctx)) |
| 87 |
self.assertFalse(policy_engine.check_is_service_role(ctx)) |
| 88 |
|
| 89 |
def test_check_is_service_role_no_roles_no_service_role(self): |
| 90 |
policy_engine.init(policy_file='dummy_policy.yaml') |
| 91 |
ctx = context.Context('me', 'my_project', roles=['service']) |
| 92 |
# No service role in the policy file, so cannot assume the role. |
| 93 |
self.assertFalse(policy_engine.check_is_service_role(ctx)) |
| 94 |
|
| 95 |
def test_check_is_service_role_with_default_policy(self): |
| 96 |
policy_engine.init(policy_file='no_policy.yaml') |
| 97 |
ctx = context.Context('me', 'my_project', roles=['service']) |
| 98 |
self.assertTrue(policy_engine.check_is_service_role(ctx)) |