public void LtsaServiceCollection_Success() { // Arrange var helper = new TestHelper(); var user = PrincipalHelper.CreateForPermission(); helper.AddSingleton(user); var builder = new ConfigurationBuilder(); var options = new LtsaOptions() { IntegratorUsername = "******", IntegratorPassword = "******", MyLtsaUsername = "******", MyLtsaUserPassword = "******", }; var ltsaJson = JsonSerializer.Serialize(new { Ltsa = options }); IConfigurationRoot ltsaConfig; using (var io = new MemoryStream(Encoding.UTF8.GetBytes(ltsaJson))) { builder.AddJsonStream(io); ltsaConfig = builder.Build(); } var mockClientFactory = new Mock <IHttpClientFactory>(); var mockIOptionsMonitor = new Mock <IOptionsMonitor <JsonSerializerOptions> >(); var mockIlogger = new Mock <ILogger <HttpRequestClient> >(); var mockILtsaService = new Mock <ILogger <ILtsaService> >(); helper.AddSingleton(mockClientFactory.Object); helper.AddSingleton(mockIOptionsMonitor.Object); helper.AddSingleton(mockIlogger.Object); helper.AddSingleton(mockILtsaService.Object); // Act _ = helper.Services.AddLtsaService(section: ltsaConfig.GetSection("Ltsa")); var ltsaOptions = helper.GetService <IOptions <LtsaOptions> >(); var ltsaService = helper.GetService <ILtsaService>(); var httpRequestClient = helper.GetService <IHttpRequestClient>(); var jwtSecurityTokenHandler = helper.GetService <JwtSecurityTokenHandler>(); // Assert Assert.NotNull(ltsaService); Assert.NotNull(httpRequestClient); Assert.NotNull(jwtSecurityTokenHandler); Assert.NotNull(ltsaOptions); ltsaOptions.Value.AuthUrl.Should().Be(options.AuthUrl); ltsaOptions.Value.HostUri.Should().Be(options.HostUri); ltsaOptions.Value.IntegratorPassword.Should().Be(options.IntegratorPassword); ltsaOptions.Value.IntegratorUsername.Should().Be(options.IntegratorUsername); ltsaOptions.Value.MyLtsaUsername.Should().Be(options.MyLtsaUsername); ltsaOptions.Value.OrdersEndpoint.Should().Be(options.OrdersEndpoint); ltsaOptions.Value.RefreshEndpoint.Should().Be(options.RefreshEndpoint); }
/// <summary> /// Creates a new instance of a LtsaService, initializes with specified arguments. /// </summary> /// <param name="options"></param> /// <param name="client"></param> /// <param name="tokenHandler"></param> /// <param name="logger"></param> /// <param name="serializerOptions"></param> public LtsaService(IOptions <LtsaOptions> options, IHttpRequestClient client, ILogger <ILtsaService> logger, IOptions <JsonSerializerOptions> serializerOptions) { this.Options = options.Value; this.Client = client; _logger = logger; _jsonSerializerOptions = serializerOptions.Value; _authPolicy = Policy .Handle <HttpClientRequestException>(ex => ex.StatusCode == HttpStatusCode.Forbidden || ex.StatusCode == HttpStatusCode.Unauthorized) .RetryAsync(async(exception, retryCount) => { _token = await RefreshAccessTokenAsync(); this.Client.Client?.DefaultRequestHeaders?.Clear(); this.Client.Client?.DefaultRequestHeaders?.Add("X-Authorization", $"Bearer {_token.AccessToken}"); }); }