Example #1
0
        public async Task CanResetPassword_WithGlobalAuthorizeFilter()
        {
            // Arrange
            var emailSender = new ContosoEmailSender();

            void ConfigureTestServices(IServiceCollection services) =>
            services.SetupGlobalAuthorizeFilter().SetupTestEmailSender(emailSender);

            var server = ServerFactory.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));

            var client = server.CreateClient();
            var resetPasswordClient = server.CreateClient();
            var newClient           = server.CreateClient();

            var userName    = $"{Guid.NewGuid()}@example.com";
            var password    = $"[PLACEHOLDER]-1a";
            var newPassword = $"[PLACEHOLDER]-1a-updated";

            await UserStories.RegisterNewUserAsync(client, userName, password);

            var registrationEmail = Assert.Single(emailSender.SentEmails);
            await UserStories.ConfirmEmailAsync(registrationEmail, client);

            // Act & Assert
            await UserStories.ForgotPasswordAsync(resetPasswordClient, userName);

            Assert.Equal(2, emailSender.SentEmails.Count);
            var email = emailSender.SentEmails[1];
            await UserStories.ResetPasswordAsync(resetPasswordClient, email, userName, newPassword);

            await UserStories.LoginExistingUserAsync(newClient, userName, newPassword);
        }
Example #2
0
        public async Task UserNotLockedOut_AfterMaxFailedAccessAttempts_WithGlobalAuthorizeFilter()
        {
            // Arrange
            var emailSender = new ContosoEmailSender();

            void ConfigureTestServices(IServiceCollection services) =>
            services.SetupGlobalAuthorizeFilter().SetupMaxFailedAccessAttempts().SetupTestEmailSender(emailSender);

            var server = ServerFactory.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));

            var client    = server.CreateClient();
            var newClient = server.CreateClient();

            var userName      = $"{Guid.NewGuid()}@example.com";
            var password      = $"[PLACEHOLDER]-1a";
            var wrongPassword = $"[PLACEHOLDER]-1a-wrong";

            await UserStories.RegisterNewUserAsync(client, userName, password);

            var registrationEmail = Assert.Single(emailSender.SentEmails);
            await UserStories.ConfirmEmailAsync(registrationEmail, client);

            // Act & Assert
            await UserStories.LoginFailsWithWrongPasswordAsync(newClient, userName, wrongPassword);
        }
Example #3
0
        public async Task CanResendConfirmingEmail()
        {
            // Arrange
            var emailSender = new ContosoEmailSender();

            void ConfigureTestServices(IServiceCollection services) => services
            .SetupTestEmailSender(emailSender)
            .SetupEmailRequired();

            var server = ServerFactory.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));

            var client    = server.CreateClient();
            var newClient = server.CreateClient();

            var userName = $"{Guid.NewGuid()}@example.com";
            var password = $"[PLACEHOLDER]-1a";

            var loggedIn = await UserStories.RegisterNewUserAsync(client, userName, password);

            // Act & Assert
            // Use a new client to simulate a new browser session.
            await UserStories.ResendConfirmEmailAsync(server.CreateClient(), userName);

            Assert.Equal(2, emailSender.SentEmails.Count);
            var email = emailSender.SentEmails.Last();
            await UserStories.ConfirmEmailAsync(email, newClient);
        }
Example #4
0
        public async Task CanLogInAfterConfirmingEmail_WithGlobalAuthorizeFilter()
        {
            // Arrange
            var emailSender = new ContosoEmailSender();

            void ConfigureTestServices(IServiceCollection services) => services
            .SetupTestEmailSender(emailSender)
            .SetupEmailRequired()
            .SetupGlobalAuthorizeFilter();

            var server = ServerFactory.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));

            var client    = server.CreateClient();
            var newClient = server.CreateClient();

            var userName = $"{Guid.NewGuid()}@example.com";
            var password = $"[PLACEHOLDER]-1a";

            var loggedIn = await UserStories.RegisterNewUserAsync(client, userName, password);

            // Act & Assert
            // Use a new client to simulate a new browser session.
            var email = Assert.Single(emailSender.SentEmails);
            await UserStories.ConfirmEmailAsync(email, newClient);

            await UserStories.LoginExistingUserAsync(newClient, userName, password);
        }
Example #5
0
        public async Task CanChangeEmail()
        {
            // Arrange
            var emails = new ContosoEmailSender();

            void ConfigureTestServices(IServiceCollection services) =>
            services.SetupTestEmailSender(emails);

            var server = ServerFactory
                         .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));
            var client       = server.CreateClient();
            var newClient    = server.CreateClient();
            var failedClient = server.CreateClient();

            var userName = $"{Guid.NewGuid()}@example.com";
            var password = $"[PLACEHOLDER]-1a";
            var newEmail = "*****@*****.**";

            var index = await UserStories.RegisterNewUserAsync(client, userName, password);

            var email = await UserStories.SendUpdateEmailAsync(index, newEmail);

            // Act & Assert
            Assert.Equal(2, emails.SentEmails.Count);
            await UserStories.ConfirmEmailAsync(emails.SentEmails[1], client);

            // Verify can login with new email, fails with old
            await UserStories.LoginExistingUserAsync(newClient, newEmail, password);

            await UserStories.LoginFailsWithWrongPasswordAsync(failedClient, userName, password);
        }
