-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store the user from claim details during authentication
- Loading branch information
1 parent
c8de2e5
commit d9a4fff
Showing
9 changed files
with
342 additions
and
210 deletions.
There are no files selected for viewing
251 changes: 125 additions & 126 deletions
251
src/SFA.DAS.EmployerFinance.AcceptanceTests/Steps/Hooks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,128 @@ | ||
using System; | ||
using System.Data.Common; | ||
using System.Diagnostics; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using BoDi; | ||
using Moq; | ||
using NServiceBus; | ||
using SFA.DAS.EmployerFinance.AcceptanceTests.DependencyResolution; | ||
using SFA.DAS.EmployerFinance.AcceptanceTests.Extensions; | ||
using SFA.DAS.EmployerFinance.Configuration; | ||
using SFA.DAS.EmployerFinance.Interfaces; | ||
using SFA.DAS.NLog.Logger; | ||
using SFA.DAS.NServiceBus.Configuration; | ||
using SFA.DAS.NServiceBus.Configuration.NLog; | ||
using SFA.DAS.NServiceBus.Configuration.StructureMap; | ||
using SFA.DAS.NServiceBus.Configuration.NewtonsoftJsonSerializer; | ||
using SFA.DAS.NServiceBus.SqlServer.Configuration; | ||
using SFA.DAS.Testing.AzureStorageEmulator; | ||
using SFA.DAS.UnitOfWork.NServiceBus; | ||
using StructureMap; | ||
using TechTalk.SpecFlow; | ||
using BoDi; | ||
using Moq; | ||
using NServiceBus; | ||
using SFA.DAS.EmployerFinance.AcceptanceTests.DependencyResolution; | ||
using SFA.DAS.EmployerFinance.AcceptanceTests.Extensions; | ||
using SFA.DAS.EmployerFinance.Configuration; | ||
using SFA.DAS.EmployerFinance.Interfaces; | ||
using SFA.DAS.NLog.Logger; | ||
using SFA.DAS.NServiceBus.Configuration; | ||
using SFA.DAS.NServiceBus.Configuration.NewtonsoftJsonSerializer; | ||
using SFA.DAS.NServiceBus.Configuration.NLog; | ||
using SFA.DAS.NServiceBus.Configuration.StructureMap; | ||
using SFA.DAS.NServiceBus.SqlServer.Configuration; | ||
using SFA.DAS.Testing.AzureStorageEmulator; | ||
using SFA.DAS.UnitOfWork.NServiceBus.Configuration; | ||
using StructureMap; | ||
using System; | ||
using System.Data.Common; | ||
using System.Diagnostics; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using TechTalk.SpecFlow; | ||
|
||
namespace SFA.DAS.EmployerFinance.AcceptanceTests.Steps | ||
{ | ||
[Binding] | ||
public class Hooks | ||
{ | ||
private static IContainer _container; | ||
private static IEndpointInstance _endpoint; | ||
private readonly IObjectContainer _objectContainer; | ||
|
||
public Hooks(IObjectContainer objectContainer) | ||
{ | ||
_objectContainer = objectContainer; | ||
} | ||
|
||
[BeforeTestRun] | ||
public static async Task BeforeTestRun() | ||
{ | ||
AzureStorageEmulatorManager.StartStorageEmulator(); | ||
|
||
_container = IoC.Initialize(); | ||
|
||
await StartNServiceBusEndpoint(); | ||
} | ||
|
||
[BeforeScenario] | ||
public void BeforeScenario() | ||
{ | ||
_container.GetInstance<ILog>().Info("Starting Scenario."); | ||
|
||
ResetCurrentDateTime(_container); | ||
ResetFundsExpiryPeriod(_container); | ||
|
||
_objectContainer.RegisterInstances(_container); | ||
_objectContainer.RegisterMocks(_container); | ||
} | ||
|
||
[AfterScenario] | ||
public void AfterScenario() | ||
{ | ||
_objectContainer.Dispose(); | ||
} | ||
|
||
[AfterTestRun] | ||
public static async Task AfterTestRun() | ||
{ | ||
using (_container) | ||
{ | ||
await StopNServiceBusEndpoint(); | ||
} | ||
} | ||
|
||
private static void ResetCurrentDateTime(IContainer container) | ||
{ | ||
var currentDateTime = container.GetInstance<Mock<ICurrentDateTime>>(); | ||
currentDateTime.Setup(x => x.Now).Returns(DateTime.Now); | ||
} | ||
|
||
private static void ResetFundsExpiryPeriod(IContainer container) | ||
{ | ||
var config = container.GetInstance<EmployerFinanceConfiguration>(); | ||
config.FundsExpiryPeriod = 24; | ||
} | ||
|
||
private static async Task StartNServiceBusEndpoint() | ||
{ | ||
try | ||
{ | ||
_container.GetInstance<ILog>().Info("Starting endpoint."); | ||
|
||
var endpointConfiguration = new EndpointConfiguration("SFA.DAS.EmployerFinance.AcceptanceTests") | ||
.UseAzureServiceBusTransport() | ||
.UseErrorQueue("SFA.DAS.EmployerFinance.AcceptanceTests-errors") | ||
.UseInstallers() | ||
.UseLicense(WebUtility.HtmlDecode(_container.GetInstance<EmployerFinanceConfiguration>().NServiceBusLicense)) | ||
.UseSqlServerPersistence(() => _container.GetInstance<DbConnection>()) | ||
.UseNewtonsoftJsonSerializer() | ||
.UseNLogFactory() | ||
.UseOutbox() | ||
.UseStructureMapBuilder(_container) | ||
.UseUnitOfWork(); | ||
|
||
if (Debugger.IsAttached) | ||
{ | ||
endpointConfiguration.PurgeOnStartup(true); | ||
} | ||
|
||
_endpoint = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); | ||
|
||
_container.Configure(c => c.For<IMessageSession>().Use(_endpoint)); | ||
|
||
_container.GetInstance<ILog>().Info("Endpoint Started."); | ||
} | ||
catch (Exception e) | ||
{ | ||
_container.GetInstance<ILog>().Error(e, "Error starting endpoint."); | ||
throw; | ||
} | ||
} | ||
|
||
private static Task StopNServiceBusEndpoint() | ||
{ | ||
return _endpoint.Stop(); | ||
} | ||
} | ||
namespace SFA.DAS.EmployerFinance.AcceptanceTests.Steps | ||
{ | ||
[Binding] | ||
public class Hooks | ||
{ | ||
private static IContainer _container; | ||
private static IEndpointInstance _endpoint; | ||
private readonly IObjectContainer _objectContainer; | ||
|
||
public Hooks(IObjectContainer objectContainer) | ||
{ | ||
_objectContainer = objectContainer; | ||
} | ||
|
||
[BeforeTestRun] | ||
public static async Task BeforeTestRun() | ||
{ | ||
AzureStorageEmulatorManager.StartStorageEmulator(); | ||
|
||
_container = IoC.Initialize(); | ||
|
||
await StartNServiceBusEndpoint(); | ||
} | ||
|
||
[BeforeScenario] | ||
public void BeforeScenario() | ||
{ | ||
_container.GetInstance<ILog>().Info("Starting Scenario."); | ||
|
||
ResetCurrentDateTime(_container); | ||
ResetFundsExpiryPeriod(_container); | ||
|
||
_objectContainer.RegisterInstances(_container); | ||
_objectContainer.RegisterMocks(_container); | ||
} | ||
|
||
[AfterScenario] | ||
public void AfterScenario() | ||
{ | ||
_objectContainer.Dispose(); | ||
} | ||
|
||
[AfterTestRun] | ||
public static async Task AfterTestRun() | ||
{ | ||
using (_container) | ||
{ | ||
await StopNServiceBusEndpoint(); | ||
} | ||
} | ||
|
||
private static void ResetCurrentDateTime(IContainer container) | ||
{ | ||
var currentDateTime = container.GetInstance<Mock<ICurrentDateTime>>(); | ||
currentDateTime.Setup(x => x.Now).Returns(DateTime.Now); | ||
} | ||
|
||
private static void ResetFundsExpiryPeriod(IContainer container) | ||
{ | ||
var config = container.GetInstance<EmployerFinanceConfiguration>(); | ||
config.FundsExpiryPeriod = 24; | ||
} | ||
|
||
private static async Task StartNServiceBusEndpoint() | ||
{ | ||
try | ||
{ | ||
_container.GetInstance<ILog>().Info("Starting endpoint."); | ||
|
||
var endpointConfiguration = new EndpointConfiguration("SFA.DAS.EmployerFinance.AcceptanceTests") | ||
.UseAzureServiceBusTransport() | ||
.UseErrorQueue("SFA.DAS.EmployerFinance.AcceptanceTests-errors") | ||
.UseInstallers() | ||
.UseLicense(WebUtility.HtmlDecode(_container.GetInstance<EmployerFinanceConfiguration>().NServiceBusLicense)) | ||
.UseSqlServerPersistence(() => _container.GetInstance<DbConnection>()) | ||
.UseNewtonsoftJsonSerializer() | ||
.UseNLogFactory() | ||
.UseOutbox() | ||
.UseStructureMapBuilder(_container) | ||
.UseUnitOfWork(); | ||
|
||
if (Debugger.IsAttached) | ||
{ | ||
endpointConfiguration.PurgeOnStartup(true); | ||
} | ||
|
||
_endpoint = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); | ||
|
||
_container.Configure(c => c.For<IMessageSession>().Use(_endpoint)); | ||
|
||
_container.GetInstance<ILog>().Info("Endpoint Started."); | ||
} | ||
catch (Exception e) | ||
{ | ||
_container.GetInstance<ILog>().Error(e, "Error starting endpoint."); | ||
throw; | ||
} | ||
} | ||
|
||
private static Task StopNServiceBusEndpoint() | ||
{ | ||
return _endpoint.Stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/SFA.DAS.EmployerFinance/Commands/UpsertRegisteredUser/UpsertRegisteredUser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using MediatR; | ||
|
||
namespace SFA.DAS.EmployerFinance.Commands.UpsertRegisteredUser | ||
{ | ||
public class UpsertRegisteredUserCommand : IAsyncRequest | ||
{ | ||
public string UserRef { get; set; } | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
public string EmailAddress { get; set; } | ||
public string CorrelationId { get; set; } | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...A.DAS.EmployerFinance/Commands/UpsertRegisteredUser/UpsertRegisteredUserCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using MediatR; | ||
using SFA.DAS.EmployerFinance.Data; | ||
using SFA.DAS.EmployerFinance.Models.UserProfile; | ||
using SFA.DAS.Validation; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerFinance.Commands.UpsertRegisteredUser | ||
{ | ||
public class UpsertRegisteredUserCommandHandler : AsyncRequestHandler<UpsertRegisteredUserCommand> | ||
{ | ||
private readonly IValidator<UpsertRegisteredUserCommand> _validator; | ||
private readonly IUserAccountRepository _userRepository; | ||
|
||
public UpsertRegisteredUserCommandHandler( | ||
IValidator<UpsertRegisteredUserCommand> validator, | ||
IUserAccountRepository userRepository) | ||
{ | ||
_validator = validator; | ||
_userRepository = userRepository; | ||
} | ||
|
||
protected override async Task HandleCore(UpsertRegisteredUserCommand message) | ||
{ | ||
var validationResult = _validator.Validate(message); | ||
|
||
if (!validationResult.IsValid()) throw new InvalidRequestException(validationResult.ValidationDictionary); | ||
|
||
await _userRepository.Upsert(new User | ||
{ | ||
Ref = new Guid(message.UserRef), | ||
Email = message.EmailAddress, | ||
FirstName = message.FirstName, | ||
LastName = message.LastName, | ||
CorrelationId = message.CorrelationId | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.