コード例 #1
0
        public void MapToTokenModelTest()
        {
            const string email = "*****@*****.**";
            const int id = 1;
            const string hash = "HashToken";
            IEnumerable<string> roles = new[] {"Admin", "Student", "Teacher"};
            Mock<ITokenValidation> token = new Mock<ITokenValidation>();
            UserToTokenMapper mapper = new UserToTokenMapper(token.Object);

            var initial = new UserWithPasswordModel()
            {
                Email = email,
                Id = id,
                HashToken = hash,
                Roles = roles
            };
            var expected = new TokenModel()
            {
                EmailAndIdToken = email,
                RolesToken = roles.ToString(),
                HashToken = hash
            };

            token.Setup(x => x.EncodeEmailAndIdToken(id.ToString() + ' ' + email.ToLower()))
                .Returns(email);

            token.Setup(x => x.EncodeRoleToken(roles))
                .Returns(roles.ToString);

            var action = mapper.MapToTokenModel(initial);
            Assert.IsNotNull(action);
            Assert.AreEqual(action.EmailAndIdToken, expected.EmailAndIdToken);
            Assert.AreEqual(action.HashToken, expected.HashToken);
            Assert.AreEqual(action.RolesToken, expected.RolesToken);
        }
コード例 #2
0
 public RegisterController(IUserService userService,
     ITokenValidation token, IIncomingUserMapper incomingUserMapper,
     IIdentityMessageService emailService)
 {
     _service = userService;
     _token = token;
     _incomingUserMapper = incomingUserMapper;
     _emailService = emailService;
     _tokenModel = new TokenModel();
 }
コード例 #3
0
        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());
        }
コード例 #4
0
        public async Task Post_ForgotPassword_Should_Return_Ok_TokenModel()                                               
        {
            //Arrange
            var forgotPasswordUser = new ForgotPasswordUserModel
            {
                Email = "*****@*****.**",
                Id = "1",
                Password = "******"
            };

            var message = new IdentityMessage
            {
                Body = "Reset pasword at test.com",
                Destination = "*****@*****.**",
                Subject = "Password reset"
            };

            //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/";

            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));

            userService.Setup(x => x.CheckUserEmail(forgotPasswordUser))
                .Returns(true);
           
            tokenValidation.Setup(x => x.GetHashSha256("asdf"))
                .Returns("fdsa");
                
            var actual = await registerController.ForgotPassword(forgotPasswordUser) as OkNegotiatedContentResult<TokenModel>;
            var contentResult = actual as OkNegotiatedContentResult<TokenModel>;

            //Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(actual.Content.GetType(), tokenModel.GetType());
        }