BlueGit

timfa bluegit

Files

bluegit / internal / server / api_visibility_test.go

api_visibility_test.go

api_visibility_test.go 5.9 KiB Download Raw
1package server
2
3import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/http/httptest"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "gitlab.com/Timfa/bluegit/internal/access"
13 "gitlab.com/Timfa/bluegit/internal/auth"
14 "gitlab.com/Timfa/bluegit/internal/config"
15 "gitlab.com/Timfa/bluegit/internal/store"
16)
17
18// Anoniem en als niet-lid: publiek lezen mag, privé en groepsprivé geeft 404,
19// schrijven zonder token geeft 401. Geldt voor de leesroutes die agents gebruiken.
20func TestAPIAnonymousReadsRespectVisibility(t *testing.T) {
21 storage := t.TempDir()
22 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
23 if err != nil {
24 t.Fatal(err)
25 }
26 t.Cleanup(func() { _ = st.Close() })
27 ctx := context.Background()
28 ada, _ := st.UpsertUser(ctx, 1, "ada", "", false)
29 eve, _ := st.UpsertUser(ctx, 2, "eve", "", false)
30 tok := func(u store.User) string {
31 plain, _, err := st.IssueToken(ctx, u.ID, store.TokenRequest{Name: "t", Scopes: "api"})
32 if err != nil {
33 t.Fatal(err)
34 }
35 return plain
36 }
37 adaTok, eveTok := tok(ada), tok(eve)
38 team, err := st.CreateGroup(ctx, ada.ID, "Team", "team", "", access.RoleDeveloper)
39 if err != nil {
40 t.Fatal(err)
41 }
42 type fixture struct {
43 path string
44 pipe, job int64
45 mr, issues int64
46 }
47 mk := func(kind string, owner int64, ownerPath, name, vis string) fixture {
48 repo, err := st.CreateRepo(ctx, kind, owner, name, "", "master", vis)
49 if err != nil {
50 t.Fatal(err)
51 }
52 sha := strings.Repeat("a", 40)
53 pid, err := st.CreatePipeline(ctx, repo.ID, sha, "refs/heads/master", "push", "ada", "c", []store.NewJob{{Name: "t", Stage: "test", Script: "true"}})
54 if err != nil {
55 t.Fatal(err)
56 }
57 jobs, _ := st.JobsByPipeline(ctx, pid)
58 _ = st.AppendJobLog(ctx, jobs[0].ID, "geheim-log-"+name+"\n")
59 mr, err := st.CreateMergeRequest(ctx, repo.ID, ada.ID, "mr "+name, "", "topic", "master")
60 if err != nil {
61 t.Fatal(err)
62 }
63 if _, err := st.CreateIssue(ctx, repo.ID, ada.ID, store.NewIssue{Title: "issue " + name}); err != nil {
64 t.Fatal(err)
65 }
66 return fixture{path: ownerPath + "/" + name, pipe: pid, job: jobs[0].ID, mr: mr.Number}
67 }
68 pub := mk("user", ada.ID, "ada", "pub", "public")
69 priv := mk("user", ada.ID, "ada", "priv", "private")
70 grp := mk("group", team.ID, "team", "intern", "private")
71
72 cfg := config.Config{
73 BaseURL: "http://127.0.0.1:9", StoragePath: storage,
74 Auth: config.Auth{Mode: "dev", DevUser: "ada", DevAuthID: 1},
75 Git: config.Git{Binary: "git", DefaultBranch: "master"},
76 CI: config.CI{Enabled: true},
77 }
78 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "ada", AuthID: 1}, Git: "git", HookBin: "/bin/true", StateDir: t.TempDir()}
79 ts := httptest.NewServer(srv.Handler())
80 t.Cleanup(ts.Close)
81 call := func(method, path, token string) (int, string) {
82 t.Helper()
83 req, _ := http.NewRequest(method, ts.URL+path, strings.NewReader("{}"))
84 req.Header.Set("Content-Type", "application/json")
85 if token != "" {
86 req.Header.Set("Authorization", "Bearer "+token)
87 }
88 res, err := http.DefaultClient.Do(req)
89 if err != nil {
90 t.Fatal(err)
91 }
92 return res.StatusCode, read(t, res)
93 }
94 reads := func(f fixture) []string {
95 p := "/api/v1/projects/" + f.path
96 return []string{
97 p,
98 p + "/pipelines",
99 p + "/pipelines?ref=master",
100 p + "/pipelines?sha=aaaaaaa",
101 fmt.Sprintf("%s/pipelines/%d", p, f.pipe),
102 fmt.Sprintf("%s/pipelines/%d/jobs", p, f.pipe),
103 fmt.Sprintf("%s/pipelines/%d/jobs/%d", p, f.pipe, f.job),
104 fmt.Sprintf("%s/pipelines/%d/jobs/%d/log", p, f.pipe, f.job),
105 p + "/merge_requests",
106 p + "/merge_requests?source_branch=topic",
107 fmt.Sprintf("%s/merge_requests/%d", p, f.mr),
108 fmt.Sprintf("%s/merge_requests/%d/notes", p, f.mr),
109 p + "/releases",
110 "/api/v1/repos/" + f.path + "/issues",
111 "/api/v1/repos/" + f.path + "/issues/1",
112 }
113 }
114 pubRepo, _ := st.RepoByPath(ctx, "ada", "pub")
115 if _, err := st.CreateIssue(ctx, pubRepo.ID, ada.ID, store.NewIssue{Title: "vertrouwelijk-x", Confidential: true}); err != nil {
116 t.Fatal(err)
117 }
118 for _, token := range []string{"", eveTok} {
119 if code, body := call(http.MethodGet, "/api/v1/repos/ada/pub/issues/2", token); code == 200 || strings.Contains(body, "vertrouwelijk-x") {
120 t.Fatalf("vertrouwelijk issue zichtbaar: %d %s", code, body)
121 }
122 if _, body := call(http.MethodGet, "/api/v1/repos/ada/pub/issues", token); strings.Contains(body, "vertrouwelijk-x") {
123 t.Fatalf("vertrouwelijk issue in lijst: %s", body)
124 }
125 }
126 for _, path := range reads(pub) {
127 if code, body := call(http.MethodGet, path, ""); code != 200 {
128 t.Fatalf("anoniem publiek %s: %d %s", path, code, body)
129 }
130 }
131 for _, f := range []fixture{priv, grp} {
132 for _, path := range reads(f) {
133 for who, token := range map[string]string{"anoniem": "", "niet-lid": eveTok} {
134 code, body := call(http.MethodGet, path, token)
135 if code != 404 || strings.Contains(body, "geheim-log") || strings.Contains(body, "mr ") || strings.Contains(body, "issue ") {
136 t.Fatalf("%s ziet privé %s: %d %s", who, path, code, body)
137 }
138 }
139 if code, body := call(http.MethodGet, path, adaTok); code != 200 {
140 t.Fatalf("eigenaar %s: %d %s", path, code, body)
141 }
142 }
143 }
144 writes := func(f fixture) []string {
145 p := "/api/v1/projects/" + f.path
146 return []string{
147 p + "/merge_requests",
148 p + "/releases",
149 fmt.Sprintf("%s/merge_requests/%d/notes", p, f.mr),
150 fmt.Sprintf("%s/pipelines/%d/retry", p, f.pipe),
151 "/api/v1/repos/" + f.path + "/issues",
152 }
153 }
154 for _, f := range []fixture{pub, priv, grp} {
155 for _, path := range writes(f) {
156 if code, body := call(http.MethodPost, path, ""); code != 401 {
157 t.Fatalf("anoniem schrijven %s: %d %s", path, code, body)
158 }
159 }
160 }
161 for _, f := range []fixture{priv, grp} {
162 for _, path := range writes(f) {
163 if code, body := call(http.MethodPost, path, eveTok); code != 404 {
164 t.Fatalf("niet-lid schrijft privé %s: %d %s", path, code, body)
165 }
166 }
167 }
168 if code, body := call(http.MethodPost, fmt.Sprintf("/api/v1/projects/%s/pipelines/%d/retry", pub.path, pub.pipe), eveTok); code != 403 {
169 t.Fatalf("niet-lid retry publiek: %d %s", code, body)
170 }
171}