public async Task Handle_WhenCommandIsHandled_ThenShouldUpdateInvitationStatus( ProviderRegistrationsDbContext setupContext, ProviderRegistrationsDbContext confirmationContext, UpsertUserCommandHandler handler, Invitation invitation) { //arrange invitation.UpdateStatus((int)InvitationStatus.InvitationSent, DateTime.Now); setupContext.Invitations.Add(invitation); await setupContext.SaveChangesAsync(); var command = new UpsertUserCommand(invitation.UserRef, DateTime.Now, invitation.Reference.ToString()); //act await((IRequestHandler <UpsertUserCommand, Unit>)handler).Handle(command, new CancellationToken()); //assert var updatedInvite = await confirmationContext.Invitations.SingleAsync(s => s.Reference == invitation.Reference); updatedInvite.Status.Should().Be((int)InvitationStatus.AccountStarted); }
async Task <User> IUserService.UpsertUser(UpsertUserCommand command) { Check.NotNull(command, "Command"); await validationService.Validate(command); var currentPrincipal = await currentUserResolver.ResolveCurrentClaimsPrincipalAsync(); var user = await userRepository.GetUserById(command.Id); if (user != null) { await authorizationService.AuthorizeResource(currentPrincipal, Operation.Update, user); user.Email = command.Email; user.Name = command.Name; await userRepository.UpdateUser(user); } else { user = new User() { Id = command.Id, Email = command.Email, Name = command.Name }; await authorizationService.AuthorizeResource(currentPrincipal, Operation.Create, user); await userManager.CreateAsync(user); await userRepository.CreateUser(user); } if (command.Roles != null) { await userManager.UpdateUserRoles(user.Id, command.Roles); } return(user); }
public async Task <IHttpActionResult> Put([FromUri] string id, [FromBody] UpsertUserCommand command) { try { if (command != null) { command.Id = id; } await userService.UpsertUser(command); return(Ok()); } catch (ValidationException ex) { return(BadRequest(ex.Message)); } catch (AuthorizationException) { return(StatusCode(System.Net.HttpStatusCode.Forbidden)); } }
public async Task UpsertUser_NoRoles_DoNotUpdateRoles() { // Arrange var id = Guid.NewGuid().ToString(); var userRepositoryMock = new Mock <IUserRepository>(); userRepositoryMock .Setup(r => r.GetUserById(It.IsAny <string>())) .Returns(Task.FromResult(new User() { Id = id })); userRepositoryMock .Setup(r => r.CreateUser(It.IsAny <User>())) .Returns(Task.FromResult(0)); var userManagerMock = new Mock <IUserManager>(); var userService = InstantiateUserService( new Tuple <Type, object>(typeof(IUserManager), userManagerMock.Object), new Tuple <Type, object>(typeof(IUserRepository), userRepositoryMock.Object) ); var command = new UpsertUserCommand() { Id = id, Email = "*****@*****.**", Name = "Updated User", Roles = null }; // Act await userService.UpsertUser(command); // Assert userManagerMock.Verify(um => um.UpdateUserRoles(id, It.IsAny <string[]>()), Times.Never); }
public async Task UpsertUser_ValidData_CallUserRepository() { // Arrange var userRepositoryMock = new Mock <IUserRepository>(); userRepositoryMock .Setup(r => r.GetUserById(It.IsAny <string>())) .Returns(Task.FromResult <User>(null)); User passedUser = null; userRepositoryMock .Setup(r => r.CreateUser(It.IsAny <User>())) .Callback <User>(u => passedUser = u) .Returns(Task.FromResult(0)); var userService = InstantiateUserService( new Tuple <Type, object>(typeof(IUserRepository), userRepositoryMock.Object) ); var command = new UpsertUserCommand() { Id = "160db952-ce02-44b4-adb9-224d635cc11c", Email = "*****@*****.**", Name = "New User", Roles = new [] { "User", "Admin" } }; // Act await userService.UpsertUser(command); // Assert userRepositoryMock.Verify(r => r.GetUserById(command.Id)); userRepositoryMock.Verify(r => r.CreateUser(It.IsAny <User>())); Assert.NotNull(passedUser); Assert.Equal(command.Id, passedUser.Id); Assert.Equal(command.Email, passedUser.Email); Assert.Equal(command.Name, passedUser.Name); }
public IHttpActionResult UpsertUser(UpsertUserCommand upsertUserCommand) { return(ApiOk(_identityService.UpsertUser(upsertUserCommand))); }
public async Task <IActionResult> Upsert( [FromBody] UpsertUserCommand command, CancellationToken cancellationToken) => Ok(await Mediator.Send(command, cancellationToken));