Files
tenant-registry/internal/branding/image_test.go
T
Sharang ParnerkarandClaude Opus 5.5 c7dda6da5a feat(branding): tenant branding store, endpoints and check field
Customer branding (#155), the contract part: products and the portal
can read and set a workspace's CI colour and logo. The derivation from
the signup domain follows in a separate change.

- Migration 0035: tenant_branding (colour, 256×256 PNG logo with its
  sha256 ETag, manual flag, provenance, derivation queue, notice ack).
  ON DELETE CASCADE, so a purge removes it.
- Entitlements check: "branding" {color, logo_etag, notice, domain,
  name}, only when allowed and there is a colour or logo. A read error
  leaves the field out instead of failing the check.
- GET/PUT /v1/tenants/{id}/branding (tenants.read/write): the portal's
  Erscheinungsbild page; a PUT makes the branding manual. Logos are
  decoded (PNG, JPEG, GIF, ICO) and re-encoded; SVG is refused.
- GET /v1/tenants/{id}/branding/logo (branding.read): PNG, ETag, 304.
- POST /v1/tenants/{id}/branding/ack (branding.ack): "Passt" by an IT
  admin or owner, checked in Keycloak; 403 not_admin otherwise.
- BRANDING_DERIVE config switch (off by default), reported to the
  portal as derive_enabled.
- CI runs the new internal/branding tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 13:08:54 +01:00

120 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package branding
import (
"bytes"
"encoding/binary"
"errors"
"image"
"image/color"
"image/png"
"testing"
)
func encodePNG(t *testing.T, img image.Image) []byte {
t.Helper()
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
func solid(w, h int, c color.NRGBA) *image.NRGBA {
img := image.NewNRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
img.SetNRGBA(x, y, c)
}
}
return img
}
// ico wraps one entry (PNG bytes or a DIB) into an ICO file.
func ico(w, h, bits int, entry []byte) []byte {
var b bytes.Buffer
_ = binary.Write(&b, binary.LittleEndian, []uint16{0, 1, 1})
b.Write([]byte{byte(w % 256), byte(h % 256), 0, 0})
_ = binary.Write(&b, binary.LittleEndian, []uint16{1, uint16(bits)})
_ = binary.Write(&b, binary.LittleEndian, []uint32{uint32(len(entry)), 22})
b.Write(entry)
return b.Bytes()
}
// dib32 is a 32-bit BMP entry: a red left half, a transparent right half.
func dib32(w, h int) []byte {
var b bytes.Buffer
_ = binary.Write(&b, binary.LittleEndian, []uint32{40, uint32(w), uint32(2 * h)})
_ = binary.Write(&b, binary.LittleEndian, []uint16{1, 32})
_ = binary.Write(&b, binary.LittleEndian, []uint32{0, 0, 0, 0, 0, 0})
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
if x < w/2 {
b.Write([]byte{0, 0, 255, 255}) // BGRA red
} else {
b.Write([]byte{0, 0, 0, 0})
}
}
}
b.Write(make([]byte, ((w+31)/32)*4*h)) // AND mask
return b.Bytes()
}
func TestNormalizeLogo(t *testing.T) {
red := color.NRGBA{R: 255, A: 255}
t.Run("deterministic 256 square, aspect kept", func(t *testing.T) {
src := encodePNG(t, solid(100, 50, red))
a, ea, err := NormalizeLogo(src)
if err != nil {
t.Fatal(err)
}
b, eb, _ := NormalizeLogo(src)
if !bytes.Equal(a, b) || ea != eb || len(ea) != 64 {
t.Fatalf("not deterministic: %s %s", ea, eb)
}
img, _ := png.Decode(bytes.NewReader(a))
if img.Bounds().Dx() != 256 || img.Bounds().Dy() != 256 {
t.Fatalf("size %v", img.Bounds())
}
// 100×50 fits as 256×128, centred: the top rows are transparent.
if _, _, _, alpha := img.At(128, 10).RGBA(); alpha != 0 {
t.Fatalf("top is not transparent: %v", img.At(128, 10))
}
if r, _, _, alpha := img.At(128, 128).RGBA(); alpha != 0xffff || r != 0xffff {
t.Fatalf("centre is not red: %v", img.At(128, 128))
}
})
t.Run("ICO with a PNG entry", func(t *testing.T) {
if _, _, err := NormalizeLogo(ico(32, 32, 32, encodePNG(t, solid(32, 32, red)))); err != nil {
t.Fatal(err)
}
})
t.Run("ICO with a 32-bit BMP entry", func(t *testing.T) {
img, err := DecodeImage(ico(16, 16, 32, dib32(16, 16)))
if err != nil {
t.Fatal(err)
}
if c := color.NRGBAModel.Convert(img.At(2, 3)).(color.NRGBA); c != red {
t.Fatalf("left %v", c)
}
if c := color.NRGBAModel.Convert(img.At(12, 3)).(color.NRGBA); c.A != 0 {
t.Fatalf("right %v", c)
}
})
t.Run("refused", func(t *testing.T) {
for name, tc := range map[string]struct {
data []byte
want error
}{
"svg": {[]byte(`<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg"></svg>`), ErrSVG},
"text": {[]byte("hello"), ErrNotImage},
"too big": {make([]byte, MaxImageBytes+1), ErrImageTooLarge},
"short ico": {[]byte{0, 0, 1, 0, 5, 0}, ErrNotImage},
"bad entry": {ico(16, 16, 32, []byte{1, 2, 3}), ErrNotImage},
} {
if _, _, err := NormalizeLogo(tc.data); !errors.Is(err, tc.want) {
t.Errorf("%s: %v, want %v", name, err, tc.want)
}
}
})
}