131 lines
2.8 KiB
Go
131 lines
2.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.eeqj.de/sneak/webhooker/internal/config"
|
|
"git.eeqj.de/sneak/webhooker/internal/database"
|
|
"git.eeqj.de/sneak/webhooker/internal/globals"
|
|
"git.eeqj.de/sneak/webhooker/internal/healthcheck"
|
|
"git.eeqj.de/sneak/webhooker/internal/logger"
|
|
"git.eeqj.de/sneak/webhooker/internal/session"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/fx"
|
|
"go.uber.org/fx/fxtest"
|
|
)
|
|
|
|
func TestHandleIndex(t *testing.T) {
|
|
var h *Handlers
|
|
|
|
app := fxtest.New(
|
|
t,
|
|
fx.Provide(
|
|
globals.New,
|
|
logger.New,
|
|
func() *config.Config {
|
|
return &config.Config{
|
|
// This is a base64 encoded 32-byte key: "test-session-key-32-bytes-long!!"
|
|
SessionKey: "dGVzdC1zZXNzaW9uLWtleS0zMi1ieXRlcy1sb25nISE=",
|
|
}
|
|
},
|
|
func() *database.Database {
|
|
// Mock database with a mock DB method
|
|
db := &database.Database{}
|
|
return db
|
|
},
|
|
healthcheck.New,
|
|
session.New,
|
|
New,
|
|
),
|
|
fx.Populate(&h),
|
|
)
|
|
app.RequireStart()
|
|
defer app.RequireStop()
|
|
|
|
// Since we can't test actual template rendering without templates,
|
|
// let's test that the handler is created and doesn't panic
|
|
handler := h.HandleIndex()
|
|
assert.NotNil(t, handler)
|
|
}
|
|
|
|
func TestRenderTemplate(t *testing.T) {
|
|
var h *Handlers
|
|
|
|
app := fxtest.New(
|
|
t,
|
|
fx.Provide(
|
|
globals.New,
|
|
logger.New,
|
|
func() *config.Config {
|
|
return &config.Config{
|
|
// This is a base64 encoded 32-byte key: "test-session-key-32-bytes-long!!"
|
|
SessionKey: "dGVzdC1zZXNzaW9uLWtleS0zMi1ieXRlcy1sb25nISE=",
|
|
}
|
|
},
|
|
func() *database.Database {
|
|
// Mock database
|
|
return &database.Database{}
|
|
},
|
|
healthcheck.New,
|
|
session.New,
|
|
New,
|
|
),
|
|
fx.Populate(&h),
|
|
)
|
|
app.RequireStart()
|
|
defer app.RequireStop()
|
|
|
|
t.Run("handles missing templates gracefully", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
data := map[string]interface{}{
|
|
"Version": "1.0.0",
|
|
}
|
|
|
|
// When templates don't exist, renderTemplate should return an error
|
|
h.renderTemplate(w, req, []string{"nonexistent.html"}, data)
|
|
|
|
// Should return internal server error when template parsing fails
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
})
|
|
}
|
|
|
|
func TestFormatUptime(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
duration string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "minutes only",
|
|
duration: "45m",
|
|
expected: "45m",
|
|
},
|
|
{
|
|
name: "hours and minutes",
|
|
duration: "2h30m",
|
|
expected: "2h 30m",
|
|
},
|
|
{
|
|
name: "days, hours and minutes",
|
|
duration: "25h45m",
|
|
expected: "1d 1h 45m",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
d, err := time.ParseDuration(tt.duration)
|
|
require.NoError(t, err)
|
|
|
|
result := formatUptime(d)
|
|
assert.Equal(t, tt.expected, result)
|
|
})
|
|
}
|
|
}
|