create content version
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
package content
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/document"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
"mediarr/permission"
|
||||
"mediarr/storage"
|
||||
)
|
||||
|
||||
var (
|
||||
testBackendID = ident.MustParse("00000000-0000-7000-8000-000000000201")
|
||||
testPolicyID = ident.MustParse("00000000-0000-7000-8000-000000000101")
|
||||
)
|
||||
|
||||
type testAuthorizer struct {
|
||||
allowed permission.Permission
|
||||
}
|
||||
|
||||
func allowAll() *testAuthorizer {
|
||||
return &testAuthorizer{allowed: permission.All}
|
||||
}
|
||||
|
||||
func (a *testAuthorizer) IsActive(context.Context, ident.UUID) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (a *testAuthorizer) Can(_ context.Context, _ ident.UUID, _ ident.UUID, required permission.Permission) (bool, error) {
|
||||
return a.allowed.Has(required), nil
|
||||
}
|
||||
|
||||
type testEnv struct {
|
||||
service *Service
|
||||
db *database.DatabaseClient
|
||||
store *storage.Local
|
||||
owner *identity.Principal
|
||||
libraryID ident.UUID
|
||||
videoID ident.UUID
|
||||
}
|
||||
|
||||
func newTestEnv(t *testing.T, authorizer document.Authorizer) *testEnv {
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
store, err := storage.NewLocal(filepath.Join("store"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewLocal : %v", err)
|
||||
}
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
if err := db.WriteQueries.CreateStorageBackend(context.Background(), database.CreateStorageBackendParams{
|
||||
ID: testBackendID,
|
||||
Kind: "hub_fs",
|
||||
Name: "local",
|
||||
Config: `{"root":"store"}`,
|
||||
Enabled: 1,
|
||||
State: "ok",
|
||||
CreatedAt: now,
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateStorageBackend : %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
owner, err := identity.NewService(*db).CreatePrincipal(ctx, identity.CreatePrincipalParams{
|
||||
Kind: identity.KindUser,
|
||||
Name: "owner",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePrincipal : %v", err)
|
||||
}
|
||||
|
||||
documents := document.NewService(*db, allowAll())
|
||||
library, err := documents.CreateLibrary(ctx, owner.ID, document.CreateLibraryParams{
|
||||
OwnerID: owner.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Name: "Bibliothèque",
|
||||
PolicyID: testPolicyID,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateLibrary : %v", err)
|
||||
}
|
||||
|
||||
videoID := createVideo(t, db, owner, library, "film.mkv")
|
||||
|
||||
return &testEnv{
|
||||
service: NewService(*db, store, testBackendID, authorizer),
|
||||
db: db,
|
||||
store: store,
|
||||
owner: owner,
|
||||
libraryID: library.ID,
|
||||
videoID: videoID,
|
||||
}
|
||||
}
|
||||
|
||||
func createVideo(t *testing.T, db *database.DatabaseClient, owner *identity.Principal, parent *document.Document, name string) ident.UUID {
|
||||
t.Helper()
|
||||
|
||||
id, err := ident.NewV7()
|
||||
if err != nil {
|
||||
t.Fatalf("NewV7 : %v", err)
|
||||
}
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
if _, err := db.WriteQueries.CreateDocument(context.Background(), database.CreateDocumentParams{
|
||||
ID: id,
|
||||
Kind: "video",
|
||||
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,
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateDocument : %v", err)
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
Reference in New Issue
Block a user