Skip to content

Commit

Permalink
Refactor TodoListMembersService.AddMemberAsync
Browse files Browse the repository at this point in the history
Now the method checks if user has a permission to add members first
  • Loading branch information
romandykyi committed Mar 25, 2024
1 parent ba2c682 commit 43dd6f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AdvancedTodoList.Core.Pagination;
using AdvancedTodoList.Core.Repositories;
using AdvancedTodoList.Core.Services;
using AdvancedTodoList.Core.Services.Auth;
using AdvancedTodoList.Core.Specifications;
using AdvancedTodoList.Infrastructure.Specifications;
using Mapster;
Expand All @@ -15,12 +16,14 @@ namespace AdvancedTodoList.Infrastructure.Services;
public class TodoListMembersService(
ITodoListDependantEntitiesService<TodoListMember, int> helperService,
ITodoListMembersRepository membersRepository,
IRepository<TodoListRole, int> rolesRepository) :
IRepository<TodoListRole, int> rolesRepository,
IPermissionsChecker permissionsChecker) :
ITodoListMembersService
{
private readonly ITodoListDependantEntitiesService<TodoListMember, int> _helperService = helperService;
private readonly ITodoListMembersRepository _membersRepository = membersRepository;
private readonly IRepository<TodoListRole, int> _rolesRepository = rolesRepository;
private readonly IPermissionsChecker _permissionsChecker = permissionsChecker;

/// <summary>
/// Gets a page with members of a to-do list asynchronously.
Expand Down Expand Up @@ -50,20 +53,23 @@ public Task<ServiceResponse<Page<TodoListMemberPreviewDto>>> GetMembersAsync(Tod
/// </returns>
public async Task<AddTodoListMemberServiceResult> AddMemberAsync(TodoListContext context, TodoListMemberAddDto dto)
{
// Check if user has the permission
if (!await _permissionsChecker.HasPermissionAsync(context, x => x.AddMembers))
return new(TodoListMemberServiceResultStatus.Forbidden);

// Try to find already existing member
var member = await _membersRepository.FindAsync(context.TodoListId, dto.UserId);
// Return error if it exists
if (member != null) return new(TodoListMemberServiceResultStatus.UserAlreadyAdded);

// Add member
var response = await _helperService
.CreateAsync<TodoListMemberAddDto, TodoListMemberMinimalViewDto>(context, dto, x => x.AddMembers);
.CreateAsync<TodoListMemberAddDto, TodoListMemberMinimalViewDto>(context, dto);

return response.Status switch
{
ServiceResponseStatus.Success => new(TodoListMemberServiceResultStatus.Success, response.Result),
ServiceResponseStatus.NotFound => new(TodoListMemberServiceResultStatus.NotFound),
ServiceResponseStatus.Forbidden => new(TodoListMemberServiceResultStatus.Forbidden),
_ => throw new InvalidOperationException("Invalid to-do lists dependant entities (members) service response.")
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ public async Task AddMemberAsync_MemberDoesNotExist_IndicatesSuccess()
WebApplicationFactory.TodoListMembersRepository
.FindAsync(todoListId, userId)
.ReturnsNull();
WebApplicationFactory.PermissionsChecker
.HasPermissionAsync(TestContext, Arg.Any<Func<RolePermissions, bool>>())
.Returns(true);
WebApplicationFactory.TodoMembersHelperService
.CreateAsync<TodoListMemberAddDto, TodoListMemberMinimalViewDto>(
TestContext, inputDto, Arg.Any<Func<RolePermissions, bool>>())
TestContext, inputDto, null)
.Returns(new ServiceResponse<TodoListMemberMinimalViewDto>(
ServiceResponseStatus.Success, outputDto));

Expand All @@ -86,7 +89,10 @@ public async Task AddMemberAsync_MemberDoesNotExist_IndicatesSuccess()
await WebApplicationFactory.TodoMembersHelperService
.Received()
.CreateAsync<TodoListMemberAddDto, TodoListMemberMinimalViewDto>(
TestContext, inputDto, Arg.Is<Func<RolePermissions, bool>>(x => x(addMembers)));
TestContext, inputDto, null);
await WebApplicationFactory.PermissionsChecker
.Received()
.HasPermissionAsync(TestContext, Arg.Is<Func<RolePermissions, bool>>(x => x(addMembers)));
}

[Test]
Expand All @@ -102,6 +108,9 @@ public async Task AddMemberAsync_MemberExists_ReturnsUserAlreadyAddedStatus()
TodoListId = TestContext.TodoListId,
UserId = userId
});
WebApplicationFactory.PermissionsChecker
.HasPermissionAsync(TestContext, Arg.Any<Func<RolePermissions, bool>>())
.Returns(true);

// Act
var result = await _service.AddMemberAsync(TestContext, inputDto);
Expand All @@ -117,12 +126,14 @@ public async Task AddMemberAsync_TodoListDoesNotExist_ReturnsNotFoundStatus()
string todoListId = "Id";
string userId = "UserId";
TodoListMemberAddDto inputDto = new(userId);
WebApplicationFactory.PermissionsChecker
.HasPermissionAsync(TestContext, Arg.Any<Func<RolePermissions, bool>>())
.Returns(true);
WebApplicationFactory.TodoListMembersRepository
.FindAsync(todoListId, userId)
.ReturnsNull();
WebApplicationFactory.TodoMembersHelperService
.CreateAsync<TodoListMemberAddDto, TodoListMemberMinimalViewDto>(TestContext, inputDto,
Arg.Any<Func<RolePermissions, bool>>())
.CreateAsync<TodoListMemberAddDto, TodoListMemberMinimalViewDto>(TestContext, inputDto, null)
.Returns(new ServiceResponse<TodoListMemberMinimalViewDto>(ServiceResponseStatus.NotFound));

// Act
Expand All @@ -136,16 +147,11 @@ public async Task AddMemberAsync_TodoListDoesNotExist_ReturnsNotFoundStatus()
public async Task AddMemberAsync_UserHasNoPermission_ReturnsForbiddenStatus()
{
// Arrange
string todoListId = "Id";
string userId = "UserId";
TodoListMemberAddDto inputDto = new(userId);
WebApplicationFactory.TodoListMembersRepository
.FindAsync(todoListId, userId)
.ReturnsNull();
WebApplicationFactory.TodoMembersHelperService
.CreateAsync<TodoListMemberAddDto, TodoListMemberMinimalViewDto>(TestContext, inputDto,
Arg.Any<Func<RolePermissions, bool>>())
.Returns(new ServiceResponse<TodoListMemberMinimalViewDto>(ServiceResponseStatus.Forbidden));
WebApplicationFactory.PermissionsChecker
.HasPermissionAsync(TestContext, Arg.Any<Func<RolePermissions, bool>>())
.Returns(false);

// Act
var result = await _service.AddMemberAsync(TestContext, inputDto);
Expand Down

0 comments on commit 43dd6f7

Please sign in to comment.