This repository has been archived by the owner on Jun 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_test.go
146 lines (129 loc) · 2.89 KB
/
github_test.go
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
package main
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/labstack/echo/v4"
)
func TestNewGitHubAppsManifest(t *testing.T) {
url, err := url.Parse("http://example.com/foo/bar?q=v")
if err != nil {
t.Fatal(err)
}
manifest := newGitHubAppsManifest("test", *url, "/api/webhook")
if manifest.Name != "test" {
t.Fatal(manifest.Name)
}
if manifest.Url != "http://example.com/foo/bar" {
t.Fatal(manifest.Url)
}
if manifest.Url != manifest.RedirectUrl {
t.Fatal(manifest.RedirectUrl)
}
if manifest.HookAttrs.Url != "http://example.com/api/webhook" {
t.Fatal(manifest.HookAttrs.Url)
}
}
type testenv struct {
env
baseUrl string
}
func (e *testenv) gitHubBaseUrl() *string {
return &e.baseUrl
}
func newTestenv(c *httptest.Server) env {
e := newEnv()
url := c.URL
return &testenv{
env: e,
baseUrl: url,
}
}
func TestCompleteAppManifest(t *testing.T) {
cases := []struct {
name string
status int
haserr bool
}{
{
name: "ok",
status: 201,
haserr: false,
},
{
name: "notfound",
status: 404,
haserr: true,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
dummy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(c.status)
}))
defer dummy.Close()
env := newTestenv(dummy)
result := gitHubAppsManifestResult{}
_, err := completeAppManifest(context.Background(), env, &result)
if c.haserr != (err != nil) {
t.Fatal(err)
}
})
}
}
type testValidatePayloadEnv struct {
env
wbsec string
}
func (t *testValidatePayloadEnv) webhookSecret() []byte {
return []byte(t.wbsec)
}
func TestValidatePayload(t *testing.T) {
cases := []struct {
name string
secret string
payload string
requestsignature string
status int
}{
{
name: "ok",
secret: "secret",
payload: "empty",
requestsignature: "sha256=9051c49f2eed4cfe8125cba47a275e1460402ad2a4bc94d417fd1e10d9c55339",
status: 200,
},
{
name: "bad",
secret: "secret",
payload: "empty",
requestsignature: "sha256=xxxx",
status: 400,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
env := testValidatePayloadEnv{
env: &defaultEnv{},
wbsec: c.secret,
}
e := echo.New()
e.Use(injectEnv(&env))
e.Use(validatePayload)
e.GET("/", func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("GET", "/", bytes.NewBufferString(c.payload))
req.Header.Set("X-Hub-Signature-256", c.requestsignature)
req.Header.Set("Content-Type", "application/json")
res := httptest.NewRecorder()
e.ServeHTTP(res, req)
if res.Result().StatusCode != c.status {
t.Fatalf("%d: %s", res.Result().StatusCode, res.Body.String())
}
})
}
}