166 lines
3.9 KiB
Go
166 lines
3.9 KiB
Go
package document
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"strings"
|
|
"testing"
|
|
|
|
"mediarr/database"
|
|
"mediarr/ident"
|
|
"mediarr/identity"
|
|
"mediarr/permission"
|
|
)
|
|
|
|
var testPolicyID = ident.MustParse("00000000-0000-7000-8000-000000000101")
|
|
|
|
type testAuthorizer struct {
|
|
allowAll bool
|
|
active map[ident.UUID]bool
|
|
grants map[ident.UUID]map[ident.UUID]permission.Permission
|
|
}
|
|
|
|
func newAllowAllAuthorizer() *testAuthorizer {
|
|
return &testAuthorizer{allowAll: true}
|
|
}
|
|
|
|
func (a *testAuthorizer) IsActive(_ context.Context, principalID ident.UUID) (bool, error) {
|
|
if a.active == nil {
|
|
return true, nil
|
|
}
|
|
if active, exists := a.active[principalID]; exists {
|
|
return active, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (a *testAuthorizer) Can(_ context.Context, principalID, documentID ident.UUID, required permission.Permission) (bool, error) {
|
|
if a.allowAll {
|
|
return true, nil
|
|
}
|
|
if a.active != nil {
|
|
if active, exists := a.active[principalID]; exists && !active {
|
|
return false, nil
|
|
}
|
|
}
|
|
return a.grants[documentID][principalID].Has(required), nil
|
|
}
|
|
|
|
func (a *testAuthorizer) grant(principalID, documentID ident.UUID, permissions permission.Permission) {
|
|
if a.grants == nil {
|
|
a.grants = make(map[ident.UUID]map[ident.UUID]permission.Permission)
|
|
}
|
|
if a.grants[documentID] == nil {
|
|
a.grants[documentID] = make(map[ident.UUID]permission.Permission)
|
|
}
|
|
a.grants[documentID][principalID] |= permissions
|
|
}
|
|
|
|
func newTestService(t *testing.T) *Service {
|
|
t.Helper()
|
|
|
|
return newTestServiceWithAuthorizer(t, newAllowAllAuthorizer())
|
|
}
|
|
|
|
func newTestServiceWithAuthorizer(t *testing.T, authorizer Authorizer) *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, authorizer)
|
|
}
|
|
|
|
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(), owner.ID, 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(), owner.ID, 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)
|
|
}
|