| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/rastrillo/rastrillo" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "regexp" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | func TestCreditsPublicRouteAndLockedNotices(t *testing.T) { |
| 16 | mux, _, _ := homeFixture(t) |
| 17 | loc, err := rastrillo.NewLocales(Locales, DefaultLocale, rastrillo.BaseCatalog(), LocaleFS) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | handler := loc.Middleware(mux) |
| 22 | response := httptest.NewRecorder() |
| 23 | handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, homeTestOrigin+"/help/open-source", nil)) |
| 24 | if response.Code != http.StatusOK || response.Header().Get("Location") != "" || len(response.Result().Cookies()) != 0 { |
| 25 | t.Fatal("credits route unavailable or minted a cookie", response.Code) |
| 26 | } |
| 27 | if response.Header().Get("Cache-Control") != "no-store" || response.Header().Get("Referrer-Policy") != "strict-origin" { |
| 28 | t.Fatal("credits must preserve the origin for the chained Home sign-out") |
| 29 | } |
| 30 | for _, want := range []string{"Open source", "Lucide and Feather", "Onest", "krilla", "yjs", "Liberation Fonts", "Rust standard library", "No notice file is included in this release.", "/static/credits.css"} { |
| 31 | if !strings.Contains(response.Body.String(), want) { |
| 32 | t.Fatal("missing attribution", want) |
| 33 | } |
| 34 | } |
| 35 | if len(embeddedCredits.MissingNotices) != 1 || embeddedCredits.MissingNotices[0] != "github.com/keymaildev/signin" { |
| 36 | t.Fatal("new missing notice needs review") |
| 37 | } |
| 38 | style := httptest.NewRecorder() |
| 39 | handler.ServeHTTP(style, httptest.NewRequest(http.MethodGet, homeTestOrigin+"/static/credits.css", nil)) |
| 40 | if style.Code != 200 || !strings.Contains(style.Body.String(), "[estilo-credits]") || style.Header().Get("Cache-Control") != "no-cache" { |
| 41 | t.Fatal("shared style unavailable or stale") |
| 42 | } |
| 43 | if output := os.Getenv("SLIDES_CREDITS_EVIDENCE_DIR"); output != "" { |
| 44 | if err := os.MkdirAll(output, 0700); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if err := os.WriteFile(filepath.Join(output, "page.html"), response.Body.Bytes(), 0600); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | for _, m := range regexp.MustCompile(`(?:href|src)="(/static/[^" ]+)"`).FindAllStringSubmatch(response.Body.String(), -1) { |
| 51 | asset := httptest.NewRecorder() |
| 52 | handler.ServeHTTP(asset, httptest.NewRequest(http.MethodGet, homeTestOrigin+m[1], nil)) |
| 53 | if asset.Code != 200 { |
| 54 | t.Fatal("fixture asset unavailable", m[1]) |
| 55 | } |
| 56 | if err := os.WriteFile(filepath.Join(output, filepath.Base(m[1])), asset.Body.Bytes(), 0600); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestCreditsMatchReleaseArtifacts(t *testing.T) { |
| 64 | command := exec.Command("python3", "scripts/credits.py", "--check") |
| 65 | command.Dir = "../.." |
| 66 | if output, err := command.CombinedOutput(); err != nil { |
| 67 | t.Fatalf("credit inventory drift: %v\n%s", err, output) |
| 68 | } |
| 69 | } |
| 70 | |