Ejemplo n.º 1
0
        public async Task DeleteMembership_ShouldDeleteMembership()
        {
            // Arrange
            const int membershipId = 1;

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

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

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

            DeleteMembershipCommand passedDeleteCommand = null;

            mediatorMock
            .Setup(m => m.Send(It.IsAny <DeleteMembershipCommand>(), It.IsAny <CancellationToken>()))
            .Callback <IRequest <Unit>, CancellationToken>((r, _) => passedDeleteCommand = (DeleteMembershipCommand)r)
            .ReturnsAsync(Unit.Value);

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

            // Act
            ActionResult response = await controller.DeleteMembership(membershipId);

            // Assert
            Assert.IsType <NoContentResult>(response);

            mediatorMock.Verify(m => m.Send(It.IsAny <DeleteMembershipCommand>(), It.IsAny <CancellationToken>()), Times.Once);

            Assert.NotNull(passedDeleteCommand);
            Assert.Equal(membershipId, passedDeleteCommand.GroupMembershipId);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public async Task UpdateMembership_ShouldReturnForbiddenResult_WhenMembershipIsOwnMembership()
        {
            // Arrange
            const int            membershipId = 1;
            UpdateMembershipBody body         = new UpdateMembershipBody {
                IsAdmin = true
            };

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

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

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

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

            // Act
            ActionResult response = await controller.UpdateMembership(membershipId, body);

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

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

            Assert.NotNull(error);
            Assert.Equal(StatusCodes.Status403Forbidden, error.StatusCode);
        }
Ejemplo n.º 4
0
        public async Task DeleteMembership_ShouldReturnForbiddenResult_WhenUserIsNotPermittedToDelete()
        {
            // Arrange
            const int membershipId = 1;

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

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

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

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

            // Act
            ActionResult response = await controller.DeleteMembership(membershipId);

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

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

            Assert.NotNull(error);
            Assert.Equal(StatusCodes.Status403Forbidden, error.StatusCode);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        public async Task UpdateMembership_ShouldReturnBadRequestResult_WhenModelValidationFails()
        {
            // Arrange
            const int            membershipId = -3;
            UpdateMembershipBody body         = new UpdateMembershipBody();

            GroupMembershipController controller = new GroupMembershipController(null, null);

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

            // Act
            ActionResult response = await controller.UpdateMembership(membershipId, body);

            // Assert
            Assert.IsType <BadRequestObjectResult>(response);
        }
Ejemplo n.º 9
0
        public async Task GetMembershipById_ShouldReturnNotFoundResult_WhenMembershipDoesNotExist()
        {
            // Arrange
            const int membershipId = 482;

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

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

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

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

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

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

            Assert.NotNull(error);
            Assert.Equal(StatusCodes.Status404NotFound, error.StatusCode);
        }