BlueGit

timfa bluegit

Files

bluegit / internal / server / group_delete_test.go

group_delete_test.go

group_delete_test.go 3.4 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "net/http/httptest"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11 "time"
12
13 "gitlab.com/Timfa/bluegit/internal/auth"
14 "gitlab.com/Timfa/bluegit/internal/config"
15 "gitlab.com/Timfa/bluegit/internal/store"
16)
17
18func TestDeleteGroup(t *testing.T) {
19 if _, err := exec.LookPath("git"); err != nil {
20 t.Skip("git ontbreekt")
21 }
22 storage := t.TempDir()
23 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
24 if err != nil {
25 t.Fatal(err)
26 }
27 t.Cleanup(func() { _ = st.Close() })
28 ctx := context.Background()
29 ada, err := st.UpsertUser(ctx, 1, "ada", "", false)
30 if err != nil {
31 t.Fatal(err)
32 }
33 dev, err := st.UpsertUser(ctx, 2, "dev", "", false)
34 if err != nil {
35 t.Fatal(err)
36 }
37 issue := func(u store.User) string {
38 plain, _, err := st.IssueToken(ctx, u.ID, store.TokenRequest{Name: u.Username, Scopes: "api", ExpiresAt: time.Time{}})
39 if err != nil {
40 t.Fatal(err)
41 }
42 return plain
43 }
44 adaTok, devTok := issue(ada), issue(dev)
45 cfg := config.Config{
46 BaseURL: "http://127.0.0.1:9", StoragePath: storage,
47 Auth: config.Auth{Mode: "dev", DevUser: "ada", DevAuthID: 1},
48 Git: config.Git{Binary: "git", DefaultBranch: "master"},
49 }
50 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "ada", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true", StateDir: t.TempDir()}
51 ts := httptest.NewServer(srv.Handler())
52 t.Cleanup(ts.Close)
53
54 call := func(method, path, token, body string) (int, string) {
55 t.Helper()
56 req, _ := http.NewRequest(method, ts.URL+path, strings.NewReader(body))
57 if body != "" {
58 req.Header.Set("Content-Type", "application/json")
59 }
60 req.Header.Set("Authorization", "Bearer "+token)
61 res, err := http.DefaultClient.Do(req)
62 if err != nil {
63 t.Fatal(err)
64 }
65 return res.StatusCode, read(t, res)
66 }
67 if code, body := call(http.MethodPost, "/api/v1/groups", adaTok, `{"name":"Team"}`); code != 201 {
68 t.Fatalf("groep %d %s", code, body)
69 }
70 if code, body := call(http.MethodPost, "/api/v1/projects", adaTok, `{"name":"App","group":"team","visibility":"private"}`); code != 201 {
71 t.Fatalf("project %d %s", code, body)
72 }
73 team, _ := st.GroupByPath(ctx, "team")
74 _ = st.SetMember(ctx, "group", team.ID, "dev", "developer")
75 _ = st.PutSecret(ctx, "group", team.ID, "TOKEN", "x", false)
76 // developer mag niet
77 if code, _ := call(http.MethodDelete, "/api/v1/groups/team", devTok, `{"confirm":"Team"}`); code != 403 {
78 t.Fatalf("dev: %d", code)
79 }
80 // met repo: 409 met aantal
81 if code, body := call(http.MethodDelete, "/api/v1/groups/team", adaTok, `{"confirm":"Team"}`); code != 409 || !strings.Contains(body, "1") {
82 t.Fatalf("met repo: %d %s", code, body)
83 }
84 app, _ := st.RepoByPath(ctx, "team", "app")
85 if code, _ := call(http.MethodDelete, "/api/v1/projects/team/app", adaTok, `{"confirm":"`+app.Name+`"}`); code != 204 {
86 t.Fatalf("repo weg: %d", code)
87 }
88 // verkeerde bevestiging
89 if code, _ := call(http.MethodDelete, "/api/v1/groups/team", adaTok, `{"confirm":"nope"}`); code != 400 {
90 t.Fatalf("confirm: %d", code)
91 }
92 if code, body := call(http.MethodDelete, "/api/v1/groups/team", adaTok, `{"confirm":"team"}`); code != 204 {
93 t.Fatalf("leeg: %d %s", code, body)
94 }
95 if _, err := st.GroupByPath(ctx, "team"); err == nil {
96 t.Fatal("groep bestaat nog")
97 }
98 if list, _ := st.ListSecrets(ctx, "group", team.ID); len(list) != 0 {
99 t.Fatal("groepsgeheimen niet opgeruimd")
100 }
101 if m, _ := st.ListMembers(ctx, "group", team.ID); len(m) != 0 {
102 t.Fatal("leden niet opgeruimd")
103 }
104}