diff --git a/src/EmployerFinance/SFA.DAS.EmployerFinance.sln b/src/EmployerFinance/SFA.DAS.EmployerFinance.sln index 3a1b2df1a5..91cfd6943c 100644 --- a/src/EmployerFinance/SFA.DAS.EmployerFinance.sln +++ b/src/EmployerFinance/SFA.DAS.EmployerFinance.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31424.327 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34902.65 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SFA.DAS.SharedOuterApi", "..\Shared\SFA.DAS.SharedOuterApi\SFA.DAS.SharedOuterApi.csproj", "{988FB7FC-2B13-4611-AB77-A1B16C964505}" EndProject @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SFA.DAS.EmployerFinance.Api EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SFA.DAS.EmployerFinance.UnitTests", "SFA.DAS.EmployerFinance.UnitTests\SFA.DAS.EmployerFinance.UnitTests.csproj", "{EF8844A9-7309-4E8B-9306-8B4A805B5C7B}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SFA.DAS.SharedOuterApi.UnitTests", "..\Shared\SFA.DAS.SharedOuterApi.UnitTests\SFA.DAS.SharedOuterApi.UnitTests.csproj", "{9F5BF86B-5F9F-42FB-A2E2-7E5AF73618F7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {EF8844A9-7309-4E8B-9306-8B4A805B5C7B}.Debug|Any CPU.Build.0 = Debug|Any CPU {EF8844A9-7309-4E8B-9306-8B4A805B5C7B}.Release|Any CPU.ActiveCfg = Release|Any CPU {EF8844A9-7309-4E8B-9306-8B4A805B5C7B}.Release|Any CPU.Build.0 = Release|Any CPU + {9F5BF86B-5F9F-42FB-A2E2-7E5AF73618F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F5BF86B-5F9F-42FB-A2E2-7E5AF73618F7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F5BF86B-5F9F-42FB-A2E2-7E5AF73618F7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F5BF86B-5F9F-42FB-A2E2-7E5AF73618F7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Shared/SFA.DAS.SharedOuterApi.UnitTests/Services/EmployerAccountsServiceTests/WhenGettingEmployerAccountsTests.cs b/src/Shared/SFA.DAS.SharedOuterApi.UnitTests/Services/EmployerAccountsServiceTests/WhenGettingEmployerAccountsTests.cs index d25a212035..ca399b045e 100644 --- a/src/Shared/SFA.DAS.SharedOuterApi.UnitTests/Services/EmployerAccountsServiceTests/WhenGettingEmployerAccountsTests.cs +++ b/src/Shared/SFA.DAS.SharedOuterApi.UnitTests/Services/EmployerAccountsServiceTests/WhenGettingEmployerAccountsTests.cs @@ -103,6 +103,7 @@ public async Task Then_If_The_Id_Is_Not_A_Guid_Then_User_Account_IsNot_Found_The actual.TrueForAll(c => c.DisplayName.Equals(profileUserResponse.DisplayName)).Should().BeTrue(); actual.TrueForAll(c => c.IsSuspended.Equals(profileUserResponse.IsSuspended)).Should().BeTrue(); } + [Test, MoqAutoData] public async Task Then_If_The_Id_Is_Not_A_Guid_And_A_Existing_User_With_Different_Email_Then_User_Upserted_And_User_Information_Returned( Guid userId, @@ -189,9 +190,87 @@ public async Task Then_If_The_Id_Is_Not_A_Guid_And_A_New_User_With_No_Accounts_T actualRecord.DasAccountName.Should().BeNullOrEmpty(); actualRecord.EncodedAccountId.Should().BeNullOrEmpty(); actualRecord.Role.Should().BeNullOrEmpty(); - } - + + [Test, MoqAutoData] + public async Task Then_If_The_Id_Is_A_Guid_And_A_No_User_Name_With_No_Accounts_Then_User_Upserted_And_User_Information_Returned( + EmployerProfile employerProfile, + EmployerProfileUsersApiResponse profileUserResponse, + [Frozen] Mock> employerProfilesApiClient, + [Frozen] Mock> accountsApiClient, + EmployerAccountsService handler) + { + employerProfile.UserId = Guid.NewGuid().ToString(); + + accountsApiClient + .Setup(x => x.GetAll( + It.Is(c => c.GetAllUrl.Contains($"user/{profileUserResponse.Id}/accounts")))) + .ReturnsAsync(new List()); + accountsApiClient + .Setup(x => x.GetAll( + It.IsAny())) + .ReturnsAsync(new List()); + employerProfilesApiClient.Setup(x => x.GetWithResponseCode( + It.Is(c => + c.GetUrl.Contains($"api/users/{HttpUtility.UrlEncode(employerProfile.UserId)}")))) + .ReturnsAsync(new ApiResponse(null, HttpStatusCode.NotFound, "Not Found")); + + var actual = (await handler.GetEmployerAccounts(employerProfile)).ToList(); + + actual.Count.Should().Be(1); + var actualRecord = actual.First(); + actualRecord.UserId.Should().Be(employerProfile.UserId); + actualRecord.FirstName.Should().Be("Unknown"); + actualRecord.LastName.Should().Be("Unknown"); + actualRecord.IsSuspended.Should().BeFalse(); + actualRecord.DasAccountName.Should().BeNullOrEmpty(); + actualRecord.EncodedAccountId.Should().BeNullOrEmpty(); + actualRecord.Role.Should().BeNullOrEmpty(); + } + + [Test, MoqAutoData] + public async Task Then_If_The_Id_Is_Not_A_Guid_And_A_No_User_Name_With_No_Accounts_Then_User_Upserted_And_User_Information_Returned( + EmployerProfile employerProfile, + EmployerProfileUsersApiResponse profileUserResponse, + [Frozen] Mock> employerProfilesApiClient, + [Frozen] Mock> accountsApiClient, + EmployerAccountsService handler) + { + employerProfile.LastName = null; + employerProfile.FirstName = null; + profileUserResponse.LastName = null; + profileUserResponse.FirstName = null; + + accountsApiClient + .Setup(x => x.GetAll( + It.Is(c => c.GetAllUrl.Contains($"user/{profileUserResponse.Id}/accounts")))) + .ReturnsAsync(new List()); + accountsApiClient + .Setup(x => x.GetAll( + It.IsAny())) + .ReturnsAsync(new List()); + employerProfilesApiClient.Setup(x => x.GetWithResponseCode( + It.Is(c => + c.GetUrl.Contains($"api/users/{HttpUtility.UrlEncode(employerProfile.UserId)}")))) + .ReturnsAsync(new ApiResponse(null, HttpStatusCode.NotFound, "Not Found")); + employerProfilesApiClient.Setup(x => x.PutWithResponseCode( + It.Is(c => + c.PutUrl.Contains($"api/users/")))) + .ReturnsAsync(new ApiResponse(profileUserResponse, HttpStatusCode.Created, "")); + + var actual = (await handler.GetEmployerAccounts(employerProfile)).ToList(); + + actual.Count.Should().Be(1); + var actualRecord = actual.First(); + actualRecord.UserId.Should().Be(profileUserResponse.Id); + actualRecord.FirstName.Should().Be("Unknown"); + actualRecord.LastName.Should().Be("Unknown"); + actualRecord.IsSuspended.Should().Be(profileUserResponse.IsSuspended); + actualRecord.DasAccountName.Should().BeNullOrEmpty(); + actualRecord.EncodedAccountId.Should().BeNullOrEmpty(); + actualRecord.Role.Should().BeNullOrEmpty(); + } + [Test, MoqAutoData] public async Task Then_If_The_Id_Is_A_Guid_Then_User_Account_Found_And_Not_Upserted( EmployerProfile employerProfile, diff --git a/src/Shared/SFA.DAS.SharedOuterApi/Services/EmployerAccountsService.cs b/src/Shared/SFA.DAS.SharedOuterApi/Services/EmployerAccountsService.cs index 1c29f4dd35..b0a5febdfe 100644 --- a/src/Shared/SFA.DAS.SharedOuterApi/Services/EmployerAccountsService.cs +++ b/src/Shared/SFA.DAS.SharedOuterApi/Services/EmployerAccountsService.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Net; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using SFA.DAS.SharedOuterApi.Configuration; using SFA.DAS.SharedOuterApi.Infrastructure; using SFA.DAS.SharedOuterApi.InnerApi.Requests; @@ -22,11 +23,13 @@ public class EmployerAccountsService : IEmployerAccountsService { private readonly IEmployerProfilesApiClient _employerProfilesApiClient; private readonly IAccountsApiClient _accountsApiClient; + private readonly ILogger _logger; - public EmployerAccountsService(IEmployerProfilesApiClient employerProfilesApiClient, IAccountsApiClient accountsApiClient) + public EmployerAccountsService(IEmployerProfilesApiClient employerProfilesApiClient, IAccountsApiClient accountsApiClient, ILogger logger) { _employerProfilesApiClient = employerProfilesApiClient; _accountsApiClient = accountsApiClient; + _logger = logger; } public async Task> GetEmployerAccounts(EmployerProfile employerProfile) @@ -41,7 +44,6 @@ public async Task> GetEmployerAccounts(Employer await _employerProfilesApiClient.GetWithResponseCode( new GetEmployerUserAccountRequest(employerProfile.UserId)); - if (userResponse.StatusCode == HttpStatusCode.NotFound) { if (!Guid.TryParse(employerProfile.UserId, out _)) @@ -56,6 +58,18 @@ await _employerProfilesApiClient.PutWithResponseCode