def test_remove_role(self, mock_osclients): fc = fakes.FakeClients() mock_osclients.Clients.return_value = fc self.create_default_roles_and_patch_add_remove_functions(fc) ctx = roles.RoleGenerator(self.context) ctx.context["roles"] = {"r1": "test_role1", "r2": "test_role2"} ctx.context["users"] = [{ "id": "u1", "tenant_id": "t1", "assigned_roles": ["r1", "r2"] }, { "id": "u2", "tenant_id": "t2", "assigned_roles": ["r1", "r2"] }] ctx.credential = mock.MagicMock() ctx.cleanup() calls = [ mock.call(user="******", role="r1", tenant="t1"), mock.call(user="******", role="r1", tenant="t2"), mock.call(user="******", role="r2", tenant="t1"), mock.call(user="******", role="r2", tenant="t2") ] fc.keystone().roles.remove_user_role.assert_has_calls(calls, any_order=True)
def wrapper(*args, **kw): func_type = inspect.getcallargs(func, *args, **kw) config = func_type.get("config", {}) context = func_type.get("context", {}) if config.get("contexts", {}).get("roles") \ and context.get("admin", {}): context["config"] = config["contexts"] rolegenerator = roles.RoleGenerator(context) with rolegenerator: rolegenerator.setup() func(*args, **kw) else: func(*args, **kw)
def test_setup_and_cleanup(self, mock_osclients): fc = fakes.FakeClients() mock_osclients.Clients.return_value = fc self.create_default_roles_and_patch_add_remove_functions(fc) def _get_user_role_ids_side_effect(user_id, project_id): return ["r1", "r2"] if user_id == "u3" else [] with roles.RoleGenerator(self.context) as ctx: ctx.context["users"] = [{ "id": "u1", "tenant_id": "t1" }, { "id": "u2", "tenant_id": "t2" }, { "id": "u3", "tenant_id": "t3" }] ctx._get_user_role_ids = mock.MagicMock() ctx._get_user_role_ids.side_effect = _get_user_role_ids_side_effect ctx.setup() ctx.credential = mock.MagicMock() calls = [ mock.call(user="******", role="r1", tenant="t1"), mock.call(user="******", role="r1", tenant="t2"), mock.call(user="******", role="r2", tenant="t1"), mock.call(user="******", role="r2", tenant="t2"), ] fc.keystone().roles.add_user_role.assert_has_calls(calls, any_order=True) self.assertEqual(4, fc.keystone().roles.add_user_role.call_count) self.assertEqual(0, fc.keystone().roles.remove_user_role.call_count) self.assertEqual(2, len(ctx.context["roles"])) self.assertEqual(2, len(fc.keystone().roles.list())) # Cleanup (called by context manager) self.assertEqual(2, len(fc.keystone().roles.list())) self.assertEqual(4, fc.keystone().roles.add_user_role.call_count) self.assertEqual(4, fc.keystone().roles.remove_user_role.call_count) calls = [ mock.call(user="******", role="r1", tenant="t1"), mock.call(user="******", role="r1", tenant="t2"), mock.call(user="******", role="r2", tenant="t1"), mock.call(user="******", role="r2", tenant="t2") ] fc.keystone().roles.remove_user_role.assert_has_calls(calls, any_order=True)
def test_add_role(self, mock_osclients): fc = fakes.FakeClients() mock_osclients.Clients.return_value = fc self.create_default_roles_and_patch_add_remove_functions(fc) ctx = roles.RoleGenerator(self.context) ctx.context["users"] = [{ "id": "u1", "tenant_id": "t1" }, { "id": "u2", "tenant_id": "t2" }] ctx.credential = mock.MagicMock() ctx.setup() expected = {"r1": "test_role1", "r2": "test_role2"} self.assertEqual(expected, ctx.context["roles"])
def test_add_role_which_does_not_exist(self, mock_osclients): fc = fakes.FakeClients() mock_osclients.Clients.return_value = fc self.create_default_roles_and_patch_add_remove_functions(fc) ctx = roles.RoleGenerator(self.context) ctx.context["users"] = [{ "id": "u1", "tenant_id": "t1" }, { "id": "u2", "tenant_id": "t2" }] ctx.config = ["unknown_role"] ctx.credential = mock.MagicMock() ex = self.assertRaises(exceptions.NotFoundException, ctx._get_role_object, "unknown_role") expected = ("The resource can not be found: There is no role " "with name `unknown_role`") self.assertEqual(expected, str(ex))