-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to allow testing + add sample test.
- Loading branch information
1 parent
c76db92
commit 5ab2b29
Showing
6 changed files
with
190 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...terfaceTests/Services/DefaultAdminAccountServiceTests/DefaultAdminAccountServiceHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using FactorioWebInterface; | ||
using FactorioWebInterface.Data; | ||
using FactorioWebInterface.Services; | ||
using FactorioWebInterfaceTests.Utils; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.IO; | ||
using System.IO.Abstractions; | ||
using System.IO.Abstractions.TestingHelpers; | ||
|
||
namespace FactorioWebInterfaceTests.Services.DefaultAdminAccountServiceTests | ||
{ | ||
public static class DefaultAdminAccountServiceHelper | ||
{ | ||
public static readonly string directoryPath = AppDomain.CurrentDomain.BaseDirectory!; | ||
public static readonly string filePath = Path.Combine(directoryPath, Constants.DefaultAdminFile); | ||
|
||
public static ServiceProvider MakeDefaultAdminAccountServiceProvider() | ||
{ | ||
var dbContextFactory = new TestDbContextFactory(); | ||
|
||
var serviceCollection = new ServiceCollection(); | ||
|
||
serviceCollection | ||
.AddSingleton(typeof(ILogger<>), typeof(TestLogger<>)) | ||
.AddSingleton<IFileSystem, MockFileSystem>() | ||
.AddTransient<ApplicationDbContext>(_ => dbContextFactory.Create<ApplicationDbContext>()) | ||
.AddTransient<DefaultAdminAccountService>(); | ||
Startup.SetupIdentity(serviceCollection); | ||
|
||
return serviceCollection.BuildServiceProvider(); | ||
} | ||
|
||
public static void SetupRoles(RoleManager<IdentityRole> roleManager) | ||
{ | ||
roleManager.CreateAsync(new IdentityRole(Constants.RootRole)); | ||
roleManager.CreateAsync(new IdentityRole(Constants.AdminRole)); | ||
} | ||
|
||
public static void SetupFileSystem(MockFileSystem fileSystem) | ||
{ | ||
fileSystem.Directory.CreateDirectory(directoryPath); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
FactorioWebInterfaceTests/Services/DefaultAdminAccountServiceTests/SetupDefaultUserAsync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using FactorioWebInterface; | ||
using FactorioWebInterface.Data; | ||
using FactorioWebInterface.Services; | ||
using FactorioWebInterfaceTests.Utils; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.IO.Abstractions; | ||
using System.IO.Abstractions.TestingHelpers; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace FactorioWebInterfaceTests.Services.DefaultAdminAccountServiceTests | ||
{ | ||
public class SetupDefaultUserAsync : IDisposable | ||
{ | ||
private readonly ServiceProvider serviceProvider; | ||
private readonly TestLogger<IDefaultAdminAccountService> logger; | ||
private readonly MockFileSystem fileSystem; | ||
private readonly RoleManager<IdentityRole> roleManager; | ||
private readonly DefaultAdminAccountService defaultAdminAccountService; | ||
|
||
public SetupDefaultUserAsync() | ||
{ | ||
serviceProvider = DefaultAdminAccountServiceHelper.MakeDefaultAdminAccountServiceProvider(); | ||
logger = (TestLogger<IDefaultAdminAccountService>)serviceProvider.GetRequiredService<ILogger<IDefaultAdminAccountService>>(); | ||
fileSystem = (MockFileSystem)serviceProvider.GetRequiredService<IFileSystem>(); | ||
roleManager = serviceProvider.GetService<RoleManager<IdentityRole>>(); | ||
defaultAdminAccountService = serviceProvider.GetRequiredService<DefaultAdminAccountService>(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
serviceProvider.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public async Task CreatedSuccessfully() | ||
{ | ||
// Arrange. | ||
DefaultAdminAccountServiceHelper.SetupRoles(roleManager); | ||
DefaultAdminAccountServiceHelper.SetupFileSystem(fileSystem); | ||
|
||
// Act. | ||
await defaultAdminAccountService.SetupDefaultUserAsync(); | ||
|
||
// Assert. | ||
var db = serviceProvider.GetRequiredService<ApplicationDbContext>(); | ||
var user = db.Users.Single(); | ||
|
||
Assert.Equal(Constants.DefaultAdminAccount, user.Id); | ||
Assert.Equal(Constants.DefaultAdminName, user.UserName); | ||
Assert.NotEmpty(user.PasswordHash); | ||
|
||
string fileContent = fileSystem.File.ReadAllText(DefaultAdminAccountServiceHelper.filePath); | ||
Assert.Contains("This account is unsecure. Please setup a personal account", fileContent); | ||
Assert.Contains($"Username: {Constants.DefaultAdminName}", fileContent); | ||
Assert.Contains("Password:", fileContent); | ||
|
||
logger.AssertContainsLog(LogLevel.Warning, $"{Constants.DefaultAdminAccount} created. This action potential exposes your interface, creating a new account and restarting this web interface will disable the default admin account"); | ||
} | ||
} | ||
} |