mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-26 10:23:28 +08:00
### Summary Refactor dataset.go document.do file.go file2document.go in internal/service.
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package file
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"ragflow/internal/dao"
|
|
"ragflow/internal/entity"
|
|
)
|
|
|
|
func setupPermissionDB(t *testing.T) {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{TranslateError: true})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(
|
|
&entity.File{},
|
|
&entity.File2Document{},
|
|
&entity.Document{},
|
|
&entity.Knowledgebase{},
|
|
); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
old := dao.DB
|
|
dao.DB = db
|
|
t.Cleanup(func() { dao.DB = old })
|
|
}
|
|
|
|
// checkFileTeamPermissionStub is a minimal permission checker for tests
|
|
// that avoids importing the parent service package (which would create a
|
|
// cycle in the test binary).
|
|
func checkFileTeamPermissionStub(_ *dao.FileDAO, file *entity.File, userID string) bool {
|
|
return file.TenantID == userID
|
|
}
|
|
|
|
func TestCheckFileTeamPermissionStub(t *testing.T) {
|
|
setupPermissionDB(t)
|
|
|
|
// Direct tenant match short-circuits before any DB lookup.
|
|
if !checkFileTeamPermissionStub(nil, &entity.File{TenantID: "u1"}, "u1") {
|
|
t.Error("file tenant match should be authorized")
|
|
}
|
|
|
|
// A file owned by another tenant is denied for a different user.
|
|
if checkFileTeamPermissionStub(nil, &entity.File{TenantID: "other"}, "u1") {
|
|
t.Error("file with different tenant should be denied")
|
|
}
|
|
}
|