public TwoFactorAuthController(
     AppUserManager userManager,
     SignInManager <ApplicationUser> signInManager,
     UrlEncoder urlEncoder,
     IEmailSender emailSender,
     ISmsSender smsSender)
 {
     _authService = new TwoFactorAuthService(userManager, signInManager, urlEncoder, emailSender, smsSender);
 }
Example #2
0
        public async Task ValidateTwoFactorAuthenticationOTPAsync_GivenUnverifiedTokenandFindableUser_ReturnsFailed()
        {
            var userRepository       = Substitute.For <IUserRepository>();
            var twoFactorAuthService = new TwoFactorAuthService(userRepository, customUserManagerFake);

            userRepository.GetByIdAsync(twoFactorAuthOTP.UserId, Arg.Any <bool>()).Returns(mockedUserModel);
            customUserManagerFake.SetAuthenticatorTokenVerified(false);
            customUserManagerFake.SetAuthenticatorOtpValid(true);

            ValidationResultResponse validationResultResponse = await twoFactorAuthService.ValidateTwoFactorAuthenticationOTPAsync(twoFactorAuthOTP);

            Assert.False(validationResultResponse.Success, "Giving unverfied token and findable user must return success false.");
        }
Example #3
0
        public async Task RemoveTwoFactorAuthenticationAsync_GivenEmptyGuid_ThrowsNotFoundException()
        {
            var userRepository       = Substitute.For <IUserRepository>();
            var twoFactorAuthService = new TwoFactorAuthService(userRepository, customUserManagerFake);

            Exception catchingException = null;

            try
            {
                await twoFactorAuthService.RemoveTwoFactorAuthenticationAsync(Guid.Empty);
            }
            catch (Exception ex)
            {
                catchingException = ex;
            }

            Assert.True(catchingException is ItemNotFoundException);
        }
Example #4
0
        public async Task ValidateTwoFactorAuthenticationOTPAsync_GivenUnfindableUser_ThrowsNotFoundException()
        {
            var userRepository       = Substitute.For <IUserRepository>();
            var twoFactorAuthService = new TwoFactorAuthService(userRepository, customUserManagerFake);

            customUserManagerFake.SetAuthenticatorTokenVerified(true);
            customUserManagerFake.SetAuthenticatorOtpValid(true);

            Exception catchingException = null;

            try
            {
                await twoFactorAuthService.ValidateTwoFactorAuthenticationOTPAsync(twoFactorAuthOTP);
            }
            catch (Exception ex)
            {
                catchingException = ex;
            }

            Assert.True(catchingException is ItemNotFoundException, "Giving unfindable user must throw ItemNotFoundException.");
        }
Example #5
0
        public async Task RemoveTwoFactorAuthenticationAsync_GivenCorrectGuid_ThrowsNoException()
        {
            var userRepository       = Substitute.For <IUserRepository>();
            var twoFactorAuthService = new TwoFactorAuthService(userRepository, customUserManagerFake);

            userRepository.GetByIdAsync(Guid.Parse(mockedUserModel.Id), Arg.Any <bool>()).Returns(mockedUserModel);

            var testGuid = Guid.NewGuid();

            userRepository.GetByIdAsync(testGuid, Arg.Any <bool>()).Returns(
                new UserModel()
            {
                Id         = testGuid.ToString(),
                UserTokens = new List <UserTokenModel>()
                {
                    new UserTokenModel(),
                    new UserTokenModel(),
                    new UserTokenModel(),
                }
            });

            await twoFactorAuthService.RemoveTwoFactorAuthenticationAsync(testGuid);

            Exception catchingException = null;

            try
            {
                await twoFactorAuthService.RemoveTwoFactorAuthenticationAsync(Guid.Parse(mockedUserModel.Id));
            }
            catch (Exception ex)
            {
                catchingException = ex;
                Assert.True(false);
            }

            Assert.True(catchingException is null);
        }