public async Task RemoveUserFromRoleWithMultipleRoles()
        {
            var db          = UnitTestHelper.CreateDefaultDb();
            var userManager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var roleManager =
                new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(UnitTestHelper.CreateDefaultDb()));
            var user = new IdentityUser("MultiRoleUser");

            UnitTestHelper.IsSuccess(await userManager.CreateAsync(user));
            IdentityRole[] roles =
            {
                new IdentityRole("r1"), new IdentityRole("r2"), new IdentityRole("r3"),
                new IdentityRole("r4")
            };
            foreach (var r in roles)
            {
                UnitTestHelper.IsSuccess(await roleManager.CreateAsync(r));
                UnitTestHelper.IsSuccess(await userManager.AddToRoleAsync(user.Id, r.Name));
                Assert.Equal(1, user.Roles.Count(ur => ur.RoleId == r.Id));
                Assert.True(await userManager.IsInRoleAsync(user.Id, r.Name));
            }
            UnitTestHelper.IsSuccess(await userManager.RemoveFromRoleAsync(user.Id, roles[2].Name));
            Assert.Equal(0, user.Roles.Count(ur => ur.RoleId == roles[2].Id));
            Assert.False(await userManager.IsInRoleAsync(user.Id, roles[2].Name));

            Clear(db);
        }
        public async Task LinkUnlinkDeletesTest()
        {
            var db   = UnitTestHelper.CreateDefaultDb();
            var mgr  = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var user = new IdentityUser("linkunlinktest");

            UnitTestHelper.IsSuccess(await mgr.CreateAsync(user));
            var userLogin1 = new UserLoginInfo("provider1", "p1-1");
            var userLogin2 = new UserLoginInfo("provider2", "p2-1");

            Assert.Empty((await mgr.GetLoginsAsync(user.Id)));
            UnitTestHelper.IsSuccess(await mgr.AddLoginAsync(user.Id, userLogin1));
            Assert.Single(user.Logins.Where(l => l.ProviderKey == "p1-1"));
            Assert.Single((await mgr.GetLoginsAsync(user.Id)));
            UnitTestHelper.IsSuccess(await mgr.AddLoginAsync(user.Id, userLogin2));
            Assert.Single(user.Logins.Where(l => l.ProviderKey == "p2-1"));
            Assert.Equal(2, (await mgr.GetLoginsAsync(user.Id)).Count);
            UnitTestHelper.IsSuccess(await mgr.RemoveLoginAsync(user.Id, userLogin1));
            Assert.Empty(user.Logins.Where(l => l.ProviderKey == "p1-1"));
            Assert.Single(user.Logins.Where(l => l.ProviderKey == "p2-1"));
            Assert.Single((await mgr.GetLoginsAsync(user.Id)));
            UnitTestHelper.IsSuccess(await mgr.RemoveLoginAsync(user.Id, userLogin2));
            Assert.Empty((await mgr.GetLoginsAsync(user.Id)));
            Assert.Empty(db.Set <IdentityUserLogin>());
        }
        public void RolesQueryableFailWhenStoreNotImplementedTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new RoleManager <IdentityRole>(new NoopRoleStore());

            Assert.Throws <NotSupportedException>(() => manager.Roles.Count());
        }
        public void RemoveUserFromRoleSyncTest()
        {
            var db          = UnitTestHelper.CreateDefaultDb();
            var store       = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var roleManager =
                new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(UnitTestHelper.CreateDefaultDb()));

            IdentityUser[] users =
            {
                new IdentityUser("1"), new IdentityUser("2"), new IdentityUser("3"),
                new IdentityUser("4")
            };
            foreach (var u in users)
            {
                UnitTestHelper.IsSuccess(store.Create(u, "password"));
            }
            var r = new IdentityRole("r1");

            UnitTestHelper.IsSuccess(roleManager.Create(r));
            foreach (var u in users)
            {
                UnitTestHelper.IsSuccess(store.AddToRole(u.Id, r.Name));
                Assert.True(store.IsInRole(u.Id, r.Name));
            }
            foreach (var u in users)
            {
                UnitTestHelper.IsSuccess(store.RemoveFromRole(u.Id, r.Name));
                Assert.False(store.IsInRole(u.Id, r.Name));
            }
            Assert.Equal(0, db.Set <IdentityUserRole>().Count());

            Clear(db);
        }
Example #5
0
        public async Task QuerableRolesTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));

            //string[] users = { "u1", "u2", "u3", "u4" };
            IdentityRole[] roles =
            {
                new IdentityRole("r1"), new IdentityRole("r2"), new IdentityRole("r3"),
                new IdentityRole("r4")
            };
            //foreach (var u in users) {
            //    UnitTestHelper.IsSuccess(await store.CreateAsync(u, "password"));
            //}
            foreach (IdentityRole r in roles)
            {
                UnitTestHelper.IsSuccess(await manager.CreateAsync(r));
                //foreach (var u in users) {
                //    UnitTestHelper.IsSuccess(await store.Roles.AddUserToRoleAsync(u, r.Name));
                //    Assert.True(await store.Roles.IsUserInRoleAsync(u, r.Name));
                //}
            }
            Assert.Equal(roles.Length, manager.Roles.Count());
            var r1 = manager.Roles.FirstOrDefault(r => r.Name == "r1");

            Assert.Equal(roles[0], r1);
        }
Example #6
0
        public void UserStoreMethodsThrowWhenDisposedTest()
        {
            var db    = UnitTestHelper.CreateDefaultDb();
            var store = new UserStore <IdentityUser>(db);

            store.Dispose();
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.AddClaimAsync(null, null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.AddLoginAsync(null, null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.AddToRoleAsync(null, null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.GetClaimsAsync(null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.GetLoginsAsync(null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.GetRolesAsync(null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.IsInRoleAsync(null, null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.RemoveClaimAsync(null, null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.RemoveLoginAsync(null, null)));
            Assert.Throws <ObjectDisposedException>(
                () => AsyncHelper.RunSync(() => store.RemoveFromRoleAsync(null, null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.RemoveClaimAsync(null, null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.FindAsync(null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.FindByIdAsync(null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.FindByNameAsync(null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.UpdateAsync(null)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.DeleteAsync(null)));
            Assert.Throws <ObjectDisposedException>(
                () => AsyncHelper.RunSync(() => store.SetEmailConfirmedAsync(null, true)));
            Assert.Throws <ObjectDisposedException>(() => AsyncHelper.RunSync(() => store.GetEmailConfirmedAsync(null)));
            Assert.Throws <ObjectDisposedException>(
                () => AsyncHelper.RunSync(() => store.SetPhoneNumberConfirmedAsync(null, true)));
            Assert.Throws <ObjectDisposedException>(
                () => AsyncHelper.RunSync(() => store.GetPhoneNumberConfirmedAsync(null)));
        }
        public async Task RemoveUserFromRoleTest()
        {
            var db          = UnitTestHelper.CreateDefaultDb();
            var userManager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var roleManager =
                new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(UnitTestHelper.CreateDefaultDb()));

            IdentityUser[] users =
            {
                new IdentityUser("1"), new IdentityUser("2"), new IdentityUser("3"),
                new IdentityUser("4")
            };
            foreach (var u in users)
            {
                UnitTestHelper.IsSuccess(await userManager.CreateAsync(u, "password"));
            }
            var r = new IdentityRole("r1");

            UnitTestHelper.IsSuccess(await roleManager.CreateAsync(r));
            foreach (var u in users)
            {
                UnitTestHelper.IsSuccess(await userManager.AddToRoleAsync(u.Id, r.Name));
                Assert.True(await userManager.IsInRoleAsync(u.Id, r.Name));
            }
            foreach (var u in users)
            {
                UnitTestHelper.IsSuccess(await userManager.RemoveFromRoleAsync(u.Id, r.Name));
                Assert.Equal(0, u.Roles.Count(ur => ur.RoleId == r.Id));
                Assert.False(await userManager.IsInRoleAsync(u.Id, r.Name));
            }
            Assert.Equal(0, db.Set <IdentityUserRole>().Count());

            Clear(db);
        }
        public void GetUserClaimSyncTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var user    = new IdentityUser("u1");
            var result  = manager.Create(user);

            UnitTestHelper.IsSuccess(result);
            Assert.NotNull(user);
            var claims = new[]
            {
                new Claim("c1", "v1"),
                new Claim("c2", "v2"),
                new Claim("c3", "v3")
            };

            foreach (Claim c in claims)
            {
                UnitTestHelper.IsSuccess(manager.AddClaim(user.Id, c));
            }
            var userClaims = new List <Claim>(manager.GetClaims(user.Id));

            Assert.Equal(3, userClaims.Count);
            foreach (Claim c in claims)
            {
                Assert.True(userClaims.Exists(u => u.Type == c.Type && u.Value == c.Value));
            }
        }
        public async Task RemoveUserClaimTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var user    = new IdentityUser("u1");
            var result  = await manager.CreateAsync(user);

            Assert.NotNull(user);
            var claims = new[]
            {
                new Claim("c1", "v1"),
                new Claim("c2", "v2"),
                new Claim("c3", "v3")
            };

            foreach (Claim c in claims)
            {
                UnitTestHelper.IsSuccess(await manager.AddClaimAsync(user.Id, c));
            }

            var userClaims = new List <Claim>(await manager.GetClaimsAsync(user.Id));

            Assert.Equal(3, userClaims.Count);
            foreach (Claim c in claims)
            {
                Assert.True(userClaims.Exists(u => u.Type == c.Type && u.Value == c.Value));
                UnitTestHelper.IsSuccess(await manager.RemoveClaimAsync(user.Id, c));
            }
            var cs = await manager.GetClaimsAsync(user.Id);

            Assert.Equal(0, cs.Count());
            Assert.Equal(0, db.Set <IdentityUserClaim>().Count());
        }
        public async Task GetAllUsersTest()
        {
            var db    = UnitTestHelper.CreateDefaultDb();
            var store = new UserStore <IdentityUser>(db);

            var oldCount = store.Users.Count();

            var users = new[]
            {
                new IdentityUser("user1"),
                new IdentityUser("user2"),
                new IdentityUser("user3")
            };

            foreach (IdentityUser u in users)
            {
                await store.CreateAsync(u);
            }
            IQueryable <IUser> usersQ = store.Users;

            Assert.Equal(oldCount + 3, usersQ.Count());
            Assert.NotNull(usersQ.Where(u => u.UserName == "user1").FirstOrDefault());
            Assert.NotNull(usersQ.Where(u => u.UserName == "user2").FirstOrDefault());
            Assert.NotNull(usersQ.Where(u => u.UserName == "user3").FirstOrDefault());
            Assert.Null(usersQ.Where(u => u.UserName == "bogus").FirstOrDefault());

            Clear(db);
        }
Example #11
0
        public void AddUserWithNoUserNameFailsTest()
        {
            var db    = UnitTestHelper.CreateDefaultDb();
            var store = new UserStore <IdentityUser>(db);

            Assert.Throws <DbEntityValidationException>(
                () => AsyncHelper.RunSync(() => store.CreateAsync(new IdentityUser())));
        }
Example #12
0
        public async Task AddToBogusRolesFails()
        {
            var db          = UnitTestHelper.CreateDefaultDb();
            var userManager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var user        = new IdentityUser("1");

            UnitTestHelper.IsSuccess(await userManager.CreateAsync(user));
            Assert.Throws <InvalidOperationException>(() => userManager.AddToRoles(user.Id, "bogus"));
        }
Example #13
0
        public async Task RemoveUserFromBogusRolesFails()
        {
            var db          = UnitTestHelper.CreateDefaultDb();
            var userManager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var user        = new IdentityUser("1");

            UnitTestHelper.IsSuccess(await userManager.CreateAsync(user));
            UnitTestHelper.IsFailure(await userManager.RemoveFromRolesAsync(user.Id, "bogus"), "User is not in role.");
        }
Example #14
0
        public async Task FindRoleWithManagerTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var role    = new IdentityRole("findRoleTest");

            UnitTestHelper.IsSuccess(await roleMgr.CreateAsync(role));
            Assert.Equal(role.Name, (await roleMgr.FindByIdAsync(role.Id)).Name);
        }
        public async Task AddLoginNullLoginFailsTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var user    = new IdentityUser("Hao");

            UnitTestHelper.IsSuccess(await manager.CreateAsync(user));
            ExceptionHelper.ThrowsArgumentNull(() => AsyncHelper.RunSync(() => manager.AddLoginAsync(user.Id, null)),
                                               "login");
        }
        public void RoleFindByNameSyncTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var role    = new IdentityRole("FindByName");

            Assert.False(manager.RoleExists(role.Name));
            UnitTestHelper.IsSuccess(manager.Create(role));
            Assert.Equal(role, manager.FindByName(role.Name));

            Clear(db);
        }
Example #17
0
        public async Task BadValidatorBlocksAllUpdatesTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var role    = new IdentityRole("poorguy");

            UnitTestHelper.IsSuccess(await manager.CreateAsync(role));
            var error = AlwaysBadValidator <IdentityRole> .ErrorMessage;

            manager.RoleValidator = new AlwaysBadValidator <IdentityRole>();
            UnitTestHelper.IsFailure(await manager.UpdateAsync(role), error);
        }
        public void RoleFindByIdSyncTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var role    = new IdentityRole("FindById");

            Assert.Null(manager.FindById(role.Id));
            UnitTestHelper.IsSuccess(manager.Create(role));
            Assert.Equal(role, manager.FindById(role.Id));

            Clear(db);
        }
        public async Task RoleFindByIdTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var role    = new IdentityRole("FindById");

            Assert.Null(await manager.FindByIdAsync(role.Id));
            UnitTestHelper.IsSuccess(await manager.CreateAsync(role));
            Assert.Equal(role, await manager.FindByIdAsync(role.Id));

            Clear(db);
        }
Example #20
0
        public async Task CreateDuplicateRoleAtStoreFailsTest()
        {
            var db    = UnitTestHelper.CreateDefaultDb();
            var store = new RoleStore <IdentityRole>(db);
            var role  = new IdentityRole("dupeRole");
            await store.CreateAsync(role);

            db.SaveChanges();
            var role2 = new IdentityRole("dupeRole");

            Assert.Throws <DbEntityValidationException>(() => AsyncHelper.RunSync(() => store.CreateAsync(role2)));
        }
Example #21
0
        public async Task DeleteUserTest()
        {
            var db    = UnitTestHelper.CreateDefaultDb();
            var store = new UserStore <IdentityUser>(db);
            var mgmt  = new IdentityUser("deletemgmttest");
            await store.CreateAsync(mgmt);

            Assert.NotNull(await store.FindByIdAsync(mgmt.Id));
            await store.DeleteAsync(mgmt);

            Assert.Null(await store.FindByIdAsync(mgmt.Id));
        }
Example #22
0
        public async Task AddDupeUserNameWithStoreFailsTest()
        {
            var db    = UnitTestHelper.CreateDefaultDb();
            var mgr   = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var store = new UserStore <IdentityUser>(db);
            var user  = new IdentityUser("dupe");

            UnitTestHelper.IsSuccess(await mgr.CreateAsync(user));
            var u2 = new IdentityUser("DUPe");

            Assert.Throws <DbEntityValidationException>(() => AsyncHelper.RunSync(() => store.CreateAsync(u2)));
        }
        public async Task AddDuplicateLoginFailsTest()
        {
            var db   = UnitTestHelper.CreateDefaultDb();
            var mgr  = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var user = new IdentityUser("dupeLogintest");

            UnitTestHelper.IsSuccess(await mgr.CreateAsync(user));
            var userLogin1 = new UserLoginInfo("provider1", "p1-1");

            UnitTestHelper.IsSuccess(await mgr.AddLoginAsync(user.Id, userLogin1));
            UnitTestHelper.IsFailure(await mgr.AddLoginAsync(user.Id, userLogin1));
        }
        public async Task CreateRoleTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var role    = new IdentityRole("create");

            Assert.False(await manager.RoleExistsAsync(role.Name));
            UnitTestHelper.IsSuccess(await manager.CreateAsync(role));
            Assert.True(await manager.RoleExistsAsync(role.Name));

            Clear(db);
        }
Example #25
0
 public static async Task CreateManager(OwinContext context)
 {
     var options = new IdentityFactoryOptions <UserManager <IdentityUser> >
     {
         Provider = new TestProvider(UnitTestHelper.CreateDefaultDb()),
         DataProtectionProvider = new DpapiDataProtectionProvider()
     };
     var middleware =
         new IdentityFactoryMiddleware
         <UserManager <IdentityUser>, IdentityFactoryOptions <UserManager <IdentityUser> > >(null, options);
     await middleware.Invoke(context);
 }
        public async Task UnknownRoleThrowsTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var u       = new IdentityUser("u1");

            UnitTestHelper.IsSuccess(await manager.CreateAsync(u));
            Assert.Throws <InvalidOperationException>(
                () => AsyncHelper.RunSync(() => manager.AddToRoleAsync(u.Id, "bogus")));

            Clear(db);
        }
        public async Task RoleFindByNameTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(UnitTestHelper.CreateDefaultDb()));
            var role    = new IdentityRole("FindByName");

            Assert.Null(await manager.FindByNameAsync(role.Name));
            Assert.False(await manager.RoleExistsAsync(role.Name));
            UnitTestHelper.IsSuccess(await manager.CreateAsync(role));
            Assert.Equal(role, await manager.FindByNameAsync(role.Name));

            Clear(db);
        }
Example #28
0
        public async Task RemoveUserNotInRoleFailsTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var userMgr = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var role    = new IdentityRole("addUserDupeTest");
            var user    = new IdentityUser("user1");

            UnitTestHelper.IsSuccess(await userMgr.CreateAsync(user));
            UnitTestHelper.IsSuccess(await roleMgr.CreateAsync(role));
            var result = await userMgr.RemoveFromRoleAsync(user.Id, role.Name);

            UnitTestHelper.IsFailure(result, "User is not in role.");
        }
Example #29
0
        public async Task AddUserToRoleFailsIfAlreadyInRoleTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var userMgr = new UserManager <IdentityUser>(new UserStore <IdentityUser>(db));
            var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var role    = new IdentityRole("addUserDupeTest");
            var user    = new IdentityUser("user1");

            UnitTestHelper.IsSuccess(await userMgr.CreateAsync(user));
            UnitTestHelper.IsSuccess(await roleMgr.CreateAsync(role));
            UnitTestHelper.IsSuccess(await userMgr.AddToRoleAsync(user.Id, role.Name));
            Assert.True(await userMgr.IsInRoleAsync(user.Id, role.Name));
            UnitTestHelper.IsFailure(await userMgr.AddToRoleAsync(user.Id, role.Name), "User already in role.");
        }
        public async Task UpdateRoleNameTest()
        {
            var db      = UnitTestHelper.CreateDefaultDb();
            var manager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(db));
            var role    = new IdentityRole("update");

            Assert.False(await manager.RoleExistsAsync(role.Name));
            UnitTestHelper.IsSuccess(await manager.CreateAsync(role));
            Assert.True(await manager.RoleExistsAsync(role.Name));
            role.Name = "Changed";
            UnitTestHelper.IsSuccess(await manager.UpdateAsync(role));
            Assert.False(await manager.RoleExistsAsync("update"));
            Assert.Equal(role, await manager.FindByNameAsync(role.Name));

            Clear(db);
        }