-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
132 lines (122 loc) · 3.44 KB
/
main_test.go
File metadata and controls
132 lines (122 loc) · 3.44 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
/*
Copyright 2020 Keyporttech Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the Licens}ze at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"net/http"
"testing"
"encoding/json"
"io/ioutil"
)
type GiteaEvent struct {
secret string `json:"secret"`
}
// tests validate payload
func TestValidatePayload(t *testing.T) {
//load the sample request from files
file, _ := ioutil.ReadFile("testEvent.json")
giteaEvent := GiteaEvent{}
_ = json.Unmarshal([]byte(file), &giteaEvent)
defaultBody := string(file)
const defaultSignature = "1887c76452d0b1b4798a775ca9f9e62d57ae535708e089d6674aef4fb8de3e89"
secretKey := []byte("YOUR_secret")
validTests := []struct {
signature string
eventID string
event string
wantEventID string
wantEvent string
wantPayload string
}{
// The following tests generate expected errors:
//{signature: "yo"}, // Signature not hex string
//{signature: "012345"}, // Invalid signature
{
signature: defaultSignature,
eventID: "ping test",
event: "ping",
wantEventID: "ping test",
wantEvent: "ping",
wantPayload: defaultBody,
},
{
signature: defaultSignature,
eventID: "push test",
event: "push",
wantEvent: "push",
wantPayload: defaultBody,
},
{
signature: defaultSignature,
eventID: "pull_request",
event: "pull_request",
wantEvent: "pull_request",
wantPayload: defaultBody,
},
}
// invalidTests := []struct {
// signature string
// eventID string
// event string
// wantEventID string
// wantEvent string
// wantPayload string
// }{
// // The following tests generate expected errors:
// {}, // Missing signature
// {signature: "yo"}, // Signature not hex string
// {signature: "012345"}, // Invalid signature
// // The following tests expect err=nil:
// {
// signature: defaultSignature,
// eventID: "caesar-salad",
// event: "ping",
// wantEventID: "caesar-salad",
// wantEvent: "ping",
// wantPayload: defaultBody,
// },
// {
// signature: defaultSignature,
// event: "ping",
// wantEvent: "ping",
// wantPayload: defaultBody,
// },
// {
// signature: "b1f8020f5b4cd42042f807dd939015c4a418bc1ff7f604dd55b0a19b5d953d9b",
// event: "ping",
// wantEvent: "ping",
// wantPayload: defaultBody,
// },
// }
for _, test := range validTests {
buf := bytes.NewBufferString(defaultBody)
req, err := http.NewRequest("GET", "http://localhost/event", buf)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
if test.signature != "" {
req.Header.Set(signatureHeader, test.signature)
}
req.Header.Set("Content-Type", "application/json")
got, err := ValidatePayload(req, secretKey)
if err != nil {
if test.wantPayload != "" {
t.Errorf("err = %v", err)
}
continue
}
if string(got) != test.wantPayload {
t.Errorf("Event Id: %s - ValidatePayload = %q, want %q", test.eventID, got, test.wantPayload)
}
}
}