def test_remove_role(self, mock_osclients): role = mock.MagicMock() ctx = roles.RoleGenerator(self.context) ctx.context["users"] = [{"id": "u1", "tenant_id": "t1"}, {"id": "u2", "tenant_id": "t2"}] ctx._remove_role(mock.MagicMock(), role) calls = [ mock.call("u1", role["id"], tenant="t1"), mock.call("u2", role["id"], tenant="t2"), ] mock_osclients.Clients().keystone()\ .roles.remove_user_role.assert_has_calls(calls)
def test_add_role(self, mock_osclients): fc = fakes.FakeClients() mock_osclients.Clients.return_value = fc self.create_default_roles_and_patch_add_remove_fuinctions(fc) ctx = roles.RoleGenerator(self.context) ctx.context["users"] = [{"id": "u1", "tenant_id": "t1"}, {"id": "u2", "tenant_id": "t2"}] result = ctx._add_role(mock.MagicMock(), self.context["config"]["roles"][0]) expected = {"id": "r1", "name": "test_role1"} self.assertEqual(expected, result)
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_fuinctions(fc) ctx = roles.RoleGenerator(self.context) ctx.context["users"] = [{"id": "u1", "tenant_id": "t1"}, {"id": "u2", "tenant_id": "t2"}] ex = self.assertRaises(Exception, ctx._add_role, mock.MagicMock(), "unknown_role") expected = "Role 'unknown_role' does not exist in the list of roles" self.assertEqual(expected, str(ex))
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) with roles.RoleGenerator(self.context) as ctx: ctx.context["users"] = [{ "id": "u1", "tenant_id": "t1" }, { "id": "u2", "tenant_id": "t2" }] ctx.setup() calls = [ mock.call("u1", "r1", tenant="t1"), mock.call("u2", "r1", tenant="t2"), mock.call("u1", "r2", tenant="t1"), mock.call("u2", "r2", tenant="t2") ] fc.keystone().roles.add_user_role.assert_has_calls(calls) 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 content 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("u1", "r1", tenant="t1"), mock.call("u2", "r1", tenant="t2"), mock.call("u1", "r2", tenant="t1"), mock.call("u2", "r2", tenant="t2") ] fc.keystone().roles.remove_user_role.assert_has_calls(calls)