Files
election-system/tests/ElectionSystem.Api.Tests/PersistenceModelTests.cs
T
2026-08-29 19:07:04 +08:00

90 lines
5.1 KiB
C#

using ElectionSystem.Api.Infrastructure.Auditing;
using ElectionSystem.Api.Infrastructure.Persistence;
using ElectionSystem.Api.Modules.Identity.Domain;
using ElectionSystem.Api.Modules.Organization;
using ElectionSystem.Api.Modules.Organization.Domain;
using ElectionSystem.Api.Modules.Voters.Domain;
using System.Text;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace ElectionSystem.Api.Tests;
public sealed class PersistenceModelTests
{
[Fact]
public async Task UserName_must_be_unique()
{
var options = new DbContextOptionsBuilder<ElectionDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
await using var context = new ElectionDbContext(options);
context.Users.Add(new ApplicationUser { UserName = "admin", DisplayName = "Administrator" });
await context.SaveChangesAsync();
var index = context.Model.FindEntityType(typeof(ApplicationUser))!.GetIndexes().Single(x => x.Properties.Single().Name == nameof(ApplicationUser.UserName));
Assert.True(index.IsUnique);
}
[Fact]
public void Audit_entry_does_not_contain_vote_content_field()
{
var properties = typeof(AuditEntry).GetProperties().Select(property => property.Name);
Assert.DoesNotContain(properties, property => property.Contains("Vote", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public async Task Provincial_administrator_scope_includes_all_descendant_regions()
{
var options = new DbContextOptionsBuilder<ElectionDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
await using var context = new ElectionDbContext(options);
var province = new AdministrativeRegion { Code = "11", Name = "省", Level = AdministrativeRegionLevel.Province, Source = "test", SourceVersion = "test" };
var city = new AdministrativeRegion { Code = "1101", Name = "市", Level = AdministrativeRegionLevel.City, ParentId = province.Id, Source = "test", SourceVersion = "test" };
var village = new AdministrativeRegion { Code = "110101001001", Name = "村", Level = AdministrativeRegionLevel.Village, ParentId = city.Id, Source = "test", SourceVersion = "test" };
var admin = new ApplicationUser { UserName = "province-admin", DisplayName = "Province administrator" };
context.AddRange(province, city, village, admin);
context.UserRegionalRoleAssignments.Add(new UserRegionalRoleAssignment { UserId = admin.Id, RegionId = province.Id, Role = RegionalRole.ProvinceAdministrator });
await context.SaveChangesAsync();
var scope = await new RegionScopeService(context).GetManagedRegionIdsAsync(admin.Id, CancellationToken.None);
Assert.Equal(3, scope.Count);
Assert.Contains(village.Id, scope);
}
[Fact]
public void Voter_number_and_region_status_indexes_are_defined()
{
var options = new DbContextOptionsBuilder<ElectionDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
using var context = new ElectionDbContext(options);
var entity = context.Model.FindEntityType(typeof(VoterProfile))!;
Assert.Contains(entity.GetIndexes(), x => x.IsUnique && x.Properties.Single().Name == nameof(VoterProfile.VoterNumber));
Assert.Contains(entity.GetIndexes(), x => x.Properties.Select(p => p.Name).SequenceEqual(new[] { nameof(VoterProfile.RegionId), nameof(VoterProfile.Status), nameof(VoterProfile.DisplayName) }));
}
[Fact]
public async Task AreaCity_import_preserves_parent_chain_and_four_levels()
{
const string csv = "id,pid,deep,name\n11,0,0,北京市\n1101,11,1,市辖区\n110101,1101,2,东城区\n110101001,110101,3,东华门街道\n";
var options = new DbContextOptionsBuilder<ElectionDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
await using var context = new ElectionDbContext(options);
await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(csv));
var result = await new AreaCityCatalogImportService(context).ImportAsync(stream, "test-2026", CancellationToken.None);
Assert.Equal(4, result.CreatedCount);
var township = await context.AdministrativeRegions.SingleAsync(x => x.Code == "110101001");
var district = await context.AdministrativeRegions.SingleAsync(x => x.Code == "110101");
Assert.Equal(district.Id, township.ParentId);
Assert.Equal(AdministrativeRegionLevel.Township, township.Level);
}
[Fact]
public void Local_identity_password_hash_cannot_be_reused_as_plaintext()
{
var user = new ApplicationUser { UserName = "local-admin", DisplayName = "Local administrator" };
var hasher = new PasswordHasher<ApplicationUser>();
var hash = hasher.HashPassword(user, "Correct-Horse-7!Battery");
Assert.NotEqual("Correct-Horse-7!Battery", hash);
Assert.NotEqual(PasswordVerificationResult.Failed, hasher.VerifyHashedPassword(user, hash, "Correct-Horse-7!Battery"));
Assert.Equal(PasswordVerificationResult.Failed, hasher.VerifyHashedPassword(user, hash, "wrong-password"));
}
}