46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
using Microsoft.Data.Sqlite;
|
|
using VolcanicCore.Api.Auth;
|
|
using VolcanicCore.Persistence;
|
|
using VolcanicCore.Persistence.Encryption;
|
|
using VolcanicCore.Persistence.Migrations;
|
|
using VolcanicCore.Persistence.Repositories;
|
|
|
|
namespace VolcanicCore.Tests;
|
|
|
|
public class AdminBootstrapTests
|
|
{
|
|
private static (SqliteConnectionFactory f, AdminConfigRepository r) Setup()
|
|
{
|
|
var path = Path.Combine(Path.GetTempPath(), $"vc-{Guid.NewGuid():N}.db");
|
|
var f = new SqliteConnectionFactory(path);
|
|
using (var conn = f.Open())
|
|
new Migrator(new IMigration[] { new Migration001Initial() }).Apply(conn);
|
|
var key = new byte[32]; System.Security.Cryptography.RandomNumberGenerator.Fill(key);
|
|
return (f, new AdminConfigRepository(f, new EncryptionService(key)));
|
|
}
|
|
|
|
[Fact]
|
|
public void Refuses_to_boot_without_admin_password()
|
|
{
|
|
var (_, repo) = Setup();
|
|
var pw = new PasswordService();
|
|
Assert.Throws<InvalidOperationException>(() => AdminBootstrap.Seed(repo, pw, ""));
|
|
}
|
|
|
|
[Fact]
|
|
public void Stores_hash_on_first_boot_and_updates_it_when_password_changes()
|
|
{
|
|
var (_, repo) = Setup();
|
|
var pw = new PasswordService();
|
|
AdminBootstrap.Seed(repo, pw, "hunter2");
|
|
var h1 = repo.Get("admin.password_hash");
|
|
Assert.NotNull(h1);
|
|
Assert.True(pw.Verify("hunter2", h1!));
|
|
|
|
AdminBootstrap.Seed(repo, pw, "correct-horse");
|
|
var h2 = repo.Get("admin.password_hash");
|
|
Assert.NotEqual(h1, h2);
|
|
Assert.True(pw.Verify("correct-horse", h2!));
|
|
}
|
|
}
|