public IdentityDatabaseContextTests(string collectionPrefix)
        {
            RegisterClassMap <IdentityUser, IdentityRole, string> .Init();

            collectionPrefix = $"{typeof(IdentityDatabaseContextTests).Name}_{collectionPrefix}";

            var configuration = new ConfigurationBuilder().AddJsonFile("config.json").Build();

            _databaseContext = new IdentityDatabaseContext
            {
                ConnectionString        = configuration["Data:ConnectionString"],
                DatabaseName            = "Testing",
                EnsureCollectionIndexes = false
            };
            _databaseContext.UserCollectionName = collectionPrefix + "_" + _databaseContext.UserCollectionName;
            _databaseContext.RoleCollectionName = collectionPrefix + "_" + _databaseContext.RoleCollectionName;
        }
Beispiel #2
0
        /// <param name="collectionPrefix">Unique prefix for the collection(s). Used so dropping collections during a test run wont interfere with other test classes running in parallel. Suggest using the test class Name/FullName.</param>
        /// <param name="databaseName">Defaults to "Testing"</param>
        /// <param name="dropCollectionOnInit">Drops any collections in the database that start the collectionPrefix.</param>
        /// <param name="dropCollectionOnDispose">Drops any collections created during the test run and any collections in the database that start the collectionPrefix.</param>
        /// <remarks>
        /// config.json in test project: requires "data:connectionString" for the MongoDB connection string details
        /// </remarks>
        /// <example>
        /// collectionPrefix = $"{typeof([TEST CLASS]).Name}");
        /// </example>
        public DatabaseFixture(string collectionPrefix, string databaseName, bool dropCollectionOnInit, bool dropCollectionOnDispose)
        {
            RegisterClassMap <IdentityUser, IdentityRole, string> .Init();

            //Get connection string from config.json
            var builder = new ConfigurationBuilder().AddJsonFile("config.json");

            Configuration = builder.Build();

            ConnectionString = Configuration["Data:ConnectionString"];
            CollectionPrefix = string.IsNullOrWhiteSpace(collectionPrefix) ? DateTime.UtcNow.ToShortTimeString() : collectionPrefix;
            DatabaseName     = !string.IsNullOrWhiteSpace(databaseName) ? databaseName
                                                                        : "Testing";

            DropCollectionOnInit    = dropCollectionOnInit;
            DropCollectionOnDispose = dropCollectionOnDispose;

            if (DropCollectionOnInit)
            {
                DropCollection();
            }
        }
Beispiel #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Registers MongoDB conventions for ignoring default and blank fields
            // NOTE: if you have registered default conventions elsewhere, probably don't need to do this
            RegisterClassMap <ApplicationUser, IdentityRole, string> .Init();

            // Add Mongo Identity services to the services container.
            // registers all of the interfaces it implements to the services as "Scoped"
            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddMongoDBIdentityStores <ApplicationDbContext, ApplicationUser, IdentityRole, string>(options =>
            {
                options.ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"];                                        // No default, must be configured if using (eg "mongodb://localhost:27017" in this project's appsettings.json)
                // Only required if need to create [Client] from the connection string
                // If you are providing the [Client] or [Database] or [UserCollection] and [RoleCollection] options instead, don't need to supply [ConnectionString]
                // options.Client = [IMongoClient];									// Defaults to: uses either Client attached to [Database] (if supplied), otherwise it creates a new client using [ConnectionString]
                // options.DatabaseName = [string];									// Defaults to: "AspNetIdentity"
                // options.Database = [IMongoDatabase];								// Defaults to: Creating Database using [DatabaseName] and [Client]

                // options.UserCollectionName = [string];							// Defaults to: "AspNetUsers"
                // options.RoleCollectionName = [string];							// Defaults to: "AspNetRoles"
                // options.UserCollection = [IMongoCollection<TUser>];				// Defaults to: Creating user collection in [Database] using [UserCollectionName] and [CollectionSettings]
                // options.RoleCollection = [IMongoCollection<TRole>];				// Defaults to: Creating user collection in [Database] using [RoleCollectionName] and [CollectionSettings]
                // options.CollectionSettings = [MongoCollectionSettings];			// Defaults to: { WriteConcern = WriteConcern.WMajority } => Used when creating default [UserCollection] and [RoleCollection]

                // options.EnsureCollectionIndexes = [bool];						// Defaults to: false => Used to ensure the User and Role collections have been created in MongoDB and indexes assigned. Only runs on first calls to user and role collections.
                // options.CreateCollectionOptions = [CreateCollectionOptions];		// Defaults to: { AutoIndexId = true } => Used when [EnsureCollectionIndexes] is true and User or Role collections need to be created.
                // options.CreateIndexOptions = [CreateIndexOptions];				// Defaults to: { Background = true, Sparse = true } => Used when [EnsureCollectionIndexes] is true and any indexes need to be created.
            })
            .AddDefaultTokenProviders();

            // Add MVC services to the services container.
            services.AddMvc();

            // Add application services.
            services.AddTransient <IEmailSender, AuthMessageSender>();
            services.AddTransient <ISmsSender, AuthMessageSender>();
        }