DavidPurcell | 029d8c3 | 2017-01-06 15:27:41 -0500 | [diff] [blame] | 1 | # Copyright 2017 AT&T Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import mock |
| 16 | |
| 17 | from patrole_tempest_plugin import rbac_exceptions |
| 18 | from patrole_tempest_plugin import rbac_rule_validation as rbac_rv |
| 19 | |
| 20 | from tempest.lib import exceptions |
| 21 | |
| 22 | from tempest.tests import base |
| 23 | |
| 24 | |
| 25 | class RBACRuleValidationTest(base.TestCase): |
| 26 | @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority') |
| 27 | def test_RBAC_rv_happy_path(self, mock_auth): |
| 28 | decorator = rbac_rv.action("", "", "") |
| 29 | mock_function = mock.Mock() |
| 30 | wrapper = decorator(mock_function) |
| 31 | wrapper() |
| 32 | self.assertTrue(mock_function.called) |
| 33 | |
| 34 | @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority') |
| 35 | def test_RBAC_rv_forbidden(self, mock_auth): |
| 36 | decorator = rbac_rv.action("", "", "") |
| 37 | mock_function = mock.Mock() |
| 38 | mock_function.side_effect = exceptions.Forbidden |
| 39 | wrapper = decorator(mock_function) |
| 40 | self.assertRaises(exceptions.Forbidden, wrapper) |
| 41 | |
| 42 | @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority') |
| 43 | def test_RBAC_rv_rbac_action_failed(self, mock_auth): |
| 44 | decorator = rbac_rv.action("", "", "") |
| 45 | mock_function = mock.Mock() |
| 46 | mock_function.side_effect = rbac_exceptions.RbacActionFailed |
| 47 | wrapper = decorator(mock_function) |
| 48 | self.assertRaises(exceptions.Forbidden, wrapper) |
| 49 | |
| 50 | @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority') |
| 51 | def test_RBAC_rv_not_allowed(self, mock_auth): |
| 52 | decorator = rbac_rv.action("", "", "") |
| 53 | |
| 54 | mock_function = mock.Mock() |
| 55 | wrapper = decorator(mock_function) |
| 56 | |
| 57 | mock_permission = mock.Mock() |
| 58 | mock_permission.get_permission.return_value = False |
| 59 | mock_auth.return_value = mock_permission |
| 60 | |
| 61 | self.assertRaises(rbac_exceptions.RbacOverPermission, wrapper) |
| 62 | |
| 63 | @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority') |
| 64 | def test_RBAC_rv_forbidden_not_allowed(self, mock_auth): |
| 65 | decorator = rbac_rv.action("", "", "") |
| 66 | |
| 67 | mock_function = mock.Mock() |
| 68 | mock_function.side_effect = exceptions.Forbidden |
| 69 | wrapper = decorator(mock_function) |
| 70 | |
| 71 | mock_permission = mock.Mock() |
| 72 | mock_permission.get_permission.return_value = False |
| 73 | mock_auth.return_value = mock_permission |
| 74 | |
| 75 | self.assertIsNone(wrapper()) |
| 76 | |
| 77 | @mock.patch('patrole_tempest_plugin.rbac_auth.RbacAuthority') |
| 78 | def test_RBAC_rv_rbac_action_failed_not_allowed(self, mock_auth): |
| 79 | decorator = rbac_rv.action("", "", "") |
| 80 | |
| 81 | mock_function = mock.Mock() |
| 82 | mock_function.side_effect = rbac_exceptions.RbacActionFailed |
| 83 | wrapper = decorator(mock_function) |
| 84 | |
| 85 | mock_permission = mock.Mock() |
| 86 | mock_permission.get_permission.return_value = False |
| 87 | mock_auth.return_value = mock_permission |
| 88 | |
| 89 | self.assertIsNone(wrapper()) |