mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-06-30 02:31:08 +00:00
Fix tests
This commit is contained in:
parent
b36aaff112
commit
5214c7c4ed
@ -2,6 +2,7 @@ using System;
|
||||
using System.Linq;
|
||||
using CleanArchitecture.Application.Queries.Users.GetAll;
|
||||
using CleanArchitecture.Domain.Entities;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.Domain.Interfaces.Repositories;
|
||||
using MockQueryable.Moq;
|
||||
using Moq;
|
||||
@ -28,7 +29,9 @@ public sealed class GetAllUsersTestFixture : QueryHandlerBaseFixture
|
||||
ExistingUserId,
|
||||
"max@mustermann.com",
|
||||
"Max",
|
||||
"Mustermann"));
|
||||
"Mustermann",
|
||||
"Password",
|
||||
UserRole.User));
|
||||
|
||||
var query = new[] { user.Object }.AsQueryable().BuildMock();
|
||||
|
||||
@ -44,7 +47,9 @@ public sealed class GetAllUsersTestFixture : QueryHandlerBaseFixture
|
||||
ExistingUserId,
|
||||
"max@mustermann.com",
|
||||
"Max",
|
||||
"Mustermann"));
|
||||
"Mustermann",
|
||||
"Password",
|
||||
UserRole.User));
|
||||
|
||||
user.Object.Delete();
|
||||
|
||||
|
@ -2,6 +2,7 @@ using System;
|
||||
using System.Linq;
|
||||
using CleanArchitecture.Application.Queries.Users.GetUserById;
|
||||
using CleanArchitecture.Domain.Entities;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.Domain.Interfaces.Repositories;
|
||||
using MockQueryable.Moq;
|
||||
using Moq;
|
||||
@ -28,7 +29,9 @@ public sealed class GetUserByIdTestFixture : QueryHandlerBaseFixture
|
||||
ExistingUserId,
|
||||
"max@mustermann.com",
|
||||
"Max",
|
||||
"Mustermann"));
|
||||
"Mustermann",
|
||||
"Password",
|
||||
UserRole.User));
|
||||
|
||||
var query = new[] { user.Object }.AsQueryable().BuildMock();
|
||||
|
||||
@ -44,7 +47,9 @@ public sealed class GetUserByIdTestFixture : QueryHandlerBaseFixture
|
||||
ExistingUserId,
|
||||
"max@mustermann.com",
|
||||
"Max",
|
||||
"Mustermann"));
|
||||
"Mustermann",
|
||||
"Password",
|
||||
UserRole.User));
|
||||
|
||||
user.Object.Delete();
|
||||
|
||||
|
@ -39,7 +39,8 @@ public sealed class UserService : IUserService
|
||||
userId,
|
||||
user.Email,
|
||||
user.Surname,
|
||||
user.GivenName));
|
||||
user.GivenName,
|
||||
user.Password));
|
||||
|
||||
return userId;
|
||||
}
|
||||
@ -50,7 +51,8 @@ public sealed class UserService : IUserService
|
||||
user.Id,
|
||||
user.Email,
|
||||
user.Surname,
|
||||
user.GivenName));
|
||||
user.GivenName,
|
||||
user.Role));
|
||||
}
|
||||
|
||||
public async Task DeleteUserAsync(Guid userId)
|
||||
|
@ -19,7 +19,8 @@ public sealed class CreateUserCommandHandlerTests
|
||||
Guid.NewGuid(),
|
||||
"test@email.com",
|
||||
"Test",
|
||||
"Email");
|
||||
"Email",
|
||||
"SomePassword");
|
||||
|
||||
_fixture.CommandHandler.Handle(command, default).Wait();
|
||||
|
||||
@ -38,7 +39,8 @@ public sealed class CreateUserCommandHandlerTests
|
||||
user.Id,
|
||||
"test@email.com",
|
||||
"Test",
|
||||
"Email");
|
||||
"Email",
|
||||
"SomePassword");
|
||||
|
||||
_fixture.CommandHandler.Handle(command, default).Wait();
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using CleanArchitecture.Domain.Commands.Users.CreateUser;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.Domain.Interfaces.Repositories;
|
||||
using Moq;
|
||||
|
||||
@ -27,7 +28,9 @@ public sealed class CreateUserCommandTestFixture : CommandHandlerFixtureBase
|
||||
Guid.NewGuid(),
|
||||
"max@mustermann.com",
|
||||
"Max",
|
||||
"Mustermann");
|
||||
"Mustermann",
|
||||
"Password",
|
||||
UserRole.User);
|
||||
|
||||
UserRepository
|
||||
.Setup(x => x.GetByIdAsync(It.Is<Guid>(y => y == user.Id)))
|
||||
|
@ -112,10 +112,12 @@ public sealed class CreateUserCommandValidationTests :
|
||||
Guid? userId = null,
|
||||
string? email = null,
|
||||
string? surName = null,
|
||||
string? givenName = null) =>
|
||||
string? givenName = null,
|
||||
string? password = null) =>
|
||||
new (
|
||||
userId ?? Guid.NewGuid(),
|
||||
email ?? "test@email.com",
|
||||
surName ?? "test",
|
||||
givenName ?? "email");
|
||||
givenName ?? "email",
|
||||
password ?? "some password");
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using CleanArchitecture.Domain.Commands.Users.DeleteUser;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.Domain.Interfaces.Repositories;
|
||||
using Moq;
|
||||
|
||||
@ -18,7 +19,8 @@ public sealed class DeleteUserCommandTestFixture : CommandHandlerFixtureBase
|
||||
Bus.Object,
|
||||
UnitOfWork.Object,
|
||||
NotificationHandler.Object,
|
||||
UserRepository.Object);
|
||||
UserRepository.Object,
|
||||
User.Object);
|
||||
}
|
||||
|
||||
public Entities.User SetupUser()
|
||||
@ -27,7 +29,9 @@ public sealed class DeleteUserCommandTestFixture : CommandHandlerFixtureBase
|
||||
Guid.NewGuid(),
|
||||
"max@mustermann.com",
|
||||
"Max",
|
||||
"Mustermann");
|
||||
"Mustermann",
|
||||
"Password",
|
||||
UserRole.User);
|
||||
|
||||
UserRepository
|
||||
.Setup(x => x.GetByIdAsync(It.Is<Guid>(y => y == user.Id)))
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CleanArchitecture.Domain.Commands.Users.UpdateUser;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.Domain.Errors;
|
||||
using CleanArchitecture.Domain.Events.User;
|
||||
using Xunit;
|
||||
@ -20,7 +21,8 @@ public sealed class UpdateUserCommandHandlerTests
|
||||
user.Id,
|
||||
"test@email.com",
|
||||
"Test",
|
||||
"Email");
|
||||
"Email",
|
||||
UserRole.User);
|
||||
|
||||
await _fixture.CommandHandler.Handle(command, default);
|
||||
|
||||
@ -39,7 +41,8 @@ public sealed class UpdateUserCommandHandlerTests
|
||||
Guid.NewGuid(),
|
||||
"test@email.com",
|
||||
"Test",
|
||||
"Email");
|
||||
"Email",
|
||||
UserRole.User);
|
||||
|
||||
await _fixture.CommandHandler.Handle(command, default);
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using CleanArchitecture.Domain.Commands.Users.UpdateUser;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.Domain.Interfaces.Repositories;
|
||||
using Moq;
|
||||
|
||||
@ -18,7 +19,8 @@ public sealed class UpdateUserCommandTestFixture : CommandHandlerFixtureBase
|
||||
Bus.Object,
|
||||
UnitOfWork.Object,
|
||||
NotificationHandler.Object,
|
||||
UserRepository.Object);
|
||||
UserRepository.Object,
|
||||
User.Object);
|
||||
}
|
||||
|
||||
public Entities.User SetupUser()
|
||||
@ -27,7 +29,9 @@ public sealed class UpdateUserCommandTestFixture : CommandHandlerFixtureBase
|
||||
Guid.NewGuid(),
|
||||
"max@mustermann.com",
|
||||
"Max",
|
||||
"Mustermann");
|
||||
"Mustermann",
|
||||
"Password",
|
||||
UserRole.User);
|
||||
|
||||
UserRepository
|
||||
.Setup(x => x.GetByIdAsync(It.Is<Guid>(y => y == user.Id)))
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using CleanArchitecture.Domain.Commands.Users.UpdateUser;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.Domain.Errors;
|
||||
using Xunit;
|
||||
|
||||
@ -112,10 +113,12 @@ public sealed class UpdateUserCommandValidationTests :
|
||||
Guid? userId = null,
|
||||
string? email = null,
|
||||
string? surName = null,
|
||||
string? givenName = null) =>
|
||||
string? givenName = null,
|
||||
UserRole? role = null) =>
|
||||
new (
|
||||
userId ?? Guid.NewGuid(),
|
||||
email ?? "test@email.com",
|
||||
surName ?? "test",
|
||||
givenName ?? "email");
|
||||
givenName ?? "email",
|
||||
role ?? UserRole.User);
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.Domain.Interfaces;
|
||||
using CleanArchitecture.Domain.Notifications;
|
||||
using Moq;
|
||||
@ -11,12 +12,17 @@ public class CommandHandlerFixtureBase
|
||||
protected Mock<IMediatorHandler> Bus { get; }
|
||||
protected Mock<IUnitOfWork> UnitOfWork { get; }
|
||||
protected Mock<DomainNotificationHandler> NotificationHandler { get; }
|
||||
protected Mock<IUser> User { get; }
|
||||
|
||||
protected CommandHandlerFixtureBase()
|
||||
{
|
||||
Bus = new Mock<IMediatorHandler>();
|
||||
UnitOfWork = new Mock<IUnitOfWork>();
|
||||
NotificationHandler = new Mock<DomainNotificationHandler>();
|
||||
User = new Mock<IUser>();
|
||||
|
||||
User.Setup(x => x.GetUserId()).Returns(Guid.NewGuid());
|
||||
User.Setup(x => x.GetUserRole()).Returns(UserRole.Admin);
|
||||
|
||||
UnitOfWork.Setup(unit => unit.CommitAsync()).ReturnsAsync(true);
|
||||
}
|
||||
|
@ -47,8 +47,14 @@ public sealed class DeleteUserCommandHandler : CommandHandlerBase,
|
||||
return;
|
||||
}
|
||||
|
||||
if (_user.GetUserId() != request.UserId || _user.GetUserRole() != UserRole.Admin)
|
||||
if (_user.GetUserId() != request.UserId && _user.GetUserRole() != UserRole.Admin)
|
||||
{
|
||||
await NotifyAsync(
|
||||
new DomainNotification(
|
||||
request.MessageType,
|
||||
$"No permission to delete user {request.UserId}",
|
||||
ErrorCodes.Unauthorized));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -47,8 +47,14 @@ public sealed class UpdateUserCommandHandler : CommandHandlerBase,
|
||||
return;
|
||||
}
|
||||
|
||||
if (_user.GetUserId() != request.UserId || _user.GetUserRole() != UserRole.Admin)
|
||||
if (_user.GetUserId() != request.UserId && _user.GetUserRole() != UserRole.Admin)
|
||||
{
|
||||
await NotifyAsync(
|
||||
new DomainNotification(
|
||||
request.MessageType,
|
||||
$"No permission to update user {request.UserId}",
|
||||
ErrorCodes.Unauthorized));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -4,4 +4,5 @@ public static class ErrorCodes
|
||||
{
|
||||
public const string CommitFailed = "COMMIT_FAILED";
|
||||
public const string ObjectNotFound = "OBJECT_NOT_FOUND";
|
||||
public const string Unauthorized = "UNAUTHORIZED";
|
||||
}
|
@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using CleanArchitecture.Application.ViewModels.Users;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.IntegrationTests.Extensions;
|
||||
using CleanArchitecture.IntegrationTests.Fixtures;
|
||||
using FluentAssertions;
|
||||
@ -42,7 +43,11 @@ public sealed class UserControllerTests : IClassFixture<UserTestFixture>
|
||||
[Fact, Priority(5)]
|
||||
public async Task Should_Create_User()
|
||||
{
|
||||
var user = new CreateUserViewModel("test@email.com", "Test", "Email");
|
||||
var user = new CreateUserViewModel(
|
||||
"test@email.com",
|
||||
"Test",
|
||||
"Email",
|
||||
"Password");
|
||||
|
||||
var response = await _fixture.ServerClient.PostAsJsonAsync("user", user);
|
||||
|
||||
@ -81,7 +86,8 @@ public sealed class UserControllerTests : IClassFixture<UserTestFixture>
|
||||
_fixture.CreatedUserId,
|
||||
"newtest@email.com",
|
||||
"NewTest",
|
||||
"NewEmail");
|
||||
"NewEmail",
|
||||
UserRole.User);
|
||||
|
||||
var response = await _fixture.ServerClient.PutAsJsonAsync("user", user);
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CleanArchitecture.Domain.Entities;
|
||||
using CleanArchitecture.Domain.Enums;
|
||||
using CleanArchitecture.Domain.Interfaces.Repositories;
|
||||
using MockQueryable.Moq;
|
||||
using Moq;
|
||||
@ -20,9 +21,27 @@ public sealed class UserTestsFixture
|
||||
{
|
||||
ExistingUsers = new List<User>()
|
||||
{
|
||||
new (Guid.NewGuid(), "test@test.de", "Test First Name", "Test Last Name"),
|
||||
new (Guid.NewGuid(), "email@Email.de", "Email First Name", "Email Last Name"),
|
||||
new (Guid.NewGuid(), "user@user.de", "User First Name", "User Last Name"),
|
||||
new (
|
||||
Guid.NewGuid(),
|
||||
"test@test.de",
|
||||
"Test First Name",
|
||||
"Test Last Name",
|
||||
"Test Password",
|
||||
UserRole.User),
|
||||
new (
|
||||
Guid.NewGuid(),
|
||||
"email@Email.de",
|
||||
"Email First Name",
|
||||
"Email Last Name",
|
||||
"Email Password",
|
||||
UserRole.Admin),
|
||||
new (
|
||||
Guid.NewGuid(),
|
||||
"user@user.de",
|
||||
"User First Name",
|
||||
"User Last Name",
|
||||
"User Password",
|
||||
UserRole.User),
|
||||
};
|
||||
|
||||
var queryable = ExistingUsers.AsQueryable().BuildMock();
|
||||
|
Loading…
Reference in New Issue
Block a user