예제 #1
0
파일: TestBase.cs 프로젝트: inshapardaz/api
        public TestBase(Role?role = null, bool periodicalsEnabled = false, bool createLibrary = true)
        {
            _periodicalsEnabled = periodicalsEnabled;
            _role = role;

            var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var projectDir  = Directory.GetCurrentDirectory();
            var configPath  = Path.Combine(projectDir, "appsettings.json");

            _factory = new WebApplicationFactory <Startup>()
                       .WithWebHostBuilder(builder =>
            {
                builder.ConfigureAppConfiguration((context, conf) =>
                {
                    conf.AddJsonFile(configPath);

                    if (!string.IsNullOrWhiteSpace(environment))
                    {
                        conf.AddJsonFile(Path.Combine(projectDir, $"appsettings.json"), true);
                        //conf.AddJsonFile(Path.Combine(projectDir, $"appsettings.{environment}.json"), true);
                    }
                });
                builder.ConfigureTestServices(services => ConfigureServices(services));
            });

            var settings = Services.GetService <Settings>();

            AccountBuilder = _factory.Services.GetService <AccountDataBuilder>();

            if (role.HasValue)
            {
                AccountBuilder = AccountBuilder.As(_role.Value).Verified();
                _account       = AccountBuilder.Build();
            }

            if (createLibrary)
            {
                var builder = LibraryBuilder.WithPeriodicalsEnabled(_periodicalsEnabled);
                if (_account != null && role.HasValue)
                {
                    builder.AssignToUser(AccountId, _role.Value);
                }
                Library = builder.Build();
            }

            Client = _factory.CreateClient();

            if (_account != null)
            {
                var token = TokenBuilder.GenerateToken(settings, _account.Id);
                Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
            }
        }