diff --git a/api/tests/Feature.CitizenReports.Comments.UnitTests/Endpoints/DeleteEndpointTests.cs b/api/tests/Feature.CitizenReports.Comments.UnitTests/Endpoints/DeleteEndpointTests.cs index e33640321..e3860c2c0 100644 --- a/api/tests/Feature.CitizenReports.Comments.UnitTests/Endpoints/DeleteEndpointTests.cs +++ b/api/tests/Feature.CitizenReports.Comments.UnitTests/Endpoints/DeleteEndpointTests.cs @@ -1,23 +1,54 @@ +using System.Security.Claims; using Feature.CitizenReports.Comments.Delete; using Feature.CitizenReports.Comments.Specifications; +using Microsoft.AspNetCore.Authorization; using Vote.Monitor.Domain.Entities.CitizenReportCommentAggregate; namespace Feature.CitizenReports.Comments.UnitTests.Endpoints; public class DeleteEndpointTests { + private readonly IAuthorizationService _authorizationService; private readonly IRepository _repository; private readonly Endpoint _endpoint; public DeleteEndpointTests() { + _authorizationService = Substitute.For(); _repository = Substitute.For>(); - _endpoint = Factory.Create(_repository); + _endpoint = Factory.Create(_authorizationService, _repository); + } + + [Fact] + public async Task ShouldReturnNotFound_WhenNotAuthorized() + { + _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(AuthorizationResult.Failed()); + + var result = await _endpoint.ExecuteAsync(new Request + { + ElectionRoundId = Guid.NewGuid(), + CitizenReportId = Guid.NewGuid(), + Id = Guid.NewGuid(), + UserId = Guid.NewGuid() + }, CancellationToken.None); + + result + .Should().BeOfType>() + .Which + .Result.Should().BeOfType(); + + await _repository.DidNotReceive().DeleteAsync(Arg.Any()); } [Fact] public async Task ShouldReturnNotFound_WhenCommentDoesNotExist() { + _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(AuthorizationResult.Success()); + _repository.FirstOrDefaultAsync(Arg.Any()) .ReturnsNull(); @@ -43,6 +74,10 @@ public async Task ShouldDeleteComment_WhenUserIsTheAuthor() var userId = Guid.NewGuid(); var fakeComment = new CitizenReportCommentFaker(authorId: userId).Generate(); + _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(AuthorizationResult.Success()); + _repository.FirstOrDefaultAsync(Arg.Any()) .Returns(fakeComment); diff --git a/api/tests/Feature.CitizenReports.Comments.UnitTests/Endpoints/UpdateEndpointTests.cs b/api/tests/Feature.CitizenReports.Comments.UnitTests/Endpoints/UpdateEndpointTests.cs index 5f5a9278d..1e33ed2f6 100644 --- a/api/tests/Feature.CitizenReports.Comments.UnitTests/Endpoints/UpdateEndpointTests.cs +++ b/api/tests/Feature.CitizenReports.Comments.UnitTests/Endpoints/UpdateEndpointTests.cs @@ -1,23 +1,55 @@ +using System.Security.Claims; using Feature.CitizenReports.Comments.Specifications; using Feature.CitizenReports.Comments.Update; +using Microsoft.AspNetCore.Authorization; using Vote.Monitor.Domain.Entities.CitizenReportCommentAggregate; namespace Feature.CitizenReports.Comments.UnitTests.Endpoints; public class UpdateEndpointTests { + private readonly IAuthorizationService _authorizationService; private readonly IRepository _repository; private readonly Endpoint _endpoint; public UpdateEndpointTests() { + _authorizationService = Substitute.For(); _repository = Substitute.For>(); - _endpoint = Factory.Create(_repository); + _endpoint = Factory.Create(_authorizationService, _repository); + } + + [Fact] + public async Task ShouldReturnNotFound_WhenNotAuthorized() + { + _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(AuthorizationResult.Failed()); + + var result = await _endpoint.ExecuteAsync(new Request + { + ElectionRoundId = Guid.NewGuid(), + CitizenReportId = Guid.NewGuid(), + Id = Guid.NewGuid(), + UserId = Guid.NewGuid(), + Text = "updated" + }, CancellationToken.None); + + result + .Should().BeOfType, NotFound>>() + .Which + .Result.Should().BeOfType(); + + await _repository.DidNotReceive().UpdateAsync(Arg.Any()); } [Fact] public async Task ShouldReturnNotFound_WhenCommentDoesNotExist() { + _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(AuthorizationResult.Success()); + _repository.FirstOrDefaultAsync(Arg.Any()) .ReturnsNull(); @@ -44,6 +76,10 @@ public async Task ShouldUpdateComment_WhenUserIsTheAuthor() var userId = Guid.NewGuid(); var fakeComment = new CitizenReportCommentFaker(authorId: userId).Generate(); + _authorizationService.AuthorizeAsync(Arg.Any(), Arg.Any(), + Arg.Any>()) + .Returns(AuthorizationResult.Success()); + _repository.FirstOrDefaultAsync(Arg.Any()) .Returns(fakeComment);