Beispiel #1
0
        public async Task RegisterAdminAsync_Returns_Failed_RegisterResultDto()
        {
            //Arrange
            var newUser = new RegisterNormalDto
            {
                FirstName       = "New Admin",
                LastName        = "Access",
                Email           = "*****@*****.**",
                Password        = "******",
                ConfirmPassword = "******",
                Roles           = new List <string> {
                    "Normal"
                }
            };

            _fixture.MockUserManager.Setup(x => x.FindByNameAsync(newUser.Email))
            .ReturnsAsync(_fixture.Users.FirstOrDefault(d => d.Email == newUser.Email) !);

            _fixture.MockUserManager.Setup(x => x.CreateAsync(It.IsAny <User>(), It.IsAny <string>()))
            .ReturnsAsync(IdentityResult.Failed(new IdentityErrorDescriber().PasswordTooShort(4))).Verifiable();

            _fixture.MockUserManager.Setup(x => x.AddToRolesAsync(It.IsAny <User>(), It.IsAny <IEnumerable <string> >()));

            var repository = new AccountRepository(AutoMapperSingleton.Mapper, _fixture.MockUserManager.Object,
                                                   _fixture.MockJwtTokenService.Object, _fixture.MockUserAccessorService.Object);

            //Act
            var result = await repository.RegisterNormalAsync(newUser);

            //Assert
            var registerResult = result.Should().BeAssignableTo <RegisterResultDto>().Subject;

            registerResult.Status.Should().Be("Failed");
            registerResult.Message.Should().Be("Failed to create new user. Error: (Passwords must be at least 4 characters.)");
        }
Beispiel #2
0
        public async Task RegisterNormalAsync_Returns_Success_RegisterResultDto()
        {
            //Arrange
            var newUser = new RegisterNormalDto
            {
                FirstName       = "New User",
                LastName        = "Access",
                Email           = "*****@*****.**",
                Password        = "******",
                ConfirmPassword = "******",
                Roles           = new List <string> {
                    "Normal"
                }
            };

            _fixture.MockUserManager.Setup(x => x.FindByNameAsync(newUser.Email))
            .ReturnsAsync(_fixture.Users.FirstOrDefault(d => d.Email == newUser.Email) !);

            _fixture.MockUserManager.Setup(x => x.CreateAsync(It.IsAny <User>(), It.IsAny <string>()))
            .ReturnsAsync(IdentityResult.Success).Verifiable();

            _fixture.MockUserManager.Setup(x => x.AddToRolesAsync(It.IsAny <User>(), It.IsAny <IEnumerable <string> >()));

            var repository = new AccountRepository(AutoMapperSingleton.Mapper, _fixture.MockUserManager.Object,
                                                   _fixture.MockJwtTokenService.Object, _fixture.MockUserAccessorService.Object);

            //Act
            var result = await repository.RegisterNormalAsync(newUser);

            //Assert
            var registerResult = result.Should().BeAssignableTo <RegisterResultDto>().Subject;

            registerResult.Status.Should().Be("Success");
            registerResult.Message.Should().Be("User created successfully, grant Normal access.");
        }
Beispiel #3
0
        public async Task RegisterNormalAsync_Throws_ConflictException()
        {
            //Arrange
            var newUser = new RegisterNormalDto
            {
                FirstName       = "New User",
                LastName        = "Access",
                Email           = "*****@*****.**",
                Password        = "******",
                ConfirmPassword = "******",
                Roles           = new List <string> {
                    "Normal", "Report"
                }
            };

            _fixture.MockUserManager.Setup(x => x.FindByNameAsync(newUser.Email))
            .ReturnsAsync(_fixture.Users.FirstOrDefault(d => d.Email == newUser.Email) !);

            var repository = new AccountRepository(AutoMapperSingleton.Mapper, _fixture.MockUserManager.Object,
                                                   _fixture.MockJwtTokenService.Object, _fixture.MockUserAccessorService.Object);

            //Act
            var exception = await Assert.ThrowsAsync <RestException>(() => repository.RegisterNormalAsync(newUser));

            //Assert
            exception.ErrorCode.Should().Be(HttpStatusCode.Conflict);
            exception.ErrorMessage.Should().Be("Email [email protected] is already registered.");
            exception.ErrorType.Should().Be(HttpStatusCode.Conflict.ToString());
        }
        public RegisterNormalDtoValidatorFixture()
        {
            Validator = new RegisterNormalDtoValidator();

            Model = new RegisterNormalDto
            {
                Email           = "*****@*****.**",
                Password        = "******",
                ConfirmPassword = "******",
                FirstName       = "James",
                LastName        = "Vasanth",
                Roles           = new List <string> {
                    "Report", "Normal"
                }
            };
        }
