testutil.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package testutil
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "time"
  7. "perms-system-server/internal/config"
  8. "github.com/zeromicro/go-zero/core/stores/cache"
  9. "github.com/zeromicro/go-zero/core/stores/redis"
  10. "github.com/zeromicro/go-zero/core/stores/sqlx"
  11. "golang.org/x/crypto/bcrypt"
  12. )
  13. var testConfig = config.Config{
  14. MySQL: struct{ DataSource string }{
  15. DataSource: "root:NsDmWyM@312@tcp(127.0.0.1:3306)/perms_system?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai",
  16. },
  17. CacheRedis: config.CacheRedisConf{
  18. Nodes: cache.CacheConf{
  19. {
  20. RedisConf: redis.RedisConf{
  21. Host: "127.0.0.1:6379",
  22. Pass: "NsDmWyM@312",
  23. Type: "node",
  24. },
  25. Weight: 100,
  26. },
  27. },
  28. KeyPrefix: "test_perms",
  29. },
  30. Auth: struct {
  31. AccessSecret string
  32. AccessExpire int64
  33. RefreshSecret string
  34. RefreshExpire int64
  35. ManagementKey string
  36. }{
  37. AccessSecret: "test-access-secret",
  38. AccessExpire: 7200,
  39. RefreshSecret: "test-refresh-secret",
  40. RefreshExpire: 604800,
  41. ManagementKey: "test-management-key",
  42. },
  43. // 测试环境标记 cap.js 已启用,使 AdminLogin / Login 跳过图片验证码校验,
  44. // 让业务逻辑测试无需预填充验证码即可通过。
  45. Capjs: config.CapjsConf{Enable: 1},
  46. }
  47. func GetTestConfig() config.Config {
  48. return testConfig
  49. }
  50. func GetTestSqlConn() sqlx.SqlConn {
  51. return sqlx.NewMysql(testConfig.MySQL.DataSource)
  52. }
  53. func GetTestCacheConf() cache.CacheConf {
  54. return testConfig.CacheRedis.Nodes
  55. }
  56. func GetTestCachePrefix() string {
  57. return testConfig.CacheRedis.KeyPrefix
  58. }
  59. func UniqueId() string {
  60. return fmt.Sprintf("t_%d_%d", time.Now().UnixNano(), rand.Intn(100000))
  61. }
  62. func HashPassword(pwd string) string {
  63. h, _ := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.MinCost)
  64. return string(h)
  65. }
  66. func CleanTable(ctx context.Context, conn sqlx.SqlConn, table string, ids ...int64) {
  67. for _, id := range ids {
  68. query := fmt.Sprintf("DELETE FROM %s WHERE `id` = ?", table)
  69. conn.ExecCtx(ctx, query, id)
  70. }
  71. }
  72. func CleanTableByField(ctx context.Context, conn sqlx.SqlConn, table, field string, value interface{}) {
  73. query := fmt.Sprintf("DELETE FROM %s WHERE `%s` = ?", table, field)
  74. conn.ExecCtx(ctx, query, value)
  75. }
  76. // EnsureProduct guarantees that an enabled product with the given code exists in DB.
  77. // 幂等:不存在则插入为 Enabled,存在则强制刷回 Enabled 状态。返回产品 id。
  78. // 通过 INSERT ... ON DUPLICATE KEY UPDATE 避免并发竞争。
  79. func EnsureProduct(ctx context.Context, conn sqlx.SqlConn, code string) int64 {
  80. now := time.Now().Unix()
  81. _, err := conn.ExecCtx(ctx,
  82. "INSERT INTO `sys_product` (`code`,`name`,`appKey`,`appSecret`,`remark`,`status`,`createTime`,`updateTime`) VALUES (?,?,?,?,?,?,?,?) "+
  83. "ON DUPLICATE KEY UPDATE `status` = 1, `updateTime` = VALUES(`updateTime`)",
  84. code, code, code+"_k", "s", "", int64(1), now, now)
  85. if err != nil {
  86. return 0
  87. }
  88. var id int64
  89. _ = conn.QueryRowCtx(ctx, &id, "SELECT `id` FROM `sys_product` WHERE `code` = ? LIMIT 1", code)
  90. return id
  91. }