Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Apr 4, 2020
1 parent 52a03f1 commit 316ed4f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 68 deletions.
79 changes: 15 additions & 64 deletions implement/PersistentProcess/PersistentProcess.Test/TestWebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,6 @@ async System.Threading.Tasks.Task<HttpResponseMessage> PostStringContentToServer
[TestMethod]
public void Web_host_supporting_migrations_supports_set_app_config()
{
const string rootPassword = "Root-Password_1234567";

var allEventsAndExpectedResponses =
TestSetup.CounterProcessTestEventsAndExpectedResponses(
new (int addition, int expectedResponse)[]
Expand All @@ -816,26 +814,11 @@ public void Web_host_supporting_migrations_supports_set_app_config()
var webAppConfigZipArchive = ZipArchive.ZipArchiveFromEntries(
TestElmWebAppHttpServer.CounterWebApp.AsFiles());

Func<IWebHostBuilder, IWebHostBuilder> webHostBuilderMap =
builder => builder.WithSettingAdminRootPassword(rootPassword);

HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.TestServer server)
{
var adminClient = server.CreateClient();

adminClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(
WebHost.Configuration.BasicAuthenticationForAdminRoot(rootPassword))));

return adminClient;
}

using (var testSetup = WebHostSupportingMigrationsTestSetup.Setup(webHostBuilderMap))
using (var testSetup = WebHostSupportingMigrationsTestSetup.Setup(adminRootPassword: "Root-Password_1234567"))
{
using (var server = testSetup.BuildServer())
{
using (var adminClient = createClientWithAuthorizationHeader(server))
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppConfigResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndInitState,
Expand All @@ -844,7 +827,7 @@ HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.Tes
Assert.IsTrue(setAppConfigResponse.IsSuccessStatusCode, "set-app response IsSuccessStatusCode");
}

using (var adminClient = createClientWithAuthorizationHeader(server))
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var getAppConfigResponse = adminClient.GetAsync(StartupSupportingMigrations.PathApiGetAppConfig).Result;

Expand All @@ -868,7 +851,7 @@ HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.Tes
}
}

using (var adminClient = createClientWithAuthorizationHeader(server))
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppHttpResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndInitState,
Expand All @@ -880,7 +863,7 @@ HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.Tes
{
using (var server = testSetup.BuildServer())
{
using (var adminClient = createClientWithAuthorizationHeader(server))
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppHttpResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndContinueState,
Expand Down Expand Up @@ -910,34 +893,17 @@ HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.Tes
[TestMethod]
public void Web_host_supporting_migrations_supports_migrate_elm_state()
{
const string rootPassword = "Root-Password_1234567";

var webAppConfigZipArchive = ZipArchive.ZipArchiveFromEntries(
TestElmWebAppHttpServer.StringBuilderWebApp.AsFiles());

var migrateElmAppZipArchive = ZipArchive.ZipArchiveFromEntries(
TestSetup.GetElmAppFromExampleName("migrate-string-append-length"));

Func<IWebHostBuilder, IWebHostBuilder> webHostBuilderMap =
builder => builder.WithSettingAdminRootPassword(rootPassword);

HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.TestServer server)
{
var adminClient = server.CreateClient();

adminClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(
WebHost.Configuration.BasicAuthenticationForAdminRoot(rootPassword))));

return adminClient;
}

using (var testSetup = WebHostSupportingMigrationsTestSetup.Setup(webHostBuilderMap))
using (var testSetup = WebHostSupportingMigrationsTestSetup.Setup(adminRootPassword: "Root-Password_1234567"))
{
using (var server = testSetup.BuildServer())
{
using (var adminClient = createClientWithAuthorizationHeader(server))
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppConfigResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndInitState,
Expand Down Expand Up @@ -966,7 +932,7 @@ HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.Tes
Assert.AreEqual("ab", httpResponseContent, false, "Server response content.");
}

using (var adminClient = createClientWithAuthorizationHeader(server))
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var migrateHttpResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiMigrateElmState,
Expand All @@ -991,8 +957,6 @@ HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.Tes
[TestMethod]
public void Web_host_supporting_migrations_prevents_damaging_backend_state_with_invalid_migration()
{
const string rootPassword = "Root-Password_1234567";

var elmApp = TestSetup.GetElmAppFromExampleName("test-prevent-damage-by-migrate-webapp");

var webAppConfig = new WebAppConfiguration().WithElmApp(elmApp);
Expand All @@ -1001,32 +965,19 @@ public void Web_host_supporting_migrations_prevents_damaging_backend_state_with_

var migrateElmAppZipArchive = ZipArchive.ZipArchiveFromEntries(elmApp);

Func<IWebHostBuilder, IWebHostBuilder> webHostBuilderMap =
builder => builder.WithSettingAdminRootPassword(rootPassword);

HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.TestServer server)
{
var adminClient = server.CreateClient();

adminClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(
WebHost.Configuration.BasicAuthenticationForAdminRoot(rootPassword))));

