Пример #1
0
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                var serviceProvider = new ServiceCollection()
                                      .AddEntityFrameworkInMemoryDatabase()
                                      .BuildServiceProvider();

                services.AddDbContext <PGPDbContext>(options =>
                {
                    options.UseInMemoryDatabase("InMemoryDatabaseForTesting");
                    options.UseInternalServiceProvider(serviceProvider);
                });

                services.AddScoped <IPGPDbContext>(provider => provider.GetService <PGPDbContext>());

                var sp = services.BuildServiceProvider();

                using (var scope = sp.CreateScope())
                {
                    var scopedServices = scope.ServiceProvider;
                    var context        = scopedServices.GetRequiredService <PGPDbContext>();

                    context.Database.EnsureCreated();

                    ClientUtilities.InitializeDbForTests(context);
                }
            })
            .UseEnvironment("Test");
        }
Пример #2
0
        public async Task <HttpClient> GetAuthenticatedClientAsync(string role = "Admin")
        {
            var client = CreateClient();

            var command = new UserLoginCommand();

            switch (role)
            {
            case "Admin":
                command.Email    = "*****@*****.**";
                command.Password = "******";
                break;

            case "Moderator":
                command.Email    = "*****@*****.**";
                command.Password = "******";
                break;

            case "User":
                command.Email    = "*****@*****.**";
                command.Password = "******";
                break;

            default:
                throw new Exception("Specified Role not exists");
            }

            var content = ClientUtilities.GetRequestContent(command);

            var response = await client.PostAsync($"/api/users/login", content);

            var responseContent = await ClientUtilities.GetResponseContent <UserLoginCommandResponse>(response);

            client.SetBearerToken(responseContent.JwtToken);

            return(client);
        }