Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions models/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package models

import "testing"

// Test that an image request returns the appropriate key
func TestGetS3Key(t *testing.T) {
ir := &ImageRequest{
Wiki: "metawiki",
Hash1: "a",
Hash2: "a0",
Filename: "foo.png",
}

expectedKey := "metawiki/a/a0/foo.png"
if got := ir.GetS3Key(); got != expectedKey {
t.Fatalf("GetS3Key() = %q, but we expected %q", got, expectedKey)
}
}

// Test that an image request with a revision returns the appropriate archive key
func TestGetArchiveKey(t *testing.T) {
ir := &ImageRequest{
Wiki: "metawiki",
Hash1: "a",
Hash2: "a0",
Filename: "foo.png",
Revision: "20251021233101",
}

expectedKey := "metawiki/archive/a/a0/20251021233101!foo.png"
if got := ir.GetArchiveKey(); got != expectedKey {
t.Fatalf("GetArchiveKey() = %q, but we expected %q", got, expectedKey)
}
}
36 changes: 36 additions & 0 deletions models/thumbnail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package models

import "testing"

// Test that a thumbnail request returns the appropriate S3 thumb key
func TestGetS3ThumbKey(t *testing.T) {
tr := &ThumbnailRequest{
Wiki: "metawiki",
Hash1: "a",
Hash2: "a0",
Filename: "foo.png",
Width: "200",
}

expectedKey := "metawiki/thumb/a/a0/foo.png/200px-foo.png"
if got := tr.GetS3ThumbKey(); got != expectedKey {
t.Fatalf("GetS3ThumbKey() = %q, but we expected %q", got, expectedKey)
}
}

// Test that an archive thumbnail request returns the appropriate thumb archive key
func TestGetThumbArchiveKey(t *testing.T) {
tr := &ThumbnailRequest{
Wiki: "metawiki",
Hash1: "a",
Hash2: "a0",
Filename: "foo.png",
Width: "200",
Revision: "20251021233101",
}

expectedKey := "metawiki/thumb/archive/a/a0/20251021233101!foo.png/200px-foo.png"
if got := tr.GetThumbArchiveKey(); got != expectedKey {
t.Fatalf("GetThumbArchiveKey() = %q, want %q", got, expectedKey)
}
}
76 changes: 76 additions & 0 deletions services/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package services

import (
"bytes"
"image"
"image/color"
"image/png"
"os"
"path/filepath"
"testing"

"github.com/telepedia/thumbra/models"
)

// Encode and decode a PNG image to test helper functions
func TestDecodeEncodePNG(t *testing.T) {
// 10x10 red square for testing
img := image.NewRGBA(image.Rect(0, 0, 10, 10))
for y := 0; y < 10; y++ {
for x := 0; x < 10; x++ {
img.Set(x, y, color.RGBA{255, 0, 0, 255})
}
}

var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatalf("failed to encode png: %v", err)
}

dec, err := decodeImage(bytes.NewReader(buf.Bytes()), "png")
if err != nil {
t.Fatalf("decodeImage failed: %v", err)
}
if dec.Bounds().Dx() != 10 || dec.Bounds().Dy() != 10 {
t.Fatalf("decoded image has wrong dimensions: %v", dec.Bounds())
}

tmp := t.TempDir()
outPath := filepath.Join(tmp, "out.png")
f, err := os.Create(outPath)
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
if err := encodeImage(f, dec, "png"); err != nil {
t.Fatalf("encodeImage failed: %v", err)
}
f.Close()
}

// Test thumbnailing an image
func TestThumbnailImage(t *testing.T) {
// 100x50 png
img := image.NewRGBA(image.Rect(0, 0, 100, 50))
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatalf("failed to encode png: %v", err)
}

obj := &models.ImageResponse{Data: buf.Bytes()}
svc := &ImageService{}

// this is larger than the original width, therefore expect an error
tr := models.ThumbnailRequest{Filename: "foo.png", Width: "200"}
if _, err := svc.ThumbnailImage(tr, obj); err == nil {
t.Fatalf("expected error when requesting width larger than original, got nil")
}

// not actually interested in the image, just check that it returned a valid path
tr2 := models.ThumbnailRequest{Filename: "foo.png", Width: "50"}
path, err := svc.ThumbnailImage(tr2, obj)
if err != nil {
t.Fatalf("ThumbnailImage failed: %v", err)
}
// cleanup
os.Remove(path)
}
46 changes: 46 additions & 0 deletions utils/validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package utils

import (
"testing"

"github.com/telepedia/thumbra/models"
)

func TestValidateImageRequest(t *testing.T) {
good := models.ImageRequest{
Wiki: "metawiki",
Hash1: "a",
Hash2: "a0",
Filename: "foo.png",
Revision: "latest",
}

if err := ValidateImageRequest(good); err != nil {
t.Fatalf("expected nil error for valid request, got %v", err)
}

bad := models.ImageRequest{}
if err := ValidateImageRequest(bad); err == nil {
t.Fatalf("expected error for invalid request, got nil")
}
}

func TestValidateThumbnailRequest(t *testing.T) {
good := models.ThumbnailRequest{
Wiki: "metawiki",
Hash1: "a",
Hash2: "a0",
Filename: "foo.png",
Revision: "latest",
Width: "200",
}

if err := ValidateThumbnailRequest(good); err != nil {
t.Fatalf("expected nil error for valid thumbnail request, got %v", err)
}

bad := models.ThumbnailRequest{}
if err := ValidateThumbnailRequest(bad); err == nil {
t.Fatalf("expected error for invalid thumbnail request, got nil")
}
}