/
helpers_test.go
113 lines (90 loc) · 2.57 KB
/
helpers_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
package pull
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"github.com/google/go-github/github"
)
// Simple test to see if middlewares are run.
func TestRunner(t *testing.T) {
expected := "test"
mw := func(client *github.Client, event *github.PullRequest) {
client.UserAgent = expected
}
handler := New(Configuration{Middlewares: Middlewares{"opened": {mw}}})
event := github.PullRequestEvent{
Action: github.String("opened"),
}
runner(handler, event)
actual := handler.Client.UserAgent
if actual != expected {
t.Errorf("Expected %s, got %s", expected, actual)
}
}
func TestRepoStatus(t *testing.T) {
expected := &github.RepoStatus{
State: github.String("failure"),
TargetURL: github.String("example.com"),
Description: github.String("test"),
Context: github.String("pull/failure"),
}
actual := Failure("example.com", "test", "failure")
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expected %+v, got %+v", expected, actual)
}
expected = &github.RepoStatus{
State: github.String("success"),
TargetURL: github.String("example.com"),
Description: github.String("test"),
Context: github.String("pull/failure"),
}
actual = Success("example.com", "test", "failure")
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expected %+v, got %+v", expected, actual)
}
}
func TestPending(t *testing.T) {
user := "test"
repo := "repo"
number := 1
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client := github.NewClient(nil)
url, _ := url.Parse(server.URL)
client.BaseURL = url
client.UploadURL = url
status := &github.RepoStatus{
State: github.String("pending"),
Description: github.String("Running tests."),
}
statusendpoint := fmt.Sprintf("/repos/%s/%s/statuses/", user, repo)
mux.HandleFunc(statusendpoint, func(w http.ResponseWriter, r *http.Request) {
body := new(github.RepoStatus)
json.NewDecoder(r.Body).Decode(body)
if !reflect.DeepEqual(body, status) {
t.Errorf("Expected %+v, got %+v", status, body)
}
fmt.Fprint(w, `{"id":1}`)
})
commitendpoint := fmt.Sprintf("/repos/%s/%s/pulls/%d/commits", user, repo, number)
mux.HandleFunc(commitendpoint, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `[{"sha": "3", "parents": [{"sha": "2"}]},
{"sha": "2","parents": [{"sha": "1"}]}]`)
})
req := &github.PullRequest{
Number: &number,
Base: &github.PullRequestBranch{
User: &github.User{
Login: &user,
},
Repo: &github.Repository{
Name: &repo,
},
},
}
pending(client, req)
}