Files
iam/pkg/schema/avatar_test.go
zooqueen c924bb542a an organization appears the way a person does
A person has carried `avatar` since the beginning — an image reference IAM
stores and publishes as the OIDC `picture` claim and the SCIM `photos` value.
An organization had no such field, so the one screen that needed a mark for a
tenant kept an emoji in the browser's local storage: a mark that appears
everywhere the organization does, held on one device.

schema.Mark is the pair, and schema.MarkOf is its one writer. Organization now
carries the same two fields under the same names a person uses, so a screen
draws a subject without asking which kind of subject it holds.

Two fields because two TYPES: `picture` and `photos` are URL-typed by their
specs, so an emoji in `avatar` is not a smaller answer, it is an invalid one.
At most one is stored — an image wins at the write and the emoji does not reach
the row — so no reader ranks them and two readers cannot rank them differently.
Both empty is the third answer, and the screen draws an initial.

An image is a REFERENCE: https, or the bytes inline as a data URL, which is
what a crop performed in a browser produces and what `avatar` already holds.
IAM stores no blobs and needs no bucket for this. Everything else is refused —
http downgrades a page served over TLS, and SVG renders in an `<img>` while
being a document that executes. The 96 KiB bound moves to the row, because a
bound enforced only by the client that writes it binds that client alone.

Logo and LogoDark are untouched and are a different thing: the wordmark a login
screen draws. DefaultAvatar is a third, the avatar new members start with.

POST /v1/iam/organizations/avatar is its own operation rather than a field on
update because update REPLACES the record while every read is masked: a client
that read an organization, changed one field and posted it back would persist
"***" over masterPassword, passwordSalt, passwordObfuscatorKey and
kerberosKeytab. This applies two fields to the row as it stands.

Authorization is the one that already existed. The path's first segment is
`organizations`, so a write resolves to p.adminOf(name) — self-service by
whoever administers that organization, never the reserved admin org. Pinned by
tests that drive the real router with a real bearer: an org admin sets its own,
a plain member and a foreign org's admin are refused, and removing the check
turns both red.
2026-08-15 12:24:37 -07:00

155 lines
5.3 KiB
Go
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package schema
import (
"strings"
"testing"
)
// A subject has ONE mark. Sending both halves is not a preference order for a
// reader to resolve later — the image wins at the write and the emoji does not
// reach the row, so nothing downstream can rank them differently.
func TestMarkOf_oneAnswerReachesTheRow(t *testing.T) {
const img = "https://s3.hanzo.ai/avatars/hanzo.png"
for _, c := range []struct {
name string
avatar, emoji string
want Mark
}{
{"image alone", img, "", Mark{Avatar: img}},
{"emoji alone", "", "🦁", Mark{Emoji: "🦁"}},
{"image beats emoji", img, "🦁", Mark{Avatar: img}},
{"neither clears the mark", "", "", Mark{}},
{"blank is not an answer", " ", " ", Mark{}},
} {
t.Run(c.name, func(t *testing.T) {
got, err := MarkOf(c.avatar, c.emoji)
if err != nil {
t.Fatalf("MarkOf(%q, %q): %v", c.avatar, c.emoji, err)
}
if got != c.want {
t.Fatalf("MarkOf(%q, %q) = %+v, want %+v", c.avatar, c.emoji, got, c.want)
}
})
}
}
// A refused half refuses the whole write. Storing the good half of a bad request
// leaves a mark nobody asked for.
func TestMarkOf_refusesRatherThanStoringHalf(t *testing.T) {
if _, err := MarkOf("javascript:alert(1)", "🦁"); err == nil {
t.Fatal("a bad image must refuse the write, not fall through to the emoji")
}
if _, err := MarkOf("", "not an emoji"); err == nil {
t.Fatal("a bad emoji must refuse the write")
}
}
// An image is a reference this service hands to an `<img src>`: an https link, or
// the bytes inline. Anything else is refused at the write, where there is still
// somebody to tell.
func TestAvatarRef(t *testing.T) {
inline := "data:image/webp;base64,UklGRh4AAABXRUJQ"
for _, c := range []struct {
name, raw string
ok bool
}{
{"https link", "https://s3.hanzo.ai/avatars/hanzo.png", true},
{"inline webp", inline, true},
{"inline png", "data:image/png;base64,iVBORw0KGgo=", true},
{"inline jpeg", "data:image/jpeg;base64,/9j/4AAQSkZJRg==", true},
{"inline gif", "data:image/gif;base64,R0lGODlhAQAB", true},
{"empty clears it", "", true},
{"padded", " " + inline + " ", true},
{"http downgrades a page served over TLS", "http://example.com/a.png", false},
{"script", "javascript:alert(1)", false},
{"a document, not an image", "data:text/html;base64,PHNjcmlwdD4=", false},
{"svg executes", "data:image/svg+xml;base64,PHN2Zz4=", false},
{"scheme with no image behind it", "https://", false},
{"an empty inline image", "data:image/png;base64,", false},
{"a bare path", "/avatars/hanzo.png", false},
{"a protocol-relative link", "//example.com/a.png", false},
} {
t.Run(c.name, func(t *testing.T) {
got, err := AvatarRef(c.raw)
if c.ok != (err == nil) {
t.Fatalf("AvatarRef(%q) err = %v, want ok=%v", c.raw, err, c.ok)
}
if c.ok && got != strings.TrimSpace(c.raw) {
t.Fatalf("AvatarRef(%q) = %q, want the value verbatim", c.raw, got)
}
})
}
}
// The bound is on the row, not on whichever client happens to write it. A client
// that enforces its own limit is enforcing a limit for itself only.
func TestAvatarRef_bound(t *testing.T) {
fits := "data:image/png;base64," + strings.Repeat("A", AvatarLimit-len("data:image/png;base64,"))
if _, err := AvatarRef(fits); err != nil {
t.Fatalf("a reference exactly at the limit must be stored: %v", err)
}
if _, err := AvatarRef(fits + "A"); err == nil {
t.Fatal("a reference over the limit must be refused")
}
}
// One emoji is one sequence, however many runes spell it.
func TestEmoji(t *testing.T) {
for _, c := range []struct {
name, raw string
ok bool
}{
{"a pictograph", "🦁", true},
{"an older symbol", "⭐", true},
{"one needing a variation selector", "❤️", true},
{"a skin tone", "👋🏽", true},
{"a joined family", "👨‍👩‍👧‍👦", true},
{"a joined pair with tones", "👨🏻‍❤️‍💋‍👨🏻", true},
{"a flag", "🇺🇸", true},
{"a subdivision flag", "🏴󠁧󠁢󠁳󠁣󠁴󠁿", true},
{"a keycap", "1️⃣", true},
{"empty means none", "", true},
{"padded", " 🦁 ", true},
{"two emoji", "🦁🐯", false},
{"an emoji and a letter", "🦁a", false},
{"a letter", "H", false},
{"a word", "hanzo", false},
{"prose", "not an emoji", false},
{"two flags", "🇺🇸🇬🇧", false},
{"a lone regional indicator", "🇺", false},
{"a digit without its keycap", "1", false},
{"a run of pictographs", strings.Repeat("🦁", 12), false},
{"punctuation", "!", false},
} {
t.Run(c.name, func(t *testing.T) {
got, err := Emoji(c.raw)
if c.ok != (err == nil) {
t.Fatalf("Emoji(%q) err = %v, want ok=%v", c.raw, err, c.ok)
}
if c.ok && got != strings.TrimSpace(c.raw) {
t.Fatalf("Emoji(%q) = %q, want the value verbatim", c.raw, got)
}
})
}
}
// Normalization settles padding and nothing else. A glyph is not rewritten into
// some other glyph on its way to the row.
func TestEmoji_isNotRewritten(t *testing.T) {
const withSelector = "❤️" // heart + U+FE0F
got, err := Emoji(withSelector)
if err != nil {
t.Fatalf("Emoji: %v", err)
}
if got != withSelector || len([]rune(got)) != 2 {
t.Fatalf("Emoji(%q) = %q (%d runes), want the sequence verbatim", withSelector, got, len([]rune(got)))
}
}