public async Task FindByEmailAsync_Should_Return_Null_If_User_Is_Not_Available()
        {
            const string userName = "******";
            const string email = "*****@*****.**";
            const string emailToLookFor = "*****@*****.**";

            using (IDocumentStore store = CreateEmbeddableStore())
            {
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    RavenUser user = new RavenUser(userName, email);
                    RavenUserEmail userEmail = new RavenUserEmail(email, user.Id);
                    await ses.StoreAsync(user);
                    await ses.StoreAsync(userEmail);
                    await ses.SaveChangesAsync();
                }

                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    IUserEmailStore<RavenUser> userEmailStore = new RavenUserStore<RavenUser>(ses);
                    RavenUser user = await userEmailStore.FindByEmailAsync(emailToLookFor);

                    Assert.Null(user);
                }
            }
        }
        public async Task GetEmailAsync_Should_Return_User_Email_If_Available()
        {
            const string userName = "******";
            const string userId = "RavenUsers/Tugberk";
            const string email = "*****@*****.**";

            using (IDocumentStore store = CreateEmbeddableStore())
            {
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    RavenUser user = new RavenUser(userName, email);
                    RavenUserEmail userEmail = new RavenUserEmail(email, user.Id);
                    await ses.StoreAsync(user);
                    await ses.StoreAsync(userEmail);
                    await ses.SaveChangesAsync();
                }

                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    IUserEmailStore<RavenUser> userEmailStore = new RavenUserStore<RavenUser>(ses);
                    RavenUser ravenUser = await ses.LoadAsync<RavenUser>(userId);
                    string userEmail = await userEmailStore.GetEmailAsync(ravenUser);

                    Assert.NotNull(userEmail);
                    Assert.Equal(email, userEmail);
                }
            }
        }
        public async Task SetEmailConfirmedAsync_With_Confirmed_Param_False_Should_Set_The_Email_As_Not_Confirmed_If_Confirmed_Already()
        {
            const string userName = "******";
            const string userId = "RavenUsers/Tugberk";
            const string email = "*****@*****.**";

            using (IDocumentStore store = CreateEmbeddableStore())
            {
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    RavenUser user = new RavenUser(userName, email);
                    RavenUserEmail userEmail = new RavenUserEmail(email, user.Id);
                    userEmail.SetConfirmed();
                    await ses.StoreAsync(user);
                    await ses.StoreAsync(userEmail);
                    await ses.SaveChangesAsync();
                }

                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    IUserEmailStore<RavenUser> userEmailStore = new RavenUserStore<RavenUser>(ses);
                    RavenUser ravenUser = await ses.LoadAsync<RavenUser>(userId);
                    await userEmailStore.SetEmailConfirmedAsync(ravenUser, confirmed: false);
                    await ses.SaveChangesAsync();
                }

                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    string keyToLookFor = RavenUserEmail.GenerateKey(email);
                    RavenUserEmail userEmail = await ses.LoadAsync<RavenUserEmail>(keyToLookFor);

                    Assert.Null(userEmail.ConfirmationRecord);
                }
            }
        }
        public async Task SetEmailConfirmedAsync_Should_Throw_InvalidOperationException_If_User_Email_Property_Is_Not_Available()
        {
            const string userName = "******";
            const string userId = "RavenUsers/Tugberk";
            const string email = "*****@*****.**";

            using (IDocumentStore store = CreateEmbeddableStore())
            {
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    RavenUser user = new RavenUser(userName);
                    RavenUserEmail userEmail = new RavenUserEmail(email, user.Id);
                    await ses.StoreAsync(user);
                    await ses.StoreAsync(userEmail);
                    await ses.SaveChangesAsync();
                }

                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    IUserEmailStore<RavenUser> userEmailStore = new RavenUserStore<RavenUser>(ses);
                    RavenUser ravenUser = await ses.LoadAsync<RavenUser>(userId);

                    await Assert.ThrowsAsync<InvalidOperationException>(async () =>
                    {
                        await userEmailStore.SetEmailConfirmedAsync(ravenUser, confirmed: true);
                    });
                }
            }
        }
        public async Task SetEmailAsync_Should_Set_Email_And_SaveChangesAsync_Should_Throw_ConcurrencyException_If_The_Email_Already_Exists()
        {
            const string userName = "******";
            const string email = "*****@*****.**";
            const string userName2 = "Tugberk2";
            const string userId2 = "RavenUsers/Tugberk2";

            using (IDocumentStore store = CreateEmbeddableStore())
            {
                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    RavenUser user = new RavenUser(userName, email);
                    RavenUser user2 = new RavenUser(userName2);
                    RavenUserEmail userEmail = new RavenUserEmail(email, user.Id);
                    await ses.StoreAsync(user);
                    await ses.StoreAsync(user2);
                    await ses.StoreAsync(userEmail);
                    await ses.SaveChangesAsync();
                }

                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    ses.Advanced.UseOptimisticConcurrency = true;
                    IUserEmailStore<RavenUser> userEmailStore = new RavenUserStore<RavenUser>(ses);
                    RavenUser ravenUser = await ses.LoadAsync<RavenUser>(userId2);
                    await userEmailStore.SetEmailAsync(ravenUser, email);

                    await Assert.ThrowsAsync<ConcurrencyException>(async () =>
                    {
                        await ses.SaveChangesAsync();
                    });
                }

                using (IAsyncDocumentSession ses = store.OpenAsyncSession())
                {
                    RavenUser ravenUser = await ses.LoadAsync<RavenUser>(userId2);
                    Assert.Null(ravenUser.Email);
                }
            }
        }