BlueGit

timfa bluegit

Files

bluegit / internal / server / info_test.go

info_test.go

feat: voeg image-presets en een CI-infopagina toe
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
a6677fb6 History
info_test.go 2.3 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 "workflow",
33 "Differences with GitLab",
34 `href="/-/info"`,
35 } {
36 if !strings.Contains(body, want) {
37 t.Fatalf("mist %q", want)
38 }
39 }
40 const start = `<script type="application/json" id="bluegit-ci-spec">`
41 i := strings.Index(body, start)
42 rest := body[i+len(start):]
43 j := strings.Index(rest, "</script>")
44 embedded := rest[:j]
45
46 api, err := http.Get(ts.URL + "/api/v1/ci/spec")
47 if err != nil {
48 t.Fatal(err)
49 }
50 raw := read(t, api)
51 if api.StatusCode != 200 || !strings.Contains(api.Header.Get("Content-Type"), "application/json") {
52 t.Fatalf("api %d %s", api.StatusCode, api.Header.Get("Content-Type"))
53 }
54 if embedded != strings.TrimRight(raw, "\n") && embedded != raw {
55 // Marshal eindigt op een newline. Het scriptblok bevat die newline.
56 if strings.TrimSpace(embedded) != strings.TrimSpace(raw) {
57 t.Fatalf("pagina en API wijken af\npage %q\napi %q", embedded[:80], raw[:80])
58 }
59 }
60 var doc cispec.Doc
61 if err := json.Unmarshal([]byte(raw), &doc); err != nil {
62 t.Fatal(err)
63 }
64 if doc.File != "blue-ci-cd.yml" || len(doc.Sections) < 6 {
65 t.Fatalf("%+v", doc.File)
66 }
67
68 nl, err := http.Get(ts.URL + "/api/v1/ci/spec?lang=nl")
69 if err != nil {
70 t.Fatal(err)
71 }
72 dutch := read(t, nl)
73 if !strings.Contains(dutch, "Volgorde van de stages") || !strings.Contains(dutch, "bluegit/base") {
74 t.Fatalf("nl: %s", dutch[:400])
75 }
76 cu, err := http.Get(ts.URL + "/api/v1/ci/spec?lang=cuneiform")
77 if err != nil {
78 t.Fatal(err)
79 }
80 signs := read(t, cu)
81 if strings.Contains(signs, "Stage order") || !strings.Contains(signs, `"name":"stages"`) && !strings.Contains(signs, `"name": "stages"`) {
82 t.Fatalf("spijkerschrift of sleutelnaam: %s", signs[:300])
83 }
84}