Files
Heimdall/tests/Heimdall.Web.Tests/Components/Settings/NotificationSettingsSectionTests.cs
T
Arnaud Dartois 85bfdac7ee feat(settings): boutons Tester email/ntfy/IA + clés techniques masquées (partiel #80)
- Endpoints POST api/settings/{email,ntfy,ai}/test (Admin) :
  - email : envoi réel via EmailService vers l'expéditeur configuré
  - ntfy : SendTestAsync (nouvelle méthode INtfyService, retour HTTP 2xx)
  - ai : ping CodeProject.AI/Ollama via AiHealthCheck
- Client API : TestEmailAsync/TestNtfyAsync/TestAiAsync + record TestActionResult
- UI : boutons Tester dans NotificationSettingsSection (ntfy + email) et
  AiSettingsSection (moteurs IA) avec résultat inline
- AgentDetail : la clé brute n'est plus affichée (libellé global/surchargé)
- Tests bUnit : 3 nouveaux (POST par endpoint + affichage résultat) — 224 verts
2026-08-12 11:50:47 +02:00

130 lines
4.9 KiB
C#

using Bunit;
using Heimdall.Web.Components.Settings;
using Heimdall.Web.Services;
using Heimdall.Web.Tests.Components.Pages;
using Microsoft.Extensions.DependencyInjection;
using MudBlazor;
using Shouldly;
namespace Heimdall.Web.Tests.Components.Settings;
public class NotificationSettingsSectionTests : PageTestBase
{
private static AppSettingItem Setting(string key, string value) =>
new(Guid.NewGuid(), key, value, key, "integrations", "string", null, DateTime.UtcNow.AddDays(-1));
[Fact]
public void NotificationSection_AfficheChampsNtfyEtEmail()
{
Handler.OnGet("api/settings", new List<AppSettingItem>
{
Setting("ntfy.baseurl", "https://ntfy.sh"),
Setting("ntfy.token", "tok123"),
Setting("email.fromaddress", "alertes@heimdall.local"),
Setting("email.smtphost", "smtp.gmail.com"),
Setting("email.usessl", "true"),
});
LoginAs();
var cut = Render<NotificationSettingsSection>();
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("Serveur ntfy");
cut.Markup.ShouldContain("Token d'accès");
cut.Markup.ShouldContain("Adresse expéditrice");
cut.Markup.ShouldContain("Serveur SMTP");
cut.Markup.ShouldContain("https://ntfy.sh");
cut.Markup.ShouldContain("smtp.gmail.com");
cut.Markup.ShouldContain("Utiliser SSL/TLS");
});
}
[Fact]
public async Task NotificationSection_ModifieBaseUrl_AppellePutAvecBonneCle()
{
Handler.OnGet("api/settings", new List<AppSettingItem> { Setting("ntfy.baseurl", "https://ntfy.sh") });
Handler.On(HttpMethod.Put, "api/settings/ntfy.baseurl", System.Net.HttpStatusCode.OK);
LoginAs();
var cut = Render<NotificationSettingsSection>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("https://ntfy.sh"));
// Inputs texte : [0]=ntfy.baseurl (les 8 champs texte viennent avant le switch)
cut.FindAll("input")[0].Change("https://ntfy.tordais.cc");
cut.WaitForAssertion(() =>
{
Handler.Requests.ShouldContain(r =>
r.Method == HttpMethod.Put &&
r.RequestUri!.AbsolutePath.Contains("ntfy.baseurl", StringComparison.OrdinalIgnoreCase));
});
}
[Fact]
public void NotificationSection_SecretRedacte_ChampVideAvecPlaceholder()
{
Handler.OnGet("api/settings", new List<AppSettingItem>
{
Setting("ntfy.baseurl", "https://ntfy.sh"),
Setting("ntfy.token", "***REDACTED***"),
Setting("email.password", "***REDACTED***"),
});
LoginAs();
var cut = Render<NotificationSettingsSection>();
cut.WaitForAssertion(() =>
{
// Le champ token ne doit PAS exposer la valeur redactée
cut.FindAll("input")[1].GetAttribute("value").ShouldBe(string.Empty);
// Le champ mot de passe SMTP non plus (7e champ texte)
cut.FindAll("input")[7].GetAttribute("value").ShouldBe(string.Empty);
});
}
[Fact]
public void NotificationSection_TestNtfy_AppelleEndpointEtAfficheSucces()
{
Handler.OnGet("api/settings", new List<AppSettingItem> { Setting("ntfy.baseurl", "https://ntfy.sh") });
Handler.On(HttpMethod.Post, "api/settings/ntfy/test",
new { success = true, message = "Notification test envoyée sur le topic « heimdall-test »" });
LoginAs();
var cut = Render<NotificationSettingsSection>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Tester ntfy"));
cut.FindAll("button").Single(b => b.TextContent.Contains("Tester ntfy")).Click();
cut.WaitForAssertion(() =>
{
Handler.Requests.ShouldContain(r =>
r.Method == HttpMethod.Post &&
r.RequestUri!.AbsolutePath.Contains("ntfy/test", StringComparison.OrdinalIgnoreCase));
cut.Markup.ShouldContain("Notification test envoyée");
});
}
[Fact]
public void NotificationSection_TestEmail_AppelleEndpointEtAfficheErreur()
{
Handler.OnGet("api/settings", new List<AppSettingItem> { Setting("email.fromaddress", "a@b.c") });
Handler.On(HttpMethod.Post, "api/settings/email/test",
new { success = false, message = "Échec de l'envoi : SMTP injoignable" });
LoginAs();
var cut = Render<NotificationSettingsSection>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Tester l'envoi email"));
cut.FindAll("button").Single(b => b.TextContent.Contains("Tester l'envoi email")).Click();
cut.WaitForAssertion(() =>
{
Handler.Requests.ShouldContain(r =>
r.Method == HttpMethod.Post &&
r.RequestUri!.AbsolutePath.Contains("email/test", StringComparison.OrdinalIgnoreCase));
cut.Markup.ShouldContain("SMTP injoignable");
});
}
}