// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Microsoft.AspNetCore.Identity.FunctionalTests { public abstract class RegistrationTests : IClassFixture> where TStartup : class where TContext : DbContext { protected RegistrationTests(ServerFactory serverFactory) { ServerFactory = serverFactory; } public ServerFactory ServerFactory { get; } [Fact] public async Task CanRegisterAUser() { // Arrange void ConfigureTestServices(IServiceCollection services) { return; }; var client = ServerFactory .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices)) .CreateClient(); ServerFactory.EnsureDatabaseCreated(); var userName = $"{Guid.NewGuid()}@example.com"; var password = $"!Test.Password1$"; // Act & Assert await UserStories.RegisterNewUserAsync(client, userName, password); } [Fact] public async Task CanRegisterAUserWithRequiredConfirmation() { // Arrange void ConfigureTestServices(IServiceCollection services) { services.Configure(o => o.SignIn.RequireConfirmedAccount = true); }; var server = ServerFactory .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices)); var client = server.CreateClient(); var client2 = server.CreateClient(); ServerFactory.EnsureDatabaseCreated(); var userName = $"{Guid.NewGuid()}@example.com"; var password = $"!Test.Password1$"; // Act & Assert var register = await UserStories.RegisterNewUserAsyncWithConfirmation(client, userName, password); // Since we aren't confirmed yet, login should fail until we confirm await UserStories.LoginFailsWithWrongPasswordAsync(client, userName, password); await register.ClickConfirmLinkAsync(); await UserStories.LoginExistingUserAsync(client, userName, password); } private class FakeEmailSender : IEmailSender { public Task SendEmailAsync(string email, string subject, string htmlMessage) => Task.CompletedTask; } [Fact] public async Task RegisterWithRealConfirmationDoesNotShowLink() { // Arrange void ConfigureTestServices(IServiceCollection services) { services.Configure(o => o.SignIn.RequireConfirmedAccount = true); services.AddSingleton(); }; var server = ServerFactory .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices)); var client = server.CreateClient(); var client2 = server.CreateClient(); ServerFactory.EnsureDatabaseCreated(); var userName = $"{Guid.NewGuid()}@example.com"; var password = $"!Test.Password1$"; // Act & Assert var register = await UserStories.RegisterNewUserAsyncWithConfirmation(client, userName, password, hasRealEmailSender: true); // Since we aren't confirmed yet, login should fail until we confirm await UserStories.LoginFailsWithWrongPasswordAsync(client, userName, password); } [Fact] public async Task CanRegisterAUser_WithGlobalAuthorizeFilter() { // Arrange void ConfigureTestServices(IServiceCollection services) => services.SetupGlobalAuthorizeFilter(); var client = ServerFactory .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices)) .CreateClient(); ServerFactory.EnsureDatabaseCreated(); var userName = $"{Guid.NewGuid()}@example.com"; var password = $"!Test.Password1$"; // Act & Assert await UserStories.RegisterNewUserAsync(client, userName, password); } [Fact] public async Task CanRegisterWithASocialLoginProviderFromLogin() { // Arrange void ConfigureTestServices(IServiceCollection services) => services .SetupTestThirdPartyLogin(); var client = ServerFactory .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices)) .CreateClient(); ServerFactory.EnsureDatabaseCreated(); var guid = Guid.NewGuid(); var userName = $"{guid}"; var email = $"{guid}@example.com"; // Act & Assert await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email); } [Fact] public async Task CanRegisterWithASocialLoginProviderFromRegister() { // Arrange void ConfigureTestServices(IServiceCollection services) => services .SetupTestThirdPartyLogin(); var client = ServerFactory .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices)) .CreateClient(); ServerFactory.EnsureDatabaseCreated(); var guid = Guid.NewGuid(); var userName = $"{guid}"; var email = $"{guid}@example.com"; // Act & Assert await UserStories.RegisterNewUserWithSocialLoginAsyncViaRegisterPage(client, userName, email); } [Fact] public async Task CanRegisterWithASocialLoginProvider_WithGlobalAuthorizeFilter() { // Arrange void ConfigureTestServices(IServiceCollection services) => services .SetupTestThirdPartyLogin() .SetupGlobalAuthorizeFilter(); var client = ServerFactory .WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices)) .CreateClient(); ServerFactory.EnsureDatabaseCreated(); var guid = Guid.NewGuid(); var userName = $"{guid}"; var email = $"{guid}@example.com"; // Act & Assert await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email); } } }