213 lines
5.0 KiB
Go
213 lines
5.0 KiB
Go
|
|
package handlers
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/juanatsap/cv-site/internal/config"
|
||
|
|
"github.com/juanatsap/cv-site/internal/templates"
|
||
|
|
)
|
||
|
|
|
||
|
|
// TestHome tests the Home handler
|
||
|
|
func TestHome(t *testing.T) {
|
||
|
|
// Create template manager with config
|
||
|
|
cfg := &config.TemplateConfig{
|
||
|
|
Dir: "../../templates",
|
||
|
|
PartialsDir: "../../templates/partials",
|
||
|
|
HotReload: true,
|
||
|
|
}
|
||
|
|
tmplManager, err := templates.NewManager(cfg)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("Failed to create template manager: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create handler
|
||
|
|
handler := NewCVHandler(tmplManager, "localhost:8080")
|
||
|
|
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
lang string
|
||
|
|
expectStatus int
|
||
|
|
expectContains string
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "Default language (English)",
|
||
|
|
lang: "",
|
||
|
|
expectStatus: http.StatusOK,
|
||
|
|
expectContains: "Juan Andrés Moreno Rubio",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "English language",
|
||
|
|
lang: "en",
|
||
|
|
expectStatus: http.StatusOK,
|
||
|
|
expectContains: "Juan Andrés Moreno Rubio",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Spanish language",
|
||
|
|
lang: "es",
|
||
|
|
expectStatus: http.StatusOK,
|
||
|
|
expectContains: "Juan Andrés Moreno Rubio",
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Invalid language",
|
||
|
|
lang: "fr",
|
||
|
|
expectStatus: http.StatusBadRequest,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
// Create request
|
||
|
|
req := httptest.NewRequest(http.MethodGet, "/?lang="+tt.lang, nil)
|
||
|
|
w := httptest.NewRecorder()
|
||
|
|
|
||
|
|
// Call handler
|
||
|
|
handler.Home(w, req)
|
||
|
|
|
||
|
|
// Check status code
|
||
|
|
if w.Code != tt.expectStatus {
|
||
|
|
t.Errorf("Expected status %d, got %d", tt.expectStatus, w.Code)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Check response body contains expected content (if success)
|
||
|
|
if tt.expectStatus == http.StatusOK && tt.expectContains != "" {
|
||
|
|
body := w.Body.String()
|
||
|
|
if len(body) == 0 {
|
||
|
|
t.Error("Expected non-empty response body")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestCVContent tests the CVContent handler
|
||
|
|
func TestCVContent(t *testing.T) {
|
||
|
|
// Create template manager with config
|
||
|
|
cfg := &config.TemplateConfig{
|
||
|
|
Dir: "../../templates",
|
||
|
|
PartialsDir: "../../templates/partials",
|
||
|
|
HotReload: true,
|
||
|
|
}
|
||
|
|
tmplManager, err := templates.NewManager(cfg)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("Failed to create template manager: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create handler
|
||
|
|
handler := NewCVHandler(tmplManager, "localhost:8080")
|
||
|
|
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
lang string
|
||
|
|
expectStatus int
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "Default language",
|
||
|
|
lang: "",
|
||
|
|
expectStatus: http.StatusOK,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "English language",
|
||
|
|
lang: "en",
|
||
|
|
expectStatus: http.StatusOK,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Spanish language",
|
||
|
|
lang: "es",
|
||
|
|
expectStatus: http.StatusOK,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Invalid language",
|
||
|
|
lang: "de",
|
||
|
|
expectStatus: http.StatusBadRequest,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
// Create request
|
||
|
|
req := httptest.NewRequest(http.MethodGet, "/cv-content?lang="+tt.lang, nil)
|
||
|
|
w := httptest.NewRecorder()
|
||
|
|
|
||
|
|
// Call handler
|
||
|
|
handler.CVContent(w, req)
|
||
|
|
|
||
|
|
// Check status code
|
||
|
|
if w.Code != tt.expectStatus {
|
||
|
|
t.Errorf("Expected status %d, got %d", tt.expectStatus, w.Code)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestDefaultCVShortcut tests the DefaultCVShortcut handler
|
||
|
|
func TestDefaultCVShortcut(t *testing.T) {
|
||
|
|
// Create template manager with config
|
||
|
|
cfg := &config.TemplateConfig{
|
||
|
|
Dir: "../../templates",
|
||
|
|
PartialsDir: "../../templates/partials",
|
||
|
|
HotReload: true,
|
||
|
|
}
|
||
|
|
tmplManager, err := templates.NewManager(cfg)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("Failed to create template manager: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create handler
|
||
|
|
handler := NewCVHandler(tmplManager, "localhost:8080")
|
||
|
|
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
path string
|
||
|
|
expectStatus int
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "Valid shortcut URL (current year EN)",
|
||
|
|
path: "/cv-jamr-2025-en.pdf",
|
||
|
|
expectStatus: http.StatusOK,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Valid shortcut URL (current year ES)",
|
||
|
|
path: "/cv-jamr-2025-es.pdf",
|
||
|
|
expectStatus: http.StatusOK,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Invalid year",
|
||
|
|
path: "/cv-jamr-2020-en.pdf",
|
||
|
|
expectStatus: http.StatusNotFound,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Invalid language",
|
||
|
|
path: "/cv-jamr-2025-fr.pdf",
|
||
|
|
expectStatus: http.StatusNotFound,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "Invalid format",
|
||
|
|
path: "/cv-wrong-format.pdf",
|
||
|
|
expectStatus: http.StatusNotFound,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
// Skip PDF generation tests in short mode (they require Chrome)
|
||
|
|
if testing.Short() && tt.expectStatus == http.StatusOK {
|
||
|
|
t.Skip("Skipping PDF generation test in short mode")
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create request
|
||
|
|
req := httptest.NewRequest(http.MethodGet, tt.path, nil)
|
||
|
|
w := httptest.NewRecorder()
|
||
|
|
|
||
|
|
// Call handler
|
||
|
|
handler.DefaultCVShortcut(w, req)
|
||
|
|
|
||
|
|
// Check status code
|
||
|
|
if w.Code != tt.expectStatus {
|
||
|
|
t.Errorf("Expected status %d, got %d", tt.expectStatus, w.Code)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|