fix: align admin behavior and Kratos integrations
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
)
|
||||
|
||||
func newTransactionTestData(t *testing.T) *Data {
|
||||
t.Helper()
|
||||
name := strings.NewReplacer("/", "_", " ", "_").Replace(t.Name())
|
||||
db, err := openWithDriver("sqlite", "file:"+name+"?mode=memory&cache=shared")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = migrateAll(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data := &Data{gormDB: newReloadableDB(db), redis: newReloadableRedis(nil), runtime: conf.NewRuntime(&conf.Data{Database: &conf.Data_Database{Driver: "sqlite"}}, &conf.AdminBackend{})}
|
||||
t.Cleanup(func() { data.gormDB.close() })
|
||||
return data
|
||||
}
|
||||
|
||||
func TestUserAuthorityWritesAreAtomic(t *testing.T) {
|
||||
data := newTransactionTestData(t)
|
||||
ctx := context.Background()
|
||||
if err := data.gormDB.WithContext(ctx).Create(&[]authorityPO{{AuthorityID: 888, AuthorityName: "admin"}, {AuthorityID: 999, AuthorityName: "operator"}}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := &systemRepo{data: data}
|
||||
user := &biz.User{Username: "atomic", Password: "hash", NickName: "before", AuthorityID: 888, Enable: 1}
|
||||
created, err := repo.CreateUserWithAuthorities(ctx, user, []uint{888, 999})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var links int64
|
||||
if err = data.gormDB.WithContext(ctx).Model(&userAuthorityPO{}).Where("sys_user_id = ?", created.ID).Count(&links).Error; err != nil || links != 2 {
|
||||
t.Fatalf("authority links = %d, err = %v", links, err)
|
||||
}
|
||||
|
||||
created.NickName = "must rollback"
|
||||
if err = repo.UpdateUserWithAuthorities(ctx, created, []uint{888, 123456}); err == nil {
|
||||
t.Fatal("expected invalid authority update to fail")
|
||||
}
|
||||
var persisted userPO
|
||||
if err = data.gormDB.WithContext(ctx).First(&persisted, created.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if persisted.NickName != "before" {
|
||||
t.Fatalf("user update was not rolled back: %q", persisted.NickName)
|
||||
}
|
||||
|
||||
invalid := &biz.User{Username: "invalid", Password: "hash", AuthorityID: 888, Enable: 1}
|
||||
if _, err = repo.CreateUserWithAuthorities(ctx, invalid, []uint{888, 123456}); err == nil {
|
||||
t.Fatal("expected invalid authority create to fail")
|
||||
}
|
||||
var users int64
|
||||
if err = data.gormDB.WithContext(ctx).Model(&userPO{}).Where("username = ?", "invalid").Count(&users).Error; err != nil || users != 0 {
|
||||
t.Fatalf("partial user persisted: count=%d err=%v", users, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDictionaryImportKeepsHierarchyInOneTransaction(t *testing.T) {
|
||||
data := newTransactionTestData(t)
|
||||
ctx := context.Background()
|
||||
repo := &settingsRepo{data: data}
|
||||
parentID := uint(10)
|
||||
dictionary := &biz.Dictionary{Name: "status", Type: "status", Status: true}
|
||||
details := []*biz.DictionaryDetail{{ID: 10, Label: "parent", Value: "1", Status: true}, {ID: 11, Label: "child", Value: "2", ParentID: &parentID, Status: true}}
|
||||
if err := repo.ImportDictionary(ctx, dictionary, details); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var persisted []dictionaryDetailPO
|
||||
if err := data.gormDB.WithContext(ctx).Where("sys_dictionary_id = ?", dictionary.ID).Order("id").Find(&persisted).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(persisted) != 2 || persisted[1].ParentID == nil || *persisted[1].ParentID != persisted[0].ID {
|
||||
t.Fatalf("unexpected imported hierarchy: %+v", persisted)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user