-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_test.go
More file actions
253 lines (229 loc) · 8 KB
/
Copy pathgithub_test.go
File metadata and controls
253 lines (229 loc) · 8 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func testKeyPEM(t *testing.T) string {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
return string(pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}))
}
// fakeGitHub records the call sequence and answers the four endpoints the PR
// flow uses.
type fakeGitHub struct {
t *testing.T
calls []string
tokenCalls int
installationCalls int
bodies map[string]map[string]any
failOn string
}
func (f *fakeGitHub) handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := r.Method + " " + r.URL.Path
f.calls = append(f.calls, route)
if f.bodies == nil {
f.bodies = map[string]map[string]any{}
}
if payload, err := io.ReadAll(r.Body); err == nil && len(payload) > 0 {
var parsed map[string]any
if json.Unmarshal(payload, &parsed) == nil {
f.bodies[route] = parsed
}
}
if f.failOn == route {
w.WriteHeader(http.StatusUnprocessableEntity)
io.WriteString(w, `{"message":"Reference already exists"}`)
return
}
switch {
case strings.HasSuffix(r.URL.Path, "/installation"):
f.installationCalls++
assertAppJWT(f.t, r.Header.Get("Authorization"))
json.NewEncoder(w).Encode(map[string]any{"id": 987654})
case strings.HasSuffix(r.URL.Path, "/access_tokens"):
f.tokenCalls++
assertAppJWT(f.t, r.Header.Get("Authorization"))
json.NewEncoder(w).Encode(map[string]any{
"token": "ghs_installation_token",
"expires_at": time.Now().Add(time.Hour),
})
case strings.Contains(r.URL.Path, "/git/ref/heads/"):
if got := r.Header.Get("Authorization"); got != "Bearer ghs_installation_token" {
f.t.Errorf("Authorization = %q, want the installation token", got)
}
json.NewEncoder(w).Encode(map[string]any{
"object": map[string]any{"sha": "basesha123"},
})
case strings.HasSuffix(r.URL.Path, "/pulls"):
json.NewEncoder(w).Encode(map[string]any{
"html_url": "https://github.com/Makespace/makespace-site/pull/7",
})
default:
json.NewEncoder(w).Encode(map[string]any{"ok": true})
}
})
}
func assertAppJWT(t *testing.T, header string) {
t.Helper()
token, ok := strings.CutPrefix(header, "Bearer ")
if !ok {
t.Fatalf("Authorization = %q, want a Bearer JWT", header)
}
parts := strings.Split(token, ".")
if len(parts) != 3 {
t.Fatalf("JWT has %d segments, want 3", len(parts))
}
claims, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
t.Fatalf("decoding claims: %v", err)
}
var got struct {
ISS string `json:"iss"`
IAT int64 `json:"iat"`
EXP int64 `json:"exp"`
}
if err := json.Unmarshal(claims, &got); err != nil {
t.Fatalf("parsing claims: %v", err)
}
// GitHub takes the client ID as the JWT issuer.
if got.ISS != "Iv23liQpEXAMPLE" {
t.Errorf("iss = %q, want the client id", got.ISS)
}
if got.IAT > time.Now().Unix() {
t.Error("iat is in the future; GitHub rejects that on clock skew")
}
// GitHub refuses an expiry more than ten minutes out.
if d := time.Until(time.Unix(got.EXP, 0)); d <= 0 || d > 10*time.Minute {
t.Errorf("exp is %v away, want between 0 and 10 minutes", d)
}
}
func testApp(t *testing.T, fake *fakeGitHub) (*githubApp, *httptest.Server) {
t.Helper()
srv := httptest.NewServer(fake.handler())
t.Cleanup(srv.Close)
app, err := newGitHubApp("Iv23liQpEXAMPLE", testKeyPEM(t), "Makespace", "makespace-site", "main")
if err != nil {
t.Fatal(err)
}
app.api = srv.URL
return app, srv
}
func TestOpenPullRequestCallSequence(t *testing.T) {
fake := &fakeGitHub{t: t}
app, _ := testApp(t, fake)
url, err := app.OpenPullRequest(context.Background(),
"content/makes/a-shelf.md", "---\ntitle: 'A Shelf'\n---\n", "Add make: A Shelf", "Submitted by Ada L")
if err != nil {
t.Fatalf("OpenPullRequest: %v", err)
}
if url != "https://github.com/Makespace/makespace-site/pull/7" {
t.Errorf("url = %q", url)
}
want := []string{
// The installation is discovered from the repo rather than configured.
"GET /repos/Makespace/makespace-site/installation",
"POST /app/installations/987654/access_tokens",
"GET /repos/Makespace/makespace-site/git/ref/heads/main",
"POST /repos/Makespace/makespace-site/git/refs",
"PUT /repos/Makespace/makespace-site/contents/content/makes/a-shelf.md",
"POST /repos/Makespace/makespace-site/pulls",
}
if strings.Join(fake.calls, "\n") != strings.Join(want, "\n") {
t.Errorf("calls =\n%s\nwant\n%s", strings.Join(fake.calls, "\n"), strings.Join(want, "\n"))
}
ref, _ := fake.bodies["POST /repos/Makespace/makespace-site/git/refs"]["ref"].(string)
if !strings.HasPrefix(ref, "refs/heads/submit/makes-a-shelf-") {
t.Errorf("branch ref = %q, want a timestamped submit/ branch", ref)
}
if sha, _ := fake.bodies["POST /repos/Makespace/makespace-site/git/refs"]["sha"].(string); sha != "basesha123" {
t.Errorf("branch sha = %q, want the base sha", sha)
}
commit := fake.bodies["PUT /repos/Makespace/makespace-site/contents/content/makes/a-shelf.md"]
encoded, _ := commit["content"].(string)
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
t.Fatalf("commit content is not base64: %v", err)
}
if !strings.Contains(string(decoded), "title: 'A Shelf'") {
t.Errorf("commit content = %q", decoded)
}
if branch, _ := commit["branch"].(string); !strings.HasPrefix(branch, "submit/makes-a-shelf-") {
t.Errorf("commit branch = %q, want the new branch", branch)
}
pr := fake.bodies["POST /repos/Makespace/makespace-site/pulls"]
if base, _ := pr["base"].(string); base != "main" {
t.Errorf("pull request base = %q, want main", base)
}
}
// The installation token lasts an hour, so a second submission must reuse it
// rather than signing a fresh JWT every time.
func TestInstallationTokenIsCached(t *testing.T) {
fake := &fakeGitHub{t: t}
app, _ := testApp(t, fake)
for range 2 {
if _, err := app.OpenPullRequest(context.Background(), "content/makes/x.md", "body", "t", "b"); err != nil {
t.Fatal(err)
}
}
if fake.tokenCalls != 1 {
t.Errorf("minted %d tokens, want 1", fake.tokenCalls)
}
// The installation cannot change without the app being reinstalled, so one
// lookup per process is enough.
if fake.installationCalls != 1 {
t.Errorf("looked up the installation %d times, want 1", fake.installationCalls)
}
}
// A repo the app is not installed on must fail with something that points at
// the cause rather than at a missing environment variable.
func TestInstallationLookupFailureIsExplained(t *testing.T) {
fake := &fakeGitHub{t: t, failOn: "GET /repos/Makespace/makespace-site/installation"}
app, _ := testApp(t, fake)
_, err := app.OpenPullRequest(context.Background(), "content/makes/x.md", "body", "t", "b")
if err == nil {
t.Fatal("OpenPullRequest succeeded without an installation")
}
if !strings.Contains(err.Error(), "is the app installed there?") {
t.Errorf("error = %v, want it to name the likely cause", err)
}
}
func TestOpenPullRequestSurfacesAPIErrors(t *testing.T) {
fake := &fakeGitHub{t: t, failOn: "POST /repos/Makespace/makespace-site/git/refs"}
app, _ := testApp(t, fake)
_, err := app.OpenPullRequest(context.Background(), "content/makes/x.md", "body", "t", "b")
if err == nil {
t.Fatal("OpenPullRequest succeeded, want an error")
}
if !strings.Contains(err.Error(), "Reference already exists") {
t.Errorf("error = %v, want GitHub's message", err)
}
}
func TestParsePrivateKeyRejectsRubbish(t *testing.T) {
if _, err := newGitHubApp("Iv23liQpEXAMPLE", "not a pem", "o", "r", "main"); err == nil {
t.Error("newGitHubApp accepted a non-PEM key")
}
}
func TestBranchNameIsUniquePerSubmission(t *testing.T) {
at := time.Date(2026, 8, 1, 12, 30, 15, 0, time.UTC)
if got, want := branchName("content/makes/a-shelf.md", at), "submit/makes-a-shelf-20260801-123015"; got != want {
t.Errorf("branchName = %q, want %q", got, want)
}
}