package brandkit import ( "context" "encoding/json" "strings" "testing" "github.com/google/uuid" "github.com/warmbly/warmbly/internal/models" "github.com/warmbly/warmbly/internal/repository" ) func fullTheme() models.PortalTheme { return models.PortalTheme{ Font: models.PortalFontGeometric, Corners: models.PortalCornersRound, Appearance: models.PortalAppearanceDark, SignInLayout: models.PortalSignInLayoutCover, SignInBackground: models.PortalSignInBackgroundImage, SignInHeading: "Welcome back", SignInMessage: "Sign in to see how your campaigns are doing.", SupportEmail: "help@northwind.test", SupportURL: "https://northwind.test/help", TermsURL: "https://northwind.test/terms", PrivacyURL: "https://northwind.test/privacy", } } func TestNormalizeThemeAcceptsEveryListedValue(t *testing.T) { if out, xerr := NormalizeTheme(models.PortalTheme{}); xerr != nil || !out.Empty() { t.Fatalf("empty theme: %+v %v", out, xerr) } if out, xerr := NormalizeTheme(fullTheme()); xerr != nil || out != fullTheme() { t.Fatalf("full theme: %+v %v", out, xerr) } for _, th := range []models.PortalTheme{ {Font: models.PortalFontSystem}, {Font: models.PortalFontGeometric}, {Font: models.PortalFontSerif}, {Corners: models.PortalCornersSharp}, {Corners: models.PortalCornersRound}, {Appearance: models.PortalAppearanceLight}, {Appearance: models.PortalAppearanceDark}, {SignInLayout: models.PortalSignInLayoutSplit}, {SignInLayout: models.PortalSignInLayoutCover}, {SignInBackground: models.PortalSignInBackgroundAccent}, {SignInBackground: models.PortalSignInBackgroundDark}, {SignInBackground: models.PortalSignInBackgroundImage}, } { if out, xerr := NormalizeTheme(th); xerr != nil || out != th { t.Errorf("NormalizeTheme(%+v) = %+v, %v", th, out, xerr) } } // Text and links are stored in their normal form. out, xerr := NormalizeTheme(models.PortalTheme{SignInHeading: " Welcome back ", SupportEmail: " help@northwind.test ", SupportURL: " https://northwind.test/help "}) if xerr != nil || out.SignInHeading != "Welcome back" || out.SupportEmail != "help@northwind.test" || out.SupportURL != "https://northwind.test/help" { t.Fatalf("normal form: %+v %v", out, xerr) } } func TestNormalizeThemeRefusals(t *testing.T) { for name, tc := range map[string]struct { theme models.PortalTheme code string names string }{ "font": {models.PortalTheme{Font: "comic"}, "invalid_theme", "theme.font"}, "corners": {models.PortalTheme{Corners: "pill"}, "invalid_theme", "theme.corners"}, "appearance": {models.PortalTheme{Appearance: "auto"}, "invalid_theme", "theme.appearance"}, "layout": {models.PortalTheme{SignInLayout: "centered"}, "invalid_theme", "theme.sign_in_layout"}, "background": {models.PortalTheme{SignInBackground: "gradient"}, "invalid_theme", "theme.sign_in_background"}, "enum case": {models.PortalTheme{Font: "Serif"}, "invalid_theme", "theme.font"}, "heading too long": {models.PortalTheme{SignInHeading: strings.Repeat("a", 61)}, "invalid_brand_text", "sign-in heading must be 60"}, "heading link": {models.PortalTheme{SignInHeading: "Visit https://evil.test"}, "invalid_brand_text", "sign-in heading cannot contain a link"}, "heading address": {models.PortalTheme{SignInHeading: "Go to evil.test now"}, "invalid_brand_text", "sign-in heading"}, "heading markup": {models.PortalTheme{SignInHeading: "Welcome"}, "invalid_brand_text", "sign-in heading contains characters"}, "heading bidi": {models.PortalTheme{SignInHeading: "Welcome‮back"}, "invalid_brand_text", "sign-in heading"}, "heading invisible": {models.PortalTheme{SignInHeading: "Welcome​back"}, "invalid_brand_text", "sign-in heading"}, "message too long": {models.PortalTheme{SignInMessage: strings.Repeat("a", 201)}, "invalid_brand_text", "sign-in message must be 200"}, "message email": {models.PortalTheme{SignInMessage: "Write to help@evil.test"}, "invalid_brand_text", "sign-in message cannot contain"}, "message control": {models.PortalTheme{SignInMessage: "Line one\nline two"}, "invalid_brand_text", "sign-in message"}, "message markup": {models.PortalTheme{SignInMessage: "a > b"}, "invalid_brand_text", "sign-in message"}, "support email": {models.PortalTheme{SupportEmail: "not an address"}, "invalid_brand_link", "support email"}, "support email named": {models.PortalTheme{SupportEmail: "Help "}, "invalid_brand_link", "support email"}, "support email host": {models.PortalTheme{SupportEmail: "help@localhost"}, "invalid_brand_link", "support email"}, "support url http": {models.PortalTheme{SupportURL: "http://northwind.test/help"}, "invalid_brand_link", "support link"}, "support url scheme": {models.PortalTheme{SupportURL: "javascript:alert(1)"}, "invalid_brand_link", "support link"}, "terms url bare": {models.PortalTheme{TermsURL: "northwind.test/terms"}, "invalid_brand_link", "terms link"}, "privacy url creds": {models.PortalTheme{PrivacyURL: "https://user:pw@northwind.test"}, "invalid_brand_link", "privacy link"}, } { _, xerr := NormalizeTheme(tc.theme) if xerr == nil || xerr.Identifier != tc.code || !strings.Contains(xerr.Message, tc.names) { t.Errorf("%s: got %v, want %s naming %q", name, xerr, tc.code, tc.names) } } // Sixty and two hundred are counted in characters, not bytes. if _, xerr := NormalizeTheme(models.PortalTheme{SignInHeading: strings.Repeat("é", 60), SignInMessage: strings.Repeat("é", 200)}); xerr != nil { t.Fatalf("at the limit: %v", xerr) } } // A stored value that fails a rule reads as unset, field by field. func TestCleanThemeDropsOnlyWhatFails(t *testing.T) { stored := fullTheme() stored.Font, stored.SignInHeading, stored.SupportURL, stored.SupportEmail = "comic", "Visit evil.test", "http://northwind.test", "" want := fullTheme() want.Font, want.SignInHeading, want.SupportURL, want.SupportEmail = "", "", "", "" if got := CleanTheme(stored); got != want { t.Fatalf("CleanTheme = %+v, want %+v", got, want) } } func TestComposeCarriesThemeOnlyFromTheAgencyBrand(t *testing.T) { agency := &models.AgencyBrand{ AgencyID: uuid.New(), BrandSettings: models.BrandSettings{DisplayName: "Agency"}, IconURL: "https://cdn/icon.png", SignInImageURL: "https://cdn/sign-in.jpg", Theme: fullTheme(), } got := Compose(deployment, nil, agency, false) if got.Source != SourceAgency || got.Brand.IconURL != agency.IconURL || got.Brand.SignInImageURL != agency.SignInImageURL || got.Brand.Theme == nil || *got.Brand.Theme != fullTheme() { t.Fatalf("agency brand: %+v", got.Brand) } // A workspace's own brand replaces the agency's whole, theme included. ws := &models.BrandSettings{DisplayName: "Client Co"} if got := Compose(deployment, ws, agency, false).Brand; got.Theme != nil || got.IconURL != "" || got.SignInImageURL != "" { t.Fatalf("workspace brand carries the agency theme: %+v", got) } // The deployment's brand carries none either. unnamed := &models.AgencyBrand{AgencyID: uuid.New(), IconURL: "https://cdn/icon.png", Theme: fullTheme()} if got := Compose(deployment, nil, unnamed, false); got.Source != SourceDeployment || got.Brand.Theme != nil || got.Brand.IconURL != "" { t.Fatalf("deployment brand carries a theme: %+v", got.Brand) } // An agency with no theme sends none, and a stored value that fails a rule is not shown. agency.Theme = models.PortalTheme{} if got := Compose(deployment, nil, agency, false).Brand; got.Theme != nil { t.Fatalf("empty theme sent: %+v", got.Theme) } agency.Theme = models.PortalTheme{SignInHeading: "