コード例 #1
0
        public async Task CanCreateMembershipQueryHandler_ShouldReturnFalse_WhenUserIsNotPartOfGroup()
        {
            // Arrange
            CanCreateMembershipQuery request = new CanCreateMembershipQuery {
                GroupId = 1
            };

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

            _unitOfWorkMock
            .Setup(m => m.GroupMemberships.GetByCombination(request.GroupId, 1))
            .Returns(expectedMemberships);

            CanCreateMembershipQuery.Handler handler = new CanCreateMembershipQuery.Handler(_unitOfWorkMock.Object, _userProviderMock.Object);

            // Act
            bool canCreate = await handler.Handle(request);

            // Assert
            Assert.False(canCreate);
        }
コード例 #2
0
        public async Task CanCreateMembershipQueryHandler_ShouldReturnTrue_WhenUserIsAnAdministrator()
        {
            // Arrange
            CanCreateMembershipQuery request = new CanCreateMembershipQuery {
                GroupId = 1
            };

            IEnumerable <GroupMembership> expectedMemberships = new[]
            {
                new GroupMembership {
                    GroupMembershipId = 1, UserId = 1, IsAdmin = true
                }
            };

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

            _unitOfWorkMock
            .Setup(m => m.GroupMemberships.GetByCombination(request.GroupId, 1))
            .Returns(queryableMock);

            CanCreateMembershipQuery.Handler handler = new CanCreateMembershipQuery.Handler(_unitOfWorkMock.Object, _userProviderMock.Object);

            // Act
            bool canCreate = await handler.Handle(request);

            // Assert
            Assert.True(canCreate);
        }
コード例 #3
0
    public async Task CanCreateMembershipQueryHandler_ShouldReturnFalse_WhenUserIsNotPartOfGroup()
    {
        // Arrange
        CanCreateMembershipQuery request = new CanCreateMembershipQuery {
            GroupId = 1
        };

        _unitOfWorkMock
        .Setup(m => m.GroupMemberships.GetByCombination(request.GroupId, 1, It.IsAny <CancellationToken>()))
        .ReturnsAsync(null as GroupMembership);

        CanCreateMembershipQuery.Handler handler = new CanCreateMembershipQuery.Handler(_unitOfWorkMock.Object, _userProviderMock.Object);

        // Act
        bool canCreate = await handler.Handle(request);

        // Assert
        Assert.False(canCreate);
    }
コード例 #4
0
    public async Task CanCreateMembershipQueryHandler_ShouldReturnTrue_WhenUserIsAnAdministrator()
    {
        // Arrange
        CanCreateMembershipQuery request = new CanCreateMembershipQuery {
            GroupId = 1
        };

        GroupMembership expectedMembership = new GroupMembership {
            GroupMembershipId = 1, UserId = 1, IsAdmin = true
        };

        _unitOfWorkMock
        .Setup(m => m.GroupMemberships.GetByCombination(request.GroupId, 1, It.IsAny <CancellationToken>()))
        .ReturnsAsync(expectedMembership);

        CanCreateMembershipQuery.Handler handler = new CanCreateMembershipQuery.Handler(_unitOfWorkMock.Object, _userProviderMock.Object);

        // Act
        bool canCreate = await handler.Handle(request);

        // Assert
        Assert.True(canCreate);
    }
コード例 #5
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));
    }