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);
        }
        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 };;
        }
        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);
        }
        private CreateUserDto CreateCorrectUserDto()
        {
            var createUserDto = new CreateUserDto
            {
                FirstName = "John",
                LastName  = "Smith",
                TeamName = "FC Barcelona",
                StadiumName = "Great Stadium",
                Email = "*****@*****.**",
                Password = "******",
                TeamSetup = "FailSetup"
            };

            return createUserDto;
        }