public AccessTokenProvider(IntegrationTestSettings settings)
 {
     _settings = settings;
     _confidentialClientApp = ConfidentialClientApplicationBuilder
                              .Create(settings.AppAuthentication.ClientId)
                              .WithClientSecret(settings.AppAuthentication.ClientSecret)
                              .WithAuthority(settings.Authority)
                              .Build();
     _client = new HttpClient();
 }
Exemple #2
0
        public AppFixture()
        {
            _webAppFactory = new WebApplicationFactory <Startup>()
                             .WithWebHostBuilder(builder =>
            {
                builder.UseEnvironment("IntegrationTesting");
                builder.ConfigureAppConfiguration(configBuilder =>
                {
                    // Adds user secrets for the integration test project
                    // Contains the Key Vault URL and API authentication settings for me
                    configBuilder.AddUserSecrets <AppFixture>();

                    // Build temporary config, get Key Vault URL, add Key Vault as config source
                    var config         = configBuilder.Build();
                    string keyVaultUrl = config["IntegrationTest:KeyVaultUrl"];
                    if (!string.IsNullOrEmpty(keyVaultUrl))
                    {
                        // CI / CD pipeline sets up a credentials environment variable to use
                        var credentialsJson = Environment.GetEnvironmentVariable("AZURE_CREDENTIALS");
                        // If it is not present, we are running locally
                        if (string.IsNullOrEmpty(credentialsJson))
                        {
                            // Use local user authentication
                            configBuilder.AddAzureKeyVault(keyVaultUrl);
                        }
                        else
                        {
                            // Use credentials in JSON object
                            var credentials  = (JObject)JsonConvert.DeserializeObject(credentialsJson);
                            var clientId     = credentials?.Value <string>("clientId");
                            var clientSecret = credentials?.Value <string>("clientSecret");
                            configBuilder.AddAzureKeyVault(keyVaultUrl, clientId, clientSecret);
                        }

                        config = configBuilder.Build();
                    }

                    Settings = config.GetSection("IntegrationTest").Get <IntegrationTestSettings>();
                });
            });
            Client = _webAppFactory.CreateDefaultClient();
        }