Ejemplo n.º 1
0
        public void should_send_email_confirmation()
        {
            var mailDeliveryMethod = new Mock <IEmailDeliveryMethod>();

            mailDeliveryMethod.Setup(sender => sender.SendEmailAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(Task.CompletedTask);
            _app.OverrideServices(services => services.AddSingleton(mailDeliveryMethod.Object));

            _app.ShouldPost("/user/send-confirmation-mail",
                            signinStatus: SigninRequired)
            .WithApiResult((api, _) => api.HasSucceeded.ShouldEqual(true));
        }
Ejemplo n.º 2
0
        private Mock <IEmailDeliveryMethod> MockMailSender(bool willBeCalled = true)
        {
            var mailSender = new Mock <IEmailDeliveryMethod>();

            if (willBeCalled)
            {
                mailSender.Setup(sender => sender.SendEmailAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(Task.CompletedTask)
                .Verifiable();
            }

            _theApp.OverrideServices(services => services.AddSingleton(mailSender.Object));
            return(mailSender);
        }
        private Mock <IAuthenticationService> MockAuthService(Action <ClaimsPrincipal> onSignin)
        {
            var authService = new Mock <IAuthenticationService>();

            authService.Setup(auth => auth.SignInAsync(It.IsAny <HttpContext>(), It.IsAny <string>(), It.IsAny <ClaimsPrincipal>(),
                                                       It.IsAny <AuthenticationProperties>()))
            .Returns(Task.CompletedTask)
            .Callback((HttpContext ctx, string scheme, ClaimsPrincipal claimsPrincipal, AuthenticationProperties props) =>
            {
                onSignin(claimsPrincipal);
            })
            .Verifiable();
            _app.OverrideServices(services => services.AddSingleton(authService.Object));
            return(authService);
        }
Ejemplo n.º 4
0
        public void should_download_file_by_anonymous_user()
        {
            var mockFile = new Mock <IFile>();

            mockFile.Setup(f => f.OpenReadAsync()).Returns(Task.FromResult((Stream) new MemoryStream()));

            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem.Setup(fs => fs.GetFileAsync("file-path")).Returns(Task.FromResult(mockFile.Object));

            var mockRepo = new Mock <IRepository <FileRecord> >();

            mockRepo.Setup(repo => repo.All()).Returns(new []
            {
                new FileRecord {
                    StoragePath = "file-path", Slug = "file-guid", OriginalName = "file.txt"
                }
            }.AsQueryable());

            _app.OverrideServices(services =>
            {
                services.AddSingleton(mockFileSystem.Object);
                services.AddSingleton(mockRepo.Object);
            });


            _app.Path("/api/common/download/file-guid?download=true")
            .Get()
            .ShouldSuccess(_app.NoUser())
            .WithResponse(res =>
            {
                res.Content.Headers.ContentType.MediaType.ShouldEqual("text/plain");
                res.Content.Headers.ContentDisposition.FileName.ShouldEqual("file.txt");
            });
        }
Ejemplo n.º 5
0
        void should_not_view_change_password_page_when_external_idp_enabled()
        {
            var externalIdpEnabledOptions = new Mock <IOptions <ExternalIdentityServiceOptions> >();

            externalIdpEnabledOptions.Setup(op => op.Value).Returns(new ExternalIdentityServiceOptions {
                IsEnabled = true
            });
            _theApp.OverrideServices(s => s.AddSingleton(externalIdpEnabledOptions.Object));

            _theApp.MockUser();
            var userCtrl = _theApp.CreateController <UserController>();

            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                userCtrl.ChangePassword();
            });

            Assert.Contains("外部身份服务", exception.Message);
        }
Ejemplo n.º 6
0
        private void ProvideUrlHelper(IUrlHelper urlHelper)
        {
            var mockActionAccessor = new Mock <IActionContextAccessor>();
            var httpContext        = new DefaultHttpContext();

            httpContext.Items[typeof(IUrlHelper)] = urlHelper;
            mockActionAccessor.Setup(x => x.ActionContext).Returns(new ActionContext {
                HttpContext = httpContext
            });
            _app.OverrideServices(s => s.AddSingleton(mockActionAccessor.Object));
        }
Ejemplo n.º 7
0
        void should_send_reset_password_email()
        {
            var user = CreateUser();
            var mailDeliveryMethod = new Mock <IEmailDeliveryMethod>();

            mailDeliveryMethod.Setup(sender => sender.SendEmailAsync(
                                         It.IsAny <string>(),
                                         It.IsAny <string>(),
                                         It.IsAny <string>()))
            .Returns(Task.CompletedTask);
            _app.OverrideServices(services => services.AddSingleton(mailDeliveryMethod.Object));

            _app.ShouldPost("/forgot-password",
                            new ForgotPasswordModel {
                UsernameOrEmail = user.UserName
            },
                            signinStatus: SigninRequirement.SigninNotRequired)
            .WithApiResult((api, _) => api.HasSucceeded.ShouldEqual(true));
        }
Ejemplo n.º 8
0
        private void MockExternalSignin(bool disableRegisteration, out HttpContext httpContext)
        {
            httpContext = new DefaultHttpContext();
            var httpContextAccessorMock = new Mock <IHttpContextAccessor>();

            httpContextAccessorMock.SetupGet(ctxAccessor => ctxAccessor.HttpContext).Returns(httpContext);

            _app.OverrideServices(services =>
            {
                services.AddSingleton(CreateMockExternalIdp());
                services.AddScoped <ExternalSigninManager>();
                services.AddSingleton(httpContextAccessorMock.Object);

                if (disableRegisteration)
                {
                    services.AddSingleton(new SiteSettings()
                    {
                        IsReadonly = false,
                        EnableNewUserRegistration = false
                    });
                }
            });
        }