-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubapp_test.go
More file actions
108 lines (95 loc) · 2.33 KB
/
Copy pathgithubapp_test.go
File metadata and controls
108 lines (95 loc) · 2.33 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
package github_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/Pix4D/go-kit/github"
)
func TestGenerateInstallationToken(t *testing.T) {
clientID := "abcd1234"
installationID := 12345
privateKey := generatePrivateKey(t, 2048)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
_, err := fmt.Fprintln(w, "wrong HTTP method")
if err != nil {
t.Fatalf("writing response: %s", err)
}
return
}
claims := decodeJWT(t, r, privateKey)
if claims.Issuer != clientID {
w.WriteHeader(http.StatusUnauthorized)
_, err := fmt.Fprintln(w, "unauthorized: wrong JWT token")
if err != nil {
t.Fatalf("writing response: %s", err)
}
return
}
w.WriteHeader(http.StatusCreated)
_, err := fmt.Fprintln(w, `{"token": "dummy_installation_token"}`)
if err != nil {
t.Fatalf("writing response: %s", err)
}
}
ts := httptest.NewServer(http.HandlerFunc(handler))
defer ts.Close()
gotToken, err := github.GenerateInstallationToken(
ctx,
ts.Client(),
ts.URL,
github.GitHubApp{
ClientId: clientID,
InstallationId: installationID,
PrivateKey: string(encodePrivateKeyToPEM(privateKey)),
},
)
if err != nil {
t.Fatalf("%s\nhave: %v\nwant: %v", "token: error", err, "<no error>")
}
if have, want := gotToken, "dummy_installation_token"; have != want {
t.Fatalf("%s\nhave: %v\nwant: %v", "token", have, want)
}
}
func TestGitHubAppIsZero(t *testing.T) {
type testCase struct {
name string
app github.GitHubApp
want bool
}
run := func(t *testing.T, tc testCase) {
if have, want := tc.app.IsZero(), tc.want; have != want {
t.Fatalf("%s\nhave: %v\nwant: %v", "IsZero", have, want)
}
}
testCases := []testCase{
{
name: "empty app",
app: github.GitHubApp{},
want: true,
},
{
name: "one field set: client-id",
app: github.GitHubApp{ClientId: "client-id"},
want: false,
},
{
name: "all fields set",
app: github.GitHubApp{
ClientId: "client-id",
InstallationId: 12345,
PrivateKey: "dummy-private-key",
},
want: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { run(t, tc) })
}
}