Пример #1
0
        public async Task CanSeeExternalLoginProviderDisplayName()
        {
            // Arrange
            void ConfigureTestServices(IServiceCollection services) => services.SetupTestThirdPartyLogin();

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

            var client = server.CreateClient();

            // Act
            var userName = Guid.NewGuid().ToString();
            var email    = $"{userName}@example.com";
            var index    = await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);

            var manage = await index.ClickManageLinkWithExternalLoginAsync();

            var externalLogins = await manage.ClickExternalLoginsAsync();

            // Assert
            var title     = externalLogins.Document.GetElementsByTagName("h4").FirstOrDefault(e => e.TextContent == "Registered Logins");
            var table     = title?.NextElementSibling as IHtmlTableElement;
            var firstCell = table?.Bodies?.FirstOrDefault()?.Rows.FirstOrDefault()?.Cells?.FirstOrDefault();

            Assert.Equal("Contoso auth", firstCell?.TextContent);
        }
Пример #2
0
        public async Task CanDownloadPersonalData(bool twoFactor, bool social)
        {
            // Arrange
            void ConfigureTestServices(IServiceCollection services) =>
            services.SetupTestThirdPartyLogin();

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

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

            var index = social
                ? await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email)
                : await UserStories.RegisterNewUserAsync(client, email, "!TestPassword1");

            if (twoFactor)
            {
                await UserStories.EnableTwoFactorAuthentication(index);
            }

            // Act & Assert
            var jsonData = await UserStories.DownloadPersonalData(index, userName);

            Assert.NotNull(jsonData);
            Assert.True(jsonData.ContainsKey("Id"));
            Assert.NotNull(jsonData["Id"]);
            Assert.True(jsonData.ContainsKey("UserName"));
            Assert.Equal(userName, (string)jsonData["UserName"]);
            Assert.True(jsonData.ContainsKey("Email"));
            Assert.Equal(userName, (string)jsonData["Email"]);
            Assert.True(jsonData.ContainsKey("EmailConfirmed"));
            Assert.False((bool)jsonData["EmailConfirmed"]);
            Assert.True(jsonData.ContainsKey("PhoneNumber"));
            Assert.Equal("null", (string)jsonData["PhoneNumber"]);
            Assert.True(jsonData.ContainsKey("PhoneNumberConfirmed"));
            Assert.False((bool)jsonData["PhoneNumberConfirmed"]);
            Assert.Equal(twoFactor, (bool)jsonData["TwoFactorEnabled"]);

            if (twoFactor)
            {
                Assert.NotNull(jsonData["Authenticator Key"]);
            }
            else
            {
                Assert.Null((string)jsonData["Authenticator Key"]);
            }

            if (social)
            {
                Assert.Equal(userName, (string)jsonData["Contoso external login provider key"]);
            }
            else
            {
                Assert.Null((string)jsonData["Contoso external login provider key"]);
            }
        }
Пример #3
0
        public async Task CanRegisterWithASocialLoginProvider()
        {
            // Arrange
            var server = ServerFactory.CreateServer(builder =>
                                                    builder.ConfigureServices(services => services.SetupTestThirdPartyLogin()));
            var client = ServerFactory.CreateDefaultClient(server);

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

            // Act & Assert
            await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);
        }
Пример #4
0
        public async Task CanRegisterWithASocialLoginProviderFromLogin()
        {
            // Arrange
            void ConfigureTestServices(IServiceCollection services) =>
            services
            .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.RegisterNewUserWithSocialLoginAsync(client, userName, email);
        }
Пример #5
0
        public async Task CanSetPasswordWithExternalLogin()
        {
            // Arrange
            var principals = new List <ClaimsPrincipal>();

            void ConfigureTestServices(IServiceCollection services) =>
            services
            .SetupTestThirdPartyLogin()
            .SetupGetUserClaimsPrincipal(user => principals.Add(user), IdentityConstants.ApplicationScheme);

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

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

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

            // Act 1
            var index = await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);

            index = await UserStories.LoginWithSocialLoginAsync(newClient, userName);

            // Assert 1
            Assert.NotNull(principals[1].Identities.Single().Claims.Single(c => c.Type == ClaimTypes.AuthenticationMethod).Value);

            // Act 2
            await UserStories.SetPasswordAsync(index, "!Test.Password2");

            // Assert 2
            // RefreshSignIn uses the same AuthenticationMethod claim value
            AssertClaimsEqual(principals[1], principals[2], ClaimTypes.AuthenticationMethod);

            // Act & Assert 3
            // Can log in with the password set above
            await UserStories.LoginExistingUserAsync(loginAfterSetPasswordClient, email, "!Test.Password2");
        }
Пример #6
0
        public async Task CanSeeExternalLoginProviderDisplayName()
        {
            // Arrange
            void ConfigureTestServices(IServiceCollection services) => services.SetupTestThirdPartyLogin();

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

            var client = server.CreateClient();

            // Act
            var userName = Guid.NewGuid().ToString();
            var email    = $"{userName}@example.com";
            var index    = await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);

            var manage = await index.ClickManageLinkWithExternalLoginAsync();

            var externalLogins = await manage.ClickExternalLoginsAsync();

            // Assert
            Assert.Contains("Contoso", externalLogins.ExternalLoginDisplayName.TextContent);
        }
Пример #7
0
        public async Task CanLoginWithASocialLoginProvider_WithGlobalAuthorizeFilter()
        {
            // Arrange
            void ConfigureTestServices(IServiceCollection services) => services
            .SetupTestThirdPartyLogin()
            .SetupGlobalAuthorizeFilter();

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

            ServerFactory.EnsureDatabaseCreated();

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

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

            // Act & Assert
            await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);

            await UserStories.LoginWithSocialLoginAsync(newClient, userName);
        }
Пример #8
0
        public async Task RegisterWithASocialLoginProviderSetsAuthenticationMethodClaim()
        {
            // Arrange
            string authenticationMethod = null;

            void ConfigureTestServices(IServiceCollection services) =>
            services
            .SetupTestThirdPartyLogin()
            .SetupGetUserClaimsPrincipal(user =>
                                         authenticationMethod = user.FindFirstValue(ClaimTypes.AuthenticationMethod), IdentityConstants.ApplicationScheme);

            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.RegisterNewUserWithSocialLoginAsync(client, userName, email);

            Assert.Equal("Contoso", authenticationMethod);
        }