Beispiel #1
0
        public async Task CreateMembership_ShouldReturnForbiddenResult_WhenUserIsNotAdministrator()
        {
            // Arrange
            CreateMembershipBody body = new CreateMembershipBody {
                GroupId = 1, UserId = 1, IsAdmin = false
            };

            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(false);

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

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

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

            ErrorResource error = Assert.IsType <ErrorResource>(result.Value);

            Assert.NotNull(error);
            Assert.Equal(StatusCodes.Status403Forbidden, error.StatusCode);
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public async Task CreateMembership_ShouldReturnBadRequestResult_WhenModelValidationFails()
        {
            // Arrange
            GroupMembershipController controller = new GroupMembershipController(null, null);

            controller.ModelState.AddModelError("", "");

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

            // Assert
            Assert.IsType <BadRequestObjectResult>(response.Result);
        }