public async Task GetMembershipByIdQueryHandler_ShouldReturnNull_WhenMembershipDoesNotExist()
        {
            // Arrange
            GetMembershipByIdQuery request = new GetMembershipByIdQuery {
                GroupMembershipId = 1
            };

            IQueryable <GroupMembership> expectedMemberships = Enumerable
                                                               .Empty <GroupMembership>()
                                                               .AsQueryable()
                                                               .BuildMock()
                                                               .Object;

            _unitOfWorkMock
            .Setup(m => m.GroupMemberships.GetById(request.GroupMembershipId))
            .Returns(expectedMemberships);

            GetMembershipByIdQuery.Handler handler = new GetMembershipByIdQuery.Handler(_mapperMock, _unitOfWorkMock.Object);

            // Act
            GroupMembershipResource membership = await handler.Handle(request);

            // Assert
            Assert.Null(membership);
        }
        public async Task GetMembershipByIdQueryHandler_ShouldReturnMembership_WhenMembershipExists()
        {
            // Arrange
            GetMembershipByIdQuery request = new GetMembershipByIdQuery {
                GroupMembershipId = 1
            };

            IEnumerable <GroupMembership> expectedMemberships = new[]
            {
                new GroupMembership {
                    GroupMembershipId = 1
                }
            };

            IQueryable <GroupMembership> queryableMock = expectedMemberships
                                                         .AsQueryable()
                                                         .BuildMock()
                                                         .Object;

            _unitOfWorkMock
            .Setup(m => m.GroupMemberships.GetById(request.GroupMembershipId))
            .Returns(queryableMock);

            GetMembershipByIdQuery.Handler handler = new GetMembershipByIdQuery.Handler(_mapperMock, _unitOfWorkMock.Object);

            // Act
            GroupMembershipResource membership = await handler.Handle(request);

            // Assert
            Assert.NotNull(membership);
            Assert.Equal(request.GroupMembershipId, membership.GroupMembershipId);
        }
