Пример #1
0
        public static HttpClient CreateInMemoryDbClient <TStartup>(
            this WebApplicationFactory <TStartup> factory,
            Func <ApplicationDbContext, Task> seedAction = null,
            WebApplicationFactoryClientOptions options   = null)
            where TStartup : class
        {
            var client = factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    var databaseName = Guid.NewGuid().ToString();
                    services.AddDbContext <ApplicationDbContext>(o => o.UseInMemoryDatabase(databaseName));

                    using var scope = services.BuildServiceProvider().CreateScope();

                    var scopedServices = scope.ServiceProvider;
                    var context        = scopedServices.GetRequiredService <ApplicationDbContext>();

                    seedAction?.Invoke(context).GetAwaiter().GetResult();
                });
            })
                         .CreateClient(options ?? new WebApplicationFactoryClientOptions());

            return(client);
        }
        private async Task InitOnce()
        {
            if (_app != null)
            {
                return;
            }

            LogHelper.ConfigureConsoleLogger();

            _app = new DefaultWebApplicationFactory();

            var options = new WebApplicationFactoryClientOptions()
            {
                AllowAutoRedirect = false
            };

            _httpClient = _app.CreateClient(options);
            _apiClient  = new ApiClient(_httpClient, _app.GetLogger <ApiClient>());

            _apiClient.SignInWithUserPassword_WithSlug_Async(
                TestUsers.DefaultSiteAdminName, TestUsers.DefaultSiteAdminPass,
                _httpClient, "").Wait();
            _siteAdminUser = await _apiClient.GetCurrentUserAsync();

            _gryffindorHouse = await TestTenants.CreateTenantWithRolesAndUsers(_apiClient, _httpClient, TestTenants.GryffindorHouse);

            await _apiClient.SignInWithUserPassword_WithSlug_Async(
                TestUsers.DefaultSiteAdminName, TestUsers.DefaultSiteAdminPass,
                _httpClient, "");

            _slytherinHouse = await TestTenants.CreateTenantWithRolesAndUsers(_apiClient, _httpClient, TestTenants.SlytherinHouse);
        }
        public HttpClient CreateClientWithAuthenticationToken(WebApplicationFactoryClientOptions options)
        {
            var httpClient = CreateClient(options);

            AddAuthenticationToken(httpClient);
            return(httpClient);
        }
    private async Task InitOnce()
    {
      if (_app != null) return;

      _app = new DefaultWebApplicationFactory();
      var options = new WebApplicationFactoryClientOptions()
      {
        AllowAutoRedirect = false
      };
      _httpClient = _app.CreateClient(options);
      _apiClient = new ApiClient(_httpClient, _app.GetLogger<ApiClient>());

      // create tenants here
      await _apiClient.AsSiteAdminAsync(_httpClient);
      _gryffindorHouse = await TestTenants.CreateTenantWithRolesAndUsers(_apiClient, _httpClient, TestTenants.GryffindorHouse);
      await TestBranchOffice.SeedDefaultBranchOffice(_apiClient, _httpClient, _gryffindorHouse);
      await TestDepartment.SeedDefaultDepartments(_apiClient, _gryffindorHouse);
      await TestPosition.SeedDefaultPosition(_apiClient, _gryffindorHouse);
      await TestEmployees.SeedDefaultEmployees(_apiClient, _gryffindorHouse);
      _gryffindorEmployees = (await _apiClient.GetEmployeesAsync()).ToList();

      await _apiClient.AsSiteAdminAsync(_httpClient);
      _slytherinHouse = await TestTenants.CreateTenantWithRolesAndUsers(_apiClient, _httpClient, TestTenants.SlytherinHouse);
      await TestBranchOffice.SeedDefaultBranchOffice(_apiClient, _httpClient, _slytherinHouse);
      await TestDepartment.SeedDefaultDepartments(_apiClient, _slytherinHouse);
      await TestPosition.SeedDefaultPosition(_apiClient, _slytherinHouse);
      await TestEmployees.SeedDefaultEmployees(_apiClient, _slytherinHouse);
    }
        private async Task InitOnce()
        {
            if (_app != null)
            {
                return;
            }

            LogHelper.ConfigureConsoleLogger();

            _gryffindorHouseGuid = Guid.NewGuid();
            _otherTenantGuid     = Guid.NewGuid();
            _app = new DefaultWebApplicationFactory();

            var options = new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            };

            _httpClient = _app.CreateClient(options);

            _apiClient = new ApiClient(_httpClient, _app.GetLogger <ApiClient>());

            await _apiClient.AsSiteAdminAsync(_httpClient);

            _allSiteRoles = (await _apiClient.GetRolesAsync()).ToList();

            var gryff = await TestTenants.CreateTenantWithRolesAndUsers(_apiClient, _httpClient, TestTenants.GryffindorHouse);

            _gryffindorRoles            = (await _apiClient.GetRolesAsync()).ToList();
            _gryffindorHouseAdminRoleId = gryff.TenantAdminRoleId;
            _newRoleInTenantId          = _gryffindorRoles.First(r => r.Id != _gryffindorHouseAdminRoleId.ToString()).Id;
        }
Пример #6
0
        public HttpClient GetOrCreateHttpClient(
            WebApplicationFactoryClientOptions options,
            Action <IWebHostBuilder> configureWebHostBuilder)
        {
            // We disable warning because the client is contained in a volatile read block so that
            // It will always read the latest assigned value. (ensure load-acquire semantics)
            //
            // ReSharper disable once InconsistentlySynchronizedField
            HttpClient theClient = Volatile.Read(ref client);

            if (theClient != null)
            {
                return(theClient);
            }

            lock (sync)
            {
                theClient = Volatile.Read(ref client);
                if (theClient != null)
                {
                    return(theClient);
                }

                factory = new WebApplicationFactory <T>()
                          .WithWebHostBuilder(configureWebHostBuilder);

                // Ensure store-release semantics
                Volatile.Write(ref client, factory.CreateClient(options));
                return(client);
            }
        }
Пример #7
0
 // Copy constructor
 internal WebApplicationFactoryClientOptions(WebApplicationFactoryClientOptions clientOptions)
 {
     BaseAddress              = clientOptions.BaseAddress;
     AllowAutoRedirect        = clientOptions.AllowAutoRedirect;
     MaxAutomaticRedirections = clientOptions.MaxAutomaticRedirections;
     HandleCookies            = clientOptions.HandleCookies;
 }
Пример #8
0
        private async Task InitOnce()
        {
            if (_app != null)
            {
                return;
            }

            LogHelper.ConfigureConsoleLogger();

            _app = new DefaultWebApplicationFactory();

            var options = new WebApplicationFactoryClientOptions()
            {
                AllowAutoRedirect = false
            };

            _httpClient = _app.CreateClient(/* not using options */);
            _apiClient  = new ApiClient(_httpClient, _app.GetLogger <ApiClient>());

            await _apiClient.AsSiteAdminAsync(_httpClient);

            var tenant = await TestTenants.CreateTenantWithRolesAndUsers(_apiClient, _httpClient, TestTenants.GryffindorHouse);

            _gryffindorTenantId = tenant.Tenant.Id;

            await _apiClient.AsSiteAdminAsync(_httpClient);

            var tenant2 = await TestTenants.CreateTenantWithRolesAndUsers(_apiClient, _httpClient, TestTenants.SlytherinHouse);

            _slytherinTenantId = tenant2.Tenant.Id;
        }
Пример #9
0
        /// <summary>
        /// Creates an instance of <see cref="HttpClient"/> that automatically follows
        /// redirects and handles cookies.
        /// </summary>
        /// <returns>The <see cref="HttpClient"/>.</returns>
        public HttpClient CreateClient(Action <WebApplicationFactoryClientOptions> optionsAction)
        {
            var options = new WebApplicationFactoryClientOptions();

            optionsAction?.Invoke(options);
            return(CreateClient(options));
        }
Пример #10
0
        public BaseTest(WebApplicationFactory <Startup> fixture)
        {
            var clientOptions = new WebApplicationFactoryClientOptions();

            clientOptions.BaseAddress = new Uri("http://localhost:52611");

            this.HttpClient = fixture.CreateClient(clientOptions);
        }
Пример #11
0
 public BasicTests(TestingWebAppFactory <Startup> factory)
 {
     _factory = factory;
     _options = new WebApplicationFactoryClientOptions {
         BaseAddress       = new Uri("https://localhost:5001"),
         AllowAutoRedirect = true
     };
 }
Пример #12
0
        public IntegrationTestsFixture()
        {
            var clientOptions = new WebApplicationFactoryClientOptions {
            };

            Factory = new LojaAppFactory <TStartup>();
            Client  = Factory.CreateClient(clientOptions);
        }
Пример #13
0
        public HttpClient CreateDefaultHttpClient()
        {
            var options = new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false,
            };

            return(CreateClient(options));
        }
Пример #14
0
        public Fixture()
        {
            var clientOption = new WebApplicationFactoryClientOptions();

            Config    = GetConfig();
            AppConfig = BuildAppConfig();
            Factory   = new AppFactory <TStartup>();
            Client    = Factory.CreateClient(clientOption);
        }
        public IntegrationTestsFixture()
        {
            var clientOptions = new WebApplicationFactoryClientOptions
            {
            };

            factory = new LojaVirtualAplicacaoFactory <TStartup>();
            client  = factory.CreateClient();
        }
Пример #16
0
        public ConsultaTests(WebApplicationFactory <Startup> factory)
        {
            _factory = factory;
            var options = new WebApplicationFactoryClientOptions {
                AllowAutoRedirect = false
            };

            _client = _factory.CreateClient(options);
        }
Пример #17
0
        public void GivenARequestToTheController()
        {
            _factory = new APIWebApplicationFactory();
            var options = new WebApplicationFactoryClientOptions {
                AllowAutoRedirect = false
            };

            _client = _factory.CreateClient(options);
        }
Пример #18
0
        private async Task InitOnce()
        {
            if (_app != null)
            {
                return;
            }

            LogHelper.ConfigureConsoleLogger();

            _app = new DefaultWebApplicationFactory();

            var options = new WebApplicationFactoryClientOptions()
            {
                AllowAutoRedirect = false
            };

            _httpClient = _app.CreateClient(options);

            _unauthorizedHttpClient = _app.CreateClient(options);
            _unauthorizedHttpClient.DefaultRequestHeaders.Add("accept", "application/json");

            _apiClient = new ApiClient(_httpClient, _app.GetLogger <ApiClient>());
            await _apiClient.AsSiteAdminAsync(_httpClient);

            _siteAdminUser = await _apiClient.GetCurrentUserAsync();

            _siteRoles = (await _apiClient.GetRolesAsync()).ToList();

            {
                var gryffindorTenant = await TestTenants.CreateTenantWithRolesAndUsers(
                    _apiClient, _httpClient, TestTenants.GryffindorHouse);

                _gryffindorAdminUser = gryffindorTenant.AdminUser;

                await _apiClient.AsGryffindorAdminAsync(_httpClient);

                _gryffindorRoles = (await _apiClient.GetRolesAsync()).ToList();

                _gryffindorAdminRoleId = gryffindorTenant.TenantAdminRoleId.ToString();
            }

            await _apiClient.AsSiteAdminAsync(_httpClient);

            {
                var slytherinTenant = await TestTenants.CreateTenantWithRolesAndUsers(
                    _apiClient, _httpClient, TestTenants.SlytherinHouse);

                _slytherinAdminUser = slytherinTenant.AdminUser;

                await _apiClient.AsSlytherinAdminAsync(_httpClient);

                _slytherinRoles = (await _apiClient.GetRolesAsync()).ToList();

                _slytherinAdminRoleId = slytherinTenant.TenantAdminRoleId.ToString();
            }
        }
Пример #19
0
 public AuthenticationTests(CustomWebApplicationFactory<Startup> fixture)
 {
     WebApplicationFactoryClientOptions webAppFactoryClientOptions = new WebApplicationFactoryClientOptions
     {
         // Disallow redirect so that we can check the following: Status code is redirect and redirect url is login url
         // As per https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.2#test-a-secure-endpoint
         AllowAutoRedirect = false
     };
     _httpClient = fixture.CreateClient(webAppFactoryClientOptions);
 }
Пример #20
0
 public IntegrationTestsFixture()
 {
     var clientOptions = new WebApplicationFactoryClientOptions
     {
         AllowAutoRedirect        = true,
         BaseAddress              = new Uri("http://localhost"),
         HandleCookies            = true,
         MaxAutomaticRedirections = 7
     };
 }
Пример #21
0
        protected HttpClient GetClient(bool allowAutoRedirect = false)
        {
            var options = new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = allowAutoRedirect
            };
            var client = this.factory.CreateClient(options);

            return(client);
        }
Пример #22
0
        public TestsFixture()
        {
            var clientOptions = new WebApplicationFactoryClientOptions
            {
                BaseAddress = new Uri(GetBaseUrl()),
            };

            Factory = new ApiAppFactory <TStartup>();
            Client  = Factory.CreateClient(clientOptions);
        }
Пример #23
0
        public IntegrationTestsFixture()
        {
            var clientOptions = new WebApplicationFactoryClientOptions
            {
            };

            Factory = new LojaAppFactory <TStartup>();
            //Irá emular uma aplicação na memória
            Client = Factory.CreateClient(clientOptions);
        }
Пример #24
0
        public Fixture()
        {
            var clientOptions = new WebApplicationFactoryClientOptions
            {
                BaseAddress = new Uri("http://localhost"),
            };

            var factory = new ChallengeAppFactory();

            Client = factory.CreateClient(clientOptions);
        }
        public IntegrationTestsFixture()
        {
            var clientOptions = new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = true,
                BaseAddress       = new Uri("http://localhost")
            };

            Factory = new DroneDeliveryAppFactory <TStartup>();
            Client  = Factory.CreateClient(clientOptions);
        }
Пример #26
0
        private HttpClient CreateClient(string baseAddress)
        {
            var options = new WebApplicationFactoryClientOptions
            {
                BaseAddress = new Uri(baseAddress)
            };

            var client = _fixture.CreateClient(options);

            return(client);
        }
        public IntegrationTestsFixture()
        {
            var clientOption = new WebApplicationFactoryClientOptions {
            };

            Factory       = new VaquinhaAppFactory <TStartup>();
            Client        = Factory.CreateClient(clientOption);
            Configuration = GetConfiguration();

            ConfiguracaoGeralAplicacao = BuildGlobalAppConfiguration();
        }
Пример #28
0
        public async Task LoginRedirects()
        {
            var opts = new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            };

            var client = _factory.CreateClient(opts);
            var r      = await client.GetAsync("/login");

            Assert.IsTrue(r.StatusCode == HttpStatusCode.Redirect);
        }
Пример #29
0
        public MessagePackWebApiTests(WebApiHostFactory <Startup> factory)
        {
            var options = new WebApplicationFactoryClientOptions()
            {
                BaseAddress              = new Uri("http://localhost"),
                AllowAutoRedirect        = false,
                HandleCookies            = false,
                MaxAutomaticRedirections = 0
            };

            client = factory.CreateClient(options);
        }
Пример #30
0
        internal static HttpClient GetClient <T>(WebApplicationFactory <T> factory) where T : class
        {
            Environment.SetEnvironmentVariable(Keys.TestProject, "true");
            var clientOptions = new WebApplicationFactoryClientOptions
            {
                HandleCookies     = true,
                AllowAutoRedirect = false
            };
            var client = factory.CreateClient(clientOptions);

            return(client);
        }