async Task TestLogs(CancellationToken cancellationToken) { var logs = await client.ListLogs(null, cancellationToken); Assert.AreNotEqual(0, logs.Count); var logFile = logs.First(); Assert.IsNotNull(logFile); Assert.IsFalse(String.IsNullOrWhiteSpace(logFile.Name)); Assert.IsNull(logFile.FileTicket); var downloadedTuple = await client.GetLog(logFile, cancellationToken); Assert.AreEqual(logFile.Name, downloadedTuple.Item1.Name); Assert.IsTrue(logFile.LastModified <= downloadedTuple.Item1.LastModified); Assert.IsNull(logFile.FileTicket); await ApiAssert.ThrowsException <ConflictException>(() => client.GetLog(new LogFileResponse { Name = "very_fake_path.log" }, cancellationToken), ErrorCode.IOError); await Assert.ThrowsExceptionAsync <InsufficientPermissionsException>(() => client.GetLog(new LogFileResponse { Name = "../out_of_bounds.file" }, cancellationToken)); }
async Task BasicTests(CancellationToken cancellationToken) { var user = await this.client.Read(cancellationToken).ConfigureAwait(false); Assert.IsNotNull(user); Assert.AreEqual("Admin", user.Name); Assert.IsNull(user.SystemIdentifier); Assert.AreEqual(true, user.Enabled); var systemUser = user.CreatedBy; Assert.IsNotNull(systemUser); Assert.AreEqual("TGS", systemUser.Name); Assert.AreEqual(false, systemUser.Enabled); var users = await client.List(cancellationToken); Assert.IsTrue(users.Count > 0); Assert.IsFalse(users.Any(x => x.Id == systemUser.Id)); await ApiAssert.ThrowsException <InsufficientPermissionsException>(() => client.GetId(systemUser, cancellationToken), null); await ApiAssert.ThrowsException <InsufficientPermissionsException>(() => client.Update(new UserUpdate { Id = systemUser.Id }, cancellationToken), null); }
async Task TestPagination(CancellationToken cancellationToken) { // we test pagination here b/c it's the only spot we have a decent amount of entities var nullSettings = await serverClient.Users.List(null, cancellationToken); var emptySettings = await serverClient.Users.List( new PaginationSettings { }, cancellationToken); Assert.AreEqual(nullSettings.Count, emptySettings.Count); Assert.IsTrue(nullSettings.All(x => emptySettings.SingleOrDefault(y => x.Id == y.Id) != null)); await ApiAssert.ThrowsException <ApiConflictException>(() => serverClient.Users.List( new PaginationSettings { PageSize = -2143 }, cancellationToken), ErrorCode.ApiInvalidPageOrPageSize); await ApiAssert.ThrowsException <ApiConflictException>(() => serverClient.Users.List( new PaginationSettings { PageSize = Int32.MaxValue }, cancellationToken), ErrorCode.ApiPageTooLarge); await serverClient.Users.List( new PaginationSettings { PageSize = 50 }, cancellationToken); var skipped = await serverClient.Users.List(new PaginationSettings { Offset = 50, RetrieveCount = 5 }, cancellationToken); Assert.AreEqual(5, skipped.Count); var allAfterSkipped = await serverClient.Users.List(new PaginationSettings { Offset = 50, }, cancellationToken); Assert.IsTrue(5 < allAfterSkipped.Count); var limited = await serverClient.Users.List(new PaginationSettings { RetrieveCount = 12, }, cancellationToken); Assert.AreEqual(12, limited.Count); }
async Task TestCreateSysUser(CancellationToken cancellationToken) { var sysId = Environment.UserName; var update = new UserUpdate { SystemIdentifier = sysId }; if (new PlatformIdentifier().IsWindows) { await client.Create(update, cancellationToken); } else { await ApiAssert.ThrowsException <MethodNotSupportedException>(() => client.Create(update, cancellationToken), ErrorCode.RequiresPosixSystemIdentity); } }
public async Task <Api.Models.Instance> RunPreInstanceTest(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)); var firstClient = instanceManagerClient.CreateClient(firstTest); await ApiAssert.ThrowsException <ConflictException>(() => firstClient.DreamDaemon.Start(cancellationToken), ErrorCode.InstanceOffline); //cant create instances in existent directories var testNonEmpty = Path.Combine(testRootPath, Guid.NewGuid().ToString()); Directory.CreateDirectory(testNonEmpty); var testFile = Path.Combine(testNonEmpty, "asdf"); await File.WriteAllBytesAsync(testFile, Array.Empty <byte>(), cancellationToken).ConfigureAwait(false); await ApiAssert.ThrowsException <ConflictException>(() => instanceManagerClient.CreateOrAttach(new Api.Models.Instance { Path = testNonEmpty, Name = "NonEmptyTest" }, cancellationToken), ErrorCode.InstanceAtExistingPath).ConfigureAwait(false); //check it works for truly empty directories File.Delete(testFile); var secondTry = await instanceManagerClient.CreateOrAttach(new Api.Models.Instance { Path = Path.Combine(testRootPath, Guid.NewGuid().ToString()), Name = "NonEmptyTest" }, cancellationToken).ConfigureAwait(false); await Assert.ThrowsExceptionAsync <ConflictException>(() => instanceManagerClient.CreateOrAttach(firstTest, cancellationToken)).ConfigureAwait(false); Assert.IsTrue(Directory.Exists(firstTest.Path)); //can't create instances in installation directory await ApiAssert.ThrowsException <ConflictException>(() => instanceManagerClient.CreateOrAttach(new Api.Models.Instance { Path = "./A/Local/Path", Name = "NoInstallDirTest" }, cancellationToken), ErrorCode.InstanceAtConflictingPath).ConfigureAwait(false); //can't create instances as children of other instances await ApiAssert.ThrowsException <ConflictException>(() => instanceManagerClient.CreateOrAttach(new Api.Models.Instance { Path = Path.Combine(firstTest.Path, "subdir"), Name = "NoOtherInstanceDirTest" }, cancellationToken), ErrorCode.InstanceAtConflictingPath).ConfigureAwait(false); Assert.IsTrue(Directory.Exists(firstTest.Path)); //can't move to existent directories await ApiAssert.ThrowsException <ConflictException>(() => instanceManagerClient.Update(new Api.Models.Instance { Id = firstTest.Id, Path = testNonEmpty }, cancellationToken), ErrorCode.InstanceAtExistingPath).ConfigureAwait(false); // test can't create instance outside of whitelist await ApiAssert.ThrowsException <ApiConflictException>(() => instanceManagerClient.CreateOrAttach(new Api.Models.Instance { Name = "TestInstanceOutsideOfWhitelist", Path = Path.Combine(testRootPath, "..", Guid.NewGuid().ToString()), Online = true, ChatBotLimit = 1 }, cancellationToken), ErrorCode.InstanceNotAtWhitelistedPath); //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); Assert.IsTrue(Directory.Exists(firstTest.Path)); //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); Assert.IsTrue(Directory.Exists(firstTest.Path)); //can't move online instance await ApiAssert.ThrowsException <ConflictException>(() => instanceManagerClient.Update(new Api.Models.Instance { Id = firstTest.Id, Path = initialPath }, cancellationToken), ErrorCode.InstanceRelocateOnline).ConfigureAwait(false); Assert.IsTrue(Directory.Exists(firstTest.Path)); return(firstTest); }
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); }
async Task BasicTests(CancellationToken cancellationToken) { var user = await serverClient.Users.Read(cancellationToken).ConfigureAwait(false); Assert.IsNotNull(user); Assert.AreEqual("Admin", user.Name); Assert.IsNull(user.SystemIdentifier); Assert.AreEqual(true, user.Enabled); Assert.IsNotNull(user.OAuthConnections); Assert.IsNotNull(user.PermissionSet); Assert.IsNotNull(user.PermissionSet.Id); Assert.IsNotNull(user.PermissionSet.InstanceManagerRights); Assert.IsNotNull(user.PermissionSet.AdministrationRights); var systemUser = user.CreatedBy; Assert.IsNotNull(systemUser); Assert.AreEqual("TGS", systemUser.Name); var users = await serverClient.Users.List(null, cancellationToken); Assert.IsTrue(users.Count > 0); Assert.IsFalse(users.Any(x => x.Id == systemUser.Id)); await ApiAssert.ThrowsException <InsufficientPermissionsException>(() => serverClient.Users.GetId(systemUser, cancellationToken), null); await ApiAssert.ThrowsException <InsufficientPermissionsException>(() => serverClient.Users.Update(new UserUpdateRequest { Id = systemUser.Id }, cancellationToken), null); var sampleOAuthConnections = new List <OAuthConnection> { new OAuthConnection { ExternalUserId = "asdfasdf", Provider = OAuthProvider.Discord } }; await ApiAssert.ThrowsException <ApiConflictException>(() => serverClient.Users.Update(new UserUpdateRequest { Id = user.Id, OAuthConnections = sampleOAuthConnections }, cancellationToken), ErrorCode.AdminUserCannotOAuth); var testUser = await serverClient.Users.Create( new UserCreateRequest { Name = $"BasicTestUser", Password = "******" }, cancellationToken).ConfigureAwait(false); Assert.IsNotNull(testUser.OAuthConnections); testUser = await serverClient.Users.Update( new UserUpdateRequest { Id = testUser.Id, OAuthConnections = sampleOAuthConnections }, cancellationToken).ConfigureAwait(false); Assert.AreEqual(1, testUser.OAuthConnections.Count); Assert.AreEqual(sampleOAuthConnections.First().ExternalUserId, testUser.OAuthConnections.First().ExternalUserId); Assert.AreEqual(sampleOAuthConnections.First().Provider, testUser.OAuthConnections.First().Provider); var group = await serverClient.Groups.Create( new UserGroupCreateRequest { Name = "TestGroup" }, cancellationToken); Assert.AreEqual(group.Name, "TestGroup"); Assert.IsNotNull(group.PermissionSet); Assert.IsNotNull(group.PermissionSet.Id); Assert.AreEqual(AdministrationRights.None, group.PermissionSet.AdministrationRights); Assert.AreEqual(InstanceManagerRights.None, group.PermissionSet.InstanceManagerRights); var group2 = await serverClient.Groups.Create(new UserGroupCreateRequest { Name = "TestGroup2", PermissionSet = new PermissionSet { InstanceManagerRights = InstanceManagerRights.List } }, cancellationToken); Assert.AreEqual(AdministrationRights.None, group2.PermissionSet.AdministrationRights); Assert.AreEqual(InstanceManagerRights.List, group2.PermissionSet.InstanceManagerRights); var groups = await serverClient.Groups.List(null, cancellationToken); Assert.AreEqual(2, groups.Count); foreach (var igroup in groups) { Assert.IsNotNull(igroup.Users); Assert.IsNotNull(igroup.PermissionSet); } await serverClient.Groups.Delete(group2, cancellationToken); groups = await serverClient.Groups.List(null, cancellationToken); Assert.AreEqual(1, groups.Count); group = await serverClient.Groups.Update(new UserGroupUpdateRequest { Id = groups.First().Id, PermissionSet = new PermissionSet { InstanceManagerRights = RightsHelper.AllRights <InstanceManagerRights>(), AdministrationRights = RightsHelper.AllRights <AdministrationRights>(), } }, cancellationToken); Assert.AreEqual(RightsHelper.AllRights <AdministrationRights>(), group.PermissionSet.AdministrationRights); Assert.AreEqual(RightsHelper.AllRights <InstanceManagerRights>(), group.PermissionSet.InstanceManagerRights); UserUpdateRequest testUserUpdate = new UserCreateRequest { Name = "TestUserWithNoPassword", Password = String.Empty }; await ApiAssert.ThrowsException <ApiConflictException>(() => serverClient.Users.Create((UserCreateRequest)testUserUpdate, cancellationToken), ErrorCode.UserPasswordLength); testUserUpdate.OAuthConnections = new List <OAuthConnection> { new OAuthConnection { ExternalUserId = "asdf", Provider = OAuthProvider.GitHub } }; var testUser2 = await serverClient.Users.Create((UserCreateRequest)testUserUpdate, cancellationToken); testUserUpdate = new UserUpdateRequest { Id = testUser2.Id, PermissionSet = testUser2.PermissionSet, Group = new Api.Models.Internal.UserGroup { Id = group.Id }, }; await ApiAssert.ThrowsException <ApiConflictException>( () => serverClient.Users.Update( testUserUpdate, cancellationToken), ErrorCode.UserGroupAndPermissionSet); testUserUpdate.PermissionSet = null; testUser2 = await serverClient.Users.Update(testUserUpdate, cancellationToken); Assert.IsNull(testUser2.PermissionSet); Assert.IsNotNull(testUser2.Group); Assert.AreEqual(group.Id, testUser2.Group.Id); group = await serverClient.Groups.GetId(group, cancellationToken); Assert.IsNotNull(group.Users); Assert.AreEqual(1, group.Users.Count); Assert.AreEqual(testUser2.Id, group.Users.First().Id); Assert.IsNotNull(group.PermissionSet); }