public async Task <Result> Create(int nationalSocietyId, CreateSupervisorRequestDto createSupervisorRequestDto) { try { string securityStamp; SupervisorUser user; using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var identityUser = await _identityUserRegistrationService.CreateIdentityUser(createSupervisorRequestDto.Email, Role.Supervisor); securityStamp = await _identityUserRegistrationService.GenerateEmailVerification(identityUser.Email); user = await CreateSupervisorUser(identityUser, nationalSocietyId, createSupervisorRequestDto); transactionScope.Complete(); } await _verificationEmailService.SendVerificationEmail(user, securityStamp); return(Success(ResultKey.User.Registration.Success)); } catch (ResultException e) { _loggerAdapter.Debug(e); return(e.Result); } }
public async Task Create_WhenIdentityUserCreationSuccessful_NyssContextSaveChangesIsCalledOnce() { var userEmail = "*****@*****.**"; var registerSupervisorRequestDto = new CreateSupervisorRequestDto { Name = userEmail, Email = userEmail }; var nationalSocietyId = 1; var result = await _supervisorService.Create(nationalSocietyId, registerSupervisorRequestDto); await _nyssContext.Received().SaveChangesAsync(); }
public async Task Create_WhenCreatingSupervisorWithNoProjectSpecified_AddAsyncForProjectReferenceShouldNotBeCalled() { //Arrange var userEmail = "*****@*****.**"; var registerSupervisorRequestDto = new CreateSupervisorRequestDto { Name = userEmail, Email = userEmail }; //Act var result = await _supervisorService.Create(_nationalSocietyId1, registerSupervisorRequestDto); //Assert await _nyssContext.Received(0).AddAsync(Arg.Any <SupervisorUserProject>()); }
public void Create_WhenNonResultExceptionIsThrown_ShouldPassThroughWithoutBeingCaught() { _identityUserRegistrationServiceMock.When(ius => ius.CreateIdentityUser(Arg.Any <string>(), Arg.Any <Role>())) .Do(x => throw new Exception()); var userEmail = "*****@*****.**"; var registerSupervisorRequestDto = new CreateSupervisorRequestDto { Name = userEmail, Email = userEmail }; var nationalSocietyId = 1; _supervisorService.Create(nationalSocietyId, registerSupervisorRequestDto).ShouldThrowAsync <Exception>(); }
public async Task Create_WhenCreatingSupervisorWithProjectInAnotherNationalSociety_ShouldReturnError() { //Arrange var userEmail = "*****@*****.**"; var registerSupervisorRequestDto = new CreateSupervisorRequestDto { Name = userEmail, Email = userEmail, ProjectId = _projectId5 }; //Act var result = await _supervisorService.Create(_nationalSocietyId1, registerSupervisorRequestDto); //Assert result.IsSuccess.ShouldBeFalse(); result.Message.Key.ShouldBe(ResultKey.User.Supervisor.ProjectDoesNotExistOrNoAccess); }
public async Task Create_WhenCreatingInNonExistentNationalSociety_ShouldReturnError() { //Arrange var userEmail = "*****@*****.**"; var registerSupervisorRequestDto = new CreateSupervisorRequestDto { Name = userEmail, Email = userEmail }; //Act var nationalSocietyId = 666; var result = await _supervisorService.Create(nationalSocietyId, registerSupervisorRequestDto); //Assert result.IsSuccess.ShouldBeFalse(); result.Message.Key.ShouldBe(ResultKey.User.Registration.NationalSocietyDoesNotExist); }
public async Task Create_WhenIdentityUserCreationSuccessful_ShouldReturnSuccessResult() { var userEmail = "*****@*****.**"; var userName = "******"; var registerSupervisorRequestDto = new CreateSupervisorRequestDto { Name = userName, Email = userEmail }; var nationalSocietyId = 1; var result = await _supervisorService.Create(nationalSocietyId, registerSupervisorRequestDto); await _identityUserRegistrationServiceMock.Received(1).GenerateEmailVerification(userEmail); await _verificationEmailServiceMock.Received(1).SendVerificationEmail(Arg.Is <User>(u => u.EmailAddress == userEmail), Arg.Any <string>()); result.IsSuccess.ShouldBeTrue(); }
public async Task Create_WhenIdentityUserServiceThrowsResultException_ShouldReturnErrorResultWithAppropriateKey() { var exception = new ResultException(ResultKey.User.Registration.UserAlreadyExists); _identityUserRegistrationServiceMock.When(ius => ius.CreateIdentityUser(Arg.Any <string>(), Arg.Any <Role>())) .Do(x => throw exception); var userEmail = "*****@*****.**"; var registerSupervisorRequestDto = new CreateSupervisorRequestDto { Name = userEmail, Email = userEmail }; var nationalSocietyId = 1; var result = await _supervisorService.Create(nationalSocietyId, registerSupervisorRequestDto); result.IsSuccess.ShouldBeFalse(); result.Message.Key.ShouldBe(exception.Result.Message.Key); }
private async Task <SupervisorUser> CreateSupervisorUser(IdentityUser identityUser, int nationalSocietyId, CreateSupervisorRequestDto createSupervisorRequestDto) { var nationalSociety = await _dataContext.NationalSocieties.Include(ns => ns.ContentLanguage) .SingleOrDefaultAsync(ns => ns.Id == nationalSocietyId); if (nationalSociety == null) { throw new ResultException(ResultKey.User.Registration.NationalSocietyDoesNotExist); } if (nationalSociety.IsArchived) { throw new ResultException(ResultKey.User.Registration.CannotCreateUsersInArchivedNationalSociety); } var defaultUserApplicationLanguage = await _dataContext.ApplicationLanguages .SingleOrDefaultAsync(al => al.LanguageCode == nationalSociety.ContentLanguage.LanguageCode); var user = new SupervisorUser { IdentityUserId = identityUser.Id, EmailAddress = identityUser.Email, Name = createSupervisorRequestDto.Name, PhoneNumber = createSupervisorRequestDto.PhoneNumber, AdditionalPhoneNumber = createSupervisorRequestDto.AdditionalPhoneNumber, ApplicationLanguage = defaultUserApplicationLanguage, DecadeOfBirth = createSupervisorRequestDto.DecadeOfBirth, Sex = createSupervisorRequestDto.Sex, Organization = createSupervisorRequestDto.Organization }; await AddNewSupervisorToProject(user, createSupervisorRequestDto.ProjectId, nationalSocietyId); var userNationalSociety = CreateUserNationalSocietyReference(nationalSociety, user); await _dataContext.AddAsync(userNationalSociety); await _dataContext.SaveChangesAsync(); return(user); }
public async Task <Result> Create(int nationalSocietyId, [FromBody] CreateSupervisorRequestDto createSupervisorRequestDto) => await _supervisorService.Create(nationalSocietyId, createSupervisorRequestDto);