Repository navigation
Expand file tree
/
Copy pathe2e_test.go
More file actions
159 lines (131 loc) · 4.22 KB
/
Copy pathe2e_test.go
File metadata and controls
159 lines (131 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package patchbin
import (
"context"
"log/slog"
"os"
"strings"
"testing"
"time"
"github.com/picosh/patchbin/fixtures"
"github.com/picosh/patchbin/util"
)
func TestE2E(t *testing.T) {
testSingleTenantE2E(t)
testMultiTenantE2E(t)
}
func testSingleTenantE2E(t *testing.T) {
t.Log("single tenant end-to-end tests")
dataDir := util.CreateTmpDir()
defer func() {
_ = os.RemoveAll(dataDir)
}()
suite := setupTest(dataDir, cfgSingleTenantTmpl)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := GitSshServer(ctx, suite.cfg)
go func() {
_ = s.ListenAndServe()
}()
// Hack to wait for startup
time.Sleep(time.Millisecond * 100)
// Users are auto-created on first use, no registration needed
t.Log("User should be able to create a PR")
suite.userKey.MustCmd(suite.patch, "push test:single-tenant")
t.Log("Admin should also be able to create a PR via shorthand")
suite.adminKey.MustCmd(suite.patch, "test:admin-single-tenant")
t.Log("List PRs")
suite.userKey.MustCmd(nil, "ls")
}
func testMultiTenantE2E(t *testing.T) {
t.Log("multi tenant end-to-end tests")
dataDir := util.CreateTmpDir()
defer func() {
_ = os.RemoveAll(dataDir)
}()
suite := setupTest(dataDir, cfgMultiTenantTmpl)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := GitSshServer(ctx, suite.cfg)
go func() {
_ = s.ListenAndServe()
}()
time.Sleep(time.Millisecond * 100)
// Users are auto-created on first use, no registration needed
// In zero-trust model, no repo creation needed
// Anyone can create PRs in any repo
t.Log("User creates PR")
output := suite.userKey.MustCmd(suite.patch, "push test:my-pr")
if !strings.Contains(output, "PR test:my-pr created") {
t.Fatalf("unexpected create output: %s", output)
}
t.Log("User edits PR title (only creator can edit)")
suite.userKey.MustCmd(nil, "edit test:my-pr Updated title")
t.Log("Admin creates PR")
suite.adminKey.MustCmd(suite.patch, "push admin-repo:admin-pr")
t.Log("Admin adds patchset to user's PR (zero-trust: anyone can add)")
suite.adminKey.MustCmd(suite.otherPatch, "push test:my-pr")
t.Log("Admin comments on user's PR")
commentOutput := suite.adminKey.MustCmd([]byte("LGTM! Great work on this PR.\n"), "comment test:my-pr")
if !strings.Contains(commentOutput, "Comment added to PR test:my-pr") {
t.Fatalf("unexpected comment output: %s", commentOutput)
}
t.Log("User creates another PR")
suite.userKey.MustCmd(suite.patch, "push other-repo:other-pr")
t.Log("List PRs")
suite.userKey.MustCmd(nil, "ls")
t.Log("View event logs")
logsOutput := suite.userKey.MustCmd(nil, "logs")
if !strings.Contains(logsOutput, "pr_commented") {
t.Fatalf("expected pr_commented in event logs, got: %s", logsOutput)
}
t.Log("View PR cover letter to verify comment in discussion via pull")
pullOutput := suite.userKey.MustCmd(nil, "pull test:my-pr")
if !strings.Contains(pullOutput, "LGTM! Great work on this PR.") {
t.Fatalf("expected comment in cover letter discussion, got: %s", pullOutput)
}
t.Log("Verify shorthand pull works")
shorthandPull := suite.userKey.MustCmd(nil, "test:my-pr.patch")
if !strings.Contains(shorthandPull, "LGTM! Great work on this PR.") {
t.Fatalf("expected comment in shorthand pull, got: %s", shorthandPull)
}
}
type TestSuite struct {
cfg *GitCfg
userKey util.UserSSH
adminKey util.UserSSH
patch []byte
otherPatch []byte
}
func setupTest(dataDir string, cfgTmpl string) TestSuite {
opts := &slog.HandlerOptions{
AddSource: true,
}
logger := slog.New(
slog.NewTextHandler(os.Stdout, opts),
)
adminKey, userKey := util.GenerateKeys()
cfgPath := util.CreateCfgFile(dataDir, cfgTmpl, adminKey)
LoadConfigFile(cfgPath, logger)
cfg := NewGitCfg(logger)
// so outputs dont show dates
cfg.TimeFormat = ""
patch, err := fixtures.Fixtures.ReadFile("single.patch")
if err != nil {
panic(err)
}
otherPatch, err := fixtures.Fixtures.ReadFile("with-cover.patch")
if err != nil {
panic(err)
}
return TestSuite{cfg, userKey, adminKey, patch, otherPatch}
}
var cfgSingleTenantTmpl = `
url = "localhost"
data_dir = %q
admins = [%q]
time_format = "01/02/2006 15:04:05 07:00"`
var cfgMultiTenantTmpl = `
url = "localhost"
data_dir = %q
admins = [%q]
time_format = "01/02/2006 15:04:05 07:00"`