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(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(), "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(result.Result); } }