コード例 #1
0
        public void CreateUserMustReturnBadRequestWhenUserModelStateIsInvalid()
        {
            CreateUserControllerMocks();
            var createUserDto = new CreateUserDto { FirstName = "John", LastName = "Smith", Email = "bademailformat" };
            _usersController.ModelState.AddModelError("Email", "Bad email format");

            var actionResult = _usersController.CreateUser(createUserDto) as ObjectResult;

            Assert.NotNull(actionResult);
            Assert.Equal(400, actionResult.StatusCode);
            Assert.NotNull(actionResult.Value);
            dynamic data = actionResult.Value;
            Assert.Equal("Error in user data model", data.Message);
            Assert.Equal("Email", data.ModelErrors[0].FieldName);
        }
コード例 #2
0
        public OperationResult Create(CreateUserDto createUserDto)
        {
            var user = new User
            {
                FirstName = createUserDto.FirstName,
                LastName = createUserDto.LastName,
                Email = createUserDto.Email,
                Password = CalculatePasswordHash(createUserDto.Password)
            };
            user = _userRepository.Create(user);

            TeamSetupEnum teamSetup = TeamSetupEnum.Unknown;
            if(!TeamSetupEnum.TryParse(createUserDto.TeamSetup, true, out teamSetup))
            {
                return new OperationResult { IsSuccess = false, ErrorMessage = $"Team setup '{createUserDto.TeamSetup}' not exists." };
            }

            var team = new Team
            {
                Name = createUserDto.TeamName,
                TeamSetup = teamSetup,
                User = user,
                UserId = user.Id
            };
            team = _teamService.Create(team);

            var stadium = new Stadium
            {
                Name = createUserDto.StadiumName,
                Capacity = 5000,
                Team = team,
                TeamId = team.Id
            };
            stadium = _stadiumRepository.Create(stadium);

            return new OperationResult { IsSuccess = true, Data = user };;
        }
コード例 #3
0
        public void CreateUserMustReturnForbiddenWhenUserWithThisSameEmailExists()
        {
            CreateUserControllerMocks();
            var createUserDto = new CreateUserDto { FirstName = "John", LastName = "Smith", Email = "*****@*****.**" };
            _userService.Setup(x => x.FindByEmail(It.IsAny<string>())).Returns((string email) =>
                new User() { Id = 1, FirstName = "John", LastName = "Smith", TeamId = 1, Email = email }
            );

            var actionResult = _usersController.CreateUser(createUserDto) as ObjectResult;

            Assert.NotNull(actionResult);
            Assert.Equal(403, actionResult.StatusCode);
            dynamic data = actionResult.Value;
            Assert.Equal("User with this email already exists", data.Message);
        }
コード例 #4
0
        private CreateUserDto CreateCorrectUserDto()
        {
            var createUserDto = new CreateUserDto
            {
                FirstName = "John",
                LastName  = "Smith",
                TeamName = "FC Barcelona",
                StadiumName = "Great Stadium",
                Email = "*****@*****.**",
                Password = "******",
                TeamSetup = "FailSetup"
            };

            return createUserDto;
        }