Files
Hy2026/tests/TarninaHub.Tests/Controllers/ApplicationsControllerTests.cs
T
Rafał Tyński ded596d872 test(ideas): cover workflow, application window and controller outcomes
Add unit tests for idea status transitions, granted application windows and applications controller status mapping (409 when call closed, 201 on success).
2026-10-04 03:13:50 +02:00

88 lines
3.1 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TarninaHub.Application.Abstractions;
using TarninaHub.Application.Dtos;
using TarninaHub.Controllers;
using TarninaHub.Tests.Stubs;
namespace TarninaHub.Tests.Controllers;
public class ApplicationsControllerTests
{
private static ApplicationsController CreateController(
IApplicationService applicationService,
IIdeaService? ideaService = null)
{
var controller = new ApplicationsController(applicationService, ideaService ?? new StubIdeaService());
var identity = new ClaimsIdentity(
[new Claim("sub", "user:tester"), new Claim("role", "user")],
authenticationType: "test");
controller.ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(identity) },
};
return controller;
}
[Fact]
public async Task Create_ReturnsConflictWhenCallNotOpen()
{
var service = new StubApplicationService(
new CreateApplicationResult(CreateApplicationOutcome.CallNotOpen, null));
var controller = CreateController(service);
var result = await controller.Create(
new CreateApplicationRequest("grant_call:x", "idea:y", null),
CancellationToken.None);
var conflict = Assert.IsType<ConflictObjectResult>(result.Result);
Assert.Equal(StatusCodes.Status409Conflict, conflict.StatusCode);
}
[Fact]
public async Task Create_ReturnsCreatedWhenSuccessful()
{
var application = new ApplicationResponse(
"application:new",
"grant_call:x",
"idea:y",
new Dictionary<string, object?>(),
"draft",
DateTimeOffset.UtcNow);
var service = new StubApplicationService(
new CreateApplicationResult(CreateApplicationOutcome.Created, application));
var controller = CreateController(service);
var result = await controller.Create(
new CreateApplicationRequest("grant_call:x", "idea:y", null),
CancellationToken.None);
Assert.IsType<CreatedAtActionResult>(result.Result);
}
private sealed class StubApplicationService : IApplicationService
{
private readonly CreateApplicationResult _createResult;
public StubApplicationService(CreateApplicationResult createResult)
{
_createResult = createResult;
}
public Task<CreateApplicationResult> CreateAsync(
string userId,
CreateApplicationRequest request,
CancellationToken cancellationToken) => Task.FromResult(_createResult);
public Task<ApplicationResponse?> GetAsync(string applicationId, CancellationToken cancellationToken) =>
Task.FromResult(_createResult.Application);
public Task<IReadOnlyList<ApplicationResponse>> ListForAuthorAsync(
string authorId,
CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<ApplicationResponse>>([]);
}
}