Пример #3
0
        public async Task GetMembershipById_ShouldReturnOkResult_WhenMembershipExists()
        {
            // Arrange
            const int membershipId = 1;

            GroupMembershipResource expectedMembership = new GroupMembershipResource {
                GroupMembershipId = membershipId
            };

            Mock <IMediator> mediatorMock = new Mock <IMediator>();

            mediatorMock
            .Setup(m => m.Send(It.IsAny <GetMembershipByIdQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedMembership);

            GroupMembershipController controller = new GroupMembershipController(mediatorMock.Object, null);

            // Act
            ActionResult <GroupMembershipResource> response = await controller.GetMembershipById(membershipId);

            // Assert
            OkObjectResult result = Assert.IsType <OkObjectResult>(response.Result);

            GroupMembershipResource membership = Assert.IsType <GroupMembershipResource>(result.Value);

            Assert.NotNull(membership);
            Assert.Equal(expectedMembership, membership);
        }
Пример #4
0
        public async Task CreateMembershipCommandHandler_ShouldCreateMembership_AndReturnCreatedMembership()
        {
            // Arrange
            CreateMembershipCommand request = new CreateMembershipCommand
            {
                GroupId = 1,
                UserId  = 1,
                IsAdmin = true
            };

            IEnumerable <User> expectedUsers = new[]
            {
                new User
                {
                    UserId   = 1,
                    UserName = "******"
                }
            };

            IQueryable <User> queryableMock = expectedUsers
                                              .AsQueryable()
                                              .BuildMock()
                                              .Object;

            _unitOfWorkMock
            .Setup(m => m.GroupMemberships.Add(It.IsAny <GroupMembership>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            _unitOfWorkMock
            .Setup(m => m.Recipients.Add(It.IsAny <Recipient>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            _unitOfWorkMock
            .Setup(m => m.CommitAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(2);

            _unitOfWorkMock
            .Setup(m => m.Users.GetById(It.IsAny <int>()))
            .Returns(queryableMock);



            CreateMembershipCommand.Handler handler = new CreateMembershipCommand.Handler(_dateProviderMock.Object, _unitOfWorkMock.Object, _mapperMock);

            // Act
            GroupMembershipResource membership = await handler.Handle(request);

            // Assert
            Assert.NotNull(membership);

            _unitOfWorkMock
            .Verify(m => m.GroupMemberships.Add(It.IsAny <GroupMembership>(), It.IsAny <CancellationToken>()), Times.AtLeastOnce);

            _unitOfWorkMock
            .Verify(m => m.Recipients.Add(It.IsAny <Recipient>(), It.IsAny <CancellationToken>()), Times.AtLeastOnce);

            _unitOfWorkMock
            .Verify(m => m.CommitAsync(It.IsAny <CancellationToken>()), Times.AtLeastOnce);
        }
Пример #5
0
        public async Task CreateMembership_ShouldReturnCreatedResult_WhenMembershipWasCreated()
        {
            // Arrange
            CreateMembershipBody body = new CreateMembershipBody {
                GroupId = 1, UserId = 1, IsAdmin = false
            };

            GroupMembershipResource expectedMembership = new GroupMembershipResource
            {
                GroupMembershipId = 1,
                GroupId           = 1,
                UserId            = 1
            };

            Mock <IMediator> mediatorMock = new Mock <IMediator>();

            mediatorMock
            .Setup(m => m.Send(It.IsAny <GroupExistsQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(true);

            mediatorMock
            .Setup(m => m.Send(It.IsAny <UserExistsQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(true);

            mediatorMock
            .Setup(m => m.Send(It.IsAny <CanCreateMembershipQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(true);

            mediatorMock
            .Setup(m => m.Send(It.IsAny <MembershipCombinationExistsQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(false);

            mediatorMock
            .Setup(m => m.Send(It.IsAny <CreateMembershipCommand>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(expectedMembership);

            MapperConfiguration mapperConfiguration = new MapperConfiguration(config =>
            {
                config.CreateMap <CreateMembershipBody, CreateMembershipCommand>();
                config.CreateMap <CreateMembershipBody, MembershipCombinationExistsQuery>();
            });

            IMapper mapperMock = mapperConfiguration.CreateMapper();

            GroupMembershipController controller = new GroupMembershipController(mediatorMock.Object, mapperMock);

            // Act
            ActionResult <GroupMembershipResource> response = await controller.CreateMembership(body);

            // Assert
            CreatedAtActionResult result = Assert.IsType <CreatedAtActionResult>(response.Result);

            GroupMembershipResource createdMembership = Assert.IsType <GroupMembershipResource>(result.Value);

            Assert.NotNull(createdMembership);
            Assert.Equal(expectedMembership, createdMembership);
        }
    public async Task GetMembershipByIdQueryHandler_ShouldReturnNull_WhenMembershipDoesNotExist()
    {
        // Arrange
        GetMembershipByIdQuery request = new() { GroupMembershipId = 1 };

        _unitOfWorkMock
        .Setup(m => m.GroupMemberships.GetByIdAsync(request.GroupMembershipId))
        .ReturnsAsync(null as GroupMembership);

        GetMembershipByIdQuery.Handler handler = new(_mapperMock, _unitOfWorkMock.Object);

        // Act
        GroupMembershipResource membership = await handler.Handle(request);

        // Assert
        Assert.Null(membership);
    }
Пример #7
0
    public async Task <ActionResult <GroupMembershipResource> > GetMembershipById([FromRoute] int membershipId, CancellationToken cancellationToken = default)
    {
        GetMembershipByIdQuery fetchQuery = new GetMembershipByIdQuery {
            GroupMembershipId = membershipId
        };

        GroupMembershipResource membership = await _mediator.Send(fetchQuery, cancellationToken);

        if (membership == null)
        {
            return(NotFound(new ErrorResource
            {
                StatusCode = StatusCodes.Status404NotFound,
                Message = $"Membership with ID '{membershipId}' does not exist"
            }));
        }

        return(Ok(membership));
    }
    public async Task GetMembershipByIdQueryHandler_ShouldReturnMembership_WhenMembershipExists()
    {
        // Arrange
        GetMembershipByIdQuery request = new() { GroupMembershipId = 1 };

        GroupMembership expectedMembership = new() { GroupMembershipId = 1 };

        _unitOfWorkMock
        .Setup(m => m.GroupMemberships.GetByIdAsync(request.GroupMembershipId))
        .ReturnsAsync(expectedMembership);

        GetMembershipByIdQuery.Handler handler = new(_mapperMock, _unitOfWorkMock.Object);

        // Act
        GroupMembershipResource membership = await handler.Handle(request);

        // Assert
        Assert.NotNull(membership);
        Assert.Equal(request.GroupMembershipId, membership.GroupMembershipId);
    }
}
Пример #9
0
    public async Task <ActionResult <GroupMembershipResource> > CreateMembership([FromBody] CreateMembershipBody body, CancellationToken cancellationToken = default)
    {
        if (!ModelState.IsValid)
        {
            return(BadRequest(ModelState));
        }

        // Check if the provided group exists
        GroupExistsQuery groupExistsQuery = new GroupExistsQuery {
            GroupId = body.GroupId
        };

        bool groupExists = await _mediator.Send(groupExistsQuery, cancellationToken);

        if (!groupExists)
        {
            return(NotFound(new ErrorResource
            {
                StatusCode = StatusCodes.Status404NotFound,
                Message = $"Group with ID '{body.GroupId}' does not exist"
            }));
        }

        // Check if the provided user exists
        UserExistsQuery userExistsQuery = new UserExistsQuery {
            UserId = body.UserId
        };

        bool userExists = await _mediator.Send(userExistsQuery, cancellationToken);

        if (!userExists)
        {
            return(NotFound(new ErrorResource
            {
                StatusCode = StatusCodes.Status404NotFound,
                Message = $"User with ID '{body.UserId}' does not exist"
            }));
        }

        // Check if the current user is permitted to create memberships in this group
        CanCreateMembershipQuery canCreateQuery = new CanCreateMembershipQuery {
            GroupId = body.GroupId
        };

        bool canCreate = await _mediator.Send(canCreateQuery, cancellationToken);

        if (!canCreate)
        {
            return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource
            {
                StatusCode = StatusCodes.Status403Forbidden,
                Message = "You are not permitted to add users to this group. This privilege is only granted to administrators of the group"
            }));
        }

        // Check if such a membership does not already exist
        MembershipCombinationExistsQuery membershipExistsQuery = _mapper.Map <CreateMembershipBody, MembershipCombinationExistsQuery>(body);

        bool membershipExists = await _mediator.Send(membershipExistsQuery, cancellationToken);

        if (membershipExists)
        {
            return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource
            {
                StatusCode = StatusCodes.Status403Forbidden,
                Message = "This user is already a member of this group"
            }));
        }

        CreateMembershipCommand createCommand = _mapper.Map <CreateMembershipBody, CreateMembershipCommand>(body);

        GroupMembershipResource membership = await _mediator.Send(createCommand, cancellationToken);

        return(CreatedAtAction(nameof(GetMembershipById), new { membershipId = membership.GroupMembershipId }, membership));
    }