testutil.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }
  44. func GetTestConfig() config.Config {
  45. return testConfig
  46. }
  47. func GetTestSqlConn() sqlx.SqlConn {
  48. return sqlx.NewMysql(testConfig.MySQL.DataSource)
  49. }
  50. func GetTestCacheConf() cache.CacheConf {
  51. return testConfig.CacheRedis.Nodes
  52. }
  53. func GetTestCachePrefix() string {
  54. return testConfig.CacheRedis.KeyPrefix
  55. }
  56. func UniqueId() string {
  57. return fmt.Sprintf("t_%d_%d", time.Now().UnixNano(), rand.Intn(100000))
  58. }
  59. func HashPassword(pwd string) string {
  60. h, _ := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.MinCost)
  61. return string(h)
  62. }
  63. func CleanTable(ctx context.Context, conn sqlx.SqlConn, table string, ids ...int64) {
  64. for _, id := range ids {
  65. query := fmt.Sprintf("DELETE FROM %s WHERE `id` = ?", table)
  66. conn.ExecCtx(ctx, query, id)
  67. }
  68. }
  69. func CleanTableByField(ctx context.Context, conn sqlx.SqlConn, table, field string, value interface{}) {
  70. query := fmt.Sprintf("DELETE FROM %s WHERE `%s` = ?", table, field)
  71. conn.ExecCtx(ctx, query, value)
  72. }
  73. // EnsureProduct guarantees that an enabled product with the given code exists in DB.
  74. // 幂等:不存在则插入为 Enabled,存在则强制刷回 Enabled 状态。返回产品 id。
  75. // 通过 INSERT ... ON DUPLICATE KEY UPDATE 避免并发竞争。
  76. func EnsureProduct(ctx context.Context, conn sqlx.SqlConn, code string) int64 {
  77. now := time.Now().Unix()
  78. _, err := conn.ExecCtx(ctx,
  79. "INSERT INTO `sys_product` (`code`,`name`,`appKey`,`appSecret`,`remark`,`status`,`createTime`,`updateTime`) VALUES (?,?,?,?,?,?,?,?) "+
  80. "ON DUPLICATE KEY UPDATE `status` = 1, `updateTime` = VALUES(`updateTime`)",
  81. code, code, code+"_k", "s", "", int64(1), now, now)
  82. if err != nil {
  83. return 0
  84. }
  85. var id int64
  86. _ = conn.QueryRowCtx(ctx, &id, "SELECT `id` FROM `sys_product` WHERE `code` = ? LIMIT 1", code)
  87. return id
  88. }