Beispiel #5
0
        public async Task <RegisterResultDto> RegisterNormalAsync(RegisterNormalDto registerDto)
        {
            var dbUser = await _userManager.FindByNameAsync(registerDto.Email);

            if (dbUser != null)
            {
                throw new RestException(HttpStatusCode.Conflict, $"Email {registerDto.Email } is already registered.");
            }

            var user = _mapper.Map <User>(registerDto);

            var result = await _userManager.CreateAsync(user, registerDto.Password);

            if (result.Succeeded)
            {
                await _userManager.AddToRolesAsync(user, registerDto.Roles);
            }

            return(new RegisterResultDto
            {
                Status = result.Succeeded ? "Success" : "Failed",
                Message = result.Succeeded ? $"User created successfully, grant {string.Join(", ", registerDto.Roles!)} access." : $"Failed to create new user. Error: ({string.Join(", ", result.Errors.Select(x => x.Description))})"
            });
Beispiel #6
0
 public async Task<IActionResult> RegisterNormalUser(RegisterNormalDto registerDto)
 {
     var result = await _repository.RegisterNormalAsync(registerDto);
     return Ok(result);
 }
Beispiel #7
0
        public AccountControllerFixture()
        {
            ApiVersion = new ApiVersion(1, 0);

            MockAccountRepository = new Mock <IAccountRepository>();

            HttpContext = new DefaultHttpContext();
            Users       = new List <GetUserDto>
            {
                new GetUserDto
                {
                    Id          = Guid.NewGuid(),
                    FirstName   = "Anna",
                    LastName    = "Domino",
                    Email       = "*****@*****.**",
                    PhoneNumber = "",
                    Roles       = new List <string> {
                        "SuperAdmin", "Admin", "Report", "Normal"
                    }
                },
                new GetUserDto
                {
                    Id          = Guid.NewGuid(),
                    FirstName   = "Neil",
                    LastName    = "Down",
                    Email       = "*****@*****.**",
                    PhoneNumber = "",
                    Roles       = new List <string> {
                        "Admin"
                    }
                },
                new GetUserDto
                {
                    Id          = Guid.NewGuid(),
                    FirstName   = "Mark",
                    LastName    = "Ateer",
                    Email       = "*****@*****.**",
                    PhoneNumber = "",
                    Roles       = new List <string> {
                        "Report", "Normal"
                    }
                },
                new GetUserDto
                {
                    Id          = Guid.NewGuid(),
                    FirstName   = "Dave",
                    LastName    = "Allippa",
                    Email       = "*****@*****.**",
                    PhoneNumber = "",
                    Roles       = new List <string> {
                        "Normal"
                    }
                }
            };

            ValidLoginDto = new LoginDto
            {
                Email    = "*****@*****.**",
                Password = "******"
            };

            ValidRegisterAdminDto = new RegisterAdminDto
            {
                FirstName       = "John",
                LastName        = "Quil",
                ConfirmPassword = "******",
                Password        = "******",
                Email           = "*****@*****.**"
            };

            ValidRegisterNormalDto = new RegisterNormalDto
            {
                FirstName       = "Rose",
                LastName        = "Bush",
                ConfirmPassword = "******",
                Password        = "******",
                Email           = "*****@*****.**",
                Roles           = new List <string> {
                    "Report", "Normal"
                }
            };

            ValidLoginDto = new LoginDto
            {
                Email    = "*****@*****.**",
                Password = "******"
            };

            ValidRefreshDto = new RefreshDto
            {
                RefreshToken = "this is refresh token"
            };

            ValidRevokeDto = new RevokeDto
            {
                RefreshToken = "this is refresh JWT token"
            };

            SuccessAdminRegisterResultDto = new RegisterResultDto
            {
                Status  = "Success",
                Message = "User created successfully, grant Admin access."
            };

            FailedRegisterResultDto = new RegisterResultDto
            {
                Status  = "Failed",
                Message = "Failed to create new user."
            };

            SuccessNormalRegisterResultDto = new RegisterResultDto
            {
                Status  = "Success",
                Message = $"User created successfully, grant {string.Join(", ", ValidRegisterNormalDto.Roles)} access."
            };

            ValidTokenResultDtoResult = new TokenResultDto
            {
                AccessToken  = "this will be a JWT access token",
                RefreshToken = "this will be a JWT refresh token",
            };

            CurrentUserDtoResult = new CurrentUserDto
            {
                FirstName = "Simon",
                LastName  = "Sais",
                Email     = "*****@*****.**",
                Roles     = new List <string> {
                    "SuperAdmin", "Admin"
                },
                FullName = "Simon Sais",
                UserId   = Guid.Parse("77d8500b-dd97-4b6d-ce43-08d8aa3916b9")
            };
        }