110 lines
4.9 KiB
C#
110 lines
4.9 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using Helpdesk.Web.Api;
|
|
using Helpdesk.Web.Auth;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.AspNetCore.TestHost;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using static Helpdesk.Web.Tests.WebTestSession;
|
|
|
|
namespace Helpdesk.Web.Tests;
|
|
|
|
public sealed class IssueActionFormTests
|
|
{
|
|
[Theory]
|
|
[InlineData("assign", "/assign")]
|
|
[InlineData("transition", "/reject")]
|
|
[InlineData("edit", "")]
|
|
public async Task IssueActionsRequireAnExplicitValidVersionAndAcceptZero(string action, string apiAction)
|
|
{
|
|
var backend = new IssueBackend();
|
|
await using var application = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
|
|
builder.ConfigureTestServices(services => services.AddHttpClient(nameof(HelpdeskApiClient))
|
|
.ConfigurePrimaryHttpMessageHandler(() => backend)));
|
|
using var client = application.CreateClient(new WebApplicationFactoryClientOptions
|
|
{
|
|
AllowAutoRedirect = false,
|
|
BaseAddress = new Uri("https://localhost"),
|
|
});
|
|
SignIn(application.Services, client, UserRoles.Admin);
|
|
var ct = TestContext.Current.CancellationToken;
|
|
const string detail = "/issues/42";
|
|
using var page = await client.GetAsync(detail, ct);
|
|
Assert.Equal(HttpStatusCode.OK, page.StatusCode);
|
|
var fields = action switch
|
|
{
|
|
"assign" => new Dictionary<string, string> { ["AssigneeId"] = "7", ["Priority"] = "HIGH" },
|
|
"transition" => new Dictionary<string, string> { ["Action"] = IssueActions.Reject, ["Text"] = "Duplicitní hlášení." },
|
|
_ => new Dictionary<string, string> { ["Title"] = "Projektor", ["Description"] = "Bliká.", ["CategoryId"] = "2" },
|
|
};
|
|
fields["__RequestVerificationToken"] = AntiforgeryToken(await page.Content.ReadAsStringAsync(ct));
|
|
var route = $"{detail}/{action}";
|
|
|
|
using (var missing = new FormUrlEncodedContent(fields))
|
|
using (var response = await client.PostAsync(route + "?RowVersion=0", missing, ct))
|
|
{
|
|
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
|
|
Assert.Equal(detail, response.Headers.Location?.OriginalString);
|
|
}
|
|
|
|
foreach (var invalidVersion in new[] { "", "abc", "-1", "2147483648" })
|
|
{
|
|
fields["RowVersion"] = invalidVersion;
|
|
using var form = new FormUrlEncodedContent(fields);
|
|
using var response = await client.PostAsync(route, form, ct);
|
|
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
|
|
Assert.Equal(detail, response.Headers.Location?.OriginalString);
|
|
}
|
|
|
|
fields["RowVersion"] = "0";
|
|
if (action == "assign")
|
|
{
|
|
fields["Priority"] = "UNKNOWN";
|
|
using var invalid = new FormUrlEncodedContent(fields);
|
|
using var response = await client.PostAsync(route, invalid, ct);
|
|
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
|
|
using var next = await client.GetAsync(detail, ct);
|
|
Assert.Contains("Vyberte platnou prioritu.", WebUtility.HtmlDecode(await next.Content.ReadAsStringAsync(ct)));
|
|
fields["Priority"] = "HIGH";
|
|
}
|
|
|
|
Assert.Empty(backend.Writes);
|
|
using var valid = new FormUrlEncodedContent(fields);
|
|
using var accepted = await client.PostAsync(route, valid, ct);
|
|
Assert.Equal(HttpStatusCode.Redirect, accepted.StatusCode);
|
|
var saved = Assert.Single(backend.Writes);
|
|
Assert.Equal("/api/v1/issues/42" + apiAction, saved.Path);
|
|
Assert.Equal(0, saved.Body.GetProperty("rowVersion").GetInt32());
|
|
if (action == "assign")
|
|
Assert.Equal("HIGH", saved.Body.GetProperty("priority").GetString());
|
|
}
|
|
|
|
private sealed class IssueBackend : HttpMessageHandler
|
|
{
|
|
private const string Detail = """
|
|
{"id":42,"title":"Projektor","description":"Bliká.","categoryId":2,"status":"RECEIVED","priority":"NORMAL",
|
|
"reporterId":1,"createdAt":"2026-10-03T08:00:00Z","rowVersion":0,"allowedActions":["ASSIGN","REJECT","EDIT"],"attachments":[]}
|
|
""";
|
|
|
|
public List<(string Path, JsonElement Body)> Writes { get; } = [];
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
|
|
{
|
|
var path = request.RequestUri!.AbsolutePath;
|
|
var detail = path == "/api/v1/issues/42";
|
|
if (request.Method != HttpMethod.Get)
|
|
{
|
|
Writes.Add((path, await request.Content!.ReadFromJsonAsync<JsonElement>(ct)));
|
|
detail = true;
|
|
}
|
|
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent(detail ? Detail : "[]", Encoding.UTF8, "application/json"),
|
|
};
|
|
}
|
|
}
|
|
}
|