BlueGit

timfa bluegit

Files

bluegit / internal / server / info_test.go

info_test.go

fix: gebruik de committer-tijd als AUR-pkgver
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
2388e414 History
info_test.go 2.4 KiB Download Raw
1package server
2
3import (
4 "encoding/json"
5 "net/http"
6 "net/http/httptest"
7 "strings"
8 "testing"
9
10 "gitlab.com/Timfa/bluegit/internal/cispec"
11 "gitlab.com/Timfa/bluegit/internal/config"
12)
13
14func TestCISpecPageMatchesAPI(t *testing.T) {
15 srv := &Server{Cfg: config.Config{BaseURL: "http://127.0.0.1:9"}}
16 ts := httptest.NewServer(srv.Handler())
17 t.Cleanup(ts.Close)
18
19 page, err := http.Get(ts.URL + "/-/info")
20 if err != nil {
21 t.Fatal(err)
22 }
23 body := read(t, page)
24 if page.StatusCode != 200 {
25 t.Fatalf("pagina %d", page.StatusCode)
26 }
27 for _, want := range []string{
28 `<script type="application/json" id="bluegit-ci-spec">`,
29 "bluegit/node",
30 "node:22",
31 "CI_JOB_TOKEN",
32 "CI_COMMIT_TIMESTAMP",
33 "Committer time in ISO 8601.",
34 "workflow",
35 "Differences with GitLab",
36 `href="/-/info"`,
37 } {
38 if !strings.Contains(body, want) {
39 t.Fatalf("mist %q", want)
40 }
41 }
42 const start = `<script type="application/json" id="bluegit-ci-spec">`
43 i := strings.Index(body, start)
44 rest := body[i+len(start):]
45 j := strings.Index(rest, "</script>")
46 embedded := rest[:j]
47
48 api, err := http.Get(ts.URL + "/api/v1/ci/spec")
49 if err != nil {
50 t.Fatal(err)
51 }
52 raw := read(t, api)
53 if api.StatusCode != 200 || !strings.Contains(api.Header.Get("Content-Type"), "application/json") {
54 t.Fatalf("api %d %s", api.StatusCode, api.Header.Get("Content-Type"))
55 }
56 if embedded != strings.TrimRight(raw, "\n") && embedded != raw {
57 // Marshal eindigt op een newline. Het scriptblok bevat die newline.
58 if strings.TrimSpace(embedded) != strings.TrimSpace(raw) {
59 t.Fatalf("pagina en API wijken af\npage %q\napi %q", embedded[:80], raw[:80])
60 }
61 }
62 var doc cispec.Doc
63 if err := json.Unmarshal([]byte(raw), &doc); err != nil {
64 t.Fatal(err)
65 }
66 if doc.File != "blue-ci-cd.yml" || len(doc.Sections) < 6 {
67 t.Fatalf("%+v", doc.File)
68 }
69
70 nl, err := http.Get(ts.URL + "/api/v1/ci/spec?lang=nl")
71 if err != nil {
72 t.Fatal(err)
73 }
74 dutch := read(t, nl)
75 if !strings.Contains(dutch, "Volgorde van de stages") || !strings.Contains(dutch, "bluegit/base") || !strings.Contains(dutch, "Committer-tijd in ISO 8601.") {
76 t.Fatalf("nl: %s", dutch[:400])
77 }
78 cu, err := http.Get(ts.URL + "/api/v1/ci/spec?lang=cuneiform")
79 if err != nil {
80 t.Fatal(err)
81 }
82 signs := read(t, cu)
83 if strings.Contains(signs, "Stage order") || !strings.Contains(signs, `"name":"stages"`) && !strings.Contains(signs, `"name": "stages"`) {
84 t.Fatalf("spijkerschrift of sleutelnaam: %s", signs[:300])
85 }
86}