Beispiel #1
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);
    }
Beispiel #2
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);
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
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);
        }
Beispiel #6
0
        public async Task CanRegisterWithASocialLoginProviderFromLoginWithConfirmationAndRealEmailSender()
        {
            // Arrange
            var emailSender = new ContosoEmailSender();

            void ConfigureTestServices(IServiceCollection services)
            {
                services.SetupTestEmailSender(emailSender);
                services
                .Configure <IdentityOptions>(o => o.SignIn.RequireConfirmedAccount = true)
                .SetupTestThirdPartyLogin();
            }

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

            var guid     = Guid.NewGuid();
            var userName = $"{guid}";
            var email    = $"{guid}@example.com";

            // Act & Assert
            await UserStories.RegisterNewUserWithSocialLoginWithConfirmationAsync(client, userName, email, hasRealEmailSender : true);

            Assert.Single(emailSender.SentEmails);
        }
Beispiel #7
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);
            }
        }
Beispiel #8
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);
            }
        }
Beispiel #9
0
        public async Task CannotLogInWithoutRequiredEmailConfirmation()
        {
            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.
                await Assert.ThrowsAnyAsync <XunitException>(() => UserStories.LoginExistingUserAsync(newClient, userName, password));
            }
        }
Beispiel #10
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);
        }
Beispiel #11
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);
        }
Beispiel #12
0
        public async Task CanChangeEmail()
        {
            // Arrange
            var emails = new ContosoEmailSender();
            var client = ServerFactory
                         .CreateClient();

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

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

            var manageIndex = await UserStories.SendUpdateProfileAsync(index, newEmail);

            // Act & Assert
            var pageUserName = manageIndex.GetUserName();

            Assert.Equal(newEmail, pageUserName);
            var pageEmail = manageIndex.GetEmail();

            Assert.Equal(newEmail, pageEmail);
        }
Beispiel #13
0
        public async Task CannotLogInWithoutRequiredEmailConfirmation()
        {
            // 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 Assert.ThrowsAnyAsync <XunitException>(() => UserStories.LoginExistingUserAsync(newClient, userName, password));
        }
Beispiel #14
0
    public async Task CanConfirmEmail()
    {
        // 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 userName = $"{Guid.NewGuid()}@example.com";
        var password = $"[PLACEHOLDER]-1a";

        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);
    }