Files
2026-11-16 11:03:48 +01:00

391 lines
11 KiB
Go

package document
import (
"context"
"encoding/hex"
"errors"
"strings"
"testing"
"mediarr/ident"
)
func TestCreateLibrary(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
library, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
Name: "Photos",
PolicyID: testPolicyID,
})
if err != nil {
t.Fatalf("CreateLibrary : %v", err)
}
if library.Kind != KindLibrary {
t.Fatalf("kind = %q, attendu %q", library.Kind, KindLibrary)
}
if library.Name != "Photos" || library.NameKey != "photos" {
t.Fatalf("nom = %q/%q, attendu Photos/photos", library.Name, library.NameKey)
}
if library.ParentID != nil {
t.Fatalf("parent = %v, attendu nil", library.ParentID)
}
if library.Depth != 0 {
t.Fatalf("depth = %d, attendu 0", library.Depth)
}
if library.Path != hex.EncodeToString(library.ID[:])+"/" {
t.Fatalf("path = %q", library.Path)
}
if library.PolicyID == nil || *library.PolicyID != testPolicyID {
t.Fatalf("policy = %v, attendu %s", library.PolicyID, testPolicyID)
}
if library.Visibility != VisibilityPrivate {
t.Fatalf("visibility = %q, attendu %q", library.Visibility, VisibilityPrivate)
}
if !library.ACLInherit {
t.Fatal("acl_inherit = false, attendu true")
}
if library.Rev != 1 {
t.Fatalf("rev = %d, attendu 1", library.Rev)
}
if library.Metadata != "{}" {
t.Fatalf("metadata = %q, attendu {}", library.Metadata)
}
if library.CreatedAt == 0 || library.UpdatedAt == 0 {
t.Fatal("timestamps non renseignés")
}
}
func TestCreateLibraryExplicitOptions(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
aclInherit := false
library, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
Name: "Films",
PolicyID: testPolicyID,
Visibility: VisibilityCluster,
ACLInherit: &aclInherit,
Metadata: `{"genre":"test"}`,
})
if err != nil {
t.Fatalf("CreateLibrary : %v", err)
}
if library.Visibility != VisibilityCluster {
t.Fatalf("visibility = %q, attendu %q", library.Visibility, VisibilityCluster)
}
if library.ACLInherit {
t.Fatal("acl_inherit = true, attendu false")
}
if library.Metadata != `{"genre":"test"}` {
t.Fatalf("metadata = %q", library.Metadata)
}
}
func TestCreateLibraryRejectsInvalidName(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
cases := []string{"", " ", "a/b", strings.Repeat("a", 256)}
for _, name := range cases {
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
Name: name,
PolicyID: testPolicyID,
})
if !errors.Is(err, ErrInvalidName) {
t.Fatalf("nom %q : erreur = %v, attendu ErrInvalidName", name, err)
}
}
}
func TestCreateLibraryRejectsInvalidVisibility(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
Name: "Photos",
PolicyID: testPolicyID,
Visibility: "SECRET",
})
if !errors.Is(err, ErrInvalidVisibility) {
t.Fatalf("erreur = %v, attendu ErrInvalidVisibility", err)
}
}
func TestCreateLibraryRejectsMissingPolicy(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
missing, err := ident.NewV7()
if err != nil {
t.Fatalf("NewV7 : %v", err)
}
_, err = service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
Name: "Photos",
PolicyID: missing,
})
if !errors.Is(err, ErrPolicyNotFound) {
t.Fatalf("erreur = %v, attendu ErrPolicyNotFound", err)
}
}
func TestCreateLibraryRejectsDuplicateName(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
mustLibrary(t, service, owner, "Photos")
_, err := service.CreateLibrary(ctx, owner.ID, CreateLibraryParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
Name: "photos",
PolicyID: testPolicyID,
})
if !errors.Is(err, ErrNameTaken) {
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
}
}
func TestCreateFolder(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
library := mustLibrary(t, service, owner, "Photos")
folder, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: library.ID,
Name: "Vacances",
})
if err != nil {
t.Fatalf("CreateFolder : %v", err)
}
if folder.Kind != KindFolder {
t.Fatalf("kind = %q, attendu %q", folder.Kind, KindFolder)
}
if folder.ParentID == nil || *folder.ParentID != library.ID {
t.Fatalf("parent = %v, attendu %s", folder.ParentID, library.ID)
}
if folder.Depth != 1 {
t.Fatalf("depth = %d, attendu 1", folder.Depth)
}
if folder.Path != library.Path+hex.EncodeToString(folder.ID[:])+"/" {
t.Fatalf("path = %q", folder.Path)
}
if folder.Visibility != "" {
t.Fatalf("visibility = %q, attendu vide", folder.Visibility)
}
if folder.PolicyID != nil {
t.Fatalf("policy = %v, attendu nil", folder.PolicyID)
}
if !folder.ACLInherit {
t.Fatal("acl_inherit = false, attendu true")
}
}
func TestCreateFolderRejectsMissingParent(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
missing, err := ident.NewV7()
if err != nil {
t.Fatalf("NewV7 : %v", err)
}
_, err = service.CreateFolder(ctx, owner.ID, CreateFolderParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: missing,
Name: "Vacances",
})
if !errors.Is(err, ErrParentNotFound) {
t.Fatalf("erreur = %v, attendu ErrParentNotFound", err)
}
}
func TestCreateFolderRejectsDeletedParent(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
library := mustLibrary(t, service, owner, "Photos")
if _, err := service.Trash(ctx, owner.ID, library.ID); err != nil {
t.Fatalf("Trash : %v", err)
}
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: library.ID,
Name: "Vacances",
})
if !errors.Is(err, ErrParentDeleted) {
t.Fatalf("erreur = %v, attendu ErrParentDeleted", err)
}
}
func TestCreateFolderRejectsInvalidParent(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
library := mustLibrary(t, service, owner, "Photos")
movie := mustKindDocument(t, service, owner, library, "movie", "Film")
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: movie.ID,
Name: "Extras",
})
if !errors.Is(err, ErrInvalidParent) {
t.Fatalf("erreur = %v, attendu ErrInvalidParent", err)
}
}
func TestCreateFolderRejectsDuplicateSibling(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
library := mustLibrary(t, service, owner, "Photos")
mustFolder(t, service, owner, library.ID, "Vacances")
_, err := service.CreateFolder(ctx, owner.ID, CreateFolderParams{
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: library.ID,
Name: "vacances",
})
if !errors.Is(err, ErrNameTaken) {
t.Fatalf("erreur = %v, attendu ErrNameTaken", err)
}
}
func TestCreateChild(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
library := mustLibrary(t, service, owner, "Photos")
video, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
Kind: KindVideo,
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: library.ID,
Name: "film.mkv",
Metadata: `{"source_path":"film.mkv"}`,
})
if err != nil {
t.Fatalf("CreateChild(video) : %v", err)
}
if video.Kind != KindVideo {
t.Fatalf("kind = %q, attendu %q", video.Kind, KindVideo)
}
if video.ParentID == nil || *video.ParentID != library.ID {
t.Fatalf("parent = %v, attendu %s", video.ParentID, library.ID)
}
if video.Depth != library.Depth+1 {
t.Fatalf("depth = %d, attendu %d", video.Depth, library.Depth+1)
}
if video.Metadata != `{"source_path":"film.mkv"}` {
t.Fatalf("metadata = %q", video.Metadata)
}
folder, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
Kind: KindFolder,
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: library.ID,
Name: "Extras",
})
if err != nil {
t.Fatalf("CreateChild(folder) : %v", err)
}
if folder.Kind != KindFolder {
t.Fatalf("kind = %q, attendu %q", folder.Kind, KindFolder)
}
if folder.Metadata != "{}" {
t.Fatalf("metadata = %q, attendu {}", folder.Metadata)
}
}
func TestCreateChildRejectsInvalidInputs(t *testing.T) {
service := newTestService(t)
ctx := context.Background()
owner := mustUser(t, service, "owner")
other := mustUser(t, service, "other")
library := mustLibrary(t, service, owner, "Photos")
if _, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
Kind: KindVideo,
OwnerID: other.ID,
CreatedBy: owner.ID,
ParentID: library.ID,
Name: "film.mkv",
}); !errors.Is(err, ErrPermissionDenied) {
t.Fatalf("owner usurpé : erreur = %v, attendu ErrPermissionDenied", err)
}
if _, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
Kind: "inconnu",
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: library.ID,
Name: "film.mkv",
}); !errors.Is(err, ErrInvalidKind) {
t.Fatalf("kind invalide : erreur = %v, attendu ErrInvalidKind", err)
}
if _, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
Kind: KindVideo,
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: library.ID,
Name: "film.mkv",
Metadata: "{",
}); !errors.Is(err, ErrInvalidMetadata) {
t.Fatalf("metadata invalide : erreur = %v, attendu ErrInvalidMetadata", err)
}
video, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
Kind: KindVideo,
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: library.ID,
Name: "film.mkv",
})
if err != nil {
t.Fatalf("CreateChild(video) : %v", err)
}
if _, err := service.CreateChild(ctx, owner.ID, CreateChildParams{
Kind: KindVideo,
OwnerID: owner.ID,
CreatedBy: owner.ID,
ParentID: video.ID,
Name: "enfant.mkv",
}); !errors.Is(err, ErrInvalidParent) {
t.Fatalf("parent non conteneur : erreur = %v, attendu ErrInvalidParent", err)
}
}