Skip to content

Commit

Permalink
Merge pull request #175 from SaintAngeLs/comments_test
Browse files Browse the repository at this point in the history
(#148) Comments tests new PR
  • Loading branch information
SaintAngeLs authored May 25, 2024
2 parents b96ddb6 + 060d22a commit 63b228b
Show file tree
Hide file tree
Showing 586 changed files with 265,181 additions and 7 deletions.
17 changes: 13 additions & 4 deletions .github/workflows/build_microservices.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: .NET MiniSpace Microservice build & integration

on: [push, pull_request]
on:
push:
branches:
- '*'
pull_request:

jobs:
build-and-test:
Expand All @@ -12,12 +16,16 @@ jobs:
- project: 'MiniSpace.APIGateway/src/MiniSpace.APIGateway'
- project: 'MiniSpace.Services.Identity/src/MiniSpace.Services.Identity.Api'
test_dir: 'MiniSpace.Services.Identity/tests'
- project: 'MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Api'
test_dir: 'MiniSpace.Services.Comments/tests'
- project: 'MiniSpace.Web/src/MiniSpace.Web'
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup .NET 8.0.x
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
dotnet-version: '8.0.x'

Expand Down Expand Up @@ -46,4 +54,5 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
files: '**/coverage.cobertura.xml'
slug: SaintAngeLs/p_software_engineering_2

commit: ${{ github.event.pull_request.base.sha }}
# fail_ci_if_error: true
18 changes: 18 additions & 0 deletions MiniSpace.Services.Comments/MiniSpace.Services.Comments.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Infrastructure", "src\MiniSpace.Services.Comments.Infrastructure\MiniSpace.Services.Comments.Infrastructure.csproj", "{85941F19-E28E-467B-A338-7D358D32CFC8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Application.UnitTests", "tests\MiniSpace.Services.Comments.Application.UnitTests\MiniSpace.Services.Comments.Application.UnitTests.csproj", "{63DF71AF-1D31-4A4D-8127-9B8EB359889D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Core.UnitTests", "tests\MiniSpace.Services.Comments.Core.UnitTests\MiniSpace.Services.Comments.Core.UnitTests.csproj", "{C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniSpace.Services.Comments.Infrastructure.UnitTests", "tests\MiniSpace.Services.Comments.Infrastructure.UnitTests\MiniSpace.Services.Comments.Infrastructure.UnitTests.csproj", "{81D7C37A-34BE-43BB-9709-0A866D67F8EB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,6 +39,18 @@ Global
{85941F19-E28E-467B-A338-7D358D32CFC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85941F19-E28E-467B-A338-7D358D32CFC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85941F19-E28E-467B-A338-7D358D32CFC8}.Release|Any CPU.Build.0 = Release|Any CPU
{63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63DF71AF-1D31-4A4D-8127-9B8EB359889D}.Release|Any CPU.Build.0 = Release|Any CPU
{C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C0DE6BE3-4EE5-4DB5-A170-57351B19AAF8}.Release|Any CPU.Build.0 = Release|Any CPU
{81D7C37A-34BE-43BB-9709-0A866D67F8EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{81D7C37A-34BE-43BB-9709-0A866D67F8EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81D7C37A-34BE-43BB-9709-0A866D67F8EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81D7C37A-34BE-43BB-9709-0A866D67F8EB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using MiniSpace.Services.Comments.Core.Entities;

namespace MiniSpace.Services.Comments.Application.Dto
{
[ExcludeFromCodeCoverage]
public class CommentDto
{
public Guid Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace MiniSpace.Services.Comments.Application.Dto
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Application.Dto
{
[ExcludeFromCodeCoverage]
public class PageableDto
{
public int Page { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Application.Dto
{
[ExcludeFromCodeCoverage]
public class SortDto
{
public IEnumerable<string> SortBy { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace MiniSpace.Services.Comments.Application.Wrappers
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Application.Wrappers
{
[ExcludeFromCodeCoverage]
public class PagedResponse<T> : Response<T>
{
public int TotalPages { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace MiniSpace.Services.Comments.Application.Wrappers
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Application.Wrappers
{
[ExcludeFromCodeCoverage]
public class Response<T>
{
public T Content { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using MiniSpace.Services.Comments.Application;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("MiniSpace.Services.Comments.Application.UnitTests")]

namespace MiniSpace.Services.Comments.Infrastructure.Contexts
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using Convey.MessageBrokers;
using Convey.MessageBrokers.Outbox;
using Convey.Types;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Decorators
{
[ExcludeFromCodeCoverage]
[Decorator]
internal sealed class OutboxCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
where TCommand : class, ICommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using Convey.MessageBrokers;
using Convey.MessageBrokers.Outbox;
using Convey.Types;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Decorators
{
[ExcludeFromCodeCoverage]
[Decorator]
internal sealed class OutboxEventHandlerDecorator<TEvent> : IEventHandler<TEvent>
where TEvent : class, IEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using MiniSpace.Services.Comments.Application.Events.Rejected;
using MiniSpace.Services.Comments.Application.Exceptions;
using MiniSpace.Services.Comments.Core.Exceptions;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Exceptions
{
[ExcludeFromCodeCoverage]
internal sealed class ExceptionToMessageMapper : IExceptionToMessageMapper
{
public object Map(Exception exception, object message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using Convey;
using Convey.WebApi.Exceptions;
Expand All @@ -7,6 +8,7 @@

namespace MiniSpace.Services.Comments.Infrastructure.Exceptions
{
[ExcludeFromCodeCoverage]
internal sealed class ExceptionToResponseMapper : IExceptionToResponseMapper
{
private static readonly ConcurrentDictionary<Type, string> Codes = new ConcurrentDictionary<Type, string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using Convey.Logging.CQRS;
using Microsoft.Extensions.DependencyInjection;
using MiniSpace.Services.Comments.Application.Commands;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Logging
{
[ExcludeFromCodeCoverage]
internal static class Extensions
{
public static IConveyBuilder AddHandlersLogging(this IConveyBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using MiniSpace.Services.Comments.Application.Commands;
using MiniSpace.Services.Comments.Application.Events;
using MiniSpace.Services.Comments.Application.Events.External;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Logging
{
[ExcludeFromCodeCoverage]
internal sealed class MessageToLogTemplateMapper : IMessageToLogTemplateMapper
{
private static IReadOnlyDictionary<Type, HandlerLogTemplate> MessageTemplates
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Convey.Types;
using MiniSpace.Services.Comments.Core.Entities;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Documents
{
[ExcludeFromCodeCoverage]
public class CommentDocument : IIdentifiable<Guid>
{
public Guid Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using MiniSpace.Services.Comments.Application.Dto;
using MiniSpace.Services.Comments.Core.Entities;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Documents
{
[ExcludeFromCodeCoverage]
public static class Extensions
{
public static Comment AsEntity(this CommentDocument document)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Convey.Types;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Documents
{
[ExcludeFromCodeCoverage]
public class StudentDocument : IIdentifiable<Guid>
{
public Guid Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using MiniSpace.Services.Comments.Infrastructure.Mongo.Documents;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Repositories
{
[ExcludeFromCodeCoverage]
public class CommentMongoRepository : ICommentRepository
{
private readonly IMongoRepository<CommentDocument, Guid> _repository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand All @@ -10,6 +11,7 @@

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Repositories
{
[ExcludeFromCodeCoverage]
public static class Extensions
{
private static readonly FilterDefinitionBuilder<CommentDocument> FilterDefinitionBuilder = Builders<CommentDocument>.Filter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using MiniSpace.Services.Comments.Core.Entities;
using MiniSpace.Services.Comments.Core.Repositories;
using MiniSpace.Services.Comments.Infrastructure.Mongo.Documents;
using System.Diagnostics.CodeAnalysis;

namespace MiniSpace.Services.Comments.Infrastructure.Mongo.Repositories
{
[ExcludeFromCodeCoverage]
public class StudentMongoRepository : IStudentRepository
{
private readonly IMongoRepository<StudentDocument, Guid> _repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using Microsoft.Extensions.Logging;
using OpenTracing;
using MiniSpace.Services.Comments.Application.Services;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("MiniSpace.Services.Comments.Infrastructure.UnitTests")]
namespace MiniSpace.Services.Comments.Infrastructure.Services
{
internal sealed class MessageBroker : IMessageBroker
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Xunit;
using Moq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MiniSpace.Services.Comments.Application.Exceptions;
using MiniSpace.Services.Comments.Application.Services;
using MiniSpace.Services.Comments.Core.Entities;
using MiniSpace.Services.Comments.Core.Repositories;
using MiniSpace.Services.Comments.Application.Commands.Handlers;
using MiniSpace.Services.Comments.Application.Commands;
using MiniSpace.Services.Comments.Infrastructure.Contexts;
using System.Threading;
using FluentAssertions;

namespace MiniSpace.Services.Comments.Application.UnitTests.Commands.Handlers
{
public class AddLikeHandlerTest
{
private readonly AddLikeHandler _addLikeHandler;
private readonly Mock<ICommentRepository> _commentRepositoryMock;
private readonly Mock<IMessageBroker> _messageBrokerMock;
private readonly Mock<IAppContext> _appContextMock;

public AddLikeHandlerTest()
{
_commentRepositoryMock = new Mock<ICommentRepository>();
_messageBrokerMock = new Mock<IMessageBroker>();
_appContextMock = new Mock<IAppContext>();
_addLikeHandler = new AddLikeHandler(_commentRepositoryMock.Object, _appContextMock.Object, _messageBrokerMock.Object);
}

[Fact]
public async Task HandleAsync_WithValidCommentAndAuthorised_ShouldAddLike()
{
// Arrange
var commentId = Guid.NewGuid();
var comand = new AddLike(commentId);

var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), CommentContext.Post, Guid.NewGuid(), "Adam", Guid.NewGuid(), "text", DateTime.Now);

var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "", true, new Dictionary<string, string>());

_appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext);
_commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)).ReturnsAsync(comment);

var cancelationToken = new CancellationToken();

// Act
await _addLikeHandler.HandleAsync(comand, cancelationToken);

// Assert
_commentRepositoryMock.Verify(repo => repo.UpdateAsync(comment), Times.Once());
}

[Fact]
public async Task HandleAsync_WithInvalidComment_ShouldThrowCommentNotFoundExeption()
{
// Arrange
var commentId = Guid.NewGuid();
var comand = new AddLike(commentId);
_commentRepositoryMock.Setup(repo => repo.GetAsync(comand.CommentId)).ReturnsAsync((Comment)null);
var cancelationToken = new CancellationToken();

// Act & Assert
Func<Task> act = async () => await _addLikeHandler.HandleAsync(comand, cancelationToken);
await act.Should().ThrowAsync<CommentNotFoundException>();
}

[Fact]
public async Task HandleAsync_WithNonPermitedIdentity_ShouldThrowUnauthorizedCommentAccessException()
{
// Arrange
var commentId = Guid.NewGuid();
var comand = new AddLike(commentId);

var comment = Comment.Create(new AggregateId(commentId), Guid.NewGuid(), CommentContext.Post, Guid.NewGuid(), "Adam", Guid.NewGuid(), "text", DateTime.Now);

var identityContext = new IdentityContext(Guid.NewGuid().ToString(), "", false, new Dictionary<string, string>());

_appContextMock.Setup(ctx => ctx.Identity).Returns(identityContext);
_commentRepositoryMock.Setup(repo => repo.GetAsync(comment.Id)).ReturnsAsync(comment);

var cancelationToken = new CancellationToken();

// Act & Assert
Func<Task> act = async () => await _addLikeHandler.HandleAsync(comand, cancelationToken);
await act.Should().ThrowAsync<UnauthorizedCommentAccessException>();

}
}
}
Loading

0 comments on commit 63b228b

Please sign in to comment.