return adminClient;
}

using (var testSetup = WebHostSupportingMigrationsTestSetup.Setup(webHostBuilderMap))
using (var testSetup = WebHostSupportingMigrationsTestSetup.Setup(adminRootPassword: "Root-Password_1234567"))
{
using (var server = testSetup.BuildServer())
{
using (var adminClient = createClientWithAuthorizationHeader(server))
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var setAppConfigResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiSetAppConfigAndInitState,
new ByteArrayContent(webAppConfigZipArchive)).Result;

Assert.IsTrue(setAppConfigResponse.IsSuccessStatusCode, "set-app response IsSuccessStatusCode");
Assert.IsTrue(
setAppConfigResponse.IsSuccessStatusCode,
"set-app response IsSuccessStatusCode (" + setAppConfigResponse.StatusCode + ")");
}

var stateToTriggerInvalidMigration =
Expand All @@ -1052,7 +1003,7 @@ HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.Tes
"Get same state back.");
}

using (var adminClient = createClientWithAuthorizationHeader(server))
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var migrateHttpResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiMigrateElmState,
Expand Down Expand Up @@ -1091,7 +1042,7 @@ HttpClient createClientWithAuthorizationHeader(Microsoft.AspNetCore.TestHost.Tes
"Set state httpResponse.IsSuccessStatusCode (" + httpResponse.Content?.ReadAsStringAsync().Result + ")");
}

using (var adminClient = createClientWithAuthorizationHeader(server))
using (var adminClient = testSetup.SetDefaultRequestHeaderAuthorizeForAdminRoot(server.CreateClient()))
{
var migrateHttpResponse = adminClient.PostAsync(
StartupSupportingMigrations.PathApiMigrateElmState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
using Microsoft.AspNetCore.Hosting;
using System;
using System.IO;
using System.Net.Http.Headers;

namespace Kalmit.PersistentProcess.Test
{
public class WebHostSupportingMigrationsTestSetup : IDisposable
{
static string PublicWebHostUrl => "http://localhost:35491";

static string AdminWebHostUrl => "http://localhost:19372";

readonly string testDirectory;

readonly string adminRootPassword;

readonly Func<IWebHostBuilder, IWebHostBuilder> webHostBuilderMap;

public string ProcessStoreDirectory => Path.Combine(testDirectory, "process-store");
Expand All @@ -24,8 +29,9 @@ public Microsoft.AspNetCore.TestHost.TestServer BuildServer(
new Microsoft.AspNetCore.TestHost.TestServer(
(webHostBuilderMap ?? (builder => builder))
(Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
.UseUrls("http://*:19372")
.UseUrls(AdminWebHostUrl)
.WithSettingPublicWebHostUrls(new[] { PublicWebHostUrl })
.WithSettingAdminRootPassword(adminRootPassword)
.UseStartup<StartupSupportingMigrations>()
.WithProcessStoreFileStore(processStoreFileStoreMap?.Invoke(defaultFileStore) ?? defaultFileStore)));
}
Expand All @@ -36,11 +42,15 @@ static public WebHostSupportingMigrationsTestSetup Setup(
builder => builder.WithSettingDateTimeOffsetDelegate(persistentProcessHostDateTime ?? (() => DateTimeOffset.UtcNow)));

static public WebHostSupportingMigrationsTestSetup Setup(
Func<IWebHostBuilder, IWebHostBuilder> webHostBuilderMap)
Func<IWebHostBuilder, IWebHostBuilder> webHostBuilderMap = null,
string adminRootPassword = null)
{
var testDirectory = Filesystem.CreateRandomDirectoryInTempDirectory();

var setup = new WebHostSupportingMigrationsTestSetup(testDirectory, webHostBuilderMap);
var setup = new WebHostSupportingMigrationsTestSetup(
testDirectory,
adminRootPassword: adminRootPassword,
webHostBuilderMap: webHostBuilderMap);

return setup;
}
Expand All @@ -53,14 +63,31 @@ public System.Net.Http.HttpClient BuildPublicAppHttpClient()
};
}

public System.Net.Http.HttpClient SetDefaultRequestHeaderAuthorizeForAdminRoot(System.Net.Http.HttpClient client)
{
if (adminRootPassword == null)
return null;

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(
WebHost.Configuration.BasicAuthenticationForAdminRoot(adminRootPassword))));

return client;
}

public void Dispose()
{
Directory.Delete(testDirectory, true);
}

WebHostSupportingMigrationsTestSetup(string testDirectory, Func<IWebHostBuilder, IWebHostBuilder> webHostBuilderMap)
WebHostSupportingMigrationsTestSetup(
string testDirectory,
string adminRootPassword,
Func<IWebHostBuilder, IWebHostBuilder> webHostBuilderMap)
{
this.testDirectory = testDirectory;
this.adminRootPassword = adminRootPassword;
this.webHostBuilderMap = webHostBuilderMap;
}
}
Expand Down

0 comments on commit 316ed4f

Please sign in to comment.