Beispiel #1
0
        protected void Initialize(IEnumerable <Type> installerTypes)
        {
            var services      = new ServiceCollection();
            var configuration = TestConfigurationBuilder.GetIConfigurationRoot(AppDomain.CurrentDomain.BaseDirectory);

            var commonInstallers = typeof(DefaultInstaller).Assembly.ExportedTypes.Where(x =>
                                                                                         typeof(IInstaller).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract).Select(Activator.CreateInstance)
                                   .Cast <IInstaller>().ToList();

            commonInstallers.ForEach(installer => installer.InstallServices(services, configuration));

            var installers = typeof(Startup).Assembly.ExportedTypes.Where(x => installerTypes.Contains(x) &&
                                                                          typeof(IInstaller).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract).Select(Activator.CreateInstance)
                             .Cast <IInstaller>().ToList();

            installers.ForEach(installer => installer.InstallServices(services, configuration));

            services.AddOptions();

            //Mock Http for User Profile
            _MockHttpcontext = new Mock <IHttpContextAccessor>();

            IList <Claim> claimCollection = new List <Claim>
            {
                new Claim("sub", "D5CD2754-ED7D-4A5E-A306-11C55711E889")     //Test User UNIQUE ID
            };

            var identityMock = new Mock <ClaimsIdentity>();

            identityMock.Setup(x => x.Claims).Returns(claimCollection);

            var cp = new Mock <ClaimsPrincipal>();

            cp.Setup(m => m.HasClaim(It.IsAny <string>(), It.IsAny <string>())).Returns(true);
            cp.Setup(m => m.Identity).Returns(identityMock.Object);

            cp.SetupGet(m => m.Claims).Returns(() => {
                var result = new List <Claim>();

                result.AddRange(claimCollection);

                return(result);
            });

            var hc = new Mock <HttpContext>();

            hc.Setup(h => h.User).Returns(cp.Object);

            _MockHttpcontext.Setup(h => h.HttpContext).Returns(hc.Object);

            services.AddScoped <IUserProfile>(serviceProvider =>
            {
                var appSettings = serviceProvider.GetService <AppSettings>();

                return(new UserProfile(_MockHttpcontext.Object, serviceProvider, appSettings));
            });

            services.AddScoped <IServiceProvider>(serviceProvider => serviceProvider);

            // Build the service provider
            ObjectContainer = services.BuildServiceProvider();
        }
        protected void Initialize(IEnumerable <Type> installerTypes, bool installApiControllers = false)
        {
            var services      = new ServiceCollection();
            var configuration = TestConfigurationBuilder.GetIConfigurationRoot(AppDomain.CurrentDomain.BaseDirectory);

            var installers = typeof(Startup).Assembly.ExportedTypes.Where(x => installerTypes.Contains(x) && typeof(IInstaller).IsAssignableFrom(x) &&
                                                                          !x.IsInterface && !x.IsAbstract).Select(Activator.CreateInstance).Cast <IInstaller>().ToList();

            var commonInstallers = typeof(Web.Infrastructure.Installers.DefaultInstaller).Assembly.ExportedTypes.Where(x => installerTypes.Contains(x) && typeof(IInstaller).IsAssignableFrom(x) &&
                                                                                                                       !x.IsInterface && !x.IsAbstract).Select(Activator.CreateInstance).Cast <IInstaller>().ToList();

            var testInstallers = typeof(TestBase).Assembly.ExportedTypes.Where(x => installerTypes.Contains(x) && typeof(IInstaller).IsAssignableFrom(x) &&
                                                                               !x.IsInterface && !x.IsAbstract).Select(Activator.CreateInstance).Cast <IInstaller>().ToList();

            if (commonInstallers.Count > 0)
            {
                installers.AddRange(commonInstallers);
            }

            if (testInstallers.Count > 0)
            {
                installers.AddRange(testInstallers);
            }

            installers.ForEach(installer => installer.InstallServices(services, configuration));

            if (installApiControllers)
            {
                var controllerBaseType = typeof(ApiControllerBase);

                var controllerTypes = typeof(Startup).Assembly.DefinedTypes.Where(x => x != controllerBaseType &&
                                                                                  controllerBaseType.IsAssignableFrom(x)).ToList();

                controllerTypes.ForEach(controllerType =>
                {
                    services.AddTransient(controllerType);
                });
            }

            services.AddOptions();

            //Mock Http for User Profile
            _MockHttpcontext = new Mock <IHttpContextAccessor>();

            IList <Claim> claimCollection = new List <Claim>
            {
                new Claim("sub", "d9527ab2-94b3-4f4a-a4c3-69fa94eba8e2")     //Test User UNIQUE ID
            };

            var identityMock = new Mock <ClaimsIdentity>();

            identityMock.Setup(x => x.Claims).Returns(claimCollection);

            var cp = new Mock <ClaimsPrincipal>();

            cp.Setup(m => m.HasClaim(It.IsAny <string>(), It.IsAny <string>())).Returns(true);
            cp.Setup(m => m.Identity).Returns(identityMock.Object);

            cp.SetupGet(m => m.Claims).Returns(() =>
            {
                var result = new List <Claim>();

                result.AddRange(claimCollection);

                if (this.UserClaims != null)
                {
                    result.AddRange(this.UserClaims());
                }

                if (result.Count(x => x.Type == "sub") > 1)
                {
                    result.RemoveAt(result.FindIndex(x => x.Type == "sub"));
                }

                return(result);
            });

            var hc = new Mock <HttpContext>();

            hc.Setup(h => h.User).Returns(cp.Object);

            _MockHttpcontext.Setup(h => h.HttpContext).Returns(hc.Object);

            services.AddScoped <IUserProfile>(serviceProvider =>
            {
                return(new UserProfile(_MockHttpcontext.Object, serviceProvider));
            });

            services.AddScoped <IAuthorizationEngine>(serviceProvider => new AuthorizationFakeEngine(serviceProvider, this.UserPermissions));

            services.AddScoped(serviceProvider => serviceProvider);

            // Build the service provider
            ObjectContainer = services.BuildServiceProvider();
        }