manager-service/ManagerService.Tests/Controllers/AuthenticationControllerTests.cs
Thomas Fransolet af6b5b76ef Self-service onboarding (Stripe + Resend) + Postgres v3 schema (pgvector, media, visitor questions)
75 fichiers, ~1 mois de travail depuis f222861 (17/07). Contenu :

Onboarding self-service — jamais exécuté de bout en bout
  OnboardingController, StripeWebhookController, StripeService, ResendEmailService
  + IEmailService (10 templates), EmailTemplates/, TrialLifecycleService (Hangfire),
  PasswordTokenHelper, SlugHelper, 5 DTOs. Essai 14 j, Stripe customer/Checkout/Tax,
  mot de passe oublié + invitation user, plafond IA d'essai.

Schéma Postgres v3 — passe pendant que la base est vide
  ContentEmbedding + index HNSW (vector_cosine_ops), IEmbeddingService +
  GoogleEmbeddingService (gemini-embedding-001, 768 dims), Deployment/Dockerfile.postgres
  (postgis 3.4.3 + pgvector 0.8.6, épinglé par digest — un tag mobile rejouerait le
  warning de collation glibc). Colonnes Resource : StoragePath, FileName,
  IncludeInAiKnowledge, AiIndexStatus + nouveaux ResourceType ajoutés EN FIN d'enum.

Guide IA
  Champs Guide* sur Instance + InstanceDTO, AssistantService lit la configuration client
  dans les 4 blocs de prompt (ton codé en dur retiré, règle hors-sujet ajoutée aux deux
  variantes qui n'en avaient pas), IHttpClientFactory à la place des new HttpClient().
  Table VisitorQuestion + ConversationId sur AiChatRequest.

Stats
  Rétention unifiée à 13 mois (instances ET plans), VisitEventPurgeService.

Nettoyage
  IsStepLocked / IsHiddenInitially / FactContent supprimés de GuidedStep — IsStepLocked
  rendait une étape définitivement infranchissable même après réussite.
  SectionMap allégé (-57 lignes).

Tests
  SectionParcoursControllerTests, FakeConfiguration, FakeEmailService.
  ContentEmbedding a cassé 116 tests sur 124 (EF InMemory ne connaît pas Vector) :
  l'entité est exclue quand le provider n'est pas Npgsql. Conséquence assumée —
  le vector store n'est couvert par aucun test. dotnet test 124/124.

10 migrations EF. Base locale à jour, dotnet build 0 erreur.
Rien n'est en prod : la bascule Mongo → Postgres est décrite dans DOCS/STATUS.md §1quinquies.

⚠️ appsettings.json contient les clés Stripe (test) et Resend (prod) en clair — à rotationner.
2026-08-09 22:11:48 +02:00

84 lines
3.1 KiB
C#

using Manager.Interfaces.Models;
using Manager.Services;
using ManagerService.Data;
using ManagerService.Helpers;
using ManagerService.Service.Controllers;
using ManagerService.Service.Services;
using ManagerService.Tests.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Xunit;
namespace ManagerService.Tests.Controllers
{
public class AuthenticationControllerTests
{
private static AuthenticationController BuildController(MyInfoMateDbContext db)
{
var settings = Options.Create(new TokensSettings
{
Secret = "test-secret-key-32-chars-minimum!!",
AccessTokenExpiration = 30
});
var profileLogic = new ProfileLogic(NullLogger<ProfileLogic>.Instance);
var tokensService = new TokensService(
NullLogger<TokensService>.Instance,
settings,
profileLogic,
db);
return new AuthenticationController(
NullLogger<AuthenticationController>.Instance,
tokensService,
db,
profileLogic,
new FakeEmailService(),
FakeConfiguration.Create());
}
// Note: en mode DEBUG, email est toujours surchargé en "test@email.be"
// et password en "kljqsdkljqsd".
[Fact]
public void Authenticate_UserNotFound_ReturnsProblem()
{
using var db = DbContextFactory.Create();
// Aucun utilisateur "test@email.be" en base
var result = BuildController(db).AuthenticateWithJson(
new ManagerService.DTOs.LoginDTO { email = "anyone@test.be", password = "any" });
// KeyNotFoundException → catch(Exception) → Problem (500)
var obj = Assert.IsType<ObjectResult>(result);
Assert.Equal(500, obj.StatusCode);
}
[Fact]
public void Authenticate_WrongPassword_Returns401()
{
using var db = DbContextFactory.Create();
// Mot de passe incorrect en base (hash invalide → PasswordUtils.Compare lance une exception
// qui n'est pas UnauthorizedAccessException → retourne Problem 500).
// On met un utilisateur avec un hash valide pour un autre mot de passe.
var profileLogic = new ProfileLogic(NullLogger<ProfileLogic>.Instance);
db.Users.Add(new User
{
Id = "u1",
Email = "test@email.be",
Password = profileLogic.HashPassword("differentpassword"),
LastName = "Test",
Token = "t1",
InstanceId = "inst-test"
});
db.SaveChanges();
var result = BuildController(db).AuthenticateWithJson(
new ManagerService.DTOs.LoginDTO { email = "test@email.be", password = "kljqsdkljqsd" });
// UnauthorizedAccessException → Unauthorized (401)
Assert.IsType<UnauthorizedObjectResult>(result);
}
}
}