protected virtual void Dispose(bool disposing) { if (!this.isDisposed) { if (disposing) { StaticLogger.Instance.Info($"IT run {this.testData.Nonce} finished. Cleaning up..."); this.RemoveObjectsFromTenantAsync() .GetAwaiter().GetResult(); StaticLogger.Instance.Info($"Done cleaning up objects."); StaticLogger.Instance.Info("Caching statistics:"); StaticLogger.Instance.Info(TestClients.GetSAuthc1Client().GetCacheProvider().ToString()); var filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "its.log"); StaticLogger.Instance.Info($"Saving log to file {filename}"); System.IO.File.WriteAllText(filename, StaticLogger.GetLog()); } this.isDisposed = true; } }
private async Task RemoveObjectsFromTenantAsync() { var client = TestClients.GetSAuthc1Client(); var results = new ConcurrentDictionary <string, Exception>(); // Delete applications var deleteApplicationTasks = this.CreatedApplicationHrefs.Select(async href => { try { var application = await client.GetResourceAsync <IApplication>(href); var deleteResult = await application.DeleteAsync(); results.TryAdd(href, null); } catch (ResourceException rex) { if (rex.Code == 404) { // Already deleted results.TryAdd(href, null); } } catch (Exception e) { results.TryAdd(href, e); } }); // Delete directories var deleteDirectoryTasks = this.CreatedDirectoryHrefs.Select(async href => { try { var directory = await client.GetResourceAsync <IDirectory>(href); var deleteResult = await directory.DeleteAsync(); results.TryAdd(href, null); } catch (ResourceException rex) { if (rex.Code == 404) { // Already deleted results.TryAdd(href, null); } } catch (Exception e) { results.TryAdd(href, e); } }); // Delete organizations var deleteOrganizationTasks = this.CreatedOrganizationHrefs.Select(async href => { try { var org = await client.GetResourceAsync <IOrganization>(href); var deleteResult = await org.DeleteAsync(); results.TryAdd(href, null); } catch (ResourceException rex) { if (rex.Code == 404) { // Already deleted results.TryAdd(href, null); } } catch (Exception e) { results.TryAdd(href, e); } }); await Task.WhenAll( Task.WhenAll(deleteApplicationTasks), Task.WhenAll(deleteDirectoryTasks), Task.WhenAll(deleteOrganizationTasks)); // All done! Throw errors if any occurred bool anyErrors = results.Any(kvp => kvp.Value != null); if (anyErrors) { throw new ApplicationException( "Errors occurred during test cleanup. Full log: " + Environment.NewLine + string.Join(Environment.NewLine, results.Select(kvp => $"{kvp.Key} : '{(kvp.Value == null ? "Good" : kvp.Value.Message)}'"))); } }
private async Task AddObjectsToTenantAsync() { // Get client and tenant var client = TestClients.GetSAuthc1Client(); var tenant = await client.GetCurrentTenantAsync(); tenant.ShouldNotBe(null); tenant.Href.ShouldNotBeNullOrEmpty(); this.TenantHref = tenant.Href; // Create applications try { // Create and verify applications var applicationsToCreate = this.testData.GetTestApplications(client); var applicationCreationTasks = applicationsToCreate.Select(app => tenant.CreateApplicationAsync(app, opt => opt.CreateDirectory = false)); var resultingApplications = await Task.WhenAll(applicationCreationTasks); resultingApplications.ShouldNotContain(x => string.IsNullOrEmpty(x.Href)); // Add them all to the teardown list this.CreatedApplicationHrefs.AddRange(resultingApplications.Select(x => x.Href)); // Grab the one marked as primary this.PrimaryApplicationHref = resultingApplications.Where(x => x.Name.Contains("primary")).Single().Href; } catch (Exception e) { await this.RemoveObjectsFromTenantAsync(); throw new ApplicationException("Could not create applications", e); } // Create organizations IOrganization primaryOrganization = null; try { var orgsToCreate = this.testData.GetTestOrganizations(client); var orgCreationTasks = orgsToCreate.Select(o => tenant.CreateOrganizationAsync(o, opt => opt.CreateDirectory = true)); var resultingOrgs = await Task.WhenAll(orgCreationTasks); resultingOrgs.ShouldNotContain(x => string.IsNullOrEmpty(x.Href)); // Verify that directories were created, too var getDirectoryTasks = resultingOrgs.Select(x => x.GetDefaultAccountStoreAsync()); var resultingDirectories = await Task.WhenAll(getDirectoryTasks); resultingDirectories.ShouldNotContain(x => string.IsNullOrEmpty(x.Href)); // Add them all to the teardown list this.CreatedOrganizationHrefs.AddRange(resultingOrgs.Select(x => x.Href)); this.CreatedDirectoryHrefs.AddRange(resultingDirectories.Select(x => x.Href)); // Grab the one marked as primary primaryOrganization = resultingOrgs.Where(x => x.Name.Contains("primary")).Single(); this.PrimaryOrganizationHref = primaryOrganization.Href; this.PrimaryOrganizationNameKey = primaryOrganization.NameKey; this.PrimaryDirectoryHref = (await primaryOrganization.GetDefaultAccountStoreAsync()).Href; } catch (Exception e) { await this.RemoveObjectsFromTenantAsync(); throw new ApplicationException("Could not create organizations", e); } // Add primary organization to primary application as an account store var primaryApplication = await client.GetResourceAsync <IApplication>(this.PrimaryApplicationHref); var mapping = client.Instantiate <IApplicationAccountStoreMapping>() .SetAccountStore(primaryOrganization) .SetApplication(primaryApplication) .SetDefaultAccountStore(true) .SetDefaultGroupStore(true); await primaryApplication.CreateAccountStoreMappingAsync(mapping); // Create accounts in primary organization try { var accountsToCreate = this.testData.GetTestAccounts(client); var createOptions = new AccountCreationOptionsBuilder() { RegistrationWorkflowEnabled = false }.Build(); var accountCreationTasks = accountsToCreate.Select(acct => primaryOrganization.CreateAccountAsync(acct, createOptions)); var resultingAccounts = await Task.WhenAll(accountCreationTasks); this.CreatedAccountHrefs.AddRange(resultingAccounts.Select(x => x.Href)); } catch (Exception e) { await this.RemoveObjectsFromTenantAsync(); throw new ApplicationException("Could not create accounts", e); } // Create groups try { var groupsToCreate = this.testData.GetTestGroups(client); var groupCreationTasks = groupsToCreate.Select(g => primaryOrganization.CreateGroupAsync(g)); var resultingGroups = await Task.WhenAll(groupCreationTasks); this.CreatedGroupHrefs.AddRange(resultingGroups.Select(x => x.Href)); this.PrimaryGroupHref = resultingGroups.Where(x => x.Name.Contains("primary")).Single().Href; } catch (Exception e) { await this.RemoveObjectsFromTenantAsync(); throw new ApplicationException("Could not create groups", e); } // Add some accounts to groups try { var luke = await primaryApplication.GetAccounts() .Where(x => x.Email.StartsWith("lskywalker")) .SingleAsync(); await luke.AddGroupAsync(this.PrimaryGroupHref); // Stash an account for easy access this.PrimaryAccountHref = luke.Href; } catch (Exception e) { await this.RemoveObjectsFromTenantAsync(); throw new ApplicationException("Could not add accounts to groups", e); } }