<commit_before>﻿namespace CoreTests
{
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Identity.DocumentDB;
    using Microsoft.Azure.Documents;
    using Microsoft.Azure.Documents.Client;
    using Microsoft.Extensions.DependencyInjection;
    using NUnit.Framework;
    using System;
    using System.Linq;
    using System.Threading.Tasks;

    [TestFixture]
    public class DocumentDBIdentityBuilderExtensionsTests : AssertionHelper
    {
        static readonly string databaseId = "AspDotNetCore.Identity.DocumentDB.Test";
        static readonly string key = "<KEY>;

        DocumentClient client = new DocumentClient(new Uri("https://localhost:8081"), key);

        [Test]
        public void AddDocumentDBStores_WithDefaultTypes_ResolvesStoresAndManagers()
        {
            var services = new ServiceCollection();
            services.AddIdentityWithDocumentDBStores(client, UriFactory.CreateDatabaseUri(databaseId).ToString());
            // note: UserManager and RoleManager use logging
            services.AddLogging();

            var provider = services.BuildServiceProvider();
            var resolvedUserStore = provider.GetService<IUserStore<IdentityUser>>();
            Expect(resolvedUserStore, Is.Not.Null, "User store did not resolve");

            var resolvedRoleStore = provider.GetService<IRoleStore<IdentityRole>>();
            Expect(resolvedRoleStore, Is.Not.Null, "Role store did not resolve");

            var resolvedUserManager = provider.GetService<UserManager<IdentityUser>>();
            Expect(resolvedUserManager, Is.Not.Null, "User manager did not resolve");

            var resolvedRoleManager = provider.GetService<RoleManager<IdentityRole>>();
            Expect(resolvedRoleManager, Is.Not.Null, "Role manager did not resolve");
        }

        [Test]
        public void AddDocumentDBStores_WithDefaultTypesViaOptions_ResolvesStoresAndManagers()
        {
            var services = new ServiceCollection();
            services.AddIdentityWithDocumentDBStores(options =>
            {
                options.EnableDocumentDbEmulatorSupport();
                options.CollectionId = databaseId;
            });
            // note: UserManager and RoleManager use logging
            services.AddLogging();

            var provider = services.BuildServiceProvider();
            var resolvedUserStore = provider.GetService<IUserStore<IdentityUser>>();
            Expect(resolvedUserStore, Is.Not.Null, "User store did not resolve");

            var resolvedRoleStore = provider.GetService<IRoleStore<IdentityRole>>();
            Expect(resolvedRoleStore, Is.Not.Null, "Role store did not resolve");

            var resolvedUserManager = provider.GetService<UserManager<IdentityUser>>();
            Expect(resolvedUserManager, Is.Not.Null, "User manager did not resolve");

            var resolvedRoleManager = provider.GetService<RoleManager<IdentityRole>>();
            Expect(resolvedRoleManager, Is.Not.Null, "Role manager did not resolve");
        }

        protected class CustomUser : IdentityUser
        {
        }

        protected class CustomRole : IdentityRole
        {
        }

        [Test]
        public void AddDocumentDBStores_WithCustomTypes_ThisShouldLookReasonableForUsers()
        {
            // this test is just to make sure I consider the interface for using custom types
            // so that it's not a horrible experience even though it should be rarely used
            var services = new ServiceCollection();
            services.AddIdentityWithDocumentDBStoresUsingCustomTypes<CustomUser, CustomRole>(client, UriFactory.CreateDatabaseUri(databaseId).ToString());
            services.AddLogging();

            var provider = services.BuildServiceProvider();
            var resolvedUserStore = provider.GetService<IUserStore<CustomUser>>();
            Expect(resolvedUserStore, Is.Not.Null, "User store did not resolve");

            var resolvedRoleStore = provider.GetService<IRoleStore<CustomRole>>();
            Expect(resolvedRoleStore, Is.Not.Null, "Role store did not resolve");

            var resolvedUserManager = provider.GetService<UserManager<CustomUser>>();
            Expect(resolvedUserManager, Is.Not.Null, "User manager did not resolve");

            var resolvedRoleManager = provider.GetService<RoleManager<CustomRole>>();
            Expect(resolvedRoleManager, Is.Not.Null, "Role manager did not resolve");
        }

        [Test]
        public void AddDocumentDBStores_ConnectionStringWithoutDatabase_Throws()
        {
            DocumentClient client = null;

            string databaseId = "fake";
            TestDelegate addDocumentDbStores = () => new ServiceCollection()
                .AddIdentity<IdentityUser, IdentityRole>()
                .RegisterDocumentDBStores<IdentityUser, IdentityRole>(client, UriFactory.CreateDatabaseUri(databaseId).ToString());

            Expect(addDocumentDbStores, Throws.Exception
                .With.Message.Contains("documentClient cannot be null"));
        }

        protected class WrongUser : IdentityUser
        {
        }

        protected class WrongRole : IdentityRole
        {
        }

        [Test]
        public void AddDocumentDBStores_MismatchedTypes_ThrowsWarningToHelpUsers()
        {
            Expect(() => new ServiceCollection()
                    .AddIdentity<IdentityUser, IdentityRole>()
                    .RegisterDocumentDBStores<WrongUser, IdentityRole>(client, UriFactory.CreateDatabaseUri(databaseId).ToString()),<commit_msg>Added test around options and custom users/roles.<commit_after>﻿namespace CoreTests
{
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Identity.DocumentDB;
    using Microsoft.Azure.Documents;
    using Microsoft.Azure.Documents.Client;
    using Microsoft.Extensions.DependencyInjection;
    using NUnit.Framework;
    using System;
    using System.Linq;
    using System.Threading.Tasks;

    [TestFixture]
    public class DocumentDBIdentityBuilderExtensionsTests : AssertionHelper
    {
        static readonly string databaseId = "AspDotNetCore.Identity.DocumentDB.Test";
        static readonly string key = "<KEY>;

        DocumentClient client = new DocumentClient(new Uri("https://localhost:8081"), key);

        [Test]
        public void AddDocumentDBStores_WithDefaultTypes_ResolvesStoresAndManagers()
        {
            var services = new ServiceCollection();
            services.AddIdentityWithDocumentDBStores(client, UriFactory.CreateDatabaseUri(databaseId).ToString());
            // note: UserManager and RoleManager use logging
            services.AddLogging();

            var provider = services.BuildServiceProvider();
            var resolvedUserStore = provider.GetService<IUserStore<IdentityUser>>();
            Expect(resolvedUserStore, Is.Not.Null, "User store did not resolve");

            var resolvedRoleStore = provider.GetService<IRoleStore<IdentityRole>>();
            Expect(resolvedRoleStore, Is.Not.Null, "Role store did not resolve");

            var resolvedUserManager = provider.GetService<UserManager<IdentityUser>>();
            Expect(resolvedUserManager, Is.Not.Null, "User manager did not resolve");

            var resolvedRoleManager = provider.GetService<RoleManager<IdentityRole>>();
            Expect(resolvedRoleManager, Is.Not.Null, "Role manager did not resolve");
        }

        [Test]
        public void AddDocumentDBStores_WithDefaultTypesViaOptions_ResolvesStoresAndManagers()
        {
            var services = new ServiceCollection();
            services.AddIdentityWithDocumentDBStores(options =>
            {
                options.EnableDocumentDbEmulatorSupport();
                options.CollectionId = databaseId;
            });
            // note: UserManager and RoleManager use logging
            services.AddLogging();

            var provider = services.BuildServiceProvider();
            var resolvedUserStore = provider.GetService<IUserStore<IdentityUser>>();
            Expect(resolvedUserStore, Is.Not.Null, "User store did not resolve");

            var resolvedRoleStore = provider.GetService<IRoleStore<IdentityRole>>();
            Expect(resolvedRoleStore, Is.Not.Null, "Role store did not resolve");

            var resolvedUserManager = provider.GetService<UserManager<IdentityUser>>();
            Expect(resolvedUserManager, Is.Not.Null, "User manager did not resolve");

            var resolvedRoleManager = provider.GetService<RoleManager<IdentityRole>>();
            Expect(resolvedRoleManager, Is.Not.Null, "Role manager did not resolve");
        }

        protected class CustomUser : IdentityUser
        {
        }

        protected class CustomRole : IdentityRole
        {
        }

        [Test]
        public void AddDocumentDBStores_WithCustomTypes_ThisShouldLookReasonableForUsers()
        {
            // this test is just to make sure I consider the interface for using custom types
            // so that it's not a horrible experience even though it should be rarely used
            var services = new ServiceCollection();
            services.AddIdentityWithDocumentDBStoresUsingCustomTypes<CustomUser, CustomRole>(client, UriFactory.CreateDatabaseUri(databaseId).ToString());
            services.AddLogging();

            var provider = services.BuildServiceProvider();
            var resolvedUserStore = provider.GetService<IUserStore<CustomUser>>();
            Expect(resolvedUserStore, Is.Not.Null, "User store did not resolve");

            var resolvedRoleStore = provider.GetService<IRoleStore<CustomRole>>();
            Expect(resolvedRoleStore, Is.Not.Null, "Role store did not resolve");

            var resolvedUserManager = provider.GetService<UserManager<CustomUser>>();
            Expect(resolvedUserManager, Is.Not.Null, "User manager did not resolve");

            var resolvedRoleManager = provider.GetService<RoleManager<CustomRole>>();
            Expect(resolvedRoleManager, Is.Not.Null, "Role manager did not resolve");
        }

        [Test]
        public void AddDocumentDBStores_WithCustomTypesViaOptions_ThisShouldLookReasonableForUsers()
        {
            // this test is just to make sure I consider the interface for using custom types
            // so that it's not a horrible experience even though it should be rarely used
            var services = new ServiceCollection();

            services.AddIdentityWithDocumentDBStores<CustomUser, CustomRole>(options =>
            {
                options.EnableDocumentDbEmulatorSupport();
                options.CollectionId = databaseId;
            });
            services.AddLogging();

            var provider = services.BuildServiceProvider();
            var resolvedUserStore = provider.GetService<IUserStore<CustomUser>>();
            Expect(resolvedUserStore, Is.Not.Null, "User store did not resolve");

            var resolvedRoleStore = provider.GetService<IRoleStore<CustomRole>>();
            Expect(resolvedRoleStore, Is.Not.Null, "Role store did not resolve");

            var resolvedUserManager = provider.GetService<UserManager<CustomUser>>();
            Expect(resolvedUserManager, Is.Not.Null, "User manager did not resolve");

            var resolvedRoleManager = provider.GetService<RoleManager<CustomRole>>();
            Expect(resolvedRoleManager, Is.Not.Null, "Role manager did not resolve");
        }

        [Test]
        public void AddDocumentDBStores_ConnectionStringWithoutDatabase_Throws()
        {
            DocumentClient client = null;

            string databaseId = "fake";
            TestDelegate addDocumentDbStores = () => new ServiceCollection()
                .AddIdentity<IdentityUser, IdentityRole>()
                .RegisterDocumentDBStores<IdentityUser, IdentityRole>(client, UriFactory.CreateDatabaseUri(databaseId).ToString());

            Expect(addDocumentDbStores, Throws.Exception
                .With.Message.Contains("documentClient cannot be null"));
        }

        protected class WrongUser : IdentityUser
        {
        }

        protected class WrongRole : IdentityRole
        {
        }

        [Test]
        public void AddDocumentDBStores_MismatchedTypes_ThrowsWarningToHelpUsers()
        {
            Expect(() => new ServiceCollection()
                    .AddIdentity<IdentityUser, IdentityRole>()
                    .RegisterDocumentDBStores<WrongUser, IdentityRole>(client, UriFactory.CreateDatabaseUri(databaseId).ToString()),
{'fraction_non_alphanumeric': 0.08514694201747419, 'fraction_numerical': 0.0006354249404289118, 'mean_word_length': 2.758507462686567, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 79, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '675491', 'n_tokens_mistral': 3004, 'n_tokens_neox': 2910, 'n_words': 693}