web: formulář nového hlášení závady s přílohami
Název, kategorie, budova, upřesnění místa, popis a až 5 příloh po 10 MB (JPG, PNG, HEIC, PDF, DOCX). Přílohy jdou do API v jednom multipart požadavku spolu s hlášením. Typ, velikost a počet souborů kontroluje už web, obsah souboru ověřuje API. Tlačítko Nahlásit závadu v menu a v seznamu, delší časový limit volání API kvůli nahrávání. Unit testy příloh, místa v seznamu a odeslání formuláře. Refs #23
This commit is contained in:
1 parent
2fe939ba5c
commit
946ff5ce12
9 files changed
+348
-12
No files matched your search
@@ -0,0 +1,98 @@
|
||||
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();
|
||||
var api = new HelpdeskApiClient(new HttpClient(handler) { BaseAddress = new Uri("http://api/") });
|
||||
|
||||
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"),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user