using LehrerApp.Desktop.Services; using Xunit; namespace LehrerApp.Desktop.Tests; public sealed class WebUntisSettingsServiceTests { private const string SampleUrl = "https://example.webuntis.com/WebUntis/ical_export?school=example&id=test&elemId=1&token=geheim"; private static string BuildTempPath() { var path = Path.Combine(Path.GetTempPath(), $"lehrerapp-webuntissettingssvc-tests-{Guid.NewGuid():N}"); Directory.CreateDirectory(path); return path; } [Fact] public void SetEnabled_PersistiertUeberNeueInstanz() { var path = BuildTempPath(); new WebUntisSettingsService(path).SetEnabled(true); var reloaded = new WebUntisSettingsService(path); Assert.True(reloaded.Enabled); } [Fact] public void SetIcalUrl_IstVerschluesseltAbrufbar() { var service = new WebUntisSettingsService(BuildTempPath()); service.SetIcalUrl(SampleUrl); Assert.True(service.IsConfigured); Assert.Equal(SampleUrl, service.GetIcalUrl()); } [Fact] public void IcalUrl_UeberlebtNeueInstanzMitDemselbenPfad() { var path = BuildTempPath(); new WebUntisSettingsService(path).SetIcalUrl(SampleUrl); var reloaded = new WebUntisSettingsService(path); Assert.True(reloaded.IsConfigured); Assert.Equal(SampleUrl, reloaded.GetIcalUrl()); } [Fact] public void KonfigurationsdateiEnthaeltNichtDasTokenImKlartext() { var path = BuildTempPath(); var service = new WebUntisSettingsService(path); service.SetIcalUrl(SampleUrl); var raw = File.ReadAllText(Path.Combine(path, "webuntis-settings.json")); Assert.DoesNotContain("token=geheim", raw); } [Fact] public void ClearIcalUrl_EntferntUrlUndDeaktiviert() { var service = new WebUntisSettingsService(BuildTempPath()); service.SetIcalUrl(SampleUrl); service.SetEnabled(true); service.ClearIcalUrl(); Assert.False(service.IsConfigured); Assert.Null(service.GetIcalUrl()); Assert.False(service.Enabled); } [Fact] public void SetLastSync_PersistiertZeitstempelUndStatus() { var path = BuildTempPath(); var at = new DateTime(2026, 8, 22, 12, 0, 0, DateTimeKind.Utc); new WebUntisSettingsService(path).SetLastSync(at, "3 Vertretungen erkannt"); var reloaded = new WebUntisSettingsService(path); Assert.Equal(at, reloaded.LastSyncAt); Assert.Equal("3 Vertretungen erkannt", reloaded.LastSyncStatus); } [Fact] public void ApiCredentials_SindVerschluesseltUndPersistieren() { var path = BuildTempPath(); var credentials = new WebUntisCredentials("schule", "mese.webuntis.com", "lehrkraft", "sehr geheim"); var service = new WebUntisSettingsService(path); service.SetApiCredentials(credentials); service.SetTeacherUntisId(4711); var raw = File.ReadAllText(Path.Combine(path, "webuntis-settings.json")); var reloaded = new WebUntisSettingsService(path); Assert.DoesNotContain("sehr geheim", raw); Assert.True(reloaded.ApiIsConfigured); Assert.Equal(credentials, reloaded.GetApiCredentials()); Assert.Equal(4711, reloaded.TeacherUntisId); } [Fact] public void ClearApiCredentials_LaesstIcalKonfigurationUnveraendert() { var service = new WebUntisSettingsService(BuildTempPath()); service.SetIcalUrl(SampleUrl); service.SetApiCredentials(new("schule", "", "user", "password")); service.SetTeacherUntisId(12); service.ClearApiCredentials(); Assert.False(service.ApiIsConfigured); Assert.Null(service.GetApiCredentials()); Assert.Null(service.TeacherUntisId); Assert.True(service.IsConfigured); Assert.Equal(SampleUrl, service.GetIcalUrl()); } }