Beispiel #1
0
        public async Task InitializeAsync()
        {
            // Creates the IConfiguration instance appropriately for tests
            var cfg      = BuildConfiguration();
            var services = new ServiceCollection();

            services.AddSingleton(cfg);

            // Adds all controllers from main assembly. By default, the controllers are not loaded so this is necessary.
            // AddControllers() and AddApplicationPart().AddControllers() did not work.
            var controllers = typeof(AuthenticationController).Assembly.ExportedTypes.Where(x => !x.IsAbstract && typeof(ControllerBase).IsAssignableFrom(x)).ToList();

            controllers.ForEach(c => services.AddTransient(c));

            // Executes the same ConfigureServices() as the MatchBox application
            var startup = new MatchBox.Startup(cfg);

            startup.ConfigureServices(services);

            // Adds logging?
            services.AddLogging(builder =>
            {
                //
                builder.AddConsole();
            });

            // This is necessary for controller testing
            services.AddTransient <IHttpContextAccessor>(
                sp =>
            {
                return(new HttpContextAccessor
                {
                    HttpContext = new DefaultHttpContext
                    {
                        RequestServices = ServiceProvider
                    }
                });
            });

            // Builds the service provider
            ServiceProvider = services.BuildServiceProvider();

            // Now initializes the temporary db
            var init = new MatchBoxDbInitializer(ServiceProvider);
            await init.Initialize();
        }
Beispiel #2
0
        public async Task LoginOkForDefaultUsers()
        {
            var ctrl = CreateController();

#pragma warning disable IDE0059 // Unnecessary assignment of a value
            foreach (var name in MatchBoxDbInitializer.GetDefaultUserNames())
#pragma warning restore IDE0059 // Unnecessary assignment of a value
            {
                var result = await ctrl.Login(new LoginModel
                {
                    UsernameOrEmail = MatchBoxDbContext.AdminUserName,
                    Password        = MatchBoxDbInitializer.DefaultPassword
                });

                var okResult = result.Result as OkObjectResult;
                Assert.NotNull(okResult);
            }
        }