Пример #1
0
        public async Task<IHttpActionResult> Post(RegisterUserModel args)
        {
            var newUser = _incomingUserMapper.ToIncomingUser(args);
            if (!_service.Exists(newUser))
            {
                _service.Add(newUser);
                Guid randomPart = Guid.NewGuid();
                var tokenToHash = randomPart.ToString() + newUser.Id.ToString();
                _tokenModel.EmailConfirmationToken = _token.GetHashSha256(tokenToHash);

                var code = _tokenModel.EmailConfirmationToken;
                newUser.Token = code;
                _service.AddToken(newUser);

                var callbackUrl = new Uri(Url.Link("AngularRoute", new { }));

                await (_emailService).SendAsync(new IdentityMessage
                {
                    Body = String.Format(MessagesContainer.ConfiramtionMessage + callbackUrl + "#/EmailConfirmed?id={0}&code={1}", newUser.Id, code),
                    Destination = newUser.Email,
                    Subject = "Registration confirmation at WoW"
                });

                return Ok(_tokenModel);
            }
            return BadRequest();
        }
Пример #2
0
 public User Map(RegisterUserModel source)
 {
     return new User
     {
         Name = source.Login,
         Password = source.Password,
         Email = source.Email,
         Id = source.Id
     };
 }
        public async Task<IHttpActionResult> Post(RegisterUserModel args)
        {
            var newUser = _incomingUserMapper.ToIncomingUser(args);
            if (!_service.Exists(newUser))
            {
                _service.Add(newUser);

                var code = GenerateEmailConfirmationToken(newUser.Id.ToString(), true);
                newUser.Token = code;
                _service.AddToken(newUser);

                await SendEmailConfirmationAsync(code, newUser.Email, "Registration confirmation at WoW", newUser.Id);

                return Ok(_tokenModel);
            }
            return BadRequest();
        }
        public async Task Post_Register_Should_Return_Ok_TokenModel()
        {
            //Arrange
            var registerUser = new RegisterUserModel
            {
                Login = "******",
                Email = "*****@*****.**",
                Password = "******",
                Id = 0
            };

            var incomingUser = new IncomingUser
            {
                Name = "Cat-Ok",
                Email = "*****@*****.**",
                Password = "******",
                Token = "asdf",
                Id = 1
            };

            var user = new User
            {
                Name = "Roman Hapatyn",
                Email = "*****@*****.**",
                Password = "******",
                HashedToken = "asdf",
                Id = 1
            };

            TokenModel token = new TokenModel();
            token.RolesToken = "Student";

            //Action
            
            GenerateData("1", new[] { "NoRoles" });
            RegisterController registerController = new RegisterController(userService.Object,
                tokenValidation.Object,
                incUserMapper.Object,
                mesService.Object);
            var tokenModel = new TokenModel();
            string locationUrl = "http://location/";

            var message = new IdentityMessage
                {
                    Body = "Please confirm your account at: www.xxx.xxx ",
                    Destination = "*****@*****.**",
                    Subject = "Confirm your registration"
                };

            urlHelper.Setup(x => x.Link(It.IsAny<string>(), It.IsAny<object>()))
                .Returns(locationUrl);
            registerController.Url = urlHelper.Object;

            mesService
                .Setup(x => x.SendAsync(message))
                .Returns(Task.FromResult(0));

            incUserMapper
                .Setup(x => x.ToIncomingUser(registerUser))
                .Returns(incomingUser);

            userMapper
                .Setup(x => x.Map(registerUser))
                .Returns(user);

            userService
                .Setup(x => x.Exists(incomingUser))
                .Returns(false);

            tokenValidation
                .Setup(x => x.GetHashSha256("asdf"))
                .Returns("fdsa");

            var actual = await registerController.Post(registerUser) as OkNegotiatedContentResult<TokenModel>;
            var contentResult = actual as OkNegotiatedContentResult<TokenModel>;
            //Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(actual.Content.GetType(), tokenModel.GetType());
        }