-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathe2e_test.go
More file actions
198 lines (164 loc) · 5.43 KB
/
e2e_test.go
File metadata and controls
198 lines (164 loc) · 5.43 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package git
import (
"context"
"log/slog"
"os"
"testing"
"time"
"github.com/gkampitakis/go-snaps/snaps"
"github.com/picosh/git-pr/fixtures"
"github.com/picosh/git-pr/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)
suite.userKey.MustCmd(suite.patch, "register")
suite.adminKey.MustCmd(suite.patch, "register")
t.Log("User cannot create repo")
_, err := suite.userKey.Cmd(suite.patch, "pr create test")
if err == nil {
t.Fatal("user should not be able to create a PR")
}
suite.adminKey.MustCmd(suite.patch, "pr create test")
t.Log("User should be able to create a patch")
suite.userKey.MustCmd(suite.patch, "pr create test")
t.Log("Snapshot test ls command")
actual, err := suite.userKey.Cmd(nil, "pr ls")
bail(err)
snaps.MatchSnapshot(t, actual)
}
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)
suite.userKey.MustCmd(suite.patch, "register")
suite.adminKey.MustCmd(suite.patch, "register")
t.Log("Admin should be able to create a repo")
suite.adminKey.MustCmd(nil, "repo create test")
t.Log("Accepted pr")
suite.userKey.MustCmd(suite.patch, "pr create admin/test")
suite.userKey.MustCmd(nil, "pr edit 1 Accepted patch")
_, err := suite.userKey.Cmd(nil, "pr accept 1")
if err == nil {
t.Fatal("contrib should not be able to accept their own PR")
}
suite.adminKey.MustCmd(nil, "pr accept 1")
t.Log("Closed pr (admin)")
suite.userKey.MustCmd(suite.patch, "pr create test")
suite.userKey.MustCmd(nil, "pr edit 2 Closed patch (admin)")
suite.adminKey.MustCmd(nil, "pr close 2")
t.Log("Closed pr (contributor)")
suite.userKey.MustCmd(suite.patch, "pr create test")
suite.userKey.MustCmd(nil, "pr edit 3 Closed patch (contributor)")
suite.userKey.MustCmd(nil, "pr close 3")
t.Log("Reviewed pr")
suite.userKey.MustCmd(suite.patch, "pr create test")
suite.userKey.MustCmd(nil, "pr edit 4 Reviewed patch")
suite.adminKey.MustCmd(suite.otherPatch, "pr add --review 4")
t.Log("Accepted pr with review")
suite.userKey.MustCmd(suite.patch, "pr create test")
suite.userKey.MustCmd(nil, "pr edit 5 Accepted patch with review")
suite.adminKey.MustCmd(suite.otherPatch, "pr add --accept 5")
t.Log("Closed pr with review")
suite.userKey.MustCmd(suite.patch, "pr create test")
suite.userKey.MustCmd(nil, "pr edit 6 Closed patch with review")
suite.adminKey.MustCmd(suite.otherPatch, "pr add --close 6")
t.Log("Create pr with user repo and user can accept")
suite.userKey.MustCmd(nil, "repo create ai")
suite.adminKey.MustCmd(suite.patch, "pr create contributor/ai")
suite.userKey.MustCmd(suite.otherPatch, "pr accept 7")
t.Log("Create pr with admin repo and admin can accept")
suite.adminKey.MustCmd(nil, "repo create ai")
suite.userKey.MustCmd(suite.patch, "pr create admin/ai")
suite.adminKey.MustCmd(suite.otherPatch, "pr add --accept 8")
t.Log("Create pr with admin repo and user can accept with comment")
suite.adminKey.MustCmd(nil, "repo create ai")
suite.userKey.MustCmd(suite.patch, "pr create admin/ai")
suite.adminKey.MustCmd([]byte("nice work"), "pr accept --comment 9")
t.Log("Create pr with default `bin` repo")
actual, err := suite.userKey.Cmd(suite.patch, "pr create")
bail(err)
snaps.MatchSnapshot(t, actual)
t.Log("Snapshot test ls command")
actual, err = suite.userKey.Cmd(nil, "pr ls")
bail(err)
snaps.MatchSnapshot(t, actual)
t.Log("Snapshot test logs command")
actual, err = suite.userKey.Cmd(nil, "logs --repo admin/ai")
bail(err)
snaps.MatchSnapshot(t, actual)
t.Log("Delete repo")
suite.userKey.MustCmd(nil, "repo rm --write ai")
t.Log("Snapshot test ls command with ai prs removed")
actual, err = suite.userKey.Cmd(nil, "pr ls")
bail(err)
snaps.MatchSnapshot(t, actual)
}
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"
create_repo = "admin"`
var cfgMultiTenantTmpl = `
url = "localhost"
data_dir = %q
admins = [%q]
time_format = "01/02/2006 15:04:05 07:00"
create_repo = "user"`