124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package storage
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.nakama.town/fmartingr/hako/internal/model"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestLocalStorageSaveAndGet(t *testing.T) {
|
|
// Create a temporary directory for testing
|
|
tmpDir, err := os.MkdirTemp("", "storage-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp dir: %v", err)
|
|
}
|
|
defer func() { _ = os.RemoveAll(tmpDir) }()
|
|
|
|
storage := NewLocalStorage(tmpDir)
|
|
ctx := context.Background()
|
|
|
|
// Test data
|
|
linkID := uuid.New().String()
|
|
filename := "test.txt"
|
|
content := []byte("Hello, World!")
|
|
|
|
// Save file
|
|
path, err := storage.Save(ctx, linkID, filename, bytes.NewReader(content))
|
|
if err != nil {
|
|
t.Fatalf("Failed to save file: %v", err)
|
|
}
|
|
|
|
if path == "" {
|
|
t.Fatal("Expected non-empty path")
|
|
}
|
|
|
|
// Check if file exists
|
|
exists, err := storage.Exists(ctx, path)
|
|
if err != nil {
|
|
t.Fatalf("Failed to check existence: %v", err)
|
|
}
|
|
|
|
if !exists {
|
|
t.Fatal("File should exist")
|
|
}
|
|
|
|
// Get file
|
|
reader, err := storage.Get(ctx, path)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get file: %v", err)
|
|
}
|
|
defer func() { _ = reader.Close() }()
|
|
|
|
// Read content
|
|
buf := make([]byte, len(content))
|
|
n, err := reader.Read(buf)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read file: %v", err)
|
|
}
|
|
|
|
if n != len(content) {
|
|
t.Fatalf("Expected %d bytes, got %d", len(content), n)
|
|
}
|
|
|
|
if string(buf) != string(content) {
|
|
t.Fatalf("Expected %q, got %q", content, string(buf))
|
|
}
|
|
|
|
// Delete file
|
|
if err := storage.Delete(ctx, path); err != nil {
|
|
t.Fatalf("Failed to delete file: %v", err)
|
|
}
|
|
|
|
// Check if file was deleted
|
|
exists, err = storage.Exists(ctx, path)
|
|
if err != nil {
|
|
t.Fatalf("Failed to check existence after delete: %v", err)
|
|
}
|
|
|
|
if exists {
|
|
t.Fatal("File should not exist after delete")
|
|
}
|
|
}
|
|
|
|
func TestLocalStorageDirectoryCreation(t *testing.T) {
|
|
// Create a temporary directory for testing
|
|
tmpDir, err := os.MkdirTemp("", "storage-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp dir: %v", err)
|
|
}
|
|
defer func() { _ = os.RemoveAll(tmpDir) }()
|
|
|
|
storage := NewLocalStorage(tmpDir)
|
|
ctx := context.Background()
|
|
|
|
// Test data
|
|
linkID := uuid.New().String()
|
|
filename := "test.txt"
|
|
content := []byte("Hello, World!")
|
|
|
|
// Save file
|
|
path, err := storage.Save(ctx, linkID, filename, bytes.NewReader(content))
|
|
if err != nil {
|
|
t.Fatalf("Failed to save file: %v", err)
|
|
}
|
|
|
|
// Check if directory was created
|
|
linkDirRelPath := model.GetPathForLink(linkID)
|
|
linkDir := filepath.Join(tmpDir, linkDirRelPath)
|
|
if _, err := os.Stat(linkDir); os.IsNotExist(err) {
|
|
t.Fatal("Link directory should exist")
|
|
}
|
|
|
|
// Check if file is in the correct directory
|
|
fullFilePath := filepath.Join(tmpDir, path)
|
|
relPath, err := filepath.Rel(linkDir, fullFilePath)
|
|
if err != nil || filepath.IsAbs(relPath) || relPath == ".." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) {
|
|
t.Fatal("File should be in link directory")
|
|
}
|
|
}
|