Example #6
0
        public async Task CanLogInAfterResettingThePassword()
        {
            using (StartLog(out var loggerFactory))
            {
                // Arrange
                var emailSender = new ContosoEmailSender();
                var server      = ServerFactory.CreateServer(loggerFactory, b => b.ConfigureServices(s =>
                                                                                                     s.SetupTestEmailSender(emailSender)));
                var client = ServerFactory.CreateDefaultClient(server);
                var resetPasswordClient = ServerFactory.CreateDefaultClient(server);
                var newClient           = ServerFactory.CreateDefaultClient(server);

                var userName    = $"{Guid.NewGuid()}@example.com";
                var password    = $"!Test.Password1$";
                var newPassword = $"!New.Password1$";

                await UserStories.RegisterNewUserAsync(client, userName, password);

                var registrationEmail = Assert.Single(emailSender.SentEmails);
                await UserStories.ConfirmEmailAsync(registrationEmail, client);

                // Act & Assert
                await UserStories.ForgotPasswordAsync(resetPasswordClient, userName);

                Assert.Equal(2, emailSender.SentEmails.Count);
                var email = emailSender.SentEmails[1];
                await UserStories.ResetPasswordAsync(resetPasswordClient, email, userName, newPassword);

                await UserStories.LoginExistingUserAsync(newClient, userName, newPassword);
            }
        }
Example #7
0
        public async Task CanLogInAfterConfirmingEmail()
        {
            using (StartLog(out var loggerFactory))
            {
                // Arrange
                var emailSender = new ContosoEmailSender();
                var server      = ServerFactory.CreateServer(loggerFactory, builder =>
                {
                    builder.ConfigureServices(services => services
                                              .SetupTestEmailSender(emailSender)
                                              .SetupEmailRequired());
                });

                var client    = ServerFactory.CreateDefaultClient(server);
                var newClient = ServerFactory.CreateDefaultClient(server);

                var userName = $"{Guid.NewGuid()}@example.com";
                var password = $"!Test.Password1$";

                var loggedIn = await UserStories.RegisterNewUserAsync(client, userName, password);

                // Act & Assert
                // Use a new client to simulate a new browser session.
                var email = Assert.Single(emailSender.SentEmails);
                await UserStories.ConfirmEmailAsync(email, newClient);

                await UserStories.LoginExistingUserAsync(newClient, userName, password);
            }
        }
Example #8
0
        public async Task CanLogInAfterResettingThePassword()
        {
            // Arrange
            var emailSender = new ContosoEmailSender();

            void ConfigureTestServices(IServiceCollection services) => services
            .SetupTestEmailSender(emailSender);

            var server = ServerFactory.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices));

            ServerFactory.EnsureDatabaseCreated();

            var client = server.CreateClient();
            var resetPasswordClient = server.CreateClient();
            var newClient           = server.CreateClient();

            var userName    = $"{Guid.NewGuid()}@example.com";
            var password    = $"!Test.Password1$";
            var newPassword = $"!New.Password1$";

            await UserStories.RegisterNewUserAsync(client, userName, password);

            var registrationEmail = Assert.Single(emailSender.SentEmails);
            await UserStories.ConfirmEmailAsync(registrationEmail, client);

            // Act & Assert
            await UserStories.ForgotPasswordAsync(resetPasswordClient, userName);

            Assert.Equal(2, emailSender.SentEmails.Count);
            var email = emailSender.SentEmails[1];
            await UserStories.ResetPasswordAsync(resetPasswordClient, email, userName, newPassword);

            await UserStories.LoginExistingUserAsync(newClient, userName, newPassword);
        }
Example #9
0
        public async Task CanConfirmEmail()
        {
            // Arrange
            var emails = new ContosoEmailSender();
            var server = ServerFactory.CreateServer(builder =>
                                                    builder.ConfigureServices(s => s.SetupTestEmailSender(emails)));
            var client = ServerFactory.CreateDefaultClient(server);

            var userName = $"{Guid.NewGuid()}@example.com";
            var password = $"!Test.Password1$";

            var index = await UserStories.RegisterNewUserAsync(client, userName, password);

            var manageIndex = await UserStories.SendEmailConfirmationLinkAsync(index);

            // Act & Assert
            Assert.Equal(2, emails.SentEmails.Count);
            var email = emails.SentEmails[1];
            await UserStories.ConfirmEmailAsync(email, client);
        }
Example #10
0
        public async Task CanConfirmEmail()
        {
            // Arrange
            var emails = new ContosoEmailSender();

            void ConfigureTestServices(IServiceCollection services) =>
            services.SetupTestEmailSender(emails);

            var client = ServerFactory
                         .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices))
                         .CreateClient();

            var userName = $"{Guid.NewGuid()}@example.com";
            var password = $"!Test.Password1$";

            var index = await UserStories.RegisterNewUserAsync(client, userName, password);

            var manageIndex = await UserStories.SendEmailConfirmationLinkAsync(index);

            // Act & Assert
            Assert.Equal(2, emails.SentEmails.Count);
            var email = emails.SentEmails[1];
            await UserStories.ConfirmEmailAsync(email, client);
        }