Example #1
0
        public async Task SendsWellcomeEmail()
        {
            var firstName   = "first name";
            var lastName    = "last name";
            var companyName = "company name";
            var userEmail   = "user email";
            var user        = new User
            {
                Email        = userEmail,
                SecurityUser = new SecurityUser
                {
                    Employee = new Employee
                    {
                        FirstName = firstName,
                        LastName  = lastName,
                        Company   = new Company
                        {
                            Name = companyName
                        }
                    }
                }
            };

            var emailSubject = "email subject {0} {1}";
            var emailBody    = "email body {0} {1} {2}";

            var localizedStringSubject = new LocalizedString("COMPANY_REGISTER_EMAIL_SUBJECT", emailSubject);
            var localizedStringBody    = new LocalizedString("COMPANY_REGISTER_EMAIL_BODY", emailBody);

            _mock.Mock <IStringLocalizer <CompanyRegisterEmailNotificationService> >()
            .SetupGet(context => context["COMPANY_REGISTER_EMAIL_SUBJECT"])
            .Returns(localizedStringSubject);

            _mock.Mock <IStringLocalizer <CompanyRegisterEmailNotificationService> >()
            .SetupGet(context => context["COMPANY_REGISTER_EMAIL_BODY"])
            .Returns(localizedStringBody);

            var localizer = _mock.Mock <IStringLocalizer <CompanyRegisterEmailNotificationService> >().Object;

            _mock.Mock <IUserLocalizerProvider <CompanyRegisterEmailNotificationService> >()
            .Setup(instance => instance.Get(user.SecurityUser))
            .Returns(localizer);

            await _service.SendAsync(user);

            var emailRequest = new SendSingleEmailRequest
            {
                Email   = userEmail,
                Subject = string.Format(emailSubject, firstName, lastName),
                Content = string.Format(emailBody, firstName, lastName, companyName),
            };

            _mock.Mock <IEmailService>()
            .Verify(context => context.SendAsync(
                        It.Is <SendSingleEmailRequest>(value => ContentAssert.IsEqual(emailRequest, value))));
        }
        public async Task ShouldReturnFalseWhenValidationLifeTimeElapsed()
        {
            var token        = "token";
            var validatorUrl = "uri";

            uint tokenLifespanInMinutes = 10;
            var  captchaOptions         = new CaptchaOptions
            {
                TokenLifespanInMinutes = tokenLifespanInMinutes,
                ValidatorUrl           = validatorUrl,
                Secret = "secret"
            };
            var clientIP    = "1.0.0.0";
            var formContent = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("secret", captchaOptions.Secret),
                new KeyValuePair <string, string>("response", token),
                new KeyValuePair <string, string>("remoteip", clientIP)
            });

            _mock.Mock <IOptions <CaptchaOptions> >()
            .SetupGet(instance => instance.Value)
            .Returns(captchaOptions);
            var now        = new DateTime(2018, 1, 1);
            var issuedTime = new DateTime(2018, 1, 1).AddMinutes(-tokenLifespanInMinutes).AddTicks(-1);

            _mock.Mock <INowProvider>()
            .Setup(instance => instance.Now())
            .Returns(now);

            var response = new
            {
                challenge_ts = issuedTime.ToUniversalTime(),
                success      = true
            };
            var httpResponse = new HttpResponseMessage
            {
                Content = new StringContent(JsonConvert.SerializeObject(response))
            };

            _mock.Mock <IHttpClientProvider>()
            .Setup(instance => instance.PostAsync(
                       validatorUrl,
                       It.Is <FormUrlEncodedContent>(content => ContentAssert.IsEqual(formContent, content))))
            .ReturnsAsync(httpResponse);

            _mock.Mock <ConnectionInfo>()
            .Setup(instance => instance.RemoteIpAddress)
            .Returns(IPAddress.Parse(clientIP));
            var connection = _mock.Mock <ConnectionInfo>().Object;

            _mock.Mock <HttpContext>()
            .Setup(instance => instance.Connection)
            .Returns(connection);
            var httpContext = _mock.Mock <HttpContext>().Object;

            _mock.Mock <IHttpContextAccessor>()
            .Setup(instance => instance.HttpContext)
            .Returns(httpContext);

            var service = _mock.Create <CaptchaValidationService>();
            var actual  = await service.IsValidAsync(token);

            Assert.IsFalse(actual);
        }
Example #3
0
        public void ReturnsFailResponseWhenOtpCodeIsNotValid()
        {
            const int    code            = 12345;
            const string email           = "[email protected]";
            const string password        = "******";
            const string validationToken = "validation token";
            var          userId          = new Guid("{DD71D20B-339D-4BA6-ACB5-842E42B3A673}");

            var request = new SignInRequest
            {
                Email           = email,
                Password        = password,
                ValidationToken = validationToken,
            };

            var securityUser = new SecurityUser
            {
                Id = userId,
                TwoFactorAuthenticationSecretKey = "SomeKey"
            };

            var user = new User
            {
                Id           = userId,
                SecurityUser = securityUser
            };

            var token = "token";

            var expected = new ContentResult
            {
                StatusCode = 449,
                Content    = token
            };

            _mock.Mock <IUserActiveStatusProvider>()
            .Setup(provider => provider.IsActiveAsync(userId))
            .ReturnsAsync(true);

            _mock.Mock <IUserManager>()
            .Setup(manager => manager.FindByEmailAsync(email))
            .ReturnsAsync(user);

            _mock.Mock <IUserManager>()
            .Setup(provider => provider.CheckPasswordAsync(user, password))
            .ReturnsAsync(true);

            _mock.Mock <ICaptchaValidationService>()
            .Setup(provider => provider.IsValidAsync(validationToken))
            .ReturnsAsync(true);

            _mock.Mock <IOtpSignInValidationService>()
            .Setup(provider => provider.Validate(userId, code))
            .ReturnsAsync(false);

            _mock.Mock <IOtpAuthTokenProvider>()
            .Setup(provider => provider.Get(user.Id))
            .Returns(token);

            var controller = _mock.Create <AuthController>();
            var actual     = controller.SignIn(request);

            ContentAssert.IsEqual(actual.Result, expected);
        }