117 lines
2.6 KiB
Go
117 lines
2.6 KiB
Go
package document
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"strings"
|
|
"testing"
|
|
|
|
"mediarr/database"
|
|
"mediarr/ident"
|
|
"mediarr/identity"
|
|
)
|
|
|
|
var testPolicyID = ident.MustParse("00000000-0000-7000-8000-000000000101")
|
|
|
|
func newTestService(t *testing.T) *Service {
|
|
t.Helper()
|
|
|
|
t.Chdir(t.TempDir())
|
|
|
|
err, db := database.NewDatabase(database.NewDatabaseParams{})
|
|
if err != nil {
|
|
t.Fatalf("NewDatabase : %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
if err := db.Close(); err != nil {
|
|
t.Errorf("Close : %v", err)
|
|
}
|
|
})
|
|
|
|
return NewService(*db)
|
|
}
|
|
|
|
func mustPrincipal(t *testing.T, service *Service, kind, name string) *identity.Principal {
|
|
t.Helper()
|
|
|
|
principal, err := identity.NewService(service.db).CreatePrincipal(context.Background(), identity.CreatePrincipalParams{
|
|
Kind: kind,
|
|
Name: name,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreatePrincipal(%q) : %v", name, err)
|
|
}
|
|
|
|
return principal
|
|
}
|
|
|
|
func mustUser(t *testing.T, service *Service, name string) *identity.Principal {
|
|
return mustPrincipal(t, service, identity.KindUser, name)
|
|
}
|
|
|
|
func mustLibrary(t *testing.T, service *Service, owner *identity.Principal, name string) *Document {
|
|
t.Helper()
|
|
|
|
library, err := service.CreateLibrary(context.Background(), CreateLibraryParams{
|
|
OwnerID: owner.ID,
|
|
CreatedBy: owner.ID,
|
|
Name: name,
|
|
PolicyID: testPolicyID,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreateLibrary(%q) : %v", name, err)
|
|
}
|
|
|
|
return library
|
|
}
|
|
|
|
func mustFolder(t *testing.T, service *Service, owner *identity.Principal, parentID ident.UUID, name string) *Document {
|
|
t.Helper()
|
|
|
|
folder, err := service.CreateFolder(context.Background(), CreateFolderParams{
|
|
OwnerID: owner.ID,
|
|
CreatedBy: owner.ID,
|
|
ParentID: parentID,
|
|
Name: name,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreateFolder(%q) : %v", name, err)
|
|
}
|
|
|
|
return folder
|
|
}
|
|
|
|
func mustKindDocument(t *testing.T, service *Service, owner *identity.Principal, parent *Document, kind, name string) *Document {
|
|
t.Helper()
|
|
|
|
id, err := ident.NewV7()
|
|
if err != nil {
|
|
t.Fatalf("NewV7 : %v", err)
|
|
}
|
|
|
|
now := nowMillis()
|
|
|
|
created, err := service.db.WriteQueries.CreateDocument(context.Background(), database.CreateDocumentParams{
|
|
ID: id,
|
|
Kind: kind,
|
|
OwnerID: owner.ID,
|
|
CreatedBy: owner.ID,
|
|
ParentID: &parent.ID,
|
|
Name: name,
|
|
NameKey: strings.ToLower(name),
|
|
Path: parent.Path + hex.EncodeToString(id[:]) + "/",
|
|
Depth: parent.Depth + 1,
|
|
Metadata: "{}",
|
|
AclInherit: 1,
|
|
Rev: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreateDocument(%q) : %v", name, err)
|
|
}
|
|
|
|
return documentFromDB(created)
|
|
}
|