2026-12-02 17:34:58 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using Helpdesk.Web.Api;
|
|
|
|
|
|
using Helpdesk.Web.Models;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.Web.Tests;
|
|
|
|
|
|
|
|
|
|
|
|
public class AttachmentRulesTests
|
|
|
|
|
|
{
|
|
|
|
|
|
private static IFormFile File(string name, long length) =>
|
|
|
|
|
|
new FormFile(new MemoryStream(new byte[Math.Min(length, 16)]), 0, length, "Files", name);
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public void Povolene_soubory_projdou() =>
|
|
|
|
|
|
Assert.Empty(AttachmentRules.Validate([File("projektor.JPG", 2_000_000), File("protokol.pdf", 300_000)]));
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public void Vic_nez_pet_souboru_neprojde() =>
|
|
|
|
|
|
Assert.Contains(AttachmentRules.Validate(Enumerable.Range(1, 6).Select(i => File($"foto{i}.png", 1000)).ToList()),
|
|
|
|
|
|
e => e.Contains("nejvýše 5"));
|
|
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
|
[InlineData("virus.exe", "povolený typ")]
|
|
|
|
|
|
[InlineData("velka.jpg", "10 MB")]
|
|
|
|
|
|
[InlineData("prazdny.png", "prázdný")]
|
|
|
|
|
|
public void Nepovoleny_soubor_ma_srozumitelnou_chybu(string name, string expected)
|
|
|
|
|
|
{
|
|
|
|
|
|
var length = name switch { "velka.jpg" => AttachmentRules.MaxFileBytes + 1, "prazdny.png" => 0, _ => 100 };
|
|
|
|
|
|
Assert.Contains(AttachmentRules.Validate([File(name, length)]), e => e.Contains(expected));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class IssueListViewModelTests
|
|
|
|
|
|
{
|
|
|
|
|
|
private static readonly IssueListViewModel Model = new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Issues = new PagedResult<IssueSummary>([], 0, 1, 20),
|
|
|
|
|
|
CategoryNames = new Dictionary<int, string> { [2] = "Technické vybavení" },
|
|
|
|
|
|
BuildingNames = new Dictionary<int, string> { [2] = "Budova B" },
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private static IssueSummary Issue(int? buildingId, string? detail) =>
|
|
|
|
|
|
new(1, "Projektor", 2, buildingId, detail, "RECEIVED", "NORMAL", 1, null, DateTimeOffset.UnixEpoch);
|
|
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
|
[InlineData(2, "učebna B-205", "Budova B – učebna B-205")]
|
|
|
|
|
|
[InlineData(2, null, "Budova B")]
|
|
|
|
|
|
[InlineData(null, "venku u vchodu", "venku u vchodu")]
|
|
|
|
|
|
[InlineData(null, null, null)]
|
|
|
|
|
|
public void Misto_spoji_budovu_a_upresneni(int? buildingId, string? detail, string? expected) =>
|
|
|
|
|
|
Assert.Equal(expected, Model.Location(Issue(buildingId, detail)));
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public void Neznama_kategorie_neshodi_stranku() => Assert.Equal("Neznámá kategorie", Model.CategoryName(99));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class CreateIssueRequestTests
|
|
|
|
|
|
{
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public async Task Hlaseni_s_prilohou_jde_do_API_jako_multipart()
|
|
|
|
|
|
{
|
|
|
|
|
|
var handler = new RecordingHandler();
|
2026-12-02 18:54:15 +01:00
|
|
|
|
var api = new HelpdeskApiClient(new HttpClient(handler) { BaseAddress = new ApiOptions { BaseUrl = "http://api/" }.VersionedBaseAddress() });
|
2026-12-02 17:34:58 +01:00
|
|
|
|
|
|
|
|
|
|
var issue = await api.CreateIssueAsync(
|
|
|
|
|
|
new CreateIssueRequest("Projektor", "Bliká.", 2, BuildingId: null, LocationDetail: "B-205"),
|
|
|
|
|
|
[new UploadFile("foto.jpg", "image/jpeg", () => new MemoryStream([0xFF, 0xD8, 0xFF]))],
|
|
|
|
|
|
CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal(7, issue.Id);
|
|
|
|
|
|
Assert.Equal("/api/v1/issues", handler.Path);
|
|
|
|
|
|
Assert.StartsWith("multipart/form-data", handler.ContentType);
|
|
|
|
|
|
Assert.Contains("name=title", handler.Body);
|
|
|
|
|
|
Assert.Contains("name=categoryId", handler.Body);
|
|
|
|
|
|
Assert.Contains("name=files; filename=foto.jpg", handler.Body);
|
|
|
|
|
|
Assert.DoesNotContain("name=buildingId", handler.Body); // nepovinná pole bez hodnoty se neposílají
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private sealed class RecordingHandler : HttpMessageHandler
|
|
|
|
|
|
{
|
|
|
|
|
|
public string? Path { get; private set; }
|
|
|
|
|
|
public string? ContentType { get; private set; }
|
|
|
|
|
|
public string Body { get; private set; } = "";
|
|
|
|
|
|
|
|
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
Path = request.RequestUri!.AbsolutePath;
|
|
|
|
|
|
ContentType = request.Content!.Headers.ContentType!.ToString();
|
|
|
|
|
|
Body = await request.Content.ReadAsStringAsync(ct);
|
|
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.Created)
|
|
|
|
|
|
{
|
|
|
|
|
|
Content = new StringContent("""{"id":7,"title":"Projektor","status":"RECEIVED","rowVersion":0,"allowedActions":["EDIT"]}""",
|
|
|
|
|
|
Encoding.UTF8, "application/json"),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|