chore(backend): add explicit test data seed command
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
namespace Socialize.Api.Infrastructure.Development;
|
||||
|
||||
public record DevelopmentSeedOptions
|
||||
{
|
||||
public const string SectionName = "DevelopmentSeed";
|
||||
|
||||
public bool Enabled { get; init; } = true;
|
||||
}
|
||||
@@ -16,11 +16,10 @@ using Socialize.Api.Modules.Organizations.Data;
|
||||
using Socialize.Api.Modules.Organizations.Services;
|
||||
using Socialize.Api.Modules.Workspaces.Data;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Socialize.Api.Infrastructure.Development;
|
||||
namespace Socialize.Api.Infrastructure.TestData;
|
||||
|
||||
public static class DevelopmentSeedExtensions
|
||||
public static class TestDataSeedExtensions
|
||||
{
|
||||
private static readonly Guid OrganizationId = Guid.Parse("99999999-9999-9999-9999-999999999999");
|
||||
private static readonly Guid WorkspaceId = Guid.Parse("11111111-1111-1111-1111-111111111111");
|
||||
@@ -39,23 +38,11 @@ public static class DevelopmentSeedExtensions
|
||||
private static readonly Guid ClientCommentId = Guid.Parse("77777777-7777-7777-7777-777777777777");
|
||||
private static readonly Guid NotificationId = Guid.Parse("88888888-8888-8888-8888-888888888888");
|
||||
|
||||
public static async Task<IApplicationBuilder> UseDevelopmentSeedAsync(
|
||||
this IApplicationBuilder app,
|
||||
public static async Task<IServiceProvider> SeedTestDataAsync(
|
||||
this IServiceProvider services,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
IHostEnvironment environment = app.ApplicationServices.GetRequiredService<IHostEnvironment>();
|
||||
if (!environment.IsDevelopment())
|
||||
{
|
||||
return app;
|
||||
}
|
||||
|
||||
using IServiceScope scope = app.ApplicationServices.CreateScope();
|
||||
IOptions<DevelopmentSeedOptions> options = scope.ServiceProvider.GetRequiredService<IOptions<DevelopmentSeedOptions>>();
|
||||
if (!options.Value.Enabled)
|
||||
{
|
||||
return app;
|
||||
}
|
||||
|
||||
using IServiceScope scope = services.CreateScope();
|
||||
UserManager userManager = scope.ServiceProvider.GetRequiredService<UserManager>();
|
||||
AppDbContext dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
|
||||
@@ -64,7 +51,7 @@ public static class DevelopmentSeedExtensions
|
||||
id: Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"),
|
||||
username: "manager",
|
||||
email: "manager@socialize.local",
|
||||
password: "manager",
|
||||
password: "Manager1!",
|
||||
alias: "Northstar Manager",
|
||||
firstname: "Morgan",
|
||||
lastname: "Reid",
|
||||
@@ -80,7 +67,7 @@ public static class DevelopmentSeedExtensions
|
||||
id: Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"),
|
||||
username: "client",
|
||||
email: "client@socialize.local",
|
||||
password: "client",
|
||||
password: "Client1!",
|
||||
alias: "Sofia Martin",
|
||||
firstname: "Sofia",
|
||||
lastname: "Martin",
|
||||
@@ -97,7 +84,7 @@ public static class DevelopmentSeedExtensions
|
||||
id: Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"),
|
||||
username: "provider",
|
||||
email: "provider@socialize.local",
|
||||
password: "provider",
|
||||
password: "Provider1!",
|
||||
alias: "Alex Studio",
|
||||
firstname: "Alex",
|
||||
lastname: "Studio",
|
||||
@@ -115,7 +102,7 @@ public static class DevelopmentSeedExtensions
|
||||
id: Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"),
|
||||
username: "dev",
|
||||
email: "dev@socialize.local",
|
||||
password: "dev",
|
||||
password: "Developer1!",
|
||||
alias: "Socialize Dev",
|
||||
firstname: "Jo",
|
||||
lastname: "Bumble",
|
||||
@@ -138,7 +125,7 @@ public static class DevelopmentSeedExtensions
|
||||
dbContext,
|
||||
cancellationToken);
|
||||
|
||||
return app;
|
||||
return services;
|
||||
}
|
||||
|
||||
private static async Task<User> EnsureUserAsync(
|
||||
@@ -175,7 +162,7 @@ public static class DevelopmentSeedExtensions
|
||||
if (!createResult.Succeeded)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Failed to seed development user '{username}': {string.Join(", ", createResult.Errors.Select(error => error.Description))}");
|
||||
$"Failed to seed test user '{username}': {string.Join(", ", createResult.Errors.Select(error => error.Description))}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,7 +182,7 @@ public static class DevelopmentSeedExtensions
|
||||
if (!passwordResetResult.Succeeded)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Failed to set development password for '{username}': {string.Join(", ", passwordResetResult.Errors.Select(error => error.Description))}");
|
||||
$"Failed to set test password for '{username}': {string.Join(", ", passwordResetResult.Errors.Select(error => error.Description))}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user