admin_common_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package admin_common
  2. import (
  3. "fmt"
  4. "golang.org/x/crypto/bcrypt"
  5. "testing"
  6. "time"
  7. )
  8. // checkPasswordHash 验证用户输入的密码是否与存储的哈希值匹配
  9. func checkPasswordHash(password, hash string) bool {
  10. err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
  11. return err == nil
  12. }
  13. // hashPassword 使用 bcrypt 对密码进行哈希
  14. func hashPassword(password string) (string, error) {
  15. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  16. if err != nil {
  17. return "", err
  18. }
  19. return string(hash), nil
  20. }
  21. func TestAdminLogin(t *testing.T) {
  22. // address := fmt.Sprintf("%v:%v@(%v:%v)/%v?charset=utf8mb4,utf8&parseTime=True&loc=Local", "root", "xugang131500", "47.56.16.206", 3306, "sghblog")
  23. // db, err := gorm.Open(mysql.Open(address), &gorm.Config{
  24. // Logger: logger.Default.LogMode(logger.Info),
  25. // NamingStrategy: schema.NamingStrategy{
  26. // SingularTable: true,
  27. // }})
  28. // if err != nil {
  29. // t.Fatal(err)
  30. // }
  31. // repo := repository.NewRepository(db)
  32. // newService := service.NewService(repo)
  33. t.Run("过期时间", func(t *testing.T) {
  34. old := time.Now().Unix()
  35. add := time.Now().Add(time.Second * time.Duration(0.5*3600)).Unix()
  36. fmt.Println(add < old)
  37. fmt.Println(add > old)
  38. fmt.Println(add, old)
  39. // expiryTime := time.Unix(add, 0)
  40. // fmt.Println("expiryTime", expiryTime)
  41. })
  42. t.Run("登录", func(t *testing.T) {
  43. // login, err := newService.AdminLogin("admin", "123456", 1)
  44. // fmt.Println(checkPasswordHash("123456", login.Password))
  45. // fmt.Println(err)
  46. // fmt.Println(login.Roles)
  47. // fmt.Println(login.Teams)
  48. })
  49. }