コード例 #1
0
        public async Task RegisterUserWhichHasAlreadyAExternalAccount_MergeAccountsAutomatically()
        {
            var        emailServiceMock = new Mock <IEmailService>();
            HttpClient client           = null;

            Func <string, string, object, Task> sendEmailAsync = async(templateName, email, viewData) =>
            {
                // 4. Get email and confirm registration by calling confirmation link
                var dict            = viewData.ToDictionary();
                var confirmResponse = await client.GetAsync((string)dict["ConfirmUrl"]);

                // 5. After confirmation user should be logged in
                confirmResponse.StatusCode.Should().Be(HttpStatusCode.Found);
                confirmResponse.Headers.Location.ToString().Should().StartWith("/connect/authorize/login");
            };

            emailServiceMock.Setup(c =>
                                   c.SendEmailAsync("UserAccountCreated", "bob@localhost", It.IsAny <object>())).Returns(sendEmailAsync);

            var server = ServerHelper.CreateServer((services) =>
            {
                services.AddSingleton <IEmailService>(emailServiceMock.Object);

                services.Configure <ApplicationOptions>((option) =>
                {
                    option.LoginAfterAccountCreation       = false;
                    option.LoginAfterAccountConfirmation   = true;
                    option.RequireLocalAccountVerification = true;
                    option.AutomaticAccountMerge           = true;
                });
            });

            client = server.CreateClient();

            // 1. Navigate to register page
            var response = await client.GetAsync("/register?returnUrl=" + _returnUrl);

            response.EnsureSuccessStatusCode();
            var html = await response.Content.ReadAsStringAsync();

            var doc = (new HtmlParser().Parse(html));

            // 2. Post registration form
            var formPostBodyData = new Dictionary <string, string>
            {
                { "Email", "bob@localhost" },
                { "Password", "bob@localhost" },
                { "PasswordConfirm", "bob@localhost" },
                { "__RequestVerificationToken", doc.GetAntiForgeryToken() },
                { "ReturnUrl", doc.GetReturnUrl() }
            };

            var postRequest  = response.CreatePostRequest("/register", formPostBodyData);
            var postResponse = await client.SendAsync(postRequest);

            // 3. The registration success page should be shown
            postResponse.StatusCode.Should().Be(HttpStatusCode.Found);
            postResponse.Headers.Location.ToString().Should().StartWith("/register/success");
        }
コード例 #2
0
        public async Task RecoverAccount_ConfirmMail_RedirectToLogin()
        {
            var        emailServiceMock = new Mock <IEmailService>();
            HttpClient client           = null;

            Func <string, string, object, Task> sendEmailAsync = async(templateName, email, viewData) =>
            {
                // 4. Get email and confirm registration by calling confirmation link
                var dict = viewData.ToDictionary();
                // var confirmResponse = await client.GetAsync((string)dict["ConfirmUrl"]);

                // 5. After confirmation user should be redirected to login page
                //confirmResponse.StatusCode.Should().Be(HttpStatusCode.Found);
                //confirmResponse.Headers.Location.ToString().Should().StartWith("/login");
            };

            emailServiceMock.Setup(c =>
                                   c.SendEmailAsync("UserAccountRecover", "alice@localhost", It.IsAny <object>())).Returns(sendEmailAsync);

            var server = ServerHelper.CreateServer((services) =>
            {
                services.AddSingleton <IEmailService>(emailServiceMock.Object);

                services.Configure <ApplicationOptions>((option) =>
                {
                    option.LoginAfterAccountRecovery = false;
                });
            });

            client = server.CreateClient();

            // 1 call the recover page
            var getResponse = await client.GetAsync("/recover?returnUrl=" + _returnUrl);

            getResponse.EnsureSuccessStatusCode();

            // 2 submit form on recover page with valid input
            var getResponseContent = await getResponse.Content.ReadAsStringAsync();

            var doc = (new HtmlParser().Parse(getResponseContent));
            var formPostBodyData = new Dictionary <string, string>
            {
                { "Email", "alice@localhost" },
                { "__RequestVerificationToken", doc.GetAntiForgeryToken() },
                { "ReturnUrl", doc.GetReturnUrl() }
            };
            var postRequest = getResponse.CreatePostRequest("/recover", formPostBodyData);

            var postResponse = await client.SendAsync(postRequest);

            postResponse.StatusCode.Should().Be(HttpStatusCode.Found);
            postResponse.Headers.Location.ToString().Should().StartWith("/recover/success");

            // TODO: check if message is in HTML DOM
        }
コード例 #3
0
        public async Task RegisterValidUser_LoginAfterRegistration_NoConfirmMail()
        {
            var        emailServiceMock = new Mock <IEmailService>();
            HttpClient client           = null;

            var server = ServerHelper.CreateServer((services) =>
            {
                services.AddSingleton <IEmailService>(emailServiceMock.Object);

                services.Configure <ApplicationOptions>((option) =>
                {
                    option.LoginAfterAccountCreation       = true;
                    option.LoginAfterAccountConfirmation   = true;
                    option.RequireLocalAccountVerification = false;
                });
            });

            client = server.CreateClient();

            // 1. Navigate to register page
            var response = await client.GetAsync("/register?returnUrl=" + _returnUrl);

            response.EnsureSuccessStatusCode();
            var html = await response.Content.ReadAsStringAsync();

            var doc = (new HtmlParser().Parse(html));

            // 2. Post registration form
            var formPostBodyData = new Dictionary <string, string>
            {
                { "Email", "john@localhost" },
                { "Password", "john@localhost" },
                { "PasswordConfirm", "john@localhost" },
                { "__RequestVerificationToken", doc.GetAntiForgeryToken() },
                { "ReturnUrl", doc.GetReturnUrl() }
            };

            var postRequest  = response.CreatePostRequest("/register", formPostBodyData);
            var postResponse = await client.SendAsync(postRequest);

            // 3. The registration success page should be shown
            postResponse.StatusCode.Should().Be(HttpStatusCode.Found);
            postResponse.Headers.Location.ToString().Should().StartWith("/connect/authorize/login");

            // There should be no email sent
            emailServiceMock.Verify(c => c.SendEmailAsync("UserAccountCreated", "john@localhost", It.IsAny <object>()), Times.Never());
        }
コード例 #4
0
        public async Task RegisterWithInvalidEmail()
        {
            var server = ServerHelper.CreateServer((services) =>
            {
                var emailServiceMock = new Mock <IEmailService>();
                services.AddSingleton <IEmailService>(emailServiceMock.Object);

                services.Configure <ApplicationOptions>((option) =>
                {
                    option.LoginAfterAccountCreation       = false;
                    option.LoginAfterAccountConfirmation   = true;
                    option.RequireLocalAccountVerification = true;
                });
            });

            var client = server.CreateClient();

            // 1. Navigate to register page
            var response = await client.GetAsync("/register?returnUrl=" + _returnUrl);

            response.EnsureSuccessStatusCode();
            var html = await response.Content.ReadAsStringAsync();

            var doc = (new HtmlParser().Parse(html));

            // 2. Post registration form
            var formPostBodyData = new Dictionary <string, string>
            {
                { "Email", "johnocalhost" },
                { "Password", "password" },
                { "PasswordConfirm", "password" },
                { "__RequestVerificationToken", doc.GetAntiForgeryToken() },
                { "ReturnUrl", doc.GetReturnUrl() }
            };

            var postRequest  = response.CreatePostRequest("/register", formPostBodyData);
            var postResponse = await client.SendAsync(postRequest);

            // 3. The error should be shown
            postResponse.StatusCode.Should().Be(HttpStatusCode.OK);
            postResponse.Headers.Location.Should().BeNull();
        }
コード例 #5
0
 public GeneralTests()
 {
     _server = ServerHelper.CreateServer();
     _client = _server.CreateClient();
 }
コード例 #6
0
 public ServerFixture()
 {
     this.Server = ServerHelper.CreateServer();
     this.Client = this.Server.CreateClient();
 }