All checks were successful
check / check (push) Successful in 2m3s
Replace .golangci.yml with the canonical v2-schema config (default: all minus six disabled linters, lll 88, tests included) and bump every golangci-lint pin to v2.12.2: - Dockerfile: golangci/golangci-lint:v2.12.2-alpine (hash-pinned) - script/bootstrap: GOLANGCI_LINT_VERSION 2.12.2 with new linux-amd64/arm64 release-archive sha256 pins Fix all 747 findings the stricter config surfaces, with no behavior changes: t.Parallel() throughout the test suite, static sentinel errors and errors.Is comparisons, checked error returns, context propagation (contextcheck/noctx), 88-column wrapping, extracted constants and helpers for goconst/dupl/funlen/cyclop, exhaustive switch cases replicating existing defaults, and white-box test files renamed to *_internal_test.go for testpackage. Three nolint:tagliatelle directives preserve the existing snake_case JSON wire and on-disk metadata formats.
253 lines
6.2 KiB
Go
253 lines
6.2 KiB
Go
package imgcache
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"database/sql"
|
|
"image"
|
|
"image/color"
|
|
"image/gif"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"io/fs"
|
|
"testing"
|
|
"testing/fstest"
|
|
"time"
|
|
|
|
"sneak.berlin/go/pixa/internal/database"
|
|
"sneak.berlin/go/pixa/internal/httpfetcher"
|
|
)
|
|
|
|
// Shared test data literals, extracted as constants for goconst.
|
|
const (
|
|
testHostCDN = "cdn.example.com"
|
|
testHostExample = "example.com"
|
|
testPathCat = "/photos/cat.jpg"
|
|
testContentTypeJPEG = "image/jpeg"
|
|
)
|
|
|
|
// TestFixtures contains paths to test files in the mock filesystem.
|
|
type TestFixtures struct {
|
|
// Valid image files
|
|
GoodHostJPEG string // allowlisted host, valid JPEG
|
|
GoodHostPNG string // allowlisted host, valid PNG
|
|
GoodHostGIF string // allowlisted host, valid GIF
|
|
OtherHostJPEG string // non-allowlisted host, valid JPEG
|
|
OtherHostPNG string // non-allowlisted host, valid PNG
|
|
|
|
// Invalid/edge case files
|
|
InvalidFile string // file with wrong magic bytes
|
|
EmptyFile string // zero-byte file
|
|
TextFile string // text file masquerading as image
|
|
|
|
// Hostnames
|
|
GoodHost string // allowlisted hostname
|
|
OtherHost string // non-allowlisted hostname
|
|
}
|
|
|
|
// DefaultFixtures returns the standard test fixture paths.
|
|
func DefaultFixtures() *TestFixtures {
|
|
return &TestFixtures{
|
|
GoodHostJPEG: "goodhost.example.com/images/photo.jpg",
|
|
GoodHostPNG: "goodhost.example.com/images/logo.png",
|
|
GoodHostGIF: "goodhost.example.com/images/animation.gif",
|
|
OtherHostJPEG: "otherhost.example.com/uploads/image.jpg",
|
|
OtherHostPNG: "otherhost.example.com/uploads/icon.png",
|
|
InvalidFile: "goodhost.example.com/images/fake.jpg",
|
|
EmptyFile: "goodhost.example.com/images/empty.jpg",
|
|
TextFile: "goodhost.example.com/images/text.png",
|
|
GoodHost: "goodhost.example.com",
|
|
OtherHost: "otherhost.example.com",
|
|
}
|
|
}
|
|
|
|
// NewTestFS creates a mock filesystem with test images.
|
|
func NewTestFS(t *testing.T) (fs.FS, *TestFixtures) {
|
|
t.Helper()
|
|
|
|
fixtures := DefaultFixtures()
|
|
|
|
// Generate test images
|
|
jpegData := generateTestJPEG(t, 100, 100, color.RGBA{255, 0, 0, 255})
|
|
pngData := generateTestPNG(t, 100, 100, color.RGBA{0, 255, 0, 255})
|
|
gifData := generateTestGIF(t, 100, 100, color.RGBA{0, 0, 255, 255})
|
|
|
|
// Create the mock filesystem
|
|
mockFS := fstest.MapFS{
|
|
// Good host files
|
|
fixtures.GoodHostJPEG: &fstest.MapFile{Data: jpegData},
|
|
fixtures.GoodHostPNG: &fstest.MapFile{Data: pngData},
|
|
fixtures.GoodHostGIF: &fstest.MapFile{Data: gifData},
|
|
|
|
// Other host files
|
|
fixtures.OtherHostJPEG: &fstest.MapFile{Data: jpegData},
|
|
fixtures.OtherHostPNG: &fstest.MapFile{Data: pngData},
|
|
|
|
// Invalid files
|
|
fixtures.InvalidFile: &fstest.MapFile{Data: []byte("not a real image file content")},
|
|
fixtures.EmptyFile: &fstest.MapFile{Data: []byte{}},
|
|
fixtures.TextFile: &fstest.MapFile{Data: []byte("This is just text, not a PNG")},
|
|
}
|
|
|
|
return mockFS, fixtures
|
|
}
|
|
|
|
// generateTestJPEG creates a minimal valid JPEG image.
|
|
func generateTestJPEG(t *testing.T, width, height int, c color.Color) []byte {
|
|
t.Helper()
|
|
|
|
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
for y := range height {
|
|
for x := range width {
|
|
img.Set(x, y, c)
|
|
}
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 85})
|
|
if err != nil {
|
|
t.Fatalf("failed to encode test JPEG: %v", err)
|
|
}
|
|
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// generateTestPNG creates a minimal valid PNG image.
|
|
func generateTestPNG(t *testing.T, width, height int, c color.Color) []byte {
|
|
t.Helper()
|
|
|
|
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
for y := range height {
|
|
for x := range width {
|
|
img.Set(x, y, c)
|
|
}
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := png.Encode(&buf, img)
|
|
if err != nil {
|
|
t.Fatalf("failed to encode test PNG: %v", err)
|
|
}
|
|
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// generateTestGIF creates a minimal valid GIF image.
|
|
func generateTestGIF(t *testing.T, width, height int, c color.Color) []byte {
|
|
t.Helper()
|
|
|
|
img := image.NewPaletted(
|
|
image.Rect(0, 0, width, height),
|
|
[]color.Color{c, color.White},
|
|
)
|
|
for y := range height {
|
|
for x := range width {
|
|
img.SetColorIndex(x, y, 0)
|
|
}
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := gif.Encode(&buf, img, nil)
|
|
if err != nil {
|
|
t.Fatalf("failed to encode test GIF: %v", err)
|
|
}
|
|
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// SetupTestService creates a Service with mock fetcher for testing.
|
|
func SetupTestService(
|
|
t *testing.T, opts ...TestServiceOption,
|
|
) (*Service, *TestFixtures) {
|
|
t.Helper()
|
|
|
|
mockFS, fixtures := NewTestFS(t)
|
|
|
|
cfg := &testServiceConfig{
|
|
allowlist: []string{fixtures.GoodHost},
|
|
signingKey: "test-signing-key-must-be-32-chars",
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(cfg)
|
|
}
|
|
|
|
// Create temp directory for cache
|
|
tmpDir := t.TempDir()
|
|
|
|
// Create in-memory database
|
|
db := setupServiceTestDB(t)
|
|
|
|
cache, err := NewCache(db, CacheConfig{
|
|
StateDir: tmpDir,
|
|
CacheTTL: time.Hour,
|
|
NegativeTTL: 5 * time.Minute,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("failed to create cache: %v", err)
|
|
}
|
|
|
|
svc, err := NewService(&ServiceConfig{
|
|
Cache: cache,
|
|
Fetcher: httpfetcher.NewMock(mockFS),
|
|
SigningKey: cfg.signingKey,
|
|
Allowlist: cfg.allowlist,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("failed to create service: %v", err)
|
|
}
|
|
|
|
return svc, fixtures
|
|
}
|
|
|
|
// setupServiceTestDB creates an in-memory SQLite database for testing
|
|
// using the production schema.
|
|
func setupServiceTestDB(t *testing.T) *sql.DB {
|
|
t.Helper()
|
|
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
t.Fatalf("failed to open test db: %v", err)
|
|
}
|
|
|
|
// Use the real production schema via migrations
|
|
err = database.ApplyMigrations(context.Background(), db, nil)
|
|
if err != nil {
|
|
t.Fatalf("failed to apply migrations: %v", err)
|
|
}
|
|
|
|
return db
|
|
}
|
|
|
|
type testServiceConfig struct {
|
|
allowlist []string
|
|
signingKey string
|
|
}
|
|
|
|
// TestServiceOption configures the test service.
|
|
type TestServiceOption func(*testServiceConfig)
|
|
|
|
// WithAllowlist sets the allowlist for the test service.
|
|
func WithAllowlist(hosts ...string) TestServiceOption {
|
|
return func(c *testServiceConfig) {
|
|
c.allowlist = hosts
|
|
}
|
|
}
|
|
|
|
// WithSigningKey sets the signing key for the test service.
|
|
func WithSigningKey(key string) TestServiceOption {
|
|
return func(c *testServiceConfig) {
|
|
c.signingKey = key
|
|
}
|
|
}
|
|
|
|
// WithNoAllowlist removes all allowlisted hosts.
|
|
func WithNoAllowlist() TestServiceOption {
|
|
return func(c *testServiceConfig) {
|
|
c.allowlist = nil
|
|
}
|
|
}
|