public async Task SuccessCanGenerateTwoFactorTokenAsync(bool expected)
        {
            var user = Guid.NewGuid().ToString();
            var authenticationService = new Mock <IAuthenticatorService>(MockBehavior.Strict);

            Mock <IUserAuthenticatorStore <string> > userStore;
            Mock <IDataProtector>        dataProtector;
            Mock <IAuthenticatorService> authenticatorService;

            var authenticatorUserManager = AuthenticatorUserManagerTest.GetAuthenticatorUserManagerMock <string>(out userStore, out dataProtector, out authenticatorService);

            authenticatorUserManager
            .Setup(a => a.GetAuthenticatorEnabledAsync(user, default(CancellationToken)))
            .Returns(Task.FromResult(expected))
            .Verifiable();

            var authenticatorTokenProvider = new AuthenticatorTokenProvider <string>(authenticationService.Object);

            var result = await authenticatorTokenProvider.CanGenerateTwoFactorTokenAsync(authenticatorUserManager.Object, user);

            Assert.Equal(expected, result);

            authenticationService.Verify();
            userStore.Verify();
            dataProtector.Verify();
            authenticatorService.Verify();
        }
        public async Task InvalidTokenValidateAsync()
        {
            var    rand  = new Random(DateTime.Now.Millisecond);
            var    user  = Guid.NewGuid().ToString();
            var    code  = rand.Next(999999);
            string token = "bad";

            var authenticationService = new Mock <IAuthenticatorService>(MockBehavior.Strict);
            var authenticatorParams   = new AuthenticatorParams();

            Mock <IUserAuthenticatorStore <string> > userStore;
            Mock <IDataProtector>        dataProtector;
            Mock <IAuthenticatorService> authenticatorService;

            var authenticatorUserManager = AuthenticatorUserManagerTest.GetAuthenticatorUserManagerMock <string>(out userStore, out dataProtector, out authenticatorService);

            var authenticatorTokenProvider = new AuthenticatorTokenProvider <string>(authenticationService.Object);

            var result = await authenticatorTokenProvider.ValidateAsync(Guid.NewGuid().ToString(), token, authenticatorUserManager.Object, user);

            Assert.False(result);

            authenticationService.Verify();
            userStore.Verify();
            dataProtector.Verify();
            authenticatorService.Verify();
        }
        public async Task SuccessValidateAsync(bool valid)
        {
            var    rand  = new Random(DateTime.Now.Millisecond);
            var    user  = Guid.NewGuid().ToString();
            var    code  = rand.Next(999999);
            string token = null;

            if (valid)
            {
                token = code.ToString();
            }
            else
            {
                token = rand.Next(999999).ToString();
            }

            var authenticationService = new Mock <IAuthenticatorService>(MockBehavior.Strict);
            var authenticatorParams   = new AuthenticatorParams();

            Mock <IUserAuthenticatorStore <string> > userStore;
            Mock <IDataProtector>        dataProtector;
            Mock <IAuthenticatorService> authenticatorService;

            var authenticatorUserManager = AuthenticatorUserManagerTest.GetAuthenticatorUserManagerMock <string>(out userStore, out dataProtector, out authenticatorService);

            authenticatorUserManager
            .Setup(a => a.GetAuthenticatorParamsAsync(user, default(CancellationToken)))
            .Returns(Task.FromResult(authenticatorParams))
            .Verifiable();

            authenticationService
            .Setup(a => a.GetCode(
                       authenticatorParams.HashAlgorithm,
                       authenticatorParams.Secret,
                       authenticatorParams.NumberOfDigits,
                       authenticatorParams.PeriodInSeconds))
            .Returns(code)
            .Verifiable();

            var authenticatorTokenProvider = new AuthenticatorTokenProvider <string>(authenticationService.Object);

            var result = await authenticatorTokenProvider.ValidateAsync(Guid.NewGuid().ToString(), token, authenticatorUserManager.Object, user);

            Assert.Equal(valid, result);

            authenticationService.Verify();
            userStore.Verify();
            dataProtector.Verify();
            authenticatorService.Verify();
        }
        public async Task InvalidOperationGenerateAsync()
        {
            var user = Guid.NewGuid().ToString();
            var authenticationService = new Mock <IAuthenticatorService>(MockBehavior.Strict);

            Mock <IUserAuthenticatorStore <string> > userStore;
            Mock <IDataProtector>        dataProtector;
            Mock <IAuthenticatorService> authenticatorService;

            var authenticatorUserManager = AuthenticatorUserManagerTest.GetAuthenticatorUserManagerMock <string>(out userStore, out dataProtector, out authenticatorService);

            var authenticatorTokenProvider = new AuthenticatorTokenProvider <string>(authenticationService.Object);

            await Assert.ThrowsAsync <InvalidOperationException>(() => authenticatorTokenProvider.GenerateAsync(Guid.NewGuid().ToString(), authenticatorUserManager.Object, user));

            authenticationService.Verify();
            userStore.Verify();
            dataProtector.Verify();
            authenticatorService.Verify();
        }