Skip to content

Simplify tests #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/Passwordless.Tests.Infra/PasswordlessApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Extensions.DependencyInjection;

namespace Passwordless.Tests.Infra;

public class PasswordlessApplication
{
public string Name { get; }

public string ApiUrl { get; }

public string ApiSecret { get; }

public string ApiKey { get; }

public PasswordlessApplication(string name, string apiUrl, string apiSecret, string apiKey)
{
Name = name;
ApiUrl = apiUrl;
ApiSecret = apiSecret;
ApiKey = apiKey;
}

public IPasswordlessClient CreateClient() =>
// Initialize using a service container to cover more code paths in tests
new ServiceCollection().AddPasswordlessSdk(o =>
{
o.ApiUrl = ApiUrl;
o.ApiKey = ApiKey;
o.ApiSecret = ApiSecret;
}).BuildServiceProvider().GetRequiredService<IPasswordlessClient>();
}
83 changes: 14 additions & 69 deletions tests/Passwordless.Tests.Infra/TestApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using DotNet.Testcontainers.Images;
using Microsoft.Extensions.DependencyInjection;

namespace Passwordless.Tests.Infra;

Expand All @@ -28,8 +27,6 @@ public class TestApi : IAsyncDisposable

private string PublicApiUrl => $"http://{_apiContainer.Hostname}:{_apiContainer.GetMappedPublicPort(ApiPort)}";

public static string GetAppName() => $"app{Guid.NewGuid():N}";

public TestApi()
{
_apiContainer = new ContainerBuilder()
Expand Down Expand Up @@ -84,10 +81,10 @@ public async Task InitializeAsync()
}
}

public Task<PasswordlessOptions> CreateAppAsync() => CreateAppAsync($"app{Guid.NewGuid():N}");

public async Task<PasswordlessOptions> CreateAppAsync(string appName)
public async Task<PasswordlessApplication> CreateAppAsync()
{
var appName = $"app{Guid.NewGuid():N}";

using var request = new HttpRequestMessage(
HttpMethod.Post,
$"{PublicApiUrl}/admin/apps/{appName}/create"
Expand All @@ -97,7 +94,9 @@ public async Task<PasswordlessOptions> CreateAppAsync(string appName)
// lang=json
"""
{
"adminEmail": "[email protected]"
"adminEmail": "[email protected]",
"eventLoggingIsEnabled": true,
"eventLoggingRetentionPeriod": 7
}
""",
Encoding.UTF8,
Expand All @@ -116,73 +115,19 @@ public async Task<PasswordlessOptions> CreateAppAsync(string appName)
}

var responseContent = await response.Content.ReadFromJsonAsync<JsonElement>();
var apiKey = responseContent.GetProperty("apiKey1").GetString();
var apiSecret = responseContent.GetProperty("apiSecret1").GetString();

return new PasswordlessOptions
{
ApiUrl = PublicApiUrl,
ApiKey = apiKey,
ApiSecret = apiSecret ??
throw new InvalidOperationException("Cannot extract API Secret from the response.")
};
}
var apiSecret =
responseContent.GetProperty("apiSecret1").GetString() ??
throw new InvalidOperationException("Failed to extract the API secret.");

public async Task<IPasswordlessClient> CreateClientAsync()
{
var options = await CreateAppAsync();
var apiKey =
responseContent.GetProperty("apiKey1").GetString() ??
throw new InvalidOperationException("Failed to extract the API key.");

// Initialize using a service container to cover more code paths
return new ServiceCollection().AddPasswordlessSdk(o =>
{
o.ApiUrl = options.ApiUrl;
o.ApiKey = options.ApiKey;
o.ApiSecret = options.ApiSecret;
}).BuildServiceProvider().GetRequiredService<IPasswordlessClient>();
return new PasswordlessApplication(appName, PublicApiUrl, apiSecret, apiKey);
}

public async Task<IPasswordlessClient> CreateClientAsync(string applicationName)
{
var options = await CreateAppAsync(applicationName);

// Initialize using a service container to cover more code paths
return new ServiceCollection().AddPasswordlessSdk(o =>
{
o.ApiUrl = options.ApiUrl;
o.ApiKey = options.ApiKey;
o.ApiSecret = options.ApiSecret;
}).BuildServiceProvider().GetRequiredService<IPasswordlessClient>();
}

public async Task EnableEventLogsAsync(string applicationName)
{
using var request = new HttpRequestMessage(
HttpMethod.Post,
$"{PublicApiUrl}/admin/apps/{applicationName}/features"
);
request.Content = new StringContent(
// lang=json
"""
{
"EventLoggingIsEnabled": true,
"EventLoggingRetentionPeriod": 7,
"MaxUsers": null
}
""",
Encoding.UTF8,
"application/json");

using var response = await _http.SendAsync(request);

if (!response.IsSuccessStatusCode)
{
throw new InvalidOperationException(
$"Failed to enable event logging. " +
$"Status code: {(int)response.StatusCode}. " +
$"Response body: {await response.Content.ReadAsStringAsync()}."
);
}
}
public async Task<IPasswordlessClient> CreateClientAsync() => (await CreateAppAsync()).CreateClient();

public string GetLogs()
{
Expand Down
11 changes: 6 additions & 5 deletions tests/Passwordless.Tests/ApplicationEventLogsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ public class ApplicationEventLogsTests(TestApiFixture api, ITestOutputHelper tes
public async Task I_can_view_application_event_logs_when_event_logs_are_enabled()
{
// Arrange
var applicationName = TestApi.GetAppName();
var passwordless = await Api.CreateClientAsync(applicationName);
await Api.EnableEventLogsAsync(applicationName);
var app = await Api.CreateAppAsync();
var passwordless = app.CreateClient();

// Act
var response = await passwordless.GetEventLogAsync(new GetEventLogRequest { PageNumber = 1, NumberOfResults = 100 });
var response = await passwordless.GetEventLogAsync(
new GetEventLogRequest { PageNumber = 1, NumberOfResults = 100 }
);

// Assert
response.Should().NotBeNull();
response.TenantId.Should().Be(applicationName);
response.TenantId.Should().Be(app.Name);
}
}