test(backend): cover step 17 modules and extract stubs
Add tests for partnership workflow, middleman adaptation and idea notifications; extract reusable test stubs into separate files; add architecture guard forbidding object-valued dictionaries in data contracts.
This commit is contained in:
@@ -49,7 +49,7 @@ public class ApplicationsControllerTests
|
||||
"application:new",
|
||||
"grant_call:x",
|
||||
"idea:y",
|
||||
new Dictionary<string, object?>(),
|
||||
new Dictionary<string, string?>(),
|
||||
"draft",
|
||||
DateTimeOffset.UtcNow);
|
||||
var service = new StubApplicationService(
|
||||
@@ -62,26 +62,4 @@ public class ApplicationsControllerTests
|
||||
|
||||
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>>([]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
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 IdeasControllerTests
|
||||
{
|
||||
private static IdeasController CreateController(IIdeaService ideaService, INotificationService notifications)
|
||||
{
|
||||
var controller = new IdeasController(
|
||||
ideaService,
|
||||
new StubIdeaAssistant(),
|
||||
notifications);
|
||||
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_NotifiesStaffAndReturnsCreated()
|
||||
{
|
||||
var ideaService = new StubIdeaService { Idea = SampleIdea() };
|
||||
var notifications = new StubNotificationService();
|
||||
var controller = CreateController(ideaService, notifications);
|
||||
|
||||
var result = await controller.Create(
|
||||
new CreateIdeaRequest("Nowy pomysł", "Opis nowego pomysłu mieszkańca.", null),
|
||||
CancellationToken.None);
|
||||
|
||||
Assert.IsType<CreatedAtActionResult>(result.Result);
|
||||
Assert.Equal(1, notifications.StaffNotifications);
|
||||
}
|
||||
|
||||
private static IdeaResponse SampleIdea() => new(
|
||||
"idea:new",
|
||||
"Nowy pomysł",
|
||||
"Opis nowego pomysłu mieszkańca.",
|
||||
"user:tester",
|
||||
"draft",
|
||||
null,
|
||||
DateTimeOffset.UtcNow);
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using TarninaHub.Application.Abstractions;
|
||||
using TarninaHub.Application.Dtos;
|
||||
using TarninaHub.Controllers;
|
||||
using TarninaHub.Tests.Matchmaking;
|
||||
using TarninaHub.Tests.Stubs;
|
||||
|
||||
namespace TarninaHub.Tests.Controllers;
|
||||
|
||||
@@ -12,7 +13,10 @@ public class InnovationsControllerTests
|
||||
{
|
||||
private static InnovationsController CreateController(IInnovationService service)
|
||||
{
|
||||
var controller = new InnovationsController(service, new StubMatchmakingService());
|
||||
var controller = new InnovationsController(
|
||||
service,
|
||||
new StubMatchmakingService(),
|
||||
new StubTestFeedbackService());
|
||||
var identity = new ClaimsIdentity(
|
||||
[new Claim("sub", "user:tester"), new Claim("role", "admin")],
|
||||
authenticationType: "test");
|
||||
@@ -55,37 +59,4 @@ public class InnovationsControllerTests
|
||||
|
||||
Assert.IsType<CreatedAtActionResult>(result.Result);
|
||||
}
|
||||
|
||||
private sealed class StubInnovationService : IInnovationService
|
||||
{
|
||||
public Task<IReadOnlyList<InnovationResponse>> ListAsync(
|
||||
string? challengeId,
|
||||
string? stage,
|
||||
string? query,
|
||||
bool onlyPublished,
|
||||
CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<InnovationResponse>>([]);
|
||||
|
||||
public Task<InnovationResponse?> GetAsync(string innovationId, CancellationToken cancellationToken) =>
|
||||
Task.FromResult<InnovationResponse?>(null);
|
||||
|
||||
public Task<InnovationResponse> CreateAsync(
|
||||
InnovationRequest request,
|
||||
CancellationToken cancellationToken) => Task.FromResult(new InnovationResponse(
|
||||
"innovation:new",
|
||||
request.Title,
|
||||
request.Summary ?? string.Empty,
|
||||
request.Description ?? string.Empty,
|
||||
request.ChallengeId,
|
||||
request.Stage,
|
||||
request.Cost,
|
||||
request.Contact,
|
||||
request.Media ?? [],
|
||||
request.Published,
|
||||
DateTimeOffset.UtcNow));
|
||||
|
||||
public Task<InnovationResponse?> UpdateAsync(
|
||||
string innovationId,
|
||||
InnovationRequest request,
|
||||
CancellationToken cancellationToken) => Task.FromResult<InnovationResponse?>(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using TarninaHub.Application.Abstractions;
|
||||
using TarninaHub.Application.Dtos;
|
||||
using TarninaHub.Controllers;
|
||||
using TarninaHub.Tests.Matchmaking;
|
||||
using TarninaHub.Tests.Stubs;
|
||||
|
||||
namespace TarninaHub.Tests.Controllers;
|
||||
|
||||
@@ -12,7 +13,10 @@ public class SubmissionsControllerTests
|
||||
{
|
||||
private static SubmissionsController CreateController(ISubmissionService service, string role = "operator")
|
||||
{
|
||||
var controller = new SubmissionsController(service, new StubMatchmakingService());
|
||||
var controller = new SubmissionsController(
|
||||
service,
|
||||
new StubMatchmakingService(),
|
||||
new StubNotificationService());
|
||||
var identity = new ClaimsIdentity(
|
||||
[new Claim("sub", "user:tester"), new Claim("role", role)],
|
||||
authenticationType: "test");
|
||||
@@ -81,40 +85,4 @@ public class SubmissionsControllerTests
|
||||
|
||||
Assert.IsType<NotFoundResult>(result.Result);
|
||||
}
|
||||
|
||||
private sealed class StubSubmissionService : ISubmissionService
|
||||
{
|
||||
public SubmissionResponse? Current { get; set; }
|
||||
|
||||
public SubmissionResponse? UpdateResult { get; set; }
|
||||
|
||||
public string? LastRequestedStatus { get; private set; }
|
||||
|
||||
public Task<SubmissionResponse> CreateAsync(
|
||||
string authorId,
|
||||
CreateSubmissionRequest request,
|
||||
CancellationToken cancellationToken) => Task.FromResult(SampleSubmission("new"));
|
||||
|
||||
public Task<PagedResponse<SubmissionResponse>> ListAsync(
|
||||
string viewerId,
|
||||
bool isStaff,
|
||||
string? status,
|
||||
int page,
|
||||
int pageSize,
|
||||
CancellationToken cancellationToken) => Task.FromResult(
|
||||
new PagedResponse<SubmissionResponse>([], page, pageSize, 0));
|
||||
|
||||
public Task<SubmissionResponse?> GetAsync(string submissionId, CancellationToken cancellationToken) =>
|
||||
Task.FromResult(Current);
|
||||
|
||||
public Task<SubmissionResponse?> UpdateStatusAsync(
|
||||
string submissionId,
|
||||
string newStatus,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
LastRequestedStatus = newStatus;
|
||||
|
||||
return Task.FromResult(UpdateResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user