Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Mar 14, 2024
1 parent 39a9f49 commit 2165ad9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public SpecificVersionReleaseInfo(IHttpClientHelper httpClientHelper, AppSetting

public async Task<string> SpecificVersionMessage(string? versionToCheckFor)
{
if ( string.IsNullOrWhiteSpace(versionToCheckFor) )
{
return string.Empty;
}

if ( _cache == null || _appSettings?.AddMemoryCache != true )
{
var specificVersionReleaseInfoContent =
Expand Down Expand Up @@ -64,7 +69,7 @@ internal string Parse(string json, string? versionToCheckFor)

versionToCheckFor ??= string.Empty;
Dictionary<string, Dictionary<string, string>>? dict = null;

try
{
dict = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(json);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -59,10 +60,14 @@ public async Task<IActionResult> CheckForUpdates(string currentVersion = "")
/// <response code="200">result</response>
[HttpGet("/api/health/release-info")]
[AllowAnonymous]
[ResponseCache(Duration = 7257600, Location = ResponseCacheLocation.Client)]
[Produces("application/json")]
public async Task<IActionResult> SpecificVersionReleaseInfo(string v = "")
{
if ( !string.IsNullOrWhiteSpace(v) )
{
CacheControlOverwrite.SetExpiresResponseHeaders(Request, 604800); // 1 week
}

var result =
await _specificVersionReleaseInfo.SpecificVersionMessage(v);
return Json(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe("HealthCheckForUpdates", () => {
jest
.spyOn(useFetch, "default")
.mockReset()
.mockImplementationOnce(() => mockGetIConnectionDefault)
.mockImplementationOnce(() => mockGetIConnectionDefault);

const shouldSkip = SkipDisplayOfUpdate();
Expand All @@ -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")
Expand All @@ -126,6 +130,7 @@ describe("HealthCheckForUpdates", () => {
const useFetchSpy = jest
.spyOn(useFetch, "default")
.mockReset()
.mockImplementationOnce(() => mockGetIConnectionDefault)
.mockImplementationOnce(() => mockGetIConnectionDefault);

const notificationSpy = jest
Expand Down Expand Up @@ -158,7 +163,9 @@ describe("HealthCheckForUpdates", () => {

const useFetchSpy = jest
.spyOn(useFetch, "default")
.mockImplementationOnce(() => mockGetIConnectionDefault)
.mockImplementationOnce(() => mockGetIConnectionDefault);

const component = render(<HealthCheckForUpdates></HealthCheckForUpdates>);

expect(notificationSpy).toHaveBeenCalledTimes(1);
Expand Down
10 changes: 10 additions & 0 deletions starsky/starsky/clientapp/src/shared/url/url-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
1 change: 1 addition & 0 deletions starsky/starsky/clientapp/src/shared/url/url-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<UpdateStatus, string?>());
var service2 = new FakeISpecificVersionReleaseInfo(
Expand All @@ -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<UpdateStatus, string?>());
var service2 = new FakeISpecificVersionReleaseInfo(
new Dictionary<string, Dictionary<string, string>>()
);

var controller = new HealthCheckForUpdatesController(fakeService,
service2) { ControllerContext = { HttpContext = new DefaultHttpContext() } };
var actionResult = await controller
.SpecificVersionReleaseInfo() as JsonResult;

Assert.AreEqual(string.Empty,
actionResult?.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, KeyValuePair<bool, string>>()),
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<string, KeyValuePair<bool, string>>()),
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()
{
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 2165ad9

Please sign in to comment.