166 lines
4.1 KiB
Go
166 lines
4.1 KiB
Go
package node
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"mediarr/acl"
|
|
"mediarr/document"
|
|
"mediarr/ident"
|
|
"mediarr/identity"
|
|
)
|
|
|
|
func TestNodeDocuments(t *testing.T) {
|
|
t.Chdir(t.TempDir())
|
|
|
|
err, node := NewNode(NewNodeParams{DatabaseName: "documents"})
|
|
if err != nil {
|
|
t.Fatalf("NewNode : %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := node.Close(); err != nil {
|
|
t.Errorf("Close : %v", err)
|
|
}
|
|
})
|
|
|
|
if err := node.Up(); err != nil {
|
|
t.Fatalf("Up : %v", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
owner, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
|
Kind: identity.KindUser,
|
|
Name: "alice",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreatePrincipal : %v", err)
|
|
}
|
|
|
|
policyID := ident.MustParse("00000000-0000-7000-8000-000000000101")
|
|
|
|
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
|
OwnerID: owner.ID,
|
|
CreatedBy: owner.ID,
|
|
Name: "Photos",
|
|
PolicyID: policyID,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreateLibrary : %v", err)
|
|
}
|
|
if library.Kind != document.KindLibrary {
|
|
t.Fatalf("kind = %q, attendu %q", library.Kind, document.KindLibrary)
|
|
}
|
|
|
|
folder, err := node.Documents().CreateFolder(ctx, owner.ID, document.CreateFolderParams{
|
|
OwnerID: owner.ID,
|
|
CreatedBy: owner.ID,
|
|
ParentID: library.ID,
|
|
Name: "Vacances",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreateFolder : %v", err)
|
|
}
|
|
if folder.ParentID == nil || *folder.ParentID != library.ID {
|
|
t.Fatalf("parent = %v, attendu %s", folder.ParentID, library.ID)
|
|
}
|
|
}
|
|
|
|
func TestNodeDocumentsEnforcesACL(t *testing.T) {
|
|
t.Chdir(t.TempDir())
|
|
|
|
err, node := NewNode(NewNodeParams{DatabaseName: "documents_acl"})
|
|
if err != nil {
|
|
t.Fatalf("NewNode : %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := node.Close(); err != nil {
|
|
t.Errorf("Close : %v", err)
|
|
}
|
|
})
|
|
|
|
if err := node.Up(); err != nil {
|
|
t.Fatalf("Up : %v", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
owner, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
|
Kind: identity.KindUser,
|
|
Name: "owner",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreatePrincipal(owner) : %v", err)
|
|
}
|
|
other, err := node.Identity().CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
|
Kind: identity.KindUser,
|
|
Name: "other",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreatePrincipal(other) : %v", err)
|
|
}
|
|
|
|
policyID := ident.MustParse("00000000-0000-7000-8000-000000000101")
|
|
|
|
library, err := node.Documents().CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
|
OwnerID: owner.ID,
|
|
CreatedBy: owner.ID,
|
|
Name: "Photos",
|
|
PolicyID: policyID,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreateLibrary : %v", err)
|
|
}
|
|
|
|
if _, err := node.Documents().Get(ctx, other.ID, library.ID); !errors.Is(err, document.ErrPermissionDenied) {
|
|
t.Fatalf("Get sans ACL : erreur = %v, attendu ErrPermissionDenied", err)
|
|
}
|
|
|
|
if _, err := node.ACL().Grant(ctx, acl.GrantParams{
|
|
DocumentID: library.ID,
|
|
SubjectID: other.ID,
|
|
Permissions: acl.PermissionRead,
|
|
GrantedBy: owner.ID,
|
|
}); err != nil {
|
|
t.Fatalf("Grant(Read) : %v", err)
|
|
}
|
|
|
|
if _, err := node.Documents().Get(ctx, other.ID, library.ID); err != nil {
|
|
t.Fatalf("Get avec Read : %v", err)
|
|
}
|
|
|
|
libraries, err := node.Documents().ListLibraries(ctx, other.ID)
|
|
if err != nil {
|
|
t.Fatalf("ListLibraries : %v", err)
|
|
}
|
|
if len(libraries) != 1 || libraries[0].ID != library.ID {
|
|
t.Fatalf("bibliothèques = %+v", libraries)
|
|
}
|
|
|
|
if _, err := node.Documents().CreateFolder(ctx, other.ID, document.CreateFolderParams{
|
|
OwnerID: other.ID,
|
|
CreatedBy: other.ID,
|
|
ParentID: library.ID,
|
|
Name: "Vacances",
|
|
}); !errors.Is(err, document.ErrPermissionDenied) {
|
|
t.Fatalf("CreateFolder sans Create : erreur = %v, attendu ErrPermissionDenied", err)
|
|
}
|
|
|
|
if _, err := node.ACL().Grant(ctx, acl.GrantParams{
|
|
DocumentID: library.ID,
|
|
SubjectID: other.ID,
|
|
Permissions: acl.PermissionCreate,
|
|
GrantedBy: owner.ID,
|
|
}); err != nil {
|
|
t.Fatalf("Grant(Create) : %v", err)
|
|
}
|
|
|
|
if _, err := node.Documents().CreateFolder(ctx, other.ID, document.CreateFolderParams{
|
|
OwnerID: other.ID,
|
|
CreatedBy: other.ID,
|
|
ParentID: library.ID,
|
|
Name: "Vacances",
|
|
}); err != nil {
|
|
t.Fatalf("CreateFolder avec Create : %v", err)
|
|
}
|
|
}
|