public void NameConstructorPopulatesId()
        {
            var role = new IdentityRole("Test");
            var role2 = new IdentityRole("Test2");

            role.Id.Should().NotBeNullOrWhiteSpace();
            role2.Id.Should().NotBe(role.Id);
        }
        public void IdIsGeneratedAutomatically()
        {
            var role = new IdentityRole();
            var role2 = new IdentityRole();

            role.Id.Should().NotBeNullOrWhiteSpace();
            role2.Id.Should().NotBe(role.Id);
        }
        public async Task DeleteAsync_Deletes()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole("DeleteRole");

            await store.CreateAsync(role, CancellationToken.None);
            await store.DeleteAsync(role, CancellationToken.None);

            var savedRole = _userData.GetRole(role.Id);

            savedRole.Should().BeNull();
        }
        public async Task CreateAsync_CreatesRole()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole("DummyRole")
            {
                NormalizedName = "DUMMYROLE",
            };

            await store.CreateAsync(role, CancellationToken.None);

            var savedRole = _userData.GetRole(role.Id);

            savedRole.Should().NotBeNull();
            savedRole.Name.Should().Be("DummyRole");
            savedRole.NormalizedName.Should().Be("DUMMYROLE");
            savedRole.Id.Should().Be(role.Id);
        }
        public async Task<IActionResult> Index()
        {
            var stopWatch = new Stopwatch();
            long createTime, deleteTime;

            stopWatch.Start();

            var role = new IdentityRole("TestRole");
            var users = new IdentityUser[1000];

            await _roleManager.CreateAsync(role);

            for (int i = 0; i < 1000; ++i)
            {
                var user = new IdentityUser
                {
                    UserName = "******" + i,
                    Email = $"test{i}@test.com",
                };

                users[i] = user;
                await _userManager.CreateAsync(user, "Password123*");
                await _userManager.AddToRoleAsync(user, "TestRole");
            }

            stopWatch.Stop();
            createTime = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();
            stopWatch.Start();

            for (int i = 0; i < 1000; ++i)
            {
                var user = users[i];
                await _userManager.RemoveFromRoleAsync(user, "TestRole");
                await _userManager.DeleteAsync(user);
            }
            await _roleManager.DeleteAsync(role);

            stopWatch.Stop();
            deleteTime = stopWatch.ElapsedMilliseconds;

            var response = $"Create time: {createTime}ms\r\nDelete Time: {deleteTime}ms";

            return Content(response);
        }
        public async Task UpdateAsync_Updates()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole("EditRole")
            {
                NormalizedName = "EDITROLE"
            };

            await store.CreateAsync(role, CancellationToken.None);

            role.Name = "EditRole2";
            role.NormalizedName = "EDITROLE2";

            await store.UpdateAsync(role, CancellationToken.None);

            var savedRole = _userData.GetRole(role.Id);

            savedRole.Should().NotBeNull();
            savedRole.Name.Should().Be("EditRole2");
            savedRole.NormalizedName.Should().Be("EDITROLE2");
        }
        public async Task GetRoleIdAsync_GetsId()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole("Test");

            var id = await store.GetRoleIdAsync(role, CancellationToken.None);

            id.Should().Be(role.Id);
        }
        public async Task RemoveClaimAsync_RemovesClaim()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole("RemoveClaimTest");
            var claim = new Claim("RemoveClaim", "Claim");

            await store.CreateAsync(role, CancellationToken.None);
            await store.AddClaimAsync(role, claim, CancellationToken.None);
            await store.RemoveClaimAsync(role, claim, CancellationToken.None);

            var claims = await store.GetClaimsAsync(role, CancellationToken.None);

            claims.Should().BeEmpty();
        }
        public async Task AddClaimAsync_AddsClaim()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole("AddClaimTest");
            var claim = new Claim("AddClaim", "Claim");

            await store.CreateAsync(role, CancellationToken.None);
            await store.AddClaimAsync(role, claim, CancellationToken.None);

            var claims = await store.GetClaimsAsync(role, CancellationToken.None);

            claims.Should().NotBeNullOrEmpty();
            claims.Should().HaveCount(1);
            claims.First().Type.Should().Be("AddClaim");
            claims.First().Value.Should().Be("Claim");
        }
        public async Task GetClaimsAsync_GetsClaims()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole("ClaimsTest");
            var claim1 = new Claim("GetClaims", "Claim1");
            var claim2 = new Claim("GetClaims", "Claim2");

            await store.CreateAsync(role, CancellationToken.None);
            await store.AddClaimAsync(role, claim1, CancellationToken.None);
            await store.AddClaimAsync(role, claim2, CancellationToken.None);

            var claims = await store.GetClaimsAsync(role, CancellationToken.None);

            claims.Should().NotBeNullOrEmpty();
            claims.Should().HaveCount(2);
            claims.FirstOrDefault(c => c.Type == "GetClaims" && c.Value == "Claim1").Should().NotBeNull();
            claims.FirstOrDefault(c => c.Type == "GetClaims" && c.Value == "Claim2").Should().NotBeNull();
        }
        public async Task SetNormalizedRoleNameAsync_SetsNormalizedRoleName()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole() { NormalizedName = "Wrong" };

            await store.SetNormalizedRoleNameAsync(role, "Correct", CancellationToken.None);

            role.NormalizedName.Should().Be("Correct");
        }
        public async Task GetNormalizedRoleNameAsync_GetsNormalizedName()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole() { NormalizedName = "NormName" };

            var name = await store.GetNormalizedRoleNameAsync(role, CancellationToken.None);

            name.Should().Be("NormName");
        }
        public async Task GetRoleNameAsync_GetsRoleName()
        {
            var store = _userData.GetRoleStore();
            var role = new IdentityRole("Test");

            var name = await store.GetRoleNameAsync(role, CancellationToken.None);

            name.Should().Be("Test");
        }
        public void NameConstructorPopulates()
        {
            var role = new IdentityRole("TestRole");

            role.Name.Should().Be("TestRole");
        }
        /// <summary>
        /// Adds the standard dummy users to the DB, uses NPOCO to save
        /// </summary>
        private void CreateDummyUsers()
        {
            var user = new IdentityUser(TestUserName)
            {
                NormalizedUserName = TestUserName,
                Email = TestUserEmail,
                NormalizedEmail = TestUserEmail,
                PasswordHash = TestUserPassword
            };

            var user2 = new IdentityUser("Dont Use Me")
            {
                NormalizedUserName = "******",
                Email = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                PasswordHash = "goaway"
            };

            var dummyLogin = new UserLogin()
            {
                ProviderKey = "Dummy",
                LoginProvider = "LoginProvider",
                ProviderDisplayName = "DummyDontUse",
                UserId = user2.Id
            };

            var role = new IdentityRole(TestRoleName)
            {
                NormalizedName = TestRoleName
            };

            var testClaim = new UserClaim
            {
                ClaimType = TestClaimType,
                ClaimValue = TestClaimValue,
                UserId = user.Id
            };

            using (var db = new Database(ConnectionString, ProviderName))
            {
                db.Insert(user);
                db.Insert(user2);
                db.Insert(dummyLogin);
                db.Insert(role);
                db.Insert(testClaim);
            }

            _testUserId = user.Id;
            _testRoleId = role.Id;
        }
        /// <summary>
        /// Get a specific role directly from the DB using ADO.NET rather than NPOCO. Ideal for checking data matches between store/NPOCO and the actual DB
        /// </summary>
        /// <param name="id">ID of role</param>
        /// <returns>Role or NULL</returns>
        public IdentityRole GetRole(string id)
        {
            IdentityRole role = null;

            using (var conn = new SqlConnection(ConnectionString))
            using (var cmd = new SqlCommand("SELECT * FROM AspNetRoles WHERE Id = @id", conn))
            {
                cmd.Parameters.AddWithValue("@id", id);

                conn.Open();

                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        int col = 0;
                        role = new IdentityRole();

                        // Read in role data
                        role.Id = reader.GetString(col++);
                        role.ConcurrencyStamp = reader.GetStringSafe(col++);
                        role.Name = reader.GetStringSafe(col++);
                        role.NormalizedName = reader.GetStringSafe(col++);
                    }
                }

                conn.Close();
            }

            return role;
        }