public async Task GetRolesForUser_Should_Retrieve_Correct_Roles()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    IRoleStore roleStore = new RavenRoleStore <RavenUser, RavenUserRole>(ses);
                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Admin"
                                                             }, new RavenUserRole {
                                                                 Id = "Guest"
                                                             }
                                                         } });

                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/2", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Admin"
                                                             }
                                                         } });

                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/3", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Guest"
                                                             }
                                                         } });

                    await ses.SaveChangesAsync();

                    IEnumerable <string> roles = await roleStore.GetRolesForUser("RavenUsers/1");

                    Assert.Equal(2, roles.Count());
                    Assert.True(roles.Any(role => role.Equals("Admin", StringComparison.InvariantCultureIgnoreCase)));
                    Assert.True(roles.Any(role => role.Equals("Guest", StringComparison.InvariantCultureIgnoreCase)));
                }
        }
        public async Task AddUserToRole_Should_Add_The_User_In_Roles_Consecutively_If_User_Exists_And_Is_Not_In_Role()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    IRoleStore roleStore = new RavenRoleStore <RavenUser, RavenUserRole>(ses);
                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Admin"
                                                             }, new RavenUserRole {
                                                                 Id = "Guest"
                                                             }
                                                         } });

                    await ses.SaveChangesAsync();

                    bool result = await roleStore.AddUserToRole("Sales", "RavenUsers/1");

                    bool result2 = await roleStore.AddUserToRole("Accounting", "RavenUsers/1");

                    await ses.SaveChangesAsync();

                    RavenUser user = await ses.LoadAsync <RavenUser>("RavenUsers/1");

                    Assert.True(result);
                    Assert.True(result2);
                    Assert.True(user.Roles.Any(role => role.Id.Equals("Sales", StringComparison.InvariantCultureIgnoreCase)));
                    Assert.True(user.Roles.Any(role => role.Id.Equals("Accounting", StringComparison.InvariantCultureIgnoreCase)));
                }
        }
        public async Task GetUsersInRoles_Should_Return_Enumberable_Empty_If_There_Is_No_user_In_Role()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    IRoleStore roleStore = new RavenRoleStore <RavenUser, RavenUserRole>(ses);
                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Admin"
                                                             }, new RavenUserRole {
                                                                 Id = "Guest"
                                                             }
                                                         } });

                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/2", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Admin"
                                                             }
                                                         } });

                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/3", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Guest"
                                                             }
                                                         } });

                    await ses.SaveChangesAsync();

                    IEnumerable <string> users = await roleStore.GetUsersInRoles("Sales");

                    Assert.NotNull(users);
                    Assert.False(users.Any());
                }
        }
        public async Task GetUsersInRoles_Should_Return_Correct_Usurs_In_Role()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    IRoleStore roleStore = new RavenRoleStore <RavenUser, RavenUserRole>(ses);
                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Admin"
                                                             }, new RavenUserRole {
                                                                 Id = "Guest"
                                                             }
                                                         } });

                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/2", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Admin"
                                                             }
                                                         } });

                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/3", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Guest"
                                                             }
                                                         } });

                    await ses.SaveChangesAsync();

                    IEnumerable <string> users = await roleStore.GetUsersInRoles("Admin");

                    Assert.Equal(2, users.Count());
                    Assert.Equal("RavenUsers/1", users.ElementAt(0), StringComparer.InvariantCultureIgnoreCase);
                    Assert.Equal("RavenUsers/2", users.ElementAt(1), StringComparer.InvariantCultureIgnoreCase);
                }
        }
        public async Task IsUserInRole_Should_Return_True_If_User_Is_Really_In_Role()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
            using (IAsyncDocumentSession ses = store.OpenAsyncSession())
            {
                IRoleStore roleStore = new RavenRoleStore<RavenUser, RavenUserRole>(ses);
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Admin" }, new RavenUserRole { Id = "Guest" } } });
                await ses.SaveChangesAsync();

                bool isAdmin = await roleStore.IsUserInRole("RavenUsers/1", "Admin");

                Assert.True(isAdmin);
            }
        }
        public async Task GetRolesForUser_Should_Return_Empty_Enumerable_If_User_Does_Not_Exist()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
            using (IAsyncDocumentSession ses = store.OpenAsyncSession())
            {
                IRoleStore roleStore = new RavenRoleStore<RavenUser, RavenUserRole>(ses);
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Admin" }, new RavenUserRole { Id = "Guest" } } });
                await ses.SaveChangesAsync();

                IEnumerable<string> roles = await roleStore.GetRolesForUser("RavenUsers/2");

                Assert.Equal(0, roles.Count());
            }
        }
        public async Task GetRolesForUser_Should_Return_Empty_Enumerable_If_Roles_Property_Is_Null()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
            using (IAsyncDocumentSession ses = store.OpenAsyncSession())
            {
                IRoleStore roleStore = new RavenRoleStore<RavenUser, RavenUserRole>(ses);
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******" });
                await ses.SaveChangesAsync();

                IEnumerable<string> roles = await roleStore.GetRolesForUser("RavenUsers/1");

                Assert.Equal(0, roles.Count());
            }
        }
        public async Task GetRolesForUser_Should_Return_Empty_Enumerable_If_Roles_Property_Is_Null()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    IRoleStore roleStore = new RavenRoleStore <RavenUser, RavenUserRole>(ses);
                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******" });

                    await ses.SaveChangesAsync();

                    IEnumerable <string> roles = await roleStore.GetRolesForUser("RavenUsers/1");

                    Assert.Equal(0, roles.Count());
                }
        }
        public async Task GetRolesForUser_Should_Retrieve_Correct_Roles()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
            using (IAsyncDocumentSession ses = store.OpenAsyncSession())
            {
                IRoleStore roleStore = new RavenRoleStore<RavenUser, RavenUserRole>(ses);
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Admin" }, new RavenUserRole { Id = "Guest" } } });
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/2", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Admin" } } });
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/3", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Guest" } } });
                await ses.SaveChangesAsync();

                IEnumerable<string> roles = await roleStore.GetRolesForUser("RavenUsers/1");

                Assert.Equal(2, roles.Count());
                Assert.True(roles.Any(role => role.Equals("Admin", StringComparison.InvariantCultureIgnoreCase)));
                Assert.True(roles.Any(role => role.Equals("Guest", StringComparison.InvariantCultureIgnoreCase)));
            }
        }
        public void GetUsersInRoles_Should_Throw_ArgumentException_If_RoleId_Param_Is_Whitespace()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    IRoleStore roleStore = new RavenRoleStore <RavenUser, RavenUserRole>(ses);

                    Assert.Throws <ArgumentException>(() =>
                    {
                        try
                        {
                            roleStore.GetUsersInRoles(" ").Wait();
                        }
                        catch (AggregateException ex)
                        {
                            throw ex.GetBaseException();
                        }
                    });
                }
        }
        public async Task GetRolesForUser_Should_Return_Empty_Enumerable_If_User_Does_Not_Exist()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    IRoleStore roleStore = new RavenRoleStore <RavenUser, RavenUserRole>(ses);
                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Admin"
                                                             }, new RavenUserRole {
                                                                 Id = "Guest"
                                                             }
                                                         } });

                    await ses.SaveChangesAsync();

                    IEnumerable <string> roles = await roleStore.GetRolesForUser("RavenUsers/2");

                    Assert.Equal(0, roles.Count());
                }
        }
        public async Task IsUserInRole_Should_Return_True_If_User_Is_Really_In_Role()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    IRoleStore roleStore = new RavenRoleStore <RavenUser, RavenUserRole>(ses);
                    await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List <RavenUserRole> {
                                                             new RavenUserRole {
                                                                 Id = "Admin"
                                                             }, new RavenUserRole {
                                                                 Id = "Guest"
                                                             }
                                                         } });

                    await ses.SaveChangesAsync();

                    bool isAdmin = await roleStore.IsUserInRole("RavenUsers/1", "Admin");

                    Assert.True(isAdmin);
                }
        }
        public async Task GetUsersInRoles_Should_Return_Enumberable_Empty_If_There_Is_No_user_In_Role()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
            using (IAsyncDocumentSession ses = store.OpenAsyncSession())
            {
                IRoleStore roleStore = new RavenRoleStore<RavenUser, RavenUserRole>(ses);
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Admin" }, new RavenUserRole { Id = "Guest" } } });
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/2", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Admin" } } });
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/3", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Guest" } } });
                await ses.SaveChangesAsync();

                IEnumerable<string> users = await roleStore.GetUsersInRoles("Sales");

                Assert.NotNull(users);
                Assert.False(users.Any());
            }
        }
        public async Task AddUserToRole_Should_Add_The_User_In_Roles_Consecutively_If_User_Exists_And_Is_Not_In_Role()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
            using (IAsyncDocumentSession ses = store.OpenAsyncSession())
            {
                IRoleStore roleStore = new RavenRoleStore<RavenUser, RavenUserRole>(ses);
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Admin" }, new RavenUserRole { Id = "Guest" } } });
                await ses.SaveChangesAsync();

                bool result = await roleStore.AddUserToRole("Sales", "RavenUsers/1");
                bool result2 = await roleStore.AddUserToRole("Accounting", "RavenUsers/1");
                await ses.SaveChangesAsync();

                RavenUser user = await ses.LoadAsync<RavenUser>("RavenUsers/1");

                Assert.True(result);
                Assert.True(result2);
                Assert.True(user.Roles.Any(role => role.Id.Equals("Sales", StringComparison.InvariantCultureIgnoreCase)));
                Assert.True(user.Roles.Any(role => role.Id.Equals("Accounting", StringComparison.InvariantCultureIgnoreCase)));
            }
        }
        public void GetUsersInRoles_Should_Throw_ArgumentException_If_RoleId_Param_Is_Whitespace()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
            using (IAsyncDocumentSession ses = store.OpenAsyncSession())
            {
                IRoleStore roleStore = new RavenRoleStore<RavenUser, RavenUserRole>(ses);

                Assert.Throws<ArgumentException>(() =>
                {
                    try
                    {
                        roleStore.GetUsersInRoles(" ").Wait();
                    }
                    catch (AggregateException ex)
                    {
                        throw ex.GetBaseException();
                    }
                });
            }
        }
        public async Task DeleteAsync_should_throw_exception_if_the_role_has_associated_users()
        {
            using (var store = NewDocumentStore())
            {
                new ApplicationUsersIndex().Execute(store);

                using (var session = store.OpenAsyncSession())
                {
                    // Arrange
                    var user = new ApplicationUser("jeremyholt") {Roles = {"Admin", "Accounts"}};
                    await session.StoreAsync(user);
                    await session.SaveChangesAsync();

                    var role = new ApplicationRole("Admin");
                    await session.StoreAsync(role);
                    await session.SaveChangesAsync();


                    var roleStoreService = new RavenRoleStore(session);

                    // Act
                    var act = new Func<Task>(() => roleStoreService.DeleteAsync(role));

                    // Assert
                    act.ShouldThrow<InvalidOperationException>().WithMessage("Não pode deletar carga 'Admin' porque existem usuários nessa carga");
                }
            }
        }
        public async Task GetUsersInRoles_Should_Return_Correct_Usurs_In_Role()
        {
            using (IDocumentStore store = CreateEmbeddableStore())
            using(IAsyncDocumentSession ses = store.OpenAsyncSession())
            {
                IRoleStore roleStore = new RavenRoleStore<RavenUser, RavenUserRole>(ses);
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/1", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Admin" }, new RavenUserRole { Id = "Guest" } } });
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/2", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Admin" } } });
                await ses.StoreAsync(new RavenUser { Id = "RavenUsers/3", UserName = "******", Roles = new List<RavenUserRole> { new RavenUserRole { Id = "Guest" } } });
                await ses.SaveChangesAsync();

                IEnumerable<string> users = await roleStore.GetUsersInRoles("Admin");

                Assert.Equal(2, users.Count());
                Assert.Equal("RavenUsers/1", users.ElementAt(0), StringComparer.InvariantCultureIgnoreCase);
                Assert.Equal("RavenUsers/2", users.ElementAt(1), StringComparer.InvariantCultureIgnoreCase);
            }
        }