143 lines
3.1 KiB
Go
143 lines
3.1 KiB
Go
package acl
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"mediarr/database"
|
|
"mediarr/document"
|
|
"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 := service.identity.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 mustGroup(t *testing.T, service *Service, name string) *identity.Principal {
|
|
return mustPrincipal(t, service, identity.KindGroup, name)
|
|
}
|
|
|
|
func addMember(t *testing.T, service *Service, groupID, memberID ident.UUID) {
|
|
t.Helper()
|
|
|
|
if err := service.identity.AddMember(context.Background(), groupID, memberID, identity.RoleMember); err != nil {
|
|
t.Fatalf("AddMember : %v", err)
|
|
}
|
|
}
|
|
|
|
func disablePrincipal(t *testing.T, service *Service, principalID ident.UUID) {
|
|
t.Helper()
|
|
|
|
_, err := service.db.WriteQueries.UpdatePrincipalStatus(context.Background(), database.UpdatePrincipalStatusParams{
|
|
Status: identity.StatusDisabled,
|
|
ID: principalID,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("UpdatePrincipalStatus : %v", err)
|
|
}
|
|
}
|
|
|
|
func mustDocument(t *testing.T, service *Service, ownerID ident.UUID, parentID *ident.UUID, name, visibility string, aclInherit bool) ident.UUID {
|
|
t.Helper()
|
|
|
|
ctx := context.Background()
|
|
|
|
id, err := ident.NewV7()
|
|
if err != nil {
|
|
t.Fatalf("NewV7 : %v", err)
|
|
}
|
|
|
|
if visibility == "" {
|
|
visibility = document.VisibilityPrivate
|
|
}
|
|
|
|
kind := "library"
|
|
path := hex.EncodeToString(id[:]) + "/"
|
|
depth := int64(0)
|
|
|
|
var parentValue any
|
|
var policy any = testPolicyID
|
|
var visibilityValue any = visibility
|
|
|
|
if parentID != nil {
|
|
parent, err := service.db.Queries.GetDocument(ctx, *parentID)
|
|
if err != nil {
|
|
t.Fatalf("GetDocument(parent) : %v", err)
|
|
}
|
|
|
|
kind = "folder"
|
|
path = parent.Path + hex.EncodeToString(id[:]) + "/"
|
|
depth = parent.Depth + 1
|
|
parentValue = *parentID
|
|
policy = nil
|
|
visibilityValue = nil
|
|
}
|
|
|
|
now := time.Now().UnixMilli()
|
|
|
|
_, err = service.db.WriteDB.ExecContext(ctx, `
|
|
INSERT INTO document (
|
|
id, kind, owner_id, created_by, parent_id, name, name_key, path, depth,
|
|
policy_id, visibility, acl_inherit, created_at, updated_at
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
id,
|
|
kind,
|
|
ownerID,
|
|
ownerID,
|
|
parentValue,
|
|
name,
|
|
strings.ToLower(name),
|
|
path,
|
|
depth,
|
|
policy,
|
|
visibilityValue,
|
|
boolToInt(aclInherit),
|
|
now,
|
|
now,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("insertion du document %q : %v", name, err)
|
|
}
|
|
|
|
return id
|
|
}
|