package classes import ( "strings" "testing" "git.golem15.com/golem15/sm-seo-plugin/models" ) // TestValidAutolinkURL pins T-33-50: a site path from one / or an // absolute http(s) address; javascript:, data:, protocol-relative, a // backslash path, credentials, whitespace and control characters refused. func TestValidAutolinkURL(t *testing.T) { for u, want := range map[string]bool{ "/blog/chaga": true, "/": true, "https://example.test/a": true, "HTTP://example.test": true, "": false, "javascript:alert(1)": false, "data:text/html,x": false, "//evil.test/a": false, `/\evil.test`: false, "https://user:pw@example.test/": false, "/a b": false, "/a\tb": false, "/a\x00": false, "mailto:a@b.c": false, "https://": false, "relative/path": false, "/" + strings.Repeat("a", models.MaxAutolinkURL): false, } { if got := ValidAutolinkURL(u); got != want { t.Errorf("ValidAutolinkURL(%q) = %v, want %v", u, got, want) } } } // TestNormalizeAutolink pins the stored form: the phrase's inner // whitespace collapsed, the address, locale and note trimmed, "all" as // every locale (""), and the manual origin by default. func TestNormalizeAutolink(t *testing.T) { a := &models.Autolink{Phrase: " lwia \n grzywa ", URL: " /blog/lwia ", Locale: models.AutolinkLocaleAll, Note: " n "} NormalizeAutolink(a) if a.Phrase != "lwia grzywa" || a.URL != "/blog/lwia" || a.Locale != "" || a.Note != "n" || a.Origin != models.AutolinkOriginManual { t.Fatalf("normalised = %+v", a) } b := &models.Autolink{Locale: " pl ", Origin: "keyphrase"} NormalizeAutolink(b) if b.Locale != "pl" || b.Origin != "keyphrase" { t.Fatalf("kept = %+v", b) } } // TestAutolinkWrite pins a partial write: only the fields set change, and // the values for history carry every field. func TestAutolinkWrite(t *testing.T) { a := &models.Autolink{Phrase: "chaga", URL: "/a", Locale: "pl", Enabled: true, Note: "x"} phrase, off := "inonotus", false AutolinkWrite{Phrase: &phrase, Enabled: &off}.Apply(a) if a.Phrase != "inonotus" || a.Enabled || a.URL != "/a" || a.Locale != "pl" || a.Note != "x" { t.Fatalf("after the write = %+v", a) } v := AutolinkValues(*a) for _, k := range []string{"phrase", "url", "locale", "enabled", "origin", "note"} { if _, ok := v[k]; !ok { t.Errorf("history values lack %s", k) } } if v["phrase"] != "inonotus" || v["enabled"] != false { t.Fatalf("values = %v", v) } }