示例#1
0
        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);
            }
        }
示例#2
0
        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();
        }
示例#3
0
        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>());
        }
示例#4
0
        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>();
        }
示例#5
0
        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);
        }
示例#6
0
        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);
        }
示例#7
0
        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();
        }
示例#8
0
        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);
        }
示例#9
0
        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);
        }
示例#10
0
 public async Task <Result> Create(int nationalSocietyId, [FromBody] CreateSupervisorRequestDto createSupervisorRequestDto) =>
 await _supervisorService.Create(nationalSocietyId, createSupervisorRequestDto);