Skip to content

Commit

Permalink
Merge pull request #215 from SaintAngeLs/reactions_service_tests
Browse files Browse the repository at this point in the history
(#169) Reactions service tests
  • Loading branch information
olegkiprik committed May 28, 2024
2 parents d6f038b + 8571611 commit c790eb6
Show file tree
Hide file tree
Showing 9 changed files with 565 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Convey.CQRS.Events;
using Microsoft.AspNetCore.Mvc.RazorPages;
using MiniSpace.Services.Reactions.Core.Entities;

namespace MiniSpace.Services.Reactions.Application.Events.Rejected
{
[ExcludeFromCodeCoverage]
public class AddReactionRejected(Guid userId, string reason, string code) : IRejectedEvent
{
public Guid UserId { get; } = userId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Convey.CQRS.Events;
using Microsoft.AspNetCore.Mvc.RazorPages;
using MiniSpace.Services.Reactions.Core.Entities;

namespace MiniSpace.Services.Reactions.Application.Events.Rejected
{
[ExcludeFromCodeCoverage]
public class DeleteReactionRejected(Guid userId, string reason, string code) : IRejectedEvent
{
public Guid UserId { get; } = userId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Diagnostics.CodeAnalysis;
using MiniSpace.Services.Reactions.Core.Events;

namespace MiniSpace.Services.Reactions.Core.Entities
{
[ExcludeFromCodeCoverage]
public abstract class AggregateRoot
{
private readonly List<IDomainEvent> _events = new List<IDomainEvent>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using MiniSpace.Services.Reactions.Application;

[assembly: InternalsVisibleTo("MiniSpace.Services.Reactions.Application.UnitTests")]
namespace MiniSpace.Services.Reactions.Infrastructure.Contexts
{
[ExcludeFromCodeCoverage]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Diagnostics.CodeAnalysis;
using MiniSpace.Services.Reactions.Application.Services;

namespace MiniSpace.Services.Reactions.Infrastructure.Services
{
[ExcludeFromCodeCoverage]
internal sealed class DateTimeProvider : IDateTimeProvider
{
public DateTime Now => DateTime.UtcNow;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using Convey.CQRS.Events;
using MiniSpace.Services.Reactions.Application.Services;
using MiniSpace.Services.Reactions.Core;
using MiniSpace.Services.Reactions.Core.Events;

namespace MiniSpace.Services.Reactions.Infrastructure.Services
{
[ExcludeFromCodeCoverage]
public class EventMapper : IEventMapper
{
public IEnumerable<IEvent> MapAll(IEnumerable<IDomainEvent> events)
Expand Down

Large diffs are not rendered by default.

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,23 @@ public void AggregateId_CreatedTwiceSameGuid_ShouldBeSame()
// Assert
Assert.Equal(id1.Value, id2.Value);
}

[Fact]
public void Equals_WithNullOther_ShouldBeFalse() {
var id = new AggregateId(Guid.NewGuid());
Assert.False(id.Equals(null));
}

[Fact]
public void AggregateId_CreateFromEmpty_ShouldThrowInvalidAggregateIdException() {
Assert.Throws<InvalidAggregateIdException>(() => new AggregateId(Guid.Empty));
}

[Fact]
public void Equals_WithDifferentTypes_ShouldBeFalse() {
var id = new AggregateId(Guid.NewGuid());
int x = 2;
Assert.False(id.Equals(x));
}
}
}

0 comments on commit c790eb6

Please sign in to comment.