-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from SaintAngeLs/reactions_service_tests
(#169) Reactions service tests
- Loading branch information
Showing
9 changed files
with
565 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
...tions/src/MiniSpace.Services.Reactions.Application/Events/Rejected/AddReactionRejected.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...ns/src/MiniSpace.Services.Reactions.Application/Events/Rejected/DeleteReactionRejected.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
MiniSpace.Services.Reactions/src/MiniSpace.Services.Reactions.Core/Entities/AggregateRoot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...ces.Reactions/src/MiniSpace.Services.Reactions.Infrastructure/Contexts/IdentityContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...es.Reactions/src/MiniSpace.Services.Reactions.Infrastructure/Services/DateTimeProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...ervices.Reactions/src/MiniSpace.Services.Reactions.Infrastructure/Services/EventMapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
407 changes: 407 additions & 0 deletions
407
...e.Services.Reactions.Application.UnitTests/Commands/Handlers/ChangeReactionHandlerTest.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using Xunit; | ||
using Moq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using MiniSpace.Services.Reactions.Application.Events; | ||
using MiniSpace.Services.Reactions.Application.Exceptions; | ||
using MiniSpace.Services.Reactions.Application.Services; | ||
using MiniSpace.Services.Reactions.Core.Entities; | ||
using MiniSpace.Services.Reactions.Core.Repositories; | ||
using MiniSpace.Services.Reactions.Application.Commands.Handlers; | ||
using MiniSpace.Services.Reactions.Application.Commands; | ||
using MiniSpace.Services.Reactions.Infrastructure.Contexts; | ||
using Convey.CQRS.Commands; | ||
using System.Threading; | ||
using System.Security.Claims; | ||
using FluentAssertions; | ||
using MiniSpace.Services.Reactions.Core.Exceptions; | ||
using Microsoft.OpenApi.Extensions; | ||
using System.Security.Policy; | ||
|
||
namespace MiniSpace.Services.Reactions.Application.UnitTests.Commands.Handlers { | ||
public class DeleteReactionHandlerTest { | ||
private readonly DeleteReactionHandler _deleteReactionHandler; | ||
private readonly Mock<IReactionRepository> _reactionRepositoryMock; | ||
private readonly Mock<IMessageBroker> _messageBrokerMock; | ||
private readonly Mock<IAppContext> _appContextMock; | ||
|
||
public DeleteReactionHandlerTest() { | ||
_messageBrokerMock = new(); | ||
_appContextMock = new(); | ||
_reactionRepositoryMock = new(); | ||
_deleteReactionHandler = new DeleteReactionHandler( | ||
_reactionRepositoryMock.Object, | ||
_appContextMock.Object, | ||
_messageBrokerMock.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_WithValidParameters_ShouldNotThrowException() { | ||
// Arrange | ||
var eventId = Guid.NewGuid(); | ||
var postId = Guid.NewGuid(); | ||
var reactionId = Guid.NewGuid(); | ||
var studentId = Guid.NewGuid(); | ||
var cancelationToken = new CancellationToken(); | ||
|
||
var contextId = studentId; | ||
var command = new DeleteReaction(reactionId); | ||
|
||
var identity = new IdentityContext(contextId.ToString(), "user", true, default); | ||
_appContextMock.Setup(ctx => ctx.Identity).Returns(identity); | ||
|
||
var reaction = new Reaction(reactionId, studentId, "full name", ReactionType.HateIt, | ||
Guid.NewGuid(), ReactionContentType.Event); | ||
|
||
_reactionRepositoryMock.Setup(repo => repo.GetAsync(reactionId)).ReturnsAsync(reaction); | ||
_appContextMock.Setup(cxt => cxt.Identity).Returns(identity); | ||
|
||
// Act & Assert | ||
Func<Task> act = async () => | ||
await _deleteReactionHandler.HandleAsync(command, cancelationToken); | ||
|
||
await act.Should().NotThrowAsync(); | ||
|
||
_messageBrokerMock.Verify(broker => broker.PublishAsync(It.IsAny<ReactionDeleted>()), Times.Exactly(1)); | ||
_reactionRepositoryMock.Verify(repo => repo.DeleteAsync(reactionId), Times.Exactly(1)); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_WithReactionNotInRepository_ShouldThrowReactionNotFoundException() { | ||
// Arrange | ||
var eventId = Guid.NewGuid(); | ||
var postId = Guid.NewGuid(); | ||
var reactionId = Guid.NewGuid(); | ||
var studentId = Guid.NewGuid(); | ||
var cancelationToken = new CancellationToken(); | ||
|
||
var contextId = studentId; | ||
var command = new DeleteReaction(reactionId); | ||
|
||
var reaction = new Reaction(reactionId, studentId, "full name", ReactionType.HateIt, | ||
Guid.NewGuid(), ReactionContentType.Event); | ||
|
||
_reactionRepositoryMock.Setup(repo => repo.GetAsync(reactionId)).ReturnsAsync((Reaction)null); | ||
|
||
var identity = new IdentityContext(contextId.ToString(), "user", true, default); | ||
_appContextMock.Setup(ctx => ctx.Identity).Returns(identity); | ||
|
||
// Act & Assert | ||
Func<Task> act = async () => | ||
await _deleteReactionHandler.HandleAsync(command, cancelationToken); | ||
|
||
await Assert.ThrowsAsync<ReactionNotFoundException>(act); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_WithForeignOwner_ShouldThrowUnauthorizedReactionAccessException() { | ||
// Arrange | ||
var eventId = Guid.NewGuid(); | ||
var postId = Guid.NewGuid(); | ||
var reactionId = Guid.NewGuid(); | ||
var studentId = Guid.NewGuid(); | ||
var cancelationToken = new CancellationToken(); | ||
|
||
Guid contextId; | ||
do { | ||
contextId = Guid.NewGuid(); | ||
} while (contextId == studentId); | ||
var command = new DeleteReaction(reactionId); | ||
|
||
var reaction = new Reaction(reactionId, studentId, "full name", ReactionType.HateIt, | ||
Guid.NewGuid(), ReactionContentType.Event); | ||
|
||
_reactionRepositoryMock.Setup(repo => repo.GetAsync(reactionId)).ReturnsAsync(reaction); | ||
|
||
var identity = new IdentityContext(contextId.ToString(), "user", true, default); | ||
_appContextMock.Setup(ctx => ctx.Identity).Returns(identity); | ||
|
||
// Act & Assert | ||
Func<Task> act = async () => | ||
await _deleteReactionHandler.HandleAsync(command, cancelationToken); | ||
|
||
await Assert.ThrowsAsync<UnauthorizedReactionAccessException>(act); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters