From 2165ad98791114c4c9c3d2253d8d0877f9e45b7b Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 14 Mar 2024 21:42:47 +0100 Subject: [PATCH] add tests --- .../Services/SpecificVersionReleaseInfo.cs | 10 ++++-- .../HealthCheckForUpdatesController.cs | 7 +++- .../health-check-for-updates.spec.tsx | 9 +++++- .../src/shared/url/url-query.spec.ts | 10 ++++++ .../clientapp/src/shared/url/url-query.ts | 1 + .../HealthCheckForUpdatesControllerTest.cs | 28 +++++++++++++--- .../SpecificVersionReleaseInfoTest.cs | 32 ++++++++++++++++++- 7 files changed, 87 insertions(+), 10 deletions(-) diff --git a/starsky/starsky.feature.health/UpdateCheck/Services/SpecificVersionReleaseInfo.cs b/starsky/starsky.feature.health/UpdateCheck/Services/SpecificVersionReleaseInfo.cs index 82115a5e29..b1da4f579b 100644 --- a/starsky/starsky.feature.health/UpdateCheck/Services/SpecificVersionReleaseInfo.cs +++ b/starsky/starsky.feature.health/UpdateCheck/Services/SpecificVersionReleaseInfo.cs @@ -35,6 +35,11 @@ public SpecificVersionReleaseInfo(IHttpClientHelper httpClientHelper, AppSetting public async Task SpecificVersionMessage(string? versionToCheckFor) { + if ( string.IsNullOrWhiteSpace(versionToCheckFor) ) + { + return string.Empty; + } + if ( _cache == null || _appSettings?.AddMemoryCache != true ) { var specificVersionReleaseInfoContent = @@ -64,7 +69,7 @@ internal string Parse(string json, string? versionToCheckFor) versionToCheckFor ??= string.Empty; Dictionary>? dict = null; - + try { dict = JsonSerializer.Deserialize>>(json); @@ -74,7 +79,8 @@ internal string Parse(string json, string? versionToCheckFor) _webLogger.LogError("[SpecificVersionReleaseInfo] Json parse error: " + e.Message); } - if ( dict?.TryGetValue(versionToCheckFor, out var valueDict) is not true ) return string.Empty; + if ( dict?.TryGetValue(versionToCheckFor, out var valueDict) is not true ) + return string.Empty; var outputValue = valueDict.TryGetValue("en", out var languageValue) ? ConvertMarkdownLinkToHtml(languageValue) diff --git a/starsky/starsky/Controllers/HealthCheckForUpdatesController.cs b/starsky/starsky/Controllers/HealthCheckForUpdatesController.cs index b7505a7ee8..4016c94a9a 100644 --- a/starsky/starsky/Controllers/HealthCheckForUpdatesController.cs +++ b/starsky/starsky/Controllers/HealthCheckForUpdatesController.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc; using starsky.feature.health.UpdateCheck.Interfaces; using starsky.feature.health.UpdateCheck.Models; +using starsky.Helpers; namespace starsky.Controllers { @@ -59,10 +60,14 @@ public async Task CheckForUpdates(string currentVersion = "") /// result [HttpGet("/api/health/release-info")] [AllowAnonymous] - [ResponseCache(Duration = 7257600, Location = ResponseCacheLocation.Client)] [Produces("application/json")] public async Task SpecificVersionReleaseInfo(string v = "") { + if ( !string.IsNullOrWhiteSpace(v) ) + { + CacheControlOverwrite.SetExpiresResponseHeaders(Request, 604800); // 1 week + } + var result = await _specificVersionReleaseInfo.SpecificVersionMessage(v); return Json(result); diff --git a/starsky/starsky/clientapp/src/components/molecules/health-check-for-updates/health-check-for-updates.spec.tsx b/starsky/starsky/clientapp/src/components/molecules/health-check-for-updates/health-check-for-updates.spec.tsx index 211cdde1ff..4729a9cdb5 100644 --- a/starsky/starsky/clientapp/src/components/molecules/health-check-for-updates/health-check-for-updates.spec.tsx +++ b/starsky/starsky/clientapp/src/components/molecules/health-check-for-updates/health-check-for-updates.spec.tsx @@ -75,6 +75,7 @@ describe("HealthCheckForUpdates", () => { jest .spyOn(useFetch, "default") .mockReset() + .mockImplementationOnce(() => mockGetIConnectionDefault) .mockImplementationOnce(() => mockGetIConnectionDefault); const shouldSkip = SkipDisplayOfUpdate(); @@ -99,7 +100,10 @@ describe("HealthCheckForUpdates", () => { statusCode: 202, data: null } as IConnectionDefault; - jest.spyOn(useFetch, "default").mockImplementationOnce(() => mockGetIConnectionDefault); + jest + .spyOn(useFetch, "default") + .mockImplementationOnce(() => mockGetIConnectionDefault) + .mockImplementationOnce(() => mockGetIConnectionDefault); const notificationSpy = jest .spyOn(Notification, "default") @@ -126,6 +130,7 @@ describe("HealthCheckForUpdates", () => { const useFetchSpy = jest .spyOn(useFetch, "default") .mockReset() + .mockImplementationOnce(() => mockGetIConnectionDefault) .mockImplementationOnce(() => mockGetIConnectionDefault); const notificationSpy = jest @@ -158,7 +163,9 @@ describe("HealthCheckForUpdates", () => { const useFetchSpy = jest .spyOn(useFetch, "default") + .mockImplementationOnce(() => mockGetIConnectionDefault) .mockImplementationOnce(() => mockGetIConnectionDefault); + const component = render(); expect(notificationSpy).toHaveBeenCalledTimes(1); diff --git a/starsky/starsky/clientapp/src/shared/url/url-query.spec.ts b/starsky/starsky/clientapp/src/shared/url/url-query.spec.ts index 88d5ead5b5..9acf59bade 100644 --- a/starsky/starsky/clientapp/src/shared/url/url-query.spec.ts +++ b/starsky/starsky/clientapp/src/shared/url/url-query.spec.ts @@ -155,6 +155,16 @@ describe("url-query", () => { const test = urlQuery.GetReturnUrl("ReturnUrl=test"); expect(test).toStrictEqual("test"); }); + + it("UrlHealthReleaseInfo default", () => { + const result = urlQuery.UrlHealthReleaseInfo(null!); + expect(result).toBe(urlQuery.prefix + "/api/health/release-info"); + }); + + it("UrlHealthReleaseInfo version", () => { + const result = urlQuery.UrlHealthReleaseInfo("0.6.0"); + expect(result).toBe(urlQuery.prefix + "/api/health/release-info?version=0.6.0"); + }); }); describe("updateFilePath", () => { diff --git a/starsky/starsky/clientapp/src/shared/url/url-query.ts b/starsky/starsky/clientapp/src/shared/url/url-query.ts index dcc02f7cbe..8ec667d535 100644 --- a/starsky/starsky/clientapp/src/shared/url/url-query.ts +++ b/starsky/starsky/clientapp/src/shared/url/url-query.ts @@ -400,6 +400,7 @@ export class UrlQuery { } public UrlHealthReleaseInfo(v: string): string { + if (!v) return `${this.prefix}/api/health/release-info`; return `${this.prefix}/api/health/release-info?v=${v}`; } diff --git a/starsky/starskytest/Controllers/HealthCheckForUpdatesControllerTest.cs b/starsky/starskytest/Controllers/HealthCheckForUpdatesControllerTest.cs index d12dc547be..c13af11bf0 100644 --- a/starsky/starskytest/Controllers/HealthCheckForUpdatesControllerTest.cs +++ b/starsky/starskytest/Controllers/HealthCheckForUpdatesControllerTest.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.VisualStudio.TestTools.UnitTesting; using starsky.Controllers; @@ -132,7 +133,7 @@ public async Task CheckForUpdates_CurrentVersionIsLatest() } [TestMethod] - public async Task SpecificVersionReleaseInfo() + public async Task SpecificVersionReleaseInfo_GivesResult() { var fakeService = new FakeICheckForUpdates(new KeyValuePair()); var service2 = new FakeISpecificVersionReleaseInfo( @@ -148,13 +149,30 @@ public async Task SpecificVersionReleaseInfo() } ); - var actionResult = - await new HealthCheckForUpdatesController(fakeService, - service2) - .SpecificVersionReleaseInfo("1.0.0") as JsonResult; + var controller = new HealthCheckForUpdatesController(fakeService, + service2) { ControllerContext = { HttpContext = new DefaultHttpContext() } }; + var actionResult = await controller + .SpecificVersionReleaseInfo("1.0.0") as JsonResult; Assert.AreEqual(" # 1.0.0\n\n- [x] test\n- [ ] test2\n\n", actionResult?.Value); } + + [TestMethod] + public async Task SpecificVersionReleaseInfo_NoContent() + { + var fakeService = new FakeICheckForUpdates(new KeyValuePair()); + var service2 = new FakeISpecificVersionReleaseInfo( + new Dictionary>() + ); + + var controller = new HealthCheckForUpdatesController(fakeService, + service2) { ControllerContext = { HttpContext = new DefaultHttpContext() } }; + var actionResult = await controller + .SpecificVersionReleaseInfo() as JsonResult; + + Assert.AreEqual(string.Empty, + actionResult?.Value); + } } } diff --git a/starsky/starskytest/starsky.feature.health/Services/SpecificVersionReleaseInfoTest.cs b/starsky/starskytest/starsky.feature.health/Services/SpecificVersionReleaseInfoTest.cs index 04079abfa7..d27cf109c5 100644 --- a/starsky/starskytest/starsky.feature.health/Services/SpecificVersionReleaseInfoTest.cs +++ b/starsky/starskytest/starsky.feature.health/Services/SpecificVersionReleaseInfoTest.cs @@ -158,6 +158,36 @@ public async Task SpecificVersionMessage_CacheEnabledGetValue_SetInCache() Assert.AreEqual(example, result); } + [TestMethod] + public async Task SpecificVersionMessage_NullString() + { + var specificVersionReleaseInfo = + new SpecificVersionReleaseInfo( + new FakeIHttpClientHelper(new FakeIStorage(), + new Dictionary>()), + new AppSettings { AddMemoryCache = true }, null, + new FakeIWebLogger()); + + // Act + var result = await specificVersionReleaseInfo.SpecificVersionMessage("null"); + Assert.AreEqual(string.Empty, result); + } + + [TestMethod] + public async Task SpecificVersionMessage_NullValue() + { + var specificVersionReleaseInfo = + new SpecificVersionReleaseInfo( + new FakeIHttpClientHelper(new FakeIStorage(), + new Dictionary>()), + new AppSettings { AddMemoryCache = true }, null, + new FakeIWebLogger()); + + // Act + var result = await specificVersionReleaseInfo.SpecificVersionMessage(null); + Assert.AreEqual(string.Empty, result); + } + [TestMethod] public void ParseTest_String_Empty() { @@ -332,7 +362,7 @@ public void Parse_NullVersion() var specificVersionReleaseInfo = new SpecificVersionReleaseInfo(httpClientHelper, null, null, new FakeIWebLogger()); - + var result = specificVersionReleaseInfo.Parse("-", null); Assert.AreEqual(string.Empty, result); }