public void RegisterGlobalCoordinator_WhenNonResultExceptionIsThrown_ShouldPassThroughWithoutBeingCaught()
        {
            _identityUserRegistrationServiceMock.When(ius => ius.CreateIdentityUser(Arg.Any <string>(), Arg.Any <Role>()))
            .Do(x => throw new Exception());

            var userEmail = "*****@*****.**";
            var registerGlobalCoordinatorRequestDto = new CreateGlobalCoordinatorRequestDto
            {
                Name  = userEmail,
                Email = userEmail
            };


            _globalCoordinatorService.Create(registerGlobalCoordinatorRequestDto).ShouldThrowAsync <Exception>();
        }
        public async Task RegisterGlobalCoordinator_WhenIdentityUserCreationSuccessful_NyssContextSaveChangesIsCalledOnce()
        {
            var userEmail = "*****@*****.**";
            var registerGlobalCoordinatorRequestDto = new CreateGlobalCoordinatorRequestDto
            {
                Name  = userEmail,
                Email = userEmail
            };


            var result = await _globalCoordinatorService.Create(registerGlobalCoordinatorRequestDto);


            await _nyssContext.Received().SaveChangesAsync();
        }
        public async Task RegisterGlobalCoordinator_WhenIdentityUserCreationSuccessful_ShouldReturnSuccessResult()
        {
            var userEmail = "*****@*****.**";
            var userName  = "******";
            var registerGlobalCoordinatorRequestDto = new CreateGlobalCoordinatorRequestDto
            {
                Name  = userName,
                Email = userEmail
            };

            var result = await _globalCoordinatorService.Create(registerGlobalCoordinatorRequestDto);

            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 <Result> Create(CreateGlobalCoordinatorRequestDto dto)
        {
            try
            {
                string securityStamp;
                GlobalCoordinatorUser user;
                using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    var identityUser = await _identityUserRegistrationService.CreateIdentityUser(dto.Email, Role.GlobalCoordinator);

                    securityStamp = await _identityUserRegistrationService.GenerateEmailVerification(identityUser.Email);

                    var defaultUserApplicationLanguage = await _dataContext.ApplicationLanguages
                                                         .SingleOrDefaultAsync(al => al.LanguageCode == EnglishLanguageCode);

                    user = new GlobalCoordinatorUser
                    {
                        IdentityUserId        = identityUser.Id,
                        EmailAddress          = identityUser.Email,
                        Name                  = dto.Name,
                        PhoneNumber           = dto.PhoneNumber,
                        AdditionalPhoneNumber = dto.AdditionalPhoneNumber,
                        Organization          = dto.Organization,
                        Role                  = Role.GlobalCoordinator,
                        ApplicationLanguage   = defaultUserApplicationLanguage
                    };
                    await _dataContext.AddAsync(user);

                    await _dataContext.SaveChangesAsync();

                    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 RegisterGlobalCoordinator_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 registerGlobalCoordinatorRequestDto = new CreateGlobalCoordinatorRequestDto
            {
                Name  = userEmail,
                Email = userEmail
            };


            var result = await _globalCoordinatorService.Create(registerGlobalCoordinatorRequestDto);


            result.IsSuccess.ShouldBeFalse();
            result.Message.Key.ShouldBe(exception.Result.Message.Key);
        }
 public async Task <Result> Create([FromBody] CreateGlobalCoordinatorRequestDto dto) =>
 await _globalCoordinatorService.Create(dto);