64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package infra
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFileUniqueName(t *testing.T) {
|
|
t.Run("preserva extension comun como png", func(t *testing.T) {
|
|
got := FileUniqueName("foto.png")
|
|
if !strings.HasSuffix(got, ".png") {
|
|
t.Fatalf("got %q, want suffix .png", got)
|
|
}
|
|
if len(got) < 36+4 { // uuid + ".png"
|
|
t.Fatalf("got %q, want UUID + .png", got)
|
|
}
|
|
})
|
|
|
|
t.Run("convierte extension a minusculas", func(t *testing.T) {
|
|
got := FileUniqueName("VACACIONES.JPEG")
|
|
if !strings.HasSuffix(got, ".jpeg") {
|
|
t.Fatalf("got %q, want suffix .jpeg", got)
|
|
}
|
|
})
|
|
|
|
t.Run("remueve caracteres especiales en extension", func(t *testing.T) {
|
|
got := FileUniqueName("malicious.t!x@t#")
|
|
if !strings.HasSuffix(got, ".txt") {
|
|
t.Fatalf("got %q, want suffix .txt", got)
|
|
}
|
|
})
|
|
|
|
t.Run("genera UUID sin extension si el archivo no tiene", func(t *testing.T) {
|
|
got := FileUniqueName("contrato_sin_extension")
|
|
if strings.Contains(got, ".") {
|
|
t.Fatalf("got %q, want sin punto", got)
|
|
}
|
|
if len(got) != 36 {
|
|
t.Fatalf("got %q (len %d), want UUID len 36", got, len(got))
|
|
}
|
|
})
|
|
|
|
t.Run("trunca extensiones extremadamente largas", func(t *testing.T) {
|
|
got := FileUniqueName("file." + strings.Repeat("a", 100))
|
|
// Buscar la ultima parte despues del punto
|
|
idx := strings.LastIndex(got, ".")
|
|
if idx < 0 {
|
|
t.Fatalf("got %q, want al menos un punto", got)
|
|
}
|
|
ext := got[idx+1:]
|
|
if len(ext) > 16 {
|
|
t.Fatalf("got ext len %d, want <= 16", len(ext))
|
|
}
|
|
})
|
|
|
|
t.Run("dos llamadas generan IDs distintos", func(t *testing.T) {
|
|
a := FileUniqueName("x.png")
|
|
b := FileUniqueName("x.png")
|
|
if a == b {
|
|
t.Fatalf("got %q == %q, want distintos", a, b)
|
|
}
|
|
})
|
|
}
|