2026-12-03 00:14:53 +01:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
|
using Helpdesk.Web.Api;
|
2026-12-04 22:25:00 +01:00
|
|
|
using Helpdesk.Web.Controllers;
|
2026-12-03 00:14:53 +01:00
|
|
|
using Helpdesk.Web.Models;
|
2026-12-04 22:25:00 +01:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2026-12-03 00:14:53 +01:00
|
|
|
|
|
|
|
|
namespace Helpdesk.Web.Tests;
|
|
|
|
|
|
|
|
|
|
public class IssueDetailViewModelTests
|
|
|
|
|
{
|
|
|
|
|
private static readonly DateTimeOffset T = DateTimeOffset.UnixEpoch;
|
|
|
|
|
|
|
|
|
|
private static IssueEventDto Event(string type, string? from, string to, string actor, string? note = null) =>
|
|
|
|
|
new(1, type, from, to, 1, actor, note, T);
|
|
|
|
|
|
|
|
|
|
private static IssueDetailViewModel Model(int? assigneeId, params IssueEventDto[] events) => new()
|
|
|
|
|
{
|
|
|
|
|
Issue = new IssueDetail(42, "Projektor", "Bliká.", 2, null, null, "IN_PROGRESS", "HIGH", 5, assigneeId, T, 3,
|
|
|
|
|
[IssueActions.SubmitForVerification, IssueActions.Reject, IssueActions.Comment], []),
|
|
|
|
|
Events = events,
|
|
|
|
|
CategoryName = "Technické vybavení",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Jmena_hlasice_a_resitele_vezme_z_casove_osy()
|
|
|
|
|
{
|
|
|
|
|
var model = Model(7,
|
|
|
|
|
Event("CREATED", null, "RECEIVED", "Jan Novák"),
|
|
|
|
|
Event("ASSIGNED", "RECEIVED", "ASSIGNED", "Vedení Školy", "Petr Svoboda"),
|
|
|
|
|
Event("ASSIGNED", "ASSIGNED", "ASSIGNED", "Vedení Školy", "Eva Malá"));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Jan Novák", model.ReporterName);
|
|
|
|
|
Assert.Equal("Eva Malá", model.AssigneeName); // po přeřazení platí poslední přiřazení
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Nepriprazena_zavada_nema_resitele() => Assert.Null(Model(null, Event("CREATED", null, "RECEIVED", "Jan")).AssigneeName);
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Akce_zmeny_stavu_jsou_jen_povolene_a_v_poradi_zivotniho_cyklu() =>
|
|
|
|
|
Assert.Equal([IssueActions.SubmitForVerification, IssueActions.Reject], Model(7).Transitions);
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData("CREATED", null, "RECEIVED", null, "nahlásil/a závadu", null)]
|
|
|
|
|
[InlineData("ASSIGNED", "RECEIVED", "ASSIGNED", "Petr Svoboda", "přiřadil/a závadu řešiteli Petr Svoboda", null)]
|
|
|
|
|
[InlineData("ASSIGNED", "IN_PROGRESS", "ASSIGNED", "Eva Malá", "přeřadil/a závadu na Eva Malá", null)]
|
|
|
|
|
[InlineData("STATUS_CHANGED", "ASSIGNED", "IN_PROGRESS", null, "zahájil/a řešení", null)]
|
|
|
|
|
[InlineData("STATUS_CHANGED", "PENDING_VERIFICATION", "IN_PROGRESS", "Pořád bliká", "znovu otevřel/a závadu", "Důvod")]
|
|
|
|
|
[InlineData("STATUS_CHANGED", "IN_PROGRESS", "PENDING_VERIFICATION", "Vyměněna lampa", "předal/a závadu k ověření", "Poznámka")]
|
|
|
|
|
[InlineData("STATUS_CHANGED", "IN_PROGRESS", "REJECTED", "Duplicitní hlášení", "zamítl/a závadu", "Důvod")]
|
|
|
|
|
[InlineData("COMMENTED", null, "IN_PROGRESS", "Funguje HDMI?", "napsal/a komentář", null)]
|
|
|
|
|
public void Udalost_casove_osy_ma_srozumitelny_popis(string type, string? from, string to, string? note, string text, string? noteLabel)
|
|
|
|
|
{
|
|
|
|
|
var entry = TimelineEntry.From(Event(type, from, to, "Někdo", note));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(text, entry.Text);
|
|
|
|
|
Assert.Equal(noteLabel, entry.NoteLabel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Komentar_nema_stitek_stavu()
|
|
|
|
|
{
|
|
|
|
|
var entry = TimelineEntry.From(Event("COMMENTED", null, "IN_PROGRESS", "Jan", "Funguje HDMI?"));
|
|
|
|
|
|
|
|
|
|
Assert.True(entry.IsComment);
|
|
|
|
|
Assert.Null(entry.Status);
|
|
|
|
|
Assert.Equal("Funguje HDMI?", entry.Note);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(900, "900 B")]
|
|
|
|
|
[InlineData(300_000, "293 kB")]
|
|
|
|
|
[InlineData(2_500_000, "2,4 MB")]
|
|
|
|
|
public void Velikost_prilohy_je_citelna(long bytes, string expected) =>
|
|
|
|
|
Assert.Equal(expected, IssueDetailViewModel.FileSize(bytes).Replace('.', ','));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class IssueActionsApiTests
|
|
|
|
|
{
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(IssueActions.StartWork, null, "/api/v1/issues/42/start", """{"rowVersion":3}""")]
|
|
|
|
|
[InlineData(IssueActions.SubmitForVerification, "Hotovo", "/api/v1/issues/42/submit-for-verification", """{"rowVersion":3,"note":"Hotovo"}""")]
|
|
|
|
|
[InlineData(IssueActions.Confirm, null, "/api/v1/issues/42/confirm", """{"rowVersion":3}""")]
|
|
|
|
|
[InlineData(IssueActions.Reopen, "Bliká dál", "/api/v1/issues/42/reopen", """{"rowVersion":3,"reason":"Bliká dál"}""")]
|
|
|
|
|
[InlineData(IssueActions.Reject, "Duplicita", "/api/v1/issues/42/reject", """{"rowVersion":3,"reason":"Duplicita"}""")]
|
|
|
|
|
public async Task Zmena_stavu_jde_na_akcni_endpoint_s_rowVersion(string action, string? text, string path, string body)
|
|
|
|
|
{
|
|
|
|
|
var handler = new RecordingHandler(DetailJson);
|
|
|
|
|
await Client(handler).TransitionIssueAsync(42, action, 3, text, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpMethod.Post, handler.Method);
|
|
|
|
|
Assert.Equal(path, handler.Path);
|
|
|
|
|
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(body), JsonNode.Parse(handler.Body!)), handler.Body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Neznama_akce_se_do_API_neposle() =>
|
|
|
|
|
await Assert.ThrowsAsync<ArgumentException>(() =>
|
|
|
|
|
Client(new RecordingHandler(DetailJson)).TransitionIssueAsync(42, "DELETE", 3, null, CancellationToken.None));
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Prirazeni_posle_resitele_prioritu_a_verzi()
|
|
|
|
|
{
|
|
|
|
|
var handler = new RecordingHandler(DetailJson);
|
|
|
|
|
await Client(handler).AssignIssueAsync(42, new AssignIssueRequest(7, "HIGH", 3), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
Assert.Equal("/api/v1/issues/42/assign", handler.Path);
|
|
|
|
|
Assert.Equal("""{"assigneeId":7,"priority":"HIGH","rowVersion":3}""", handler.Body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Detail_nacte_akce_i_prilohy()
|
|
|
|
|
{
|
|
|
|
|
var issue = await Client(new RecordingHandler(DetailJson)).GetIssueAsync(42, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
Assert.True(issue.Can(IssueActions.Comment));
|
|
|
|
|
Assert.False(issue.Can(IssueActions.Assign));
|
|
|
|
|
Assert.Equal("foto.jpg", Assert.Single(issue.Attachments).FileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Priloha_si_zachova_cesky_nazev_souboru()
|
|
|
|
|
{
|
|
|
|
|
var handler = new RecordingHandler("", response =>
|
|
|
|
|
{
|
|
|
|
|
response.Content = new ByteArrayContent([0xFF, 0xD8, 0xFF]);
|
|
|
|
|
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
|
|
|
|
|
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileNameStar = "učebna B-205.jpg" };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
using var file = await Client(handler).DownloadAttachmentAsync(9, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
Assert.Equal("/api/v1/attachments/9", handler.Path);
|
|
|
|
|
Assert.Equal("učebna B-205.jpg", file.FileName);
|
|
|
|
|
Assert.Equal("image/jpeg", file.ContentType);
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-04 22:25:00 +01:00
|
|
|
[Theory]
|
|
|
|
|
[InlineData("application/pdf", "attachment")]
|
|
|
|
|
[InlineData("image/png", "inline")]
|
|
|
|
|
public async Task Web_pdf_stahuje_a_png_zobrazuje_v_prohlizeci(string contentType, string expectedDisposition)
|
|
|
|
|
{
|
|
|
|
|
using var handler = new RecordingHandler("", response =>
|
|
|
|
|
{
|
|
|
|
|
response.Content = new ByteArrayContent([1]);
|
|
|
|
|
response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
|
|
|
|
|
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileNameStar = "příloha" };
|
|
|
|
|
});
|
|
|
|
|
var context = new DefaultHttpContext();
|
|
|
|
|
var controller = new IssuesController(Client(handler)) { ControllerContext = new ControllerContext { HttpContext = context } };
|
|
|
|
|
|
|
|
|
|
var result = Assert.IsType<FileStreamResult>(await controller.Attachment(9, CancellationToken.None));
|
|
|
|
|
using var stream = result.FileStream;
|
|
|
|
|
var disposition = ContentDispositionHeaderValue.Parse(context.Response.Headers.ContentDisposition.ToString());
|
|
|
|
|
Assert.Equal(expectedDisposition, disposition.DispositionType);
|
|
|
|
|
Assert.Equal("příloha", disposition.FileNameStar);
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-03 00:14:53 +01:00
|
|
|
[Fact]
|
|
|
|
|
public void Chyby_poli_z_API_se_spoji_do_jedne_zpravy()
|
|
|
|
|
{
|
|
|
|
|
var ex = new ApiException(HttpStatusCode.BadRequest, "One or more validation errors occurred.",
|
|
|
|
|
new Dictionary<string, string[]> { ["reason"] = ["Důvod je povinný."], ["rowVersion"] = ["Chybí verze."] });
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Důvod je povinný. Chybí verze.", ex.AllMessages);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const string DetailJson = """
|
|
|
|
|
{"id":42,"title":"Projektor","description":"Bliká.","categoryId":2,"buildingId":null,"locationDetail":null,
|
|
|
|
|
"status":"IN_PROGRESS","priority":"NORMAL","reporterId":5,"assigneeId":7,"createdAt":"2026-10-03T08:00:00Z",
|
|
|
|
|
"rowVersion":4,"allowedActions":["COMMENT","ADD_ATTACHMENTS"],
|
|
|
|
|
"attachments":[{"id":9,"fileName":"foto.jpg","contentType":"image/jpeg","sizeBytes":3,"createdAt":"2026-10-03T08:00:00Z"}]}
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
private static HelpdeskApiClient Client(HttpMessageHandler handler) =>
|
|
|
|
|
new(new HttpClient(handler) { BaseAddress = new ApiOptions { BaseUrl = "http://api/" }.VersionedBaseAddress() });
|
|
|
|
|
|
|
|
|
|
private sealed class RecordingHandler(string json, Action<HttpResponseMessage>? customize = null) : HttpMessageHandler
|
|
|
|
|
{
|
|
|
|
|
public HttpMethod? Method { get; private set; }
|
|
|
|
|
public string? Path { get; private set; }
|
|
|
|
|
public string? Body { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
Method = request.Method;
|
|
|
|
|
Path = request.RequestUri!.AbsolutePath;
|
|
|
|
|
Body = request.Content is null ? null : await request.Content.ReadAsStringAsync(ct);
|
|
|
|
|
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(json, Encoding.UTF8, "application/json") };
|
|
|
|
|
customize?.Invoke(response);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|