public async Task RunPostTest(CancellationToken cancellationToken)
        {
            var instances = await instanceManagerClient.List(cancellationToken);

            var firstTest      = instances.Single(x => x.Name == TestInstanceName);
            var instanceClient = instanceManagerClient.CreateClient(firstTest);

            //can regain permissions on instance without instance user
            var ourInstanceUser = await instanceClient.Users.Read(cancellationToken).ConfigureAwait(false);

            await instanceClient.Users.Delete(ourInstanceUser, cancellationToken).ConfigureAwait(false);

            await Assert.ThrowsExceptionAsync <InsufficientPermissionsException>(() => instanceClient.Users.Read(cancellationToken)).ConfigureAwait(false);

            await instanceManagerClient.GrantPermissions(new Api.Models.Instance
            {
                Id = firstTest.Id
            }, cancellationToken).ConfigureAwait(false);

            ourInstanceUser = await instanceClient.Users.Read(cancellationToken).ConfigureAwait(false);

            Assert.AreEqual(RightsHelper.AllRights <DreamDaemonRights>(), ourInstanceUser.DreamDaemonRights.Value);

            //can't detach online instance
            await ApiAssert.ThrowsException <ConflictException>(() => instanceManagerClient.Detach(firstTest, cancellationToken), ErrorCode.InstanceDetachOnline).ConfigureAwait(false);

            firstTest.Online = false;
            firstTest        = await instanceManagerClient.Update(firstTest, cancellationToken).ConfigureAwait(false);

            await instanceManagerClient.Detach(firstTest, cancellationToken).ConfigureAwait(false);

            var attachPath = Path.Combine(firstTest.Path, InstanceController.InstanceAttachFileName);

            Assert.IsTrue(File.Exists(attachPath));

            //can recreate detached instance
            firstTest = await instanceManagerClient.CreateOrAttach(firstTest, cancellationToken).ConfigureAwait(false);

            // Test updating only with SetChatBotLimit works
            var current = await usersClient.Read(cancellationToken);

            var update = new UserUpdate
            {
                Id = current.Id,
                InstanceManagerRights = InstanceManagerRights.SetChatBotLimit
            };
            await usersClient.Update(update, cancellationToken);

            var update2 = new Api.Models.Instance
            {
                Id           = firstTest.Id,
                ChatBotLimit = 77
            };
            var newThing = await instanceManagerClient.Update(update2, cancellationToken);

            update.InstanceManagerRights |= InstanceManagerRights.Delete | InstanceManagerRights.Create | InstanceManagerRights.List;
            await usersClient.Update(update, cancellationToken);

            //but only if the attach file exists
            await instanceManagerClient.Detach(firstTest, cancellationToken).ConfigureAwait(false);

            File.Delete(attachPath);
            await ApiAssert.ThrowsException <ConflictException>(() => instanceManagerClient.CreateOrAttach(firstTest, cancellationToken), ErrorCode.InstanceAtExistingPath).ConfigureAwait(false);
        }
        public async Task Run(CancellationToken cancellationToken)
        {
            var firstTest = await CreateTestInstance(cancellationToken).ConfigureAwait(false);

            //instances always start offline
            Assert.AreEqual(false, firstTest.Online);
            //check it exists
            Assert.IsTrue(Directory.Exists(firstTest.Path));

            //cant create instances in existent directories
            var testNonEmpty = Path.Combine(testRootPath, Guid.NewGuid().ToString());

            Directory.CreateDirectory(testNonEmpty);
            await Assert.ThrowsExceptionAsync <ConflictException>(() => instanceManagerClient.CreateOrAttach(new Api.Models.Instance
            {
                Path = testNonEmpty,
                Name = "NonEmptyTest"
            }, cancellationToken)).ConfigureAwait(false);

            await Assert.ThrowsExceptionAsync <ConflictException>(() => instanceManagerClient.CreateOrAttach(firstTest, cancellationToken)).ConfigureAwait(false);

            //can't create instances in installation directory
            await Assert.ThrowsExceptionAsync <ConflictException>(() => instanceManagerClient.CreateOrAttach(new Api.Models.Instance
            {
                Path = "./A/Local/Path",
                Name = "NoInstallDirTest"
            }, cancellationToken)).ConfigureAwait(false);

            //can't move to existent directories
            await Assert.ThrowsExceptionAsync <ConflictException>(() => instanceManagerClient.Update(new Api.Models.Instance
            {
                Id   = firstTest.Id,
                Path = testNonEmpty
            }, cancellationToken)).ConfigureAwait(false);

            //test basic move
            Directory.Delete(testNonEmpty);
            var initialPath = firstTest.Path;

            firstTest = await instanceManagerClient.Update(new Api.Models.Instance
            {
                Id   = firstTest.Id,
                Path = testNonEmpty
            }, cancellationToken).ConfigureAwait(false);

            Assert.IsNotNull(firstTest.MoveJob);

            do
            {
                firstTest = await instanceManagerClient.GetId(firstTest, cancellationToken).ConfigureAwait(false);

                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false);
            } while (firstTest.MoveJob != null);


            //online it for real for component tests
            firstTest.Online            = true;
            firstTest.ConfigurationType = ConfigurationType.HostWrite;
            firstTest = await instanceManagerClient.Update(firstTest, cancellationToken).ConfigureAwait(false);

            Assert.AreEqual(true, firstTest.Online);
            Assert.AreEqual(ConfigurationType.HostWrite, firstTest.ConfigurationType);

            //can't move online instance
            await Assert.ThrowsExceptionAsync <ConflictException>(() => instanceManagerClient.Update(new Api.Models.Instance
            {
                Id   = firstTest.Id,
                Path = initialPath
            }, cancellationToken)).ConfigureAwait(false);

            var testSuite1 = new InstanceTest(instanceManagerClient.CreateClient(firstTest));
            await testSuite1.RunTests(cancellationToken).ConfigureAwait(false);

            //can regain permissions on instance without instance user
            var instanceClient  = instanceManagerClient.CreateClient(firstTest);
            var ourInstanceUser = await instanceClient.Users.Read(cancellationToken).ConfigureAwait(false);

            await instanceClient.Users.Delete(ourInstanceUser, cancellationToken).ConfigureAwait(false);

            await Assert.ThrowsExceptionAsync <InsufficientPermissionsException>(() => instanceClient.Users.Read(cancellationToken)).ConfigureAwait(false);

            await instanceManagerClient.Update(new Api.Models.Instance
            {
                Id = firstTest.Id
            }, cancellationToken).ConfigureAwait(false);

            ourInstanceUser = await instanceClient.Users.Read(cancellationToken).ConfigureAwait(false);

            //can't detach online instance
            await Assert.ThrowsExceptionAsync <ConflictException>(() => instanceManagerClient.Detach(firstTest, cancellationToken)).ConfigureAwait(false);

            firstTest.Online = false;
            firstTest        = await instanceManagerClient.Update(firstTest, cancellationToken).ConfigureAwait(false);

            await instanceManagerClient.Detach(firstTest, cancellationToken).ConfigureAwait(false);

            var instanceAttachFileName = (string)typeof(InstanceController).GetField("InstanceAttachFileName", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
            var attachPath             = Path.Combine(firstTest.Path, instanceAttachFileName);

            Assert.IsTrue(File.Exists(attachPath));

            //can recreate detached instance
            firstTest = await instanceManagerClient.CreateOrAttach(firstTest, cancellationToken).ConfigureAwait(false);

            //but only if the attach file exists
            await instanceManagerClient.Detach(firstTest, cancellationToken).ConfigureAwait(false);

            File.Delete(attachPath);
            await Assert.ThrowsExceptionAsync <ConflictException>(() => instanceManagerClient.CreateOrAttach(firstTest, cancellationToken)).ConfigureAwait(false);
        }