Exemplo n.º 1
0
        public async Task <ActionResult> CreateAccount([FromBody] CreateAccountBody credentials, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            UserNameOrEmailExistsQuery existsQuery = _mapper.Map <CreateAccountBody, UserNameOrEmailExistsQuery>(credentials);

            bool exists = await _mediator.Send(existsQuery, cancellationToken);

            if (exists)
            {
                return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource
                {
                    StatusCode = StatusCodes.Status403Forbidden,
                    Message = "A user with the same user name or email already exists. Please use different credentials for creating an account"
                }));
            }

            CreateAccountCommand registerCommand = _mapper.Map <CreateAccountBody, CreateAccountCommand>(credentials);

            int userId = await _mediator.Send(registerCommand, cancellationToken);

            return(CreatedAtAction(nameof(GetUserProfile), new { userId }, null));
        }
Exemplo n.º 2
0
        public async Task CreateUserAccount_ShouldReturnCreatedResult_WhenCredentialsAreValid()
        {
            // Arrange
            CreateAccountBody credentials = new CreateAccountBody
            {
                UserName = "******",
                Email    = "*****@*****.**",
                Password = "******"
            };

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

            _mediatorMock
            .Setup(m => m.Send(It.IsAny <CreateAccountCommand>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(1);

            UserController controller = new UserController(_mediatorMock.Object, _mapperMock);

            // Act
            ActionResult response = await controller.CreateAccount(credentials);

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

            Assert.NotEmpty(result.ActionName);
            Assert.NotEmpty(result.RouteValues);
        }
Exemplo n.º 3
0
        public async Task CreateUserAccount_ShouldReturnBadRequestResult_WhenCredentialsAreInvalid()
        {
            // Arrange
            CreateAccountBody credentials = new CreateAccountBody();

            UserController controller = new UserController(null, null);

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

            // Act
            ActionResult response = await controller.CreateAccount(credentials);

            // Assert
            Assert.IsType <BadRequestObjectResult>(response);
        }
Exemplo n.º 4
0
        public async Task CreateUserAccount_ShouldReturnForbiddenResult_WhenUserNameOrEmailAlreadyExists()
        {
            // Arrange
            CreateAccountBody credentials = new CreateAccountBody {
                UserName = "", Email = "", Password = ""
            };

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

            UserController controller = new UserController(_mediatorMock.Object, _mapperMock);

            // Act
            ActionResult response = await controller.CreateAccount(credentials);

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

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

            Assert.Equal(StatusCodes.Status403Forbidden, error.StatusCode);
        }