コード例 #1
0
        public void UpdateToken_UserIDValueLengthEqual0_ActualFalse()
        {
            //Arrange
            string      UserID      = "";
            UserReadDTO userReadDTO = new UserReadDTO
            {
                UserID   = UserID,
                Email    = "*****@*****.**",
                UserName = "******"
            };

            Utilities.JWT.TokenResponse tokens = TokenUtil.GenerateTokens(userReadDTO, tokenProviderOption);

            DTO.ReadDTO.TokenReadDTO tokenReadDTO = new DTO.ReadDTO.TokenReadDTO
            {
                AccessToken           = tokens.AccessToken,
                AccessTokenExpriesIn  = tokens.AccessTokenExpiresIn,
                RefreshToken          = tokens.RefreshToken,
                RefreshTokenExpriesIn = tokens.RefreshTokenExpiresIn,
            };

            //Act
            var status = userService.UpdateToken(UserID, tokenReadDTO).Result.status;

            //Assert
            Assert.AreEqual(false, status);
        }
コード例 #2
0
        public void UpdateUserInfor_NullUserID_ActualFalse()
        {
            //Arrange
            UserReadDTO userReadDTO = new UserReadDTO {
                UserID   = null,
                Email    = "*****@*****.**",
                UserName = "******"
            };

            Utilities.JWT.TokenResponse tokens = TokenUtil.GenerateTokens(userReadDTO, tokenProviderOption);

            DTO.ReadDTO.TokenReadDTO tokenReadDTO = new DTO.ReadDTO.TokenReadDTO
            {
                AccessToken           = tokens.AccessToken,
                AccessTokenExpriesIn  = tokens.AccessTokenExpiresIn,
                RefreshToken          = tokens.RefreshToken,
                RefreshTokenExpriesIn = tokens.RefreshTokenExpiresIn,
            };

            //Act
            var status = userService.UpdateUserInfor(userReadDTO).Result.status;

            //Assert
            Assert.AreEqual(status, false);
        }
コード例 #3
0
        public void UpdateUserInfor_UserIsNotExist_ActualFalse()
        {
            //Arrange
            UserReadDTO userReadDTO = new UserReadDTO
            {
                UserID   = Guid.NewGuid().ToString(),
                Email    = "*****@*****.**",
                UserName = "******"
            };

            Utilities.JWT.TokenResponse tokens = TokenUtil.GenerateTokens(userReadDTO, tokenProviderOption);

            DTO.ReadDTO.TokenReadDTO tokenReadDTO = new DTO.ReadDTO.TokenReadDTO
            {
                AccessToken           = tokens.AccessToken,
                AccessTokenExpriesIn  = tokens.AccessTokenExpiresIn,
                RefreshToken          = tokens.RefreshToken,
                RefreshTokenExpriesIn = tokens.RefreshTokenExpiresIn,
            };

            //Act
            var status = userService.UpdateUserInfor(userReadDTO).Result.status;

            //Assert
            Assert.AreEqual(status, false);
        }
コード例 #4
0
        public void UpdateToken_TokenNotExist_ActualFalse()
        {
            //Arrange
            db.Users.RemoveRange(db.Users);
            db.Tokens.RemoveRange(db.Tokens);

            string      UserID      = Guid.NewGuid().ToString();
            UserReadDTO userReadDTO = new UserReadDTO
            {
                UserID   = UserID,
                Email    = "*****@*****.**",
                UserName = "******"
            };

            Utilities.JWT.TokenResponse tokens = TokenUtil.GenerateTokens(userReadDTO, tokenProviderOption);

            DTO.ReadDTO.TokenReadDTO tokenReadDTO = new DTO.ReadDTO.TokenReadDTO
            {
                AccessToken           = tokens.AccessToken,
                AccessTokenExpriesIn  = tokens.AccessTokenExpiresIn,
                RefreshToken          = tokens.RefreshToken,
                RefreshTokenExpriesIn = tokens.RefreshTokenExpiresIn,
            };

            User user = new User
            {
                UserID          = new Guid(UserID),
                Email           = "*****@*****.**",
                UserName        = "******",
                HashPassword    = BCryptUtil.HashPassword("123456"),
                isAuthenticated = false
            };

            db.Users.Add(user);
            db.SaveChanges();

            Token token = new Token
            {
                AccessToken           = tokenReadDTO.AccessToken,
                AccessTokenExpriesIn  = tokenReadDTO.AccessTokenExpriesIn,
                RefreshToken          = tokenReadDTO.RefreshToken,
                RefreshTokenExpriesIn = tokenReadDTO.RefreshTokenExpriesIn,
                UserID = user.UserID
            };

            db.Tokens.Add(token);
            db.SaveChanges();

            //Act
            var status = userService.UpdateToken(Guid.NewGuid().ToString(), tokenReadDTO).Result.status;

            //Assert
            Assert.AreEqual(false, status);
        }
コード例 #5
0
        public void UpdateUserInfor_NullParameters_ActualFalse()
        {
            //Arrange
            UserReadDTO userReadDTO = null;

            Utilities.JWT.TokenResponse tokens = TokenUtil.GenerateTokens(userReadDTO, tokenProviderOption);

            DTO.ReadDTO.TokenReadDTO tokenReadDTO = new DTO.ReadDTO.TokenReadDTO
            {
                AccessToken           = tokens.AccessToken,
                AccessTokenExpriesIn  = tokens.AccessTokenExpiresIn,
                RefreshToken          = tokens.RefreshToken,
                RefreshTokenExpriesIn = tokens.RefreshTokenExpiresIn,
            };

            //Act
            var status = userService.UpdateUserInfor(userReadDTO).Result.status;

            //Assert
            Assert.AreEqual(status, false);
        }
コード例 #6
0
        public void UpdateUserInfor_Success_ActualFalse()
        {
            //Arrange
            db.Users.RemoveRange(db.Users);
            db.SaveChanges();

            User user = new User();

            user.UserID          = Guid.NewGuid();
            user.UserName        = "******";
            user.Email           = "*****@*****.**";
            user.HashPassword    = BCryptUtil.HashPassword("123456");
            user.isAuthenticated = false;

            db.Users.Add(user);
            db.SaveChanges();

            UserReadDTO userReadDTO = new UserReadDTO
            {
                UserID   = user.UserID.ToString(),
                Email    = "*****@*****.**",
                UserName = "******"
            };

            Utilities.JWT.TokenResponse tokens = TokenUtil.GenerateTokens(userReadDTO, tokenProviderOption);

            DTO.ReadDTO.TokenReadDTO tokenReadDTO = new DTO.ReadDTO.TokenReadDTO
            {
                AccessToken           = tokens.AccessToken,
                AccessTokenExpriesIn  = tokens.AccessTokenExpiresIn,
                RefreshToken          = tokens.RefreshToken,
                RefreshTokenExpriesIn = tokens.RefreshTokenExpiresIn,
            };

            //Act
            var status = userService.UpdateUserInfor(userReadDTO).Result.status;

            //Assert
            Assert.AreEqual(status, true);
        }
        public void AddToken_NullParameter_ActualFalse()
        {
            //Arrange
            string UserID = null;

            //UserReadDTO userReadDTO = new UserReadDTO
            //{
            //    UserID = UserID,
            //    Email = "*****@*****.**",
            //    UserName = "******"
            //};

            //Utilities.JWT.TokenResponse tokens = TokenUtil.GenerateTokens(userReadDTO, tokenProviderOption);

            DTO.ReadDTO.TokenReadDTO tokenReadDTO = null;

            //Act
            var status = userService.AddToken(UserID, tokenReadDTO).Result.status;

            //Assert
            Assert.AreEqual(status, false);
        }
コード例 #8
0
        public dynamic Login([FromBody] UserLoginModel user)
        {
            if (ModelState.IsValid)
            {
                Services.Entities.CustomResponse Login_Response = _userService.Login(user.Email, user.Password);

                if (Login_Response.status)
                {
                    Token token_current = _userService.GetToken(Login_Response.userReadDTO.UserID);

                    //Lần Đầu Tiên Đăng Nhập
                    if (token_current == null)
                    {
                        Utilities.JWT.TokenResponse tokens = TokenUtil.GenerateTokens(Login_Response.userReadDTO, Startup.userTokenOption);

                        DTO.ReadDTO.TokenReadDTO tokenReadDTO = new DTO.ReadDTO.TokenReadDTO
                        {
                            AccessToken           = tokens.AccessToken,
                            AccessTokenExpriesIn  = tokens.AccessTokenExpiresIn,
                            RefreshToken          = tokens.RefreshToken,
                            RefreshTokenExpriesIn = tokens.RefreshTokenExpiresIn,
                        };

                        //Lưu Token Mới Vào Database
                        _userService.AddToken(Login_Response.userReadDTO.UserID, tokenReadDTO);

                        return(new
                        {
                            status = true,
                            code = ReturnCodes.DataGetSucceeded,
                            message = "Đăng Nhập Thành Công",
                            data = Login_Response.userReadDTO,
                            tokens = tokens
                        });
                    }

                    //Nếu Token Trong Database Đã Hết Hạn
                    if (!TokenUtil.isExpiredTime(token_current.AccessToken))
                    {
                        Utilities.JWT.TokenResponse tokens = TokenUtil.GenerateTokens(Login_Response.userReadDTO, Startup.userTokenOption);

                        DTO.ReadDTO.TokenReadDTO tokenReadDTO = new DTO.ReadDTO.TokenReadDTO
                        {
                            AccessToken           = tokens.AccessToken,
                            AccessTokenExpriesIn  = tokens.AccessTokenExpiresIn,
                            RefreshToken          = tokens.RefreshToken,
                            RefreshTokenExpriesIn = tokens.RefreshTokenExpiresIn,
                        };

                        _userService.UpdateToken(Login_Response.userReadDTO.UserID, tokenReadDTO);
                    }

                    return(new
                    {
                        status = true,
                        code = ReturnCodes.DataGetSucceeded,
                        message = "Đăng Nhập Thành Công",
                        data = Login_Response.userReadDTO,
                        tokens = _mapper.Map <TokenReadDTO>(token_current)
                    });
                }
            }

            return(new
            {
                status = false,
                code = ReturnCodes.DataGetFailed,
                message = "Đăng Nhập Không Thành Công"
            });
        }