userDetailsLoader_test.go 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. package loaders
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "math"
  7. "math/rand"
  8. "sort"
  9. "testing"
  10. "time"
  11. "perms-system-server/internal/consts"
  12. "perms-system-server/internal/model"
  13. deptModel "perms-system-server/internal/model/dept"
  14. permModel "perms-system-server/internal/model/perm"
  15. productModel "perms-system-server/internal/model/product"
  16. memberModel "perms-system-server/internal/model/productmember"
  17. roleModel "perms-system-server/internal/model/role"
  18. rolePermModel "perms-system-server/internal/model/roleperm"
  19. userModel "perms-system-server/internal/model/user"
  20. userPermModel "perms-system-server/internal/model/userperm"
  21. userRoleModel "perms-system-server/internal/model/userrole"
  22. "github.com/stretchr/testify/assert"
  23. "github.com/stretchr/testify/require"
  24. "github.com/zeromicro/go-zero/core/stores/cache"
  25. "github.com/zeromicro/go-zero/core/stores/redis"
  26. "github.com/zeromicro/go-zero/core/stores/sqlx"
  27. "golang.org/x/crypto/bcrypt"
  28. )
  29. // --------------- inline test config (avoid circular import with testutil) ---------------
  30. var testCacheConf = cache.CacheConf{
  31. {
  32. RedisConf: redis.RedisConf{Host: "127.0.0.1:6379", Pass: "NsDmWyM@312", Type: "node"},
  33. Weight: 100,
  34. },
  35. }
  36. var testKeyPrefix = "test_perms"
  37. var testDataSource = "root:NsDmWyM@312@tcp(127.0.0.1:3306)/perms_system?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
  38. func testConn() sqlx.SqlConn { return sqlx.NewMysql(testDataSource) }
  39. func testRedis() *redis.Redis { return redis.MustNewRedis(testCacheConf[0].RedisConf) }
  40. func testModels() *model.Models {
  41. conn := testConn()
  42. return model.NewModels(conn, testCacheConf, testKeyPrefix)
  43. }
  44. func uniqueId() string {
  45. return fmt.Sprintf("t_%d_%d", time.Now().UnixNano(), rand.Intn(100000))
  46. }
  47. func hashPwd(p string) string {
  48. h, _ := bcrypt.GenerateFromPassword([]byte(p), bcrypt.MinCost)
  49. return string(h)
  50. }
  51. func cleanTable(ctx context.Context, conn sqlx.SqlConn, table string, ids ...int64) {
  52. for _, id := range ids {
  53. conn.ExecCtx(ctx, fmt.Sprintf("DELETE FROM %s WHERE `id` = ?", table), id)
  54. }
  55. }
  56. func cleanTableByField(ctx context.Context, conn sqlx.SqlConn, table, field string, value interface{}) {
  57. conn.ExecCtx(ctx, fmt.Sprintf("DELETE FROM %s WHERE `%s` = ?", table, field), value)
  58. }
  59. func newTestLoader() *UserDetailsLoader {
  60. rds := testRedis()
  61. m := testModels()
  62. return NewUserDetailsLoader(rds, testKeyPrefix, m)
  63. }
  64. func now() int64 { return time.Now().Unix() }
  65. // --------------- helpers: insert test data ---------------
  66. func insertUser(ctx context.Context, t *testing.T, m *model.Models, u *userModel.SysUser) int64 {
  67. t.Helper()
  68. res, err := m.SysUserModel.Insert(ctx, u)
  69. require.NoError(t, err)
  70. id, _ := res.LastInsertId()
  71. return id
  72. }
  73. func insertDept(ctx context.Context, t *testing.T, m *model.Models, d *deptModel.SysDept) int64 {
  74. t.Helper()
  75. res, err := m.SysDeptModel.Insert(ctx, d)
  76. require.NoError(t, err)
  77. id, _ := res.LastInsertId()
  78. return id
  79. }
  80. func insertProduct(ctx context.Context, t *testing.T, m *model.Models, p *productModel.SysProduct) int64 {
  81. t.Helper()
  82. res, err := m.SysProductModel.Insert(ctx, p)
  83. require.NoError(t, err)
  84. id, _ := res.LastInsertId()
  85. return id
  86. }
  87. func insertMember(ctx context.Context, t *testing.T, m *model.Models, mb *memberModel.SysProductMember) int64 {
  88. t.Helper()
  89. res, err := m.SysProductMemberModel.Insert(ctx, mb)
  90. require.NoError(t, err)
  91. id, _ := res.LastInsertId()
  92. return id
  93. }
  94. func insertRole(ctx context.Context, t *testing.T, m *model.Models, r *roleModel.SysRole) int64 {
  95. t.Helper()
  96. res, err := m.SysRoleModel.Insert(ctx, r)
  97. require.NoError(t, err)
  98. id, _ := res.LastInsertId()
  99. return id
  100. }
  101. func insertPerm(ctx context.Context, t *testing.T, m *model.Models, p *permModel.SysPerm) int64 {
  102. t.Helper()
  103. res, err := m.SysPermModel.Insert(ctx, p)
  104. require.NoError(t, err)
  105. id, _ := res.LastInsertId()
  106. return id
  107. }
  108. func insertUserRole(ctx context.Context, t *testing.T, m *model.Models, ur *userRoleModel.SysUserRole) int64 {
  109. t.Helper()
  110. res, err := m.SysUserRoleModel.Insert(ctx, ur)
  111. require.NoError(t, err)
  112. id, _ := res.LastInsertId()
  113. return id
  114. }
  115. func insertRolePerm(ctx context.Context, t *testing.T, m *model.Models, rp *rolePermModel.SysRolePerm) int64 {
  116. t.Helper()
  117. res, err := m.SysRolePermModel.Insert(ctx, rp)
  118. require.NoError(t, err)
  119. id, _ := res.LastInsertId()
  120. return id
  121. }
  122. func insertUserPerm(ctx context.Context, t *testing.T, m *model.Models, up *userPermModel.SysUserPerm) int64 {
  123. t.Helper()
  124. res, err := m.SysUserPermModel.Insert(ctx, up)
  125. require.NoError(t, err)
  126. id, _ := res.LastInsertId()
  127. return id
  128. }
  129. // --------------- TC-0485: Load-DB加载(缓存miss) ---------------
  130. func TestLoad_DBMiss(t *testing.T) {
  131. ctx := context.Background()
  132. conn := testConn()
  133. m := testModels()
  134. loader := newTestLoader()
  135. uid := uniqueId()
  136. ts := now()
  137. pcode := "p_" + uid
  138. deptId := insertDept(ctx, t, m, &deptModel.SysDept{
  139. ParentId: 0, Name: "dept_" + uid, Path: "/1/", Sort: 1,
  140. DeptType: consts.DeptTypeNormal, Status: consts.StatusEnabled,
  141. CreateTime: ts, UpdateTime: ts,
  142. })
  143. userId := insertUser(ctx, t, m, &userModel.SysUser{
  144. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  145. Avatar: sql.NullString{}, Email: uid + "@test.com", Phone: "13800000001",
  146. Remark: "remark", DeptId: deptId, IsSuperAdmin: consts.IsSuperAdminNo,
  147. MustChangePassword: consts.MustChangePasswordNo, Status: consts.StatusEnabled,
  148. CreateTime: ts, UpdateTime: ts,
  149. })
  150. productId := insertProduct(ctx, t, m, &productModel.SysProduct{
  151. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  152. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  153. })
  154. memberId := insertMember(ctx, t, m, &memberModel.SysProductMember{
  155. ProductCode: pcode, UserId: userId, MemberType: consts.MemberTypeMember,
  156. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  157. })
  158. roleId := insertRole(ctx, t, m, &roleModel.SysRole{
  159. ProductCode: pcode, Name: "role_" + uid, Remark: "test",
  160. Status: consts.StatusEnabled, PermsLevel: 10, CreateTime: ts, UpdateTime: ts,
  161. })
  162. permId := insertPerm(ctx, t, m, &permModel.SysPerm{
  163. ProductCode: pcode, Name: "perm_" + uid, Code: "perm:" + uid,
  164. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  165. })
  166. urId := insertUserRole(ctx, t, m, &userRoleModel.SysUserRole{
  167. UserId: userId, RoleId: roleId, CreateTime: ts, UpdateTime: ts,
  168. })
  169. rpId := insertRolePerm(ctx, t, m, &rolePermModel.SysRolePerm{
  170. RoleId: roleId, PermId: permId, CreateTime: ts, UpdateTime: ts,
  171. })
  172. t.Cleanup(func() {
  173. loader.Del(ctx, userId, pcode)
  174. cleanTable(ctx, conn, "`sys_role_perm`", rpId)
  175. cleanTable(ctx, conn, "`sys_user_role`", urId)
  176. cleanTable(ctx, conn, "`sys_perm`", permId)
  177. cleanTable(ctx, conn, "`sys_role`", roleId)
  178. cleanTable(ctx, conn, "`sys_product_member`", memberId)
  179. cleanTable(ctx, conn, "`sys_product`", productId)
  180. cleanTable(ctx, conn, "`sys_user`", userId)
  181. cleanTable(ctx, conn, "`sys_dept`", deptId)
  182. })
  183. // clear any leftover cache
  184. loader.Del(ctx, userId, pcode)
  185. ud := loader.Load(ctx, userId, pcode)
  186. require.NotNil(t, ud)
  187. assert.Equal(t, userId, ud.UserId)
  188. assert.Equal(t, uid, ud.Username)
  189. assert.Equal(t, "nick_"+uid, ud.Nickname)
  190. assert.Equal(t, uid+"@test.com", ud.Email)
  191. assert.Equal(t, int64(consts.StatusEnabled), ud.Status)
  192. assert.Equal(t, deptId, ud.DeptId)
  193. assert.Equal(t, "dept_"+uid, ud.DeptName)
  194. assert.Equal(t, pcode, ud.ProductCode)
  195. assert.Equal(t, "prod_"+uid, ud.ProductName)
  196. assert.Equal(t, consts.MemberTypeMember, ud.MemberType)
  197. assert.Len(t, ud.Roles, 1)
  198. assert.Equal(t, roleId, ud.Roles[0].Id)
  199. assert.Equal(t, int64(10), ud.MinPermsLevel)
  200. assert.Contains(t, ud.Perms, "perm:"+uid)
  201. }
  202. // --------------- TC-0486: Load-缓存命中 ---------------
  203. func TestLoad_CacheHit(t *testing.T) {
  204. ctx := context.Background()
  205. conn := testConn()
  206. m := testModels()
  207. loader := newTestLoader()
  208. uid := uniqueId()
  209. ts := now()
  210. pcode := "p_" + uid
  211. userId := insertUser(ctx, t, m, &userModel.SysUser{
  212. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  213. Email: uid + "@test.com", Phone: "13800000002", DeptId: 0,
  214. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  215. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  216. })
  217. productId := insertProduct(ctx, t, m, &productModel.SysProduct{
  218. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  219. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  220. })
  221. t.Cleanup(func() {
  222. loader.Del(ctx, userId, pcode)
  223. cleanTable(ctx, conn, "`sys_product`", productId)
  224. cleanTable(ctx, conn, "`sys_user`", userId)
  225. })
  226. loader.Del(ctx, userId, pcode)
  227. ud1 := loader.Load(ctx, userId, pcode)
  228. require.NotNil(t, ud1)
  229. ud2 := loader.Load(ctx, userId, pcode)
  230. require.NotNil(t, ud2)
  231. assert.Equal(t, ud1.UserId, ud2.UserId)
  232. assert.Equal(t, ud1.Username, ud2.Username)
  233. assert.Equal(t, ud1.ProductName, ud2.ProductName)
  234. }
  235. // --------------- TC-0487: Load-用户不存在 ---------------
  236. func TestLoad_UserNotExist(t *testing.T) {
  237. ctx := context.Background()
  238. loader := newTestLoader()
  239. nonExistId := int64(999999999)
  240. loader.Del(ctx, nonExistId, "nonexist_product")
  241. ud := loader.Load(ctx, nonExistId, "nonexist_product")
  242. require.NotNil(t, ud)
  243. assert.Equal(t, int64(0), ud.Status)
  244. assert.Empty(t, ud.Username)
  245. assert.Empty(t, ud.Perms)
  246. assert.Empty(t, ud.Roles)
  247. loader.Del(ctx, nonExistId, "nonexist_product")
  248. }
  249. // --------------- TC-0488: Load-productCode为空 ---------------
  250. func TestLoad_EmptyProductCode(t *testing.T) {
  251. ctx := context.Background()
  252. conn := testConn()
  253. m := testModels()
  254. loader := newTestLoader()
  255. uid := uniqueId()
  256. ts := now()
  257. userId := insertUser(ctx, t, m, &userModel.SysUser{
  258. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  259. Email: uid + "@test.com", Phone: "13800000003", DeptId: 0,
  260. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  261. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  262. })
  263. t.Cleanup(func() {
  264. loader.Del(ctx, userId, "")
  265. cleanTable(ctx, conn, "`sys_user`", userId)
  266. })
  267. loader.Del(ctx, userId, "")
  268. ud := loader.Load(ctx, userId, "")
  269. require.NotNil(t, ud)
  270. assert.Equal(t, uid, ud.Username)
  271. assert.Equal(t, int64(consts.StatusEnabled), ud.Status)
  272. assert.Empty(t, ud.ProductCode)
  273. assert.Empty(t, ud.ProductName)
  274. assert.Empty(t, ud.MemberType)
  275. assert.Empty(t, ud.Roles)
  276. assert.Empty(t, ud.Perms)
  277. }
  278. // --------------- TC-0489: Del删除指定缓存 ---------------
  279. func TestDel(t *testing.T) {
  280. ctx := context.Background()
  281. conn := testConn()
  282. m := testModels()
  283. loader := newTestLoader()
  284. uid := uniqueId()
  285. ts := now()
  286. pcode := "p_" + uid
  287. userId := insertUser(ctx, t, m, &userModel.SysUser{
  288. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  289. Email: uid + "@test.com", Phone: "13800000004", DeptId: 0,
  290. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  291. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  292. })
  293. productId := insertProduct(ctx, t, m, &productModel.SysProduct{
  294. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  295. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  296. })
  297. t.Cleanup(func() {
  298. loader.Del(ctx, userId, pcode)
  299. cleanTable(ctx, conn, "`sys_product`", productId)
  300. cleanTable(ctx, conn, "`sys_user`", userId)
  301. })
  302. loader.Del(ctx, userId, pcode)
  303. ud1 := loader.Load(ctx, userId, pcode)
  304. require.NotNil(t, ud1)
  305. assert.Equal(t, uid, ud1.Username)
  306. loader.Del(ctx, userId, pcode)
  307. ud2 := loader.Load(ctx, userId, pcode)
  308. require.NotNil(t, ud2)
  309. assert.Equal(t, uid, ud2.Username)
  310. }
  311. // --------------- TC-0490: Clean清除用户所有产品缓存 ---------------
  312. func TestClean(t *testing.T) {
  313. ctx := context.Background()
  314. conn := testConn()
  315. m := testModels()
  316. loader := newTestLoader()
  317. uid := uniqueId()
  318. ts := now()
  319. pcode1 := "p1_" + uid
  320. pcode2 := "p2_" + uid
  321. userId := insertUser(ctx, t, m, &userModel.SysUser{
  322. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  323. Email: uid + "@test.com", Phone: "13800000005", DeptId: 0,
  324. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  325. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  326. })
  327. pid1 := insertProduct(ctx, t, m, &productModel.SysProduct{
  328. Code: pcode1, Name: "prod1_" + uid, AppKey: "ak1_" + uid, AppSecret: "as1_" + uid,
  329. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  330. })
  331. pid2 := insertProduct(ctx, t, m, &productModel.SysProduct{
  332. Code: pcode2, Name: "prod2_" + uid, AppKey: "ak2_" + uid, AppSecret: "as2_" + uid,
  333. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  334. })
  335. t.Cleanup(func() {
  336. loader.Del(ctx, userId, pcode1)
  337. loader.Del(ctx, userId, pcode2)
  338. cleanTable(ctx, conn, "`sys_product`", pid1, pid2)
  339. cleanTable(ctx, conn, "`sys_user`", userId)
  340. })
  341. loader.Del(ctx, userId, pcode1)
  342. loader.Del(ctx, userId, pcode2)
  343. ud1 := loader.Load(ctx, userId, pcode1)
  344. ud2 := loader.Load(ctx, userId, pcode2)
  345. require.NotNil(t, ud1)
  346. require.NotNil(t, ud2)
  347. rds := testRedis()
  348. key1 := loader.cacheKey(userId, pcode1)
  349. key2 := loader.cacheKey(userId, pcode2)
  350. v1, _ := rds.GetCtx(ctx, key1)
  351. v2, _ := rds.GetCtx(ctx, key2)
  352. assert.NotEmpty(t, v1)
  353. assert.NotEmpty(t, v2)
  354. loader.Clean(ctx, userId)
  355. v1After, _ := rds.GetCtx(ctx, key1)
  356. v2After, _ := rds.GetCtx(ctx, key2)
  357. assert.Empty(t, v1After)
  358. assert.Empty(t, v2After)
  359. }
  360. // --------------- TC-0491: CleanByProduct清除产品所有用户 ---------------
  361. func TestCleanByProduct(t *testing.T) {
  362. ctx := context.Background()
  363. conn := testConn()
  364. m := testModels()
  365. loader := newTestLoader()
  366. uid1 := uniqueId()
  367. uid2 := uniqueId()
  368. ts := now()
  369. pcode := "p_" + uid1
  370. userId1 := insertUser(ctx, t, m, &userModel.SysUser{
  371. Username: uid1, Password: hashPwd("pass123"), Nickname: "nick_" + uid1,
  372. Email: uid1 + "@test.com", Phone: "13800000006", DeptId: 0,
  373. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  374. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  375. })
  376. userId2 := insertUser(ctx, t, m, &userModel.SysUser{
  377. Username: uid2, Password: hashPwd("pass123"), Nickname: "nick_" + uid2,
  378. Email: uid2 + "@test.com", Phone: "13800000007", DeptId: 0,
  379. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  380. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  381. })
  382. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  383. Code: pcode, Name: "prod_" + uid1, AppKey: "ak_" + uid1, AppSecret: "as_" + uid1,
  384. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  385. })
  386. t.Cleanup(func() {
  387. loader.Del(ctx, userId1, pcode)
  388. loader.Del(ctx, userId2, pcode)
  389. cleanTable(ctx, conn, "`sys_product`", pid)
  390. cleanTable(ctx, conn, "`sys_user`", userId1, userId2)
  391. })
  392. loader.Del(ctx, userId1, pcode)
  393. loader.Del(ctx, userId2, pcode)
  394. loader.Load(ctx, userId1, pcode)
  395. loader.Load(ctx, userId2, pcode)
  396. rds := testRedis()
  397. k1 := loader.cacheKey(userId1, pcode)
  398. k2 := loader.cacheKey(userId2, pcode)
  399. v1, _ := rds.GetCtx(ctx, k1)
  400. v2, _ := rds.GetCtx(ctx, k2)
  401. assert.NotEmpty(t, v1)
  402. assert.NotEmpty(t, v2)
  403. loader.CleanByProduct(ctx, pcode)
  404. v1After, _ := rds.GetCtx(ctx, k1)
  405. v2After, _ := rds.GetCtx(ctx, k2)
  406. assert.Empty(t, v1After)
  407. assert.Empty(t, v2After)
  408. }
  409. // --------------- TC-0492: BatchDel批量删除 ---------------
  410. func TestBatchDel(t *testing.T) {
  411. ctx := context.Background()
  412. conn := testConn()
  413. m := testModels()
  414. loader := newTestLoader()
  415. uid1 := uniqueId()
  416. uid2 := uniqueId()
  417. ts := now()
  418. pcode := "p_" + uid1
  419. userId1 := insertUser(ctx, t, m, &userModel.SysUser{
  420. Username: uid1, Password: hashPwd("pass123"), Nickname: "nick_" + uid1,
  421. Email: uid1 + "@test.com", Phone: "13800000008", DeptId: 0,
  422. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  423. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  424. })
  425. userId2 := insertUser(ctx, t, m, &userModel.SysUser{
  426. Username: uid2, Password: hashPwd("pass123"), Nickname: "nick_" + uid2,
  427. Email: uid2 + "@test.com", Phone: "13800000009", DeptId: 0,
  428. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  429. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  430. })
  431. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  432. Code: pcode, Name: "prod_" + uid1, AppKey: "ak_" + uid1, AppSecret: "as_" + uid1,
  433. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  434. })
  435. t.Cleanup(func() {
  436. loader.Del(ctx, userId1, pcode)
  437. loader.Del(ctx, userId2, pcode)
  438. cleanTable(ctx, conn, "`sys_product`", pid)
  439. cleanTable(ctx, conn, "`sys_user`", userId1, userId2)
  440. })
  441. loader.Del(ctx, userId1, pcode)
  442. loader.Del(ctx, userId2, pcode)
  443. loader.Load(ctx, userId1, pcode)
  444. loader.Load(ctx, userId2, pcode)
  445. rds := testRedis()
  446. k1 := loader.cacheKey(userId1, pcode)
  447. k2 := loader.cacheKey(userId2, pcode)
  448. v1, _ := rds.GetCtx(ctx, k1)
  449. v2, _ := rds.GetCtx(ctx, k2)
  450. assert.NotEmpty(t, v1)
  451. assert.NotEmpty(t, v2)
  452. loader.BatchDel(ctx, []int64{userId1, userId2}, pcode)
  453. v1After, _ := rds.GetCtx(ctx, k1)
  454. v2After, _ := rds.GetCtx(ctx, k2)
  455. assert.Empty(t, v1After)
  456. assert.Empty(t, v2After)
  457. }
  458. // --------------- TC-0493: BatchDel空数组 ---------------
  459. func TestBatchDel_EmptySlice(t *testing.T) {
  460. ctx := context.Background()
  461. loader := newTestLoader()
  462. loader.BatchDel(ctx, []int64{}, "some_code")
  463. }
  464. // --------------- TC-0494: loadPerms-超管拥有全部权限 ---------------
  465. func TestLoadPerms_SuperAdmin(t *testing.T) {
  466. ctx := context.Background()
  467. conn := testConn()
  468. m := testModels()
  469. loader := newTestLoader()
  470. uid := uniqueId()
  471. ts := now()
  472. pcode := "p_" + uid
  473. userId := insertUser(ctx, t, m, &userModel.SysUser{
  474. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  475. Email: uid + "@test.com", Phone: "13800000010", DeptId: 0,
  476. IsSuperAdmin: consts.IsSuperAdminYes, MustChangePassword: consts.MustChangePasswordNo,
  477. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  478. })
  479. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  480. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  481. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  482. })
  483. permCode1 := "perm1:" + uid
  484. permCode2 := "perm2:" + uid
  485. permId1 := insertPerm(ctx, t, m, &permModel.SysPerm{
  486. ProductCode: pcode, Name: "p1_" + uid, Code: permCode1,
  487. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  488. })
  489. permId2 := insertPerm(ctx, t, m, &permModel.SysPerm{
  490. ProductCode: pcode, Name: "p2_" + uid, Code: permCode2,
  491. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  492. })
  493. t.Cleanup(func() {
  494. loader.Del(ctx, userId, pcode)
  495. cleanTable(ctx, conn, "`sys_perm`", permId1, permId2)
  496. cleanTable(ctx, conn, "`sys_product`", pid)
  497. cleanTable(ctx, conn, "`sys_user`", userId)
  498. })
  499. loader.Del(ctx, userId, pcode)
  500. ud := loader.Load(ctx, userId, pcode)
  501. require.NotNil(t, ud)
  502. assert.True(t, ud.IsSuperAdmin)
  503. assert.Equal(t, consts.MemberTypeSuperAdmin, ud.MemberType)
  504. sort.Strings(ud.Perms)
  505. expected := []string{permCode1, permCode2}
  506. sort.Strings(expected)
  507. assert.Equal(t, expected, ud.Perms)
  508. }
  509. // --------------- TC-0495: loadPerms-ADMIN成员拥有全部权限 ---------------
  510. func TestLoadPerms_AdminMember(t *testing.T) {
  511. ctx := context.Background()
  512. conn := testConn()
  513. m := testModels()
  514. loader := newTestLoader()
  515. uid := uniqueId()
  516. ts := now()
  517. pcode := "p_" + uid
  518. userId := insertUser(ctx, t, m, &userModel.SysUser{
  519. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  520. Email: uid + "@test.com", Phone: "13800000011", DeptId: 0,
  521. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  522. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  523. })
  524. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  525. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  526. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  527. })
  528. memberId := insertMember(ctx, t, m, &memberModel.SysProductMember{
  529. ProductCode: pcode, UserId: userId, MemberType: consts.MemberTypeAdmin,
  530. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  531. })
  532. permCode := "perm:" + uid
  533. permId := insertPerm(ctx, t, m, &permModel.SysPerm{
  534. ProductCode: pcode, Name: "p_" + uid, Code: permCode,
  535. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  536. })
  537. t.Cleanup(func() {
  538. loader.Del(ctx, userId, pcode)
  539. cleanTable(ctx, conn, "`sys_perm`", permId)
  540. cleanTable(ctx, conn, "`sys_product_member`", memberId)
  541. cleanTable(ctx, conn, "`sys_product`", pid)
  542. cleanTable(ctx, conn, "`sys_user`", userId)
  543. })
  544. loader.Del(ctx, userId, pcode)
  545. ud := loader.Load(ctx, userId, pcode)
  546. require.NotNil(t, ud)
  547. assert.Equal(t, consts.MemberTypeAdmin, ud.MemberType)
  548. assert.Contains(t, ud.Perms, permCode)
  549. }
  550. // --------------- TC-0496: loadPerms-DEVELOPER成员拥有全部权限 ---------------
  551. func TestLoadPerms_DeveloperMember(t *testing.T) {
  552. ctx := context.Background()
  553. conn := testConn()
  554. m := testModels()
  555. loader := newTestLoader()
  556. uid := uniqueId()
  557. ts := now()
  558. pcode := "p_" + uid
  559. userId := insertUser(ctx, t, m, &userModel.SysUser{
  560. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  561. Email: uid + "@test.com", Phone: "13800000012", DeptId: 0,
  562. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  563. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  564. })
  565. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  566. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  567. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  568. })
  569. memberId := insertMember(ctx, t, m, &memberModel.SysProductMember{
  570. ProductCode: pcode, UserId: userId, MemberType: consts.MemberTypeDeveloper,
  571. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  572. })
  573. permCode := "perm:" + uid
  574. permId := insertPerm(ctx, t, m, &permModel.SysPerm{
  575. ProductCode: pcode, Name: "p_" + uid, Code: permCode,
  576. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  577. })
  578. t.Cleanup(func() {
  579. loader.Del(ctx, userId, pcode)
  580. cleanTable(ctx, conn, "`sys_perm`", permId)
  581. cleanTable(ctx, conn, "`sys_product_member`", memberId)
  582. cleanTable(ctx, conn, "`sys_product`", pid)
  583. cleanTable(ctx, conn, "`sys_user`", userId)
  584. })
  585. loader.Del(ctx, userId, pcode)
  586. ud := loader.Load(ctx, userId, pcode)
  587. require.NotNil(t, ud)
  588. assert.Equal(t, consts.MemberTypeDeveloper, ud.MemberType)
  589. assert.Contains(t, ud.Perms, permCode)
  590. }
  591. // --------------- TC-0497: loadPerms-DEV部门成员拥有全部权限 ---------------
  592. func TestLoadPerms_DevDept(t *testing.T) {
  593. ctx := context.Background()
  594. conn := testConn()
  595. m := testModels()
  596. loader := newTestLoader()
  597. uid := uniqueId()
  598. ts := now()
  599. pcode := "p_" + uid
  600. deptId := insertDept(ctx, t, m, &deptModel.SysDept{
  601. ParentId: 0, Name: "devdept_" + uid, Path: "/1/", Sort: 1,
  602. DeptType: consts.DeptTypeDev, Status: consts.StatusEnabled,
  603. CreateTime: ts, UpdateTime: ts,
  604. })
  605. userId := insertUser(ctx, t, m, &userModel.SysUser{
  606. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  607. Email: uid + "@test.com", Phone: "13800000013", DeptId: deptId,
  608. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  609. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  610. })
  611. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  612. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  613. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  614. })
  615. memberId := insertMember(ctx, t, m, &memberModel.SysProductMember{
  616. ProductCode: pcode, UserId: userId, MemberType: consts.MemberTypeMember,
  617. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  618. })
  619. permCode := "perm:" + uid
  620. permId := insertPerm(ctx, t, m, &permModel.SysPerm{
  621. ProductCode: pcode, Name: "p_" + uid, Code: permCode,
  622. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  623. })
  624. t.Cleanup(func() {
  625. loader.Del(ctx, userId, pcode)
  626. cleanTable(ctx, conn, "`sys_perm`", permId)
  627. cleanTable(ctx, conn, "`sys_product_member`", memberId)
  628. cleanTable(ctx, conn, "`sys_product`", pid)
  629. cleanTable(ctx, conn, "`sys_user`", userId)
  630. cleanTable(ctx, conn, "`sys_dept`", deptId)
  631. })
  632. loader.Del(ctx, userId, pcode)
  633. ud := loader.Load(ctx, userId, pcode)
  634. require.NotNil(t, ud)
  635. assert.Equal(t, consts.DeptTypeDev, ud.DeptType)
  636. assert.Contains(t, ud.Perms, permCode)
  637. }
  638. // --------------- TC-0498: MEMBER角色权限+ALLOW-DENY ---------------
  639. func TestLoadPerms_MemberRolePermWithAllowDeny(t *testing.T) {
  640. ctx := context.Background()
  641. conn := testConn()
  642. m := testModels()
  643. loader := newTestLoader()
  644. uid := uniqueId()
  645. ts := now()
  646. pcode := "p_" + uid
  647. userId := insertUser(ctx, t, m, &userModel.SysUser{
  648. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  649. Email: uid + "@test.com", Phone: "13800000014", DeptId: 0,
  650. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  651. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  652. })
  653. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  654. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  655. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  656. })
  657. memberId := insertMember(ctx, t, m, &memberModel.SysProductMember{
  658. ProductCode: pcode, UserId: userId, MemberType: consts.MemberTypeMember,
  659. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  660. })
  661. roleId := insertRole(ctx, t, m, &roleModel.SysRole{
  662. ProductCode: pcode, Name: "role_" + uid, Remark: "test",
  663. Status: consts.StatusEnabled, PermsLevel: 10, CreateTime: ts, UpdateTime: ts,
  664. })
  665. urId := insertUserRole(ctx, t, m, &userRoleModel.SysUserRole{
  666. UserId: userId, RoleId: roleId, CreateTime: ts, UpdateTime: ts,
  667. })
  668. // role perm: permA, permB
  669. permIdA := insertPerm(ctx, t, m, &permModel.SysPerm{
  670. ProductCode: pcode, Name: "permA_" + uid, Code: "permA:" + uid,
  671. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  672. })
  673. permIdB := insertPerm(ctx, t, m, &permModel.SysPerm{
  674. ProductCode: pcode, Name: "permB_" + uid, Code: "permB:" + uid,
  675. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  676. })
  677. // user ALLOW perm: permC
  678. permIdC := insertPerm(ctx, t, m, &permModel.SysPerm{
  679. ProductCode: pcode, Name: "permC_" + uid, Code: "permC:" + uid,
  680. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  681. })
  682. // user DENY perm: permB (should remove permB from result)
  683. rpIdA := insertRolePerm(ctx, t, m, &rolePermModel.SysRolePerm{
  684. RoleId: roleId, PermId: permIdA, CreateTime: ts, UpdateTime: ts,
  685. })
  686. rpIdB := insertRolePerm(ctx, t, m, &rolePermModel.SysRolePerm{
  687. RoleId: roleId, PermId: permIdB, CreateTime: ts, UpdateTime: ts,
  688. })
  689. upAllow := insertUserPerm(ctx, t, m, &userPermModel.SysUserPerm{
  690. UserId: userId, PermId: permIdC, Effect: consts.PermEffectAllow,
  691. CreateTime: ts, UpdateTime: ts,
  692. })
  693. upDeny := insertUserPerm(ctx, t, m, &userPermModel.SysUserPerm{
  694. UserId: userId, PermId: permIdB, Effect: consts.PermEffectDeny,
  695. CreateTime: ts, UpdateTime: ts,
  696. })
  697. t.Cleanup(func() {
  698. loader.Del(ctx, userId, pcode)
  699. cleanTable(ctx, conn, "`sys_user_perm`", upAllow, upDeny)
  700. cleanTable(ctx, conn, "`sys_role_perm`", rpIdA, rpIdB)
  701. cleanTable(ctx, conn, "`sys_perm`", permIdA, permIdB, permIdC)
  702. cleanTable(ctx, conn, "`sys_user_role`", urId)
  703. cleanTable(ctx, conn, "`sys_role`", roleId)
  704. cleanTable(ctx, conn, "`sys_product_member`", memberId)
  705. cleanTable(ctx, conn, "`sys_product`", pid)
  706. cleanTable(ctx, conn, "`sys_user`", userId)
  707. })
  708. loader.Del(ctx, userId, pcode)
  709. ud := loader.Load(ctx, userId, pcode)
  710. require.NotNil(t, ud)
  711. // permA (from role) + permC (from ALLOW) should be present
  712. // permB (denied) should NOT be present
  713. assert.Contains(t, ud.Perms, "permA:"+uid)
  714. assert.Contains(t, ud.Perms, "permC:"+uid)
  715. assert.NotContains(t, ud.Perms, "permB:"+uid)
  716. }
  717. // --------------- TC-0501: loadRoles-多角色取最小permsLevel ---------------
  718. func TestLoadRoles_MinPermsLevel(t *testing.T) {
  719. ctx := context.Background()
  720. conn := testConn()
  721. m := testModels()
  722. loader := newTestLoader()
  723. uid := uniqueId()
  724. ts := now()
  725. pcode := "p_" + uid
  726. userId := insertUser(ctx, t, m, &userModel.SysUser{
  727. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  728. Email: uid + "@test.com", Phone: "13800000015", DeptId: 0,
  729. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  730. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  731. })
  732. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  733. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  734. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  735. })
  736. memberId := insertMember(ctx, t, m, &memberModel.SysProductMember{
  737. ProductCode: pcode, UserId: userId, MemberType: consts.MemberTypeMember,
  738. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  739. })
  740. roleId1 := insertRole(ctx, t, m, &roleModel.SysRole{
  741. ProductCode: pcode, Name: "roleH_" + uid, Remark: "high",
  742. Status: consts.StatusEnabled, PermsLevel: 10, CreateTime: ts, UpdateTime: ts,
  743. })
  744. roleId2 := insertRole(ctx, t, m, &roleModel.SysRole{
  745. ProductCode: pcode, Name: "roleL_" + uid, Remark: "low",
  746. Status: consts.StatusEnabled, PermsLevel: 5, CreateTime: ts, UpdateTime: ts,
  747. })
  748. urId1 := insertUserRole(ctx, t, m, &userRoleModel.SysUserRole{
  749. UserId: userId, RoleId: roleId1, CreateTime: ts, UpdateTime: ts,
  750. })
  751. urId2 := insertUserRole(ctx, t, m, &userRoleModel.SysUserRole{
  752. UserId: userId, RoleId: roleId2, CreateTime: ts, UpdateTime: ts,
  753. })
  754. t.Cleanup(func() {
  755. loader.Del(ctx, userId, pcode)
  756. cleanTable(ctx, conn, "`sys_user_role`", urId1, urId2)
  757. cleanTable(ctx, conn, "`sys_role`", roleId1, roleId2)
  758. cleanTable(ctx, conn, "`sys_product_member`", memberId)
  759. cleanTable(ctx, conn, "`sys_product`", pid)
  760. cleanTable(ctx, conn, "`sys_user`", userId)
  761. })
  762. loader.Del(ctx, userId, pcode)
  763. ud := loader.Load(ctx, userId, pcode)
  764. require.NotNil(t, ud)
  765. assert.Len(t, ud.Roles, 2)
  766. assert.Equal(t, int64(5), ud.MinPermsLevel)
  767. }
  768. // --------------- TC-0502: loadRoles-无角色 ---------------
  769. func TestLoadRoles_NoRoles(t *testing.T) {
  770. ctx := context.Background()
  771. conn := testConn()
  772. m := testModels()
  773. loader := newTestLoader()
  774. uid := uniqueId()
  775. ts := now()
  776. pcode := "p_" + uid
  777. userId := insertUser(ctx, t, m, &userModel.SysUser{
  778. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  779. Email: uid + "@test.com", Phone: "13800000016", DeptId: 0,
  780. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  781. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  782. })
  783. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  784. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  785. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  786. })
  787. t.Cleanup(func() {
  788. loader.Del(ctx, userId, pcode)
  789. cleanTable(ctx, conn, "`sys_product`", pid)
  790. cleanTable(ctx, conn, "`sys_user`", userId)
  791. })
  792. loader.Del(ctx, userId, pcode)
  793. ud := loader.Load(ctx, userId, pcode)
  794. require.NotNil(t, ud)
  795. assert.Equal(t, int64(math.MaxInt64), ud.MinPermsLevel)
  796. }
  797. // --------------- TC-0503: loadRoles-角色跨产品过滤 ---------------
  798. func TestLoadRoles_CrossProductFilter(t *testing.T) {
  799. ctx := context.Background()
  800. conn := testConn()
  801. m := testModels()
  802. loader := newTestLoader()
  803. uid := uniqueId()
  804. ts := now()
  805. pcodeA := "pA_" + uid
  806. pcodeB := "pB_" + uid
  807. userId := insertUser(ctx, t, m, &userModel.SysUser{
  808. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  809. Email: uid + "@test.com", Phone: "13800000017", DeptId: 0,
  810. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  811. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  812. })
  813. pidA := insertProduct(ctx, t, m, &productModel.SysProduct{
  814. Code: pcodeA, Name: "prodA_" + uid, AppKey: "akA_" + uid, AppSecret: "asA_" + uid,
  815. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  816. })
  817. pidB := insertProduct(ctx, t, m, &productModel.SysProduct{
  818. Code: pcodeB, Name: "prodB_" + uid, AppKey: "akB_" + uid, AppSecret: "asB_" + uid,
  819. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  820. })
  821. memA := insertMember(ctx, t, m, &memberModel.SysProductMember{
  822. ProductCode: pcodeA, UserId: userId, MemberType: consts.MemberTypeMember,
  823. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  824. })
  825. roleA := insertRole(ctx, t, m, &roleModel.SysRole{
  826. ProductCode: pcodeA, Name: "roleA_" + uid, Remark: "A",
  827. Status: consts.StatusEnabled, PermsLevel: 10, CreateTime: ts, UpdateTime: ts,
  828. })
  829. roleB := insertRole(ctx, t, m, &roleModel.SysRole{
  830. ProductCode: pcodeB, Name: "roleB_" + uid, Remark: "B",
  831. Status: consts.StatusEnabled, PermsLevel: 20, CreateTime: ts, UpdateTime: ts,
  832. })
  833. urA := insertUserRole(ctx, t, m, &userRoleModel.SysUserRole{
  834. UserId: userId, RoleId: roleA, CreateTime: ts, UpdateTime: ts,
  835. })
  836. urB := insertUserRole(ctx, t, m, &userRoleModel.SysUserRole{
  837. UserId: userId, RoleId: roleB, CreateTime: ts, UpdateTime: ts,
  838. })
  839. t.Cleanup(func() {
  840. loader.Del(ctx, userId, pcodeA)
  841. loader.Del(ctx, userId, pcodeB)
  842. cleanTable(ctx, conn, "`sys_user_role`", urA, urB)
  843. cleanTable(ctx, conn, "`sys_role`", roleA, roleB)
  844. cleanTable(ctx, conn, "`sys_product_member`", memA)
  845. cleanTable(ctx, conn, "`sys_product`", pidA, pidB)
  846. cleanTable(ctx, conn, "`sys_user`", userId)
  847. })
  848. loader.Del(ctx, userId, pcodeA)
  849. ud := loader.Load(ctx, userId, pcodeA)
  850. require.NotNil(t, ud)
  851. assert.Len(t, ud.Roles, 1)
  852. assert.Equal(t, roleA, ud.Roles[0].Id)
  853. assert.Equal(t, int64(10), ud.MinPermsLevel)
  854. }
  855. // --------------- TC-0504: loadRoles-禁用角色不计入 ---------------
  856. func TestLoadRoles_DisabledRoleExcluded(t *testing.T) {
  857. ctx := context.Background()
  858. conn := testConn()
  859. m := testModels()
  860. loader := newTestLoader()
  861. uid := uniqueId()
  862. ts := now()
  863. pcode := "p_" + uid
  864. userId := insertUser(ctx, t, m, &userModel.SysUser{
  865. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  866. Email: uid + "@test.com", Phone: "13800000018", DeptId: 0,
  867. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  868. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  869. })
  870. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  871. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  872. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  873. })
  874. memberId := insertMember(ctx, t, m, &memberModel.SysProductMember{
  875. ProductCode: pcode, UserId: userId, MemberType: consts.MemberTypeMember,
  876. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  877. })
  878. enabledRole := insertRole(ctx, t, m, &roleModel.SysRole{
  879. ProductCode: pcode, Name: "rEnabled_" + uid, Remark: "enabled",
  880. Status: consts.StatusEnabled, PermsLevel: 5, CreateTime: ts, UpdateTime: ts,
  881. })
  882. disabledRole := insertRole(ctx, t, m, &roleModel.SysRole{
  883. ProductCode: pcode, Name: "rDisabled_" + uid, Remark: "disabled",
  884. Status: consts.StatusDisabled, PermsLevel: 1, CreateTime: ts, UpdateTime: ts,
  885. })
  886. ur1 := insertUserRole(ctx, t, m, &userRoleModel.SysUserRole{
  887. UserId: userId, RoleId: enabledRole, CreateTime: ts, UpdateTime: ts,
  888. })
  889. ur2 := insertUserRole(ctx, t, m, &userRoleModel.SysUserRole{
  890. UserId: userId, RoleId: disabledRole, CreateTime: ts, UpdateTime: ts,
  891. })
  892. t.Cleanup(func() {
  893. loader.Del(ctx, userId, pcode)
  894. cleanTable(ctx, conn, "`sys_user_role`", ur1, ur2)
  895. cleanTable(ctx, conn, "`sys_role`", enabledRole, disabledRole)
  896. cleanTable(ctx, conn, "`sys_product_member`", memberId)
  897. cleanTable(ctx, conn, "`sys_product`", pid)
  898. cleanTable(ctx, conn, "`sys_user`", userId)
  899. })
  900. loader.Del(ctx, userId, pcode)
  901. ud := loader.Load(ctx, userId, pcode)
  902. require.NotNil(t, ud)
  903. assert.Len(t, ud.Roles, 1)
  904. assert.Equal(t, enabledRole, ud.Roles[0].Id)
  905. assert.Equal(t, int64(5), ud.MinPermsLevel)
  906. }
  907. // --------------- TC-0505: loadMembership-超管自动SUPER_ADMIN ---------------
  908. func TestLoadMembership_SuperAdminAuto(t *testing.T) {
  909. ctx := context.Background()
  910. conn := testConn()
  911. m := testModels()
  912. loader := newTestLoader()
  913. uid := uniqueId()
  914. ts := now()
  915. pcode := "p_" + uid
  916. userId := insertUser(ctx, t, m, &userModel.SysUser{
  917. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  918. Email: uid + "@test.com", Phone: "13800000019", DeptId: 0,
  919. IsSuperAdmin: consts.IsSuperAdminYes, MustChangePassword: consts.MustChangePasswordNo,
  920. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  921. })
  922. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  923. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  924. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  925. })
  926. t.Cleanup(func() {
  927. loader.Del(ctx, userId, pcode)
  928. cleanTable(ctx, conn, "`sys_product`", pid)
  929. cleanTable(ctx, conn, "`sys_user`", userId)
  930. })
  931. loader.Del(ctx, userId, pcode)
  932. ud := loader.Load(ctx, userId, pcode)
  933. require.NotNil(t, ud)
  934. assert.True(t, ud.IsSuperAdmin)
  935. assert.Equal(t, consts.MemberTypeSuperAdmin, ud.MemberType)
  936. }
  937. // --------------- TC-0506: loadMembership-非成员MemberType为空 ---------------
  938. func TestLoadMembership_NonMemberEmpty(t *testing.T) {
  939. ctx := context.Background()
  940. conn := testConn()
  941. m := testModels()
  942. loader := newTestLoader()
  943. uid := uniqueId()
  944. ts := now()
  945. pcode := "p_" + uid
  946. userId := insertUser(ctx, t, m, &userModel.SysUser{
  947. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  948. Email: uid + "@test.com", Phone: "13800000020", DeptId: 0,
  949. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  950. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  951. })
  952. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  953. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  954. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  955. })
  956. t.Cleanup(func() {
  957. loader.Del(ctx, userId, pcode)
  958. cleanTable(ctx, conn, "`sys_product`", pid)
  959. cleanTable(ctx, conn, "`sys_user`", userId)
  960. })
  961. loader.Del(ctx, userId, pcode)
  962. ud := loader.Load(ctx, userId, pcode)
  963. require.NotNil(t, ud)
  964. assert.False(t, ud.IsSuperAdmin)
  965. assert.Empty(t, ud.MemberType)
  966. }
  967. // --------------- TC-0499: loadPerms-用户ALLOW权限不跨产品泄漏(H-1修复验证) ---------------
  968. func TestLoadPerms_CrossProductPermIsolation(t *testing.T) {
  969. ctx := context.Background()
  970. conn := testConn()
  971. m := testModels()
  972. loader := newTestLoader()
  973. uid := uniqueId()
  974. ts := now()
  975. pcodeA := "pA_" + uid
  976. pcodeB := "pB_" + uid
  977. userId := insertUser(ctx, t, m, &userModel.SysUser{
  978. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  979. Email: uid + "@test.com", Phone: "13800000030", DeptId: 0,
  980. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  981. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  982. })
  983. pidA := insertProduct(ctx, t, m, &productModel.SysProduct{
  984. Code: pcodeA, Name: "prodA_" + uid, AppKey: "akA_" + uid, AppSecret: "asA_" + uid,
  985. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  986. })
  987. pidB := insertProduct(ctx, t, m, &productModel.SysProduct{
  988. Code: pcodeB, Name: "prodB_" + uid, AppKey: "akB_" + uid, AppSecret: "asB_" + uid,
  989. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  990. })
  991. memA := insertMember(ctx, t, m, &memberModel.SysProductMember{
  992. ProductCode: pcodeA, UserId: userId, MemberType: consts.MemberTypeMember,
  993. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  994. })
  995. memB := insertMember(ctx, t, m, &memberModel.SysProductMember{
  996. ProductCode: pcodeB, UserId: userId, MemberType: consts.MemberTypeMember,
  997. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  998. })
  999. permA := insertPerm(ctx, t, m, &permModel.SysPerm{
  1000. ProductCode: pcodeA, Name: "permA_" + uid, Code: "permA:" + uid,
  1001. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  1002. })
  1003. permB := insertPerm(ctx, t, m, &permModel.SysPerm{
  1004. ProductCode: pcodeB, Name: "permB_" + uid, Code: "permB:" + uid,
  1005. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  1006. })
  1007. upA := insertUserPerm(ctx, t, m, &userPermModel.SysUserPerm{
  1008. UserId: userId, PermId: permA, Effect: consts.PermEffectAllow,
  1009. CreateTime: ts, UpdateTime: ts,
  1010. })
  1011. upB := insertUserPerm(ctx, t, m, &userPermModel.SysUserPerm{
  1012. UserId: userId, PermId: permB, Effect: consts.PermEffectAllow,
  1013. CreateTime: ts, UpdateTime: ts,
  1014. })
  1015. t.Cleanup(func() {
  1016. loader.Del(ctx, userId, pcodeA)
  1017. loader.Del(ctx, userId, pcodeB)
  1018. cleanTable(ctx, conn, "`sys_user_perm`", upA, upB)
  1019. cleanTable(ctx, conn, "`sys_perm`", permA, permB)
  1020. cleanTable(ctx, conn, "`sys_product_member`", memA, memB)
  1021. cleanTable(ctx, conn, "`sys_product`", pidA, pidB)
  1022. cleanTable(ctx, conn, "`sys_user`", userId)
  1023. })
  1024. loader.Del(ctx, userId, pcodeA)
  1025. udA := loader.Load(ctx, userId, pcodeA)
  1026. require.NotNil(t, udA)
  1027. assert.Contains(t, udA.Perms, "permA:"+uid, "产品A应包含自身权限")
  1028. assert.NotContains(t, udA.Perms, "permB:"+uid, "产品A不应包含产品B的权限(H-1)")
  1029. loader.Del(ctx, userId, pcodeB)
  1030. udB := loader.Load(ctx, userId, pcodeB)
  1031. require.NotNil(t, udB)
  1032. assert.Contains(t, udB.Perms, "permB:"+uid, "产品B应包含自身权限")
  1033. assert.NotContains(t, udB.Perms, "permA:"+uid, "产品B不应包含产品A的权限(H-1)")
  1034. }
  1035. // --------------- TC-0507: loadMembership-禁用成员MemberType为空(H-3修复验证) ---------------
  1036. func TestLoadMembership_DisabledMemberEmpty(t *testing.T) {
  1037. ctx := context.Background()
  1038. conn := testConn()
  1039. m := testModels()
  1040. loader := newTestLoader()
  1041. uid := uniqueId()
  1042. ts := now()
  1043. pcode := "p_" + uid
  1044. userId := insertUser(ctx, t, m, &userModel.SysUser{
  1045. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  1046. Email: uid + "@test.com", Phone: "13800000031", DeptId: 0,
  1047. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  1048. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  1049. })
  1050. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  1051. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  1052. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  1053. })
  1054. memberId := insertMember(ctx, t, m, &memberModel.SysProductMember{
  1055. ProductCode: pcode, UserId: userId, MemberType: consts.MemberTypeMember,
  1056. Status: consts.StatusDisabled, CreateTime: ts, UpdateTime: ts,
  1057. })
  1058. t.Cleanup(func() {
  1059. loader.Del(ctx, userId, pcode)
  1060. cleanTable(ctx, conn, "`sys_product_member`", memberId)
  1061. cleanTable(ctx, conn, "`sys_product`", pid)
  1062. cleanTable(ctx, conn, "`sys_user`", userId)
  1063. })
  1064. loader.Del(ctx, userId, pcode)
  1065. ud := loader.Load(ctx, userId, pcode)
  1066. require.NotNil(t, ud)
  1067. assert.Empty(t, ud.MemberType, "禁用成员的MemberType应为空(H-3)")
  1068. }
  1069. // --------------- TC-0500: loadPerms-DEV部门禁用后不再拥有全部权限(M-3修复验证) ---------------
  1070. func TestLoadPerms_DisabledDevDeptNoFullPerms(t *testing.T) {
  1071. ctx := context.Background()
  1072. conn := testConn()
  1073. m := testModels()
  1074. loader := newTestLoader()
  1075. uid := uniqueId()
  1076. ts := now()
  1077. pcode := "p_" + uid
  1078. deptId := insertDept(ctx, t, m, &deptModel.SysDept{
  1079. ParentId: 0, Name: "devdept_disabled_" + uid, Path: "/1/", Sort: 1,
  1080. DeptType: consts.DeptTypeDev, Status: consts.StatusDisabled,
  1081. CreateTime: ts, UpdateTime: ts,
  1082. })
  1083. userId := insertUser(ctx, t, m, &userModel.SysUser{
  1084. Username: uid, Password: hashPwd("pass123"), Nickname: "nick_" + uid,
  1085. Email: uid + "@test.com", Phone: "13800000032", DeptId: deptId,
  1086. IsSuperAdmin: consts.IsSuperAdminNo, MustChangePassword: consts.MustChangePasswordNo,
  1087. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  1088. })
  1089. pid := insertProduct(ctx, t, m, &productModel.SysProduct{
  1090. Code: pcode, Name: "prod_" + uid, AppKey: "ak_" + uid, AppSecret: "as_" + uid,
  1091. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  1092. })
  1093. memberId := insertMember(ctx, t, m, &memberModel.SysProductMember{
  1094. ProductCode: pcode, UserId: userId, MemberType: consts.MemberTypeMember,
  1095. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  1096. })
  1097. permCode := "perm_devtest:" + uid
  1098. permId := insertPerm(ctx, t, m, &permModel.SysPerm{
  1099. ProductCode: pcode, Name: "p_" + uid, Code: permCode,
  1100. Status: consts.StatusEnabled, CreateTime: ts, UpdateTime: ts,
  1101. })
  1102. t.Cleanup(func() {
  1103. loader.Del(ctx, userId, pcode)
  1104. cleanTable(ctx, conn, "`sys_perm`", permId)
  1105. cleanTable(ctx, conn, "`sys_product_member`", memberId)
  1106. cleanTable(ctx, conn, "`sys_product`", pid)
  1107. cleanTable(ctx, conn, "`sys_user`", userId)
  1108. cleanTable(ctx, conn, "`sys_dept`", deptId)
  1109. })
  1110. loader.Del(ctx, userId, pcode)
  1111. ud := loader.Load(ctx, userId, pcode)
  1112. require.NotNil(t, ud)
  1113. assert.Equal(t, consts.DeptTypeDev, ud.DeptType)
  1114. assert.Equal(t, int64(consts.StatusDisabled), ud.DeptStatus)
  1115. assert.Empty(t, ud.Perms, "禁用的DEV部门成员不应拥有全部权限(M-3)")
  1116. }