using FluentAssertions;
using FsCheck;
using FsCheck.Xunit;
using Microsoft.AspNetCore.Identity;
using SchotzIadKmTracker.Api.Models.Entities;
namespace SchotzIadKmTracker.Tests.Auth;
///
/// Propriété de correction du hachage de mot de passe (feature authentification).
/// Utilise le PasswordHasher<Compte> réel d'ASP.NET Core Identity.
///
public class PasswordHasherProperties
{
// Feature: authentification, Property 1: Pour tout mot de passe non vide, le hachage produit une
// valeur différente du clair, VerifyHashedPassword retourne Success, et deux hachages du même
// mot de passe diffèrent (présence de sel).
// Validates: Requirements 1.5, 8.1, 8.3
[Property(MaxTest = 100)]
public Property Le_hachage_est_verifiable_et_sale()
{
// Générateur de mots de passe non vides et bornés (1..64 caractères imprimables).
var arb = Arb.From(
Gen.Choose(1, 64)
.SelectMany(longueur =>
Gen.ArrayOf(longueur, Gen.Choose(33, 126).Select(c => (char)c))
.Select(chars => new string(chars))));
return Prop.ForAll(arb, motDePasse =>
{
var hasher = new PasswordHasher();
var compte = new Compte { Id = 1, Email = "test@example.com" };
var hash1 = hasher.HashPassword(compte, motDePasse);
var hash2 = hasher.HashPassword(compte, motDePasse);
// Le hash diffère du clair.
hash1.Should().NotBe(motDePasse);
// La vérification réussit.
hasher.VerifyHashedPassword(compte, hash1, motDePasse)
.Should().BeOneOf(PasswordVerificationResult.Success,
PasswordVerificationResult.SuccessRehashNeeded);
// Deux hachages du même mot de passe diffèrent (sel aléatoire).
hash1.Should().NotBe(hash2);
});
}
}