Beispiel #1
0
        public async Task ShouldThrowExceptionWhenUserHasNotEmailConfirmedAccountIsNotActivated()
        {
            //Arrange
            Mock <SignInManager <User> > signInManager = SignInManagerMoq.Get();
            SchoolManagementContext      context       = new ContextBuilder().BuildClean();

            signInManager.Setup(x => x.CheckPasswordSignInAsync(It.IsAny <Model.Domain.User>(), It.IsAny <string>(), It.IsAny <bool>()))
            .ReturnsAsync(SignInResult.Success);
            Model.Domain.User user = new Model.Domain.User()
            {
                EmailConfirmed = false
            };
            signInManager.Setup(x => x.SignInAsync(It.IsAny <Model.Domain.User>(), It.IsAny <bool>(), It.IsAny <string>())).Returns(Task.FromResult(user));
            Mock <UserManager <User> > userManger = UserManagerMoq.Get();

            userManger.Setup(x => x.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(user);
            signInManager.Object.UserManager = userManger.Object;

            Command cmd = new Command
            {
                Password = "******",
                Email    = "*****@*****.**"
            };
            //Act
            var result = await new Handler(signInManager.Object, context).Handle(cmd, CancellationToken.None);

            //Assert
            userManger.Verify(x => x.FindByEmailAsync(It.IsAny <string>()), Times.Once);
            signInManager.Verify(x => x.CheckPasswordSignInAsync(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Once);
            signInManager.Verify(x => x.SignInAsync(It.IsAny <User>(), It.IsAny <bool>(), It.IsAny <string>()), Times.Never);
            result.Status.Should().Be(DataResult.ResultStatus.Error);
            Assert.Equal("Konto jest nieaktywne.", result.Message);
        }
        public async Task <DataResult <ClaimsPrincipal> > Handle(Command request, CancellationToken cancellationToken)
        {
            Model.Domain.User user = await _signInManager.UserManager.FindByEmailAsync(request.Email);

            if (user == null)
            {
                return(DataResult <ClaimsPrincipal> .Error(PolishReadableMessage.Auth.WrongLoginOrPassword));
            }

            SignInResult resultPassword =
                await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);

            if (resultPassword.Succeeded)
            {
                if (user.EmailConfirmed)
                {
                    user.UtcOffsetInMinutes = request.UtcOffsetInMinutes;
                    _context.Update(user);
                    await _context.SaveChangesAsync(cancellationToken);

                    await _signInManager.SignInAsync(user, request.RememberMe);

                    ClaimsPrincipal claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(user);

                    return(DataResult <ClaimsPrincipal> .Success(claimsPrincipal));
                }

                return(DataResult <ClaimsPrincipal> .Error(PolishReadableMessage.Auth.AccountNotActive));
            }

            return(DataResult <ClaimsPrincipal> .Error(PolishReadableMessage.Auth.WrongLoginOrPassword));
        }
Beispiel #3
0
        public async Task ShouldInvoqueSigninManager()
        {
            //Arrange
            Mock <SignInManager <User> > signInManager = SignInManagerMoq.Get();
            SchoolManagementContext      context       = new ContextBuilder().BuildClean();

            signInManager.Setup(x => x.CheckPasswordSignInAsync(It.IsAny <Model.Domain.User>(), It.IsAny <string>(), It.IsAny <bool>()))
            .ReturnsAsync(SignInResult.Success);
            Model.Domain.User user = new Model.Domain.User()
            {
                EmailConfirmed = true
            };
            signInManager.Setup(x => x.SignInAsync(It.IsAny <Model.Domain.User>(), It.IsAny <bool>(), It.IsAny <string>())).Returns(Task.FromResult(user));
            signInManager.Setup(x => x.CreateUserPrincipalAsync(It.IsAny <User>())).ReturnsAsync(new ClaimsPrincipal());
            Mock <UserManager <User> > userManger = UserManagerMoq.Get();

            userManger.Setup(x => x.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(user);

            Claim adminClaim = new Claim(CustomClaimTypes.Permission, Permissions.Users.SeeAllUser);

            signInManager.Object.UserManager = userManger.Object;

            Command cmd = new Command
            {
                Password = "******",
                Email    = "*****@*****.**"
            };
            //Act
            var result = await new Handler(signInManager.Object, context).Handle(cmd, CancellationToken.None);

            //Assert
            userManger.Verify(x => x.FindByEmailAsync(It.IsAny <string>()), Times.Once);
            signInManager.Verify(x => x.CheckPasswordSignInAsync(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Once);
            signInManager.Verify(x => x.SignInAsync(It.IsAny <User>(), It.IsAny <bool>(), It.IsAny <string>()), Times.Once);
            result.Status.Should().Be(DataResult.ResultStatus.Success);
            result.Data.Should().NotBeNull();
        }