diff --git a/src/SFA.DAS.EmployerAccounts.UnitTests/Services/ProviderRegistration/WhenIGetInvitation.cs b/src/SFA.DAS.EmployerAccounts.UnitTests/Services/ProviderRegistration/WhenIGetInvitation.cs new file mode 100644 index 0000000000..541bd5163d --- /dev/null +++ b/src/SFA.DAS.EmployerAccounts.UnitTests/Services/ProviderRegistration/WhenIGetInvitation.cs @@ -0,0 +1,95 @@ +using System; +using System.Configuration; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Moq; +using Moq.Protected; +using NUnit.Framework; +using SFA.DAS.EmployerAccounts.Configuration; +using SFA.DAS.EmployerAccounts.Interfaces; +using SFA.DAS.EmployerAccounts.Services; + +namespace SFA.DAS.EmployerAccounts.UnitTests.Services.ProviderRegistration +{ + class WhenIGetInvitation + { + private IProviderRegistrationApiClient _sut; + private ProviderRegistrationClientApiConfiguration _configuration; + private string _correlationId; + private string _testData; + private string _apiBaseUrl; + private string _identifierUri; + Mock _mockHttpMessageHandler; + Mock> _logger; + + [SetUp] + public void Arrange() + { + ConfigurationManager.AppSettings["EnvironmentName"] = "LOCAL"; + _apiBaseUrl = $"http://{Guid.NewGuid().ToString()}/"; + _identifierUri = Guid.NewGuid().ToString(); + _correlationId = Guid.NewGuid().ToString(); + _testData = "Employer details"; + + _logger = new Mock>(); + + _configuration = new ProviderRegistrationClientApiConfiguration + { + BaseUrl = _apiBaseUrl, + IdentifierUri = _identifierUri + }; + + _mockHttpMessageHandler = new Mock(); + + _mockHttpMessageHandler + .Protected() + .Setup>("SendAsync", + ItExpr.IsAny(), + ItExpr.IsAny()) + .ReturnsAsync(new HttpResponseMessage + { + Content = new StringContent(_testData), + StatusCode = HttpStatusCode.OK + } + ).Verifiable(""); + + var httpClient = new HttpClient(_mockHttpMessageHandler.Object); + + _sut = new ProviderRegistrationApiClient(httpClient, _configuration, _logger.Object); + } + + [Test] + public async Task Then_Verify_ProviderRegistrationApi_ToGetInvitationIsCalled() + { + //act + await _sut.GetInvitations(_correlationId); + + //assert + _mockHttpMessageHandler + .Protected() + .Verify("SendAsync", Times.Once(), + ItExpr.Is(r => r.Method == HttpMethod.Get + && r.RequestUri.AbsoluteUri == $"{_configuration.BaseUrl}api/invitations/{_correlationId}"), + ItExpr.IsAny()); + } + + [Test] + public async Task WhenUnsubscribeProviderEmail_Then_Verify_ProviderRegistrationApiToUnsubscribeIsCalled() + { + //act + await _sut.Unsubscribe(_correlationId); + + //assert + _mockHttpMessageHandler + .Protected() + .Verify("SendAsync", Times.Once(), + ItExpr.Is(r => r.Method == HttpMethod.Get + && r.RequestUri.AbsoluteUri == $"{_configuration.BaseUrl}api/unsubscribe/{_correlationId}"), + ItExpr.IsAny()); + } + + } +} diff --git a/src/SFA.DAS.EmployerAccounts.Web/DependencyResolution/IoC.cs b/src/SFA.DAS.EmployerAccounts.Web/DependencyResolution/IoC.cs index ce1b98f4b5..89bf249691 100644 --- a/src/SFA.DAS.EmployerAccounts.Web/DependencyResolution/IoC.cs +++ b/src/SFA.DAS.EmployerAccounts.Web/DependencyResolution/IoC.cs @@ -57,6 +57,7 @@ public static IContainer Initialize() c.AddRegistry(); c.AddRegistry(); c.AddRegistry(); + c.AddRegistry(); }); } } diff --git a/src/SFA.DAS.EmployerAccounts/Commands/UnsubscribeProviderEmail/UnsubscribeProviderEmailQueryHandler.cs b/src/SFA.DAS.EmployerAccounts/Commands/UnsubscribeProviderEmail/UnsubscribeProviderEmailQueryHandler.cs index 0d92d1dc22..4a339ca9ed 100644 --- a/src/SFA.DAS.EmployerAccounts/Commands/UnsubscribeProviderEmail/UnsubscribeProviderEmailQueryHandler.cs +++ b/src/SFA.DAS.EmployerAccounts/Commands/UnsubscribeProviderEmail/UnsubscribeProviderEmailQueryHandler.cs @@ -1,42 +1,25 @@ using System.Threading.Tasks; using MediatR; -using SFA.DAS.EmployerAccounts.Configuration; using SFA.DAS.EmployerAccounts.Interfaces; +using SFA.DAS.NLog.Logger; namespace SFA.DAS.EmployerAccounts.Commands.UnsubscribeProviderEmail { public class UnsubscribeProviderEmailQueryHandler : AsyncRequestHandler { - private readonly EmployerAccountsConfiguration _configuration; - private readonly IHttpService _httpService; + private readonly IProviderRegistrationApiClient _providerRegistrationApiClient; + private readonly ILog _logger; - public UnsubscribeProviderEmailQueryHandler( - IHttpServiceFactory httpServiceFactory, - EmployerAccountsConfiguration configuration) + public UnsubscribeProviderEmailQueryHandler(IProviderRegistrationApiClient providerRegistrationApiClient, ILog logger) { - _configuration = configuration; - _httpService = httpServiceFactory.Create( - configuration.ProviderRegistrationsApi.IdentifierUri, - configuration.ProviderRegistrationsApi.ClientId, - configuration.ProviderRegistrationsApi.ClientSecret, - configuration.ProviderRegistrationsApi.Tenant - ); + _providerRegistrationApiClient = providerRegistrationApiClient; + _logger = logger; } protected override async Task HandleCore(UnsubscribeProviderEmailQuery message) { - var baseUrl = GetBaseUrl(); - var url = $"{baseUrl}api/unsubscribe/{message.CorrelationId.ToString()}"; - await _httpService.GetAsync(url, false); - } - - private string GetBaseUrl() - { - var baseUrl = _configuration.ProviderRegistrationsApi.BaseUrl.EndsWith("/") - ? _configuration.ProviderRegistrationsApi.BaseUrl - : _configuration.ProviderRegistrationsApi.BaseUrl + "/"; - - return baseUrl; - } + await _providerRegistrationApiClient.Unsubscribe(message.CorrelationId.ToString()); + _logger.Info($"Sent ProviderEmail to Unsubscribe {message.CorrelationId}"); + } } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerAccounts/Configuration/EmployerAccountsConfiguration.cs b/src/SFA.DAS.EmployerAccounts/Configuration/EmployerAccountsConfiguration.cs index 5df569e112..a0a7e5479e 100644 --- a/src/SFA.DAS.EmployerAccounts/Configuration/EmployerAccountsConfiguration.cs +++ b/src/SFA.DAS.EmployerAccounts/Configuration/EmployerAccountsConfiguration.cs @@ -29,7 +29,7 @@ public class EmployerAccountsConfiguration : ITopicMessagePublisherConfiguration public string Hashstring { get; set; } public HmrcConfiguration Hmrc { get; set; } public PensionRegulatorConfiguration PensionRegulatorApi { get; set; } - public ProviderRegistrationsConfiguration ProviderRegistrationsApi { get; set; } + public ProviderRegistrationClientApiConfiguration ProviderRegistrationsApi { get; set; } public IdentityServerConfiguration Identity { get; set; } public string LegacyServiceBusConnectionString { get; set; } public string MessageServiceBusConnectionString => LegacyServiceBusConnectionString; diff --git a/src/SFA.DAS.EmployerAccounts/Configuration/ProviderRegistrationClientApiConfiguration.cs b/src/SFA.DAS.EmployerAccounts/Configuration/ProviderRegistrationClientApiConfiguration.cs new file mode 100644 index 0000000000..139eb0b35f --- /dev/null +++ b/src/SFA.DAS.EmployerAccounts/Configuration/ProviderRegistrationClientApiConfiguration.cs @@ -0,0 +1,10 @@ +using SFA.DAS.EmployerAccounts.Interfaces; + +namespace SFA.DAS.EmployerAccounts.Configuration +{ + public class ProviderRegistrationClientApiConfiguration : IProviderRegistrationClientApiConfiguration + { + public string BaseUrl { get; set; } + public string IdentifierUri { get; set; } + } +} diff --git a/src/SFA.DAS.EmployerAccounts/DependencyResolution/ProviderRegistrationApiClientRegistry.cs b/src/SFA.DAS.EmployerAccounts/DependencyResolution/ProviderRegistrationApiClientRegistry.cs new file mode 100644 index 0000000000..43792fa233 --- /dev/null +++ b/src/SFA.DAS.EmployerAccounts/DependencyResolution/ProviderRegistrationApiClientRegistry.cs @@ -0,0 +1,33 @@ +using System.Net.Http; +using SFA.DAS.EmployerAccounts.Configuration; +using SFA.DAS.EmployerAccounts.Interfaces; +using SFA.DAS.EmployerAccounts.Services; +using SFA.DAS.Http; +using SFA.DAS.NLog.Logger.Web.MessageHandlers; +using StructureMap; + +namespace SFA.DAS.EmployerAccounts.DependencyResolution +{ + public class ProviderRegistrationApiClientRegistry : Registry + { + public ProviderRegistrationApiClientRegistry() + { + For().Use(c => c.GetInstance().ProviderRegistrationsApi); + For().Use(c => c.GetInstance()); + For().Use() + .Ctor().Is(c => CreateClient(c)); + } + + private HttpClient CreateClient(IContext context) + { + HttpClient httpClient = new HttpClientBuilder() + .WithHandler(new RequestIdMessageRequestHandler()) + .WithHandler(new SessionIdMessageRequestHandler()) + .WithDefaultHeaders() + .Build(); + + + return httpClient; + } + } +} diff --git a/src/SFA.DAS.EmployerAccounts/Interfaces/IProviderRegistrationApiClient.cs b/src/SFA.DAS.EmployerAccounts/Interfaces/IProviderRegistrationApiClient.cs new file mode 100644 index 0000000000..a5c5db2443 --- /dev/null +++ b/src/SFA.DAS.EmployerAccounts/Interfaces/IProviderRegistrationApiClient.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerAccounts.Interfaces +{ + public interface IProviderRegistrationApiClient + { + Task Unsubscribe(string CorrelationId); + + Task GetInvitations(string CorrelationId); + } +} diff --git a/src/SFA.DAS.EmployerAccounts/Interfaces/IProviderRegistrationClientApiConfiguration.cs b/src/SFA.DAS.EmployerAccounts/Interfaces/IProviderRegistrationClientApiConfiguration.cs new file mode 100644 index 0000000000..55ca7963db --- /dev/null +++ b/src/SFA.DAS.EmployerAccounts/Interfaces/IProviderRegistrationClientApiConfiguration.cs @@ -0,0 +1,9 @@ + +namespace SFA.DAS.EmployerAccounts.Interfaces +{ + public interface IProviderRegistrationClientApiConfiguration + { + string BaseUrl { get; } + string IdentifierUri { get; } + } +} diff --git a/src/SFA.DAS.EmployerAccounts/Queries/GetProviderInvitation/GetProviderInvitationQueryHandler.cs b/src/SFA.DAS.EmployerAccounts/Queries/GetProviderInvitation/GetProviderInvitationQueryHandler.cs index b4dcb1b535..6beaddb75f 100644 --- a/src/SFA.DAS.EmployerAccounts/Queries/GetProviderInvitation/GetProviderInvitationQueryHandler.cs +++ b/src/SFA.DAS.EmployerAccounts/Queries/GetProviderInvitation/GetProviderInvitationQueryHandler.cs @@ -1,49 +1,32 @@ using System.Threading.Tasks; using MediatR; -using SFA.DAS.EmployerAccounts.Configuration; -using SFA.DAS.EmployerAccounts.Interfaces; using SFA.DAS.EmployerAccounts.Models; using Newtonsoft.Json; +using SFA.DAS.EmployerAccounts.Interfaces; +using SFA.DAS.NLog.Logger; namespace SFA.DAS.EmployerAccounts.Queries.GetProviderInvitation { public class GetProviderInvitationQueryHandler : IAsyncRequestHandler { - private readonly EmployerAccountsConfiguration _configuration; - private readonly IHttpService _httpService; + private readonly IProviderRegistrationApiClient _providerRegistrationApiClient; + private readonly ILog _logger; - public GetProviderInvitationQueryHandler( - IHttpServiceFactory httpServiceFactory, - EmployerAccountsConfiguration configuration) + public GetProviderInvitationQueryHandler(IProviderRegistrationApiClient providerRegistrationApiClient, ILog logger) { - _configuration = configuration; - _httpService = httpServiceFactory.Create( - configuration.ProviderRegistrationsApi.IdentifierUri, - configuration.ProviderRegistrationsApi.ClientId, - configuration.ProviderRegistrationsApi.ClientSecret, - configuration.ProviderRegistrationsApi.Tenant - ); + _providerRegistrationApiClient = providerRegistrationApiClient; + _logger = logger; } public async Task Handle(GetProviderInvitationQuery message) { - var baseUrl = GetBaseUrl(); - var url = $"{baseUrl}api/invitations/{message.CorrelationId.ToString()}"; - var json = await _httpService.GetAsync(url, false); - + _logger.Info($"Get Invitations for {message.CorrelationId}"); + var json = await _providerRegistrationApiClient.GetInvitations(message.CorrelationId.ToString()); + _logger.Info($"Request sent Get Invitations for {message.CorrelationId} {json}"); return new GetProviderInvitationResponse { Result = json == null ? null : JsonConvert.DeserializeObject(json) }; } - - private string GetBaseUrl() - { - var baseUrl = _configuration.ProviderRegistrationsApi.BaseUrl.EndsWith("/") - ? _configuration.ProviderRegistrationsApi.BaseUrl - : _configuration.ProviderRegistrationsApi.BaseUrl + "/"; - - return baseUrl; - } } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerAccounts/Services/ProviderRegistrationApiClient.cs b/src/SFA.DAS.EmployerAccounts/Services/ProviderRegistrationApiClient.cs new file mode 100644 index 0000000000..d106c2f129 --- /dev/null +++ b/src/SFA.DAS.EmployerAccounts/Services/ProviderRegistrationApiClient.cs @@ -0,0 +1,62 @@ +using System.Configuration; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Microsoft.Azure.Services.AppAuthentication; +using Microsoft.Extensions.Logging; +using SFA.DAS.Authentication.Extensions.Legacy; +using SFA.DAS.EmployerAccounts.Interfaces; + +namespace SFA.DAS.EmployerAccounts.Services +{ + public class ProviderRegistrationApiClient : ApiClientBase, IProviderRegistrationApiClient + { + private readonly string _apiBaseUrl; + private readonly string _identifierUri; + private readonly HttpClient _client; + private readonly ILogger _logger; + + public ProviderRegistrationApiClient(HttpClient client, IProviderRegistrationClientApiConfiguration configuration, ILogger logger) : base(client) + { + _apiBaseUrl = configuration.BaseUrl.EndsWith("/") + ? configuration.BaseUrl + : configuration.BaseUrl + "/"; + + _identifierUri = configuration.IdentifierUri; + _client = client; + _logger = logger; + } + + public async Task Unsubscribe(string CorrelationId) + { + await AddAuthenticationHeader(); + + var url = $"{_apiBaseUrl}api/unsubscribe/{CorrelationId}"; + _logger.LogInformation($"Getting Unsubscribe {url}"); + await _client.GetAsync(url); + } + + public async Task GetInvitations(string CorrelationId) + { + await AddAuthenticationHeader(); + + var url = $"{_apiBaseUrl}api/invitations/{CorrelationId}"; + _logger.LogInformation($"Getting Invitations {url}"); + var response = await _client.GetAsync(url); + response.EnsureSuccessStatusCode(); + + return await response.Content.ReadAsStringAsync().ConfigureAwait(false); + } + + private async Task AddAuthenticationHeader() + { + if (ConfigurationManager.AppSettings["EnvironmentName"].ToUpper() != "LOCAL") + { + var azureServiceTokenProvider = new AzureServiceTokenProvider(); + var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(_identifierUri); + + _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + } + } + } +}