forked from abiosoft/caddy-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbucket_hook_test.go
98 lines (85 loc) · 1.97 KB
/
bitbucket_hook_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
package git
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestBitbucketDeployPush(t *testing.T) {
repo := &Repo{Branch: "master", Hook: HookConfig{URL: "/bitbucket_deploy"}}
bbHook := BitbucketHook{}
for i, test := range []struct {
ip string
body string
event string
responseBody string
code int
}{
{"131.103.20.192", "", "", "", 403},
{"131.103.20.160", "", "", "", 400},
{"131.103.20.160", "", "repo:push", "", 400},
{"131.103.20.160", pushBBBodyValid, "repo:push", "", 200},
{"131.103.20.165", pushBBBodyValid, "repo:push", "", 200},
{"131.103.20.160", pushBBBodyEmptyBranch, "repo:push", "", 400},
{"131.103.20.160", pushBBBodyDeleteBranch, "repo:push", "", 400},
} {
req, err := http.NewRequest("POST", "/bitbucket_deploy", bytes.NewBuffer([]byte(test.body)))
if err != nil {
t.Fatalf("Test %v: Could not create HTTP request: %v", i, err)
}
req.RemoteAddr = test.ip
if test.event != "" {
req.Header.Add("X-Event-Key", test.event)
}
rec := httptest.NewRecorder()
code, err := bbHook.Handle(rec, req, repo)
if code != test.code {
t.Errorf("Test %d: Expected response code to be %d but was %d", i, test.code, code)
}
if rec.Body.String() != test.responseBody {
t.Errorf("Test %d: Expected response body to be '%v' but was '%v'", i, test.responseBody, rec.Body.String())
}
}
}
var pushBBBodyEmptyBranch = `
{
"push": {
"changes": [
{
"new": {
"type": "branch",
"name": "",
"target": {
"hash": "709d658dc5b6d6afcd46049c2f332ee3f515a67d"
}
}
}
]
}
}
`
var pushBBBodyValid = `
{
"push": {
"changes": [
{
"new": {
"type": "branch",
"name": "master",
"target": {
"hash": "709d658dc5b6d6afcd46049c2f332ee3f515a67d"
}
}
}
]
}
}
`
var pushBBBodyDeleteBranch = `
{
"push": {
"changes": [
]
}
}
`