46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package infra
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFileDelete(t *testing.T) {
|
|
t.Run("elimina archivo existente", func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "x.txt")
|
|
if err := os.WriteFile(path, []byte("hi"), 0o644); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
if err := FileDelete(path); err != nil {
|
|
t.Fatalf("FileDelete err: %v", err)
|
|
}
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Errorf("archivo aun existe: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("rechaza path con ..", func(t *testing.T) {
|
|
err := FileDelete("./uploads/../etc/passwd")
|
|
if err == nil || !strings.Contains(err.Error(), "path traversal") {
|
|
t.Errorf("got err %v, want path traversal", err)
|
|
}
|
|
})
|
|
|
|
t.Run("rechaza path vacio", func(t *testing.T) {
|
|
err := FileDelete("")
|
|
if err == nil {
|
|
t.Error("got nil, want error")
|
|
}
|
|
})
|
|
|
|
t.Run("retorna error si no existe", func(t *testing.T) {
|
|
err := FileDelete(filepath.Join(t.TempDir(), "nope.txt"))
|
|
if err == nil {
|
|
t.Error("got nil, want error not found")
|
|
}
|
|
})
|
|
}
|