-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgitlab_test.go
189 lines (164 loc) · 4.54 KB
/
gitlab_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/json"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func serveAndCatchJson(t *testing.T, jsonOut interface{}) (*httptest.Server, chan *http.Request) {
reqChan := make(chan *http.Request, 1)
serv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqChan <- r
dec := json.NewDecoder(r.Body)
err := dec.Decode(jsonOut)
if nil != err {
t.Fatal(err)
}
}))
return serv, reqChan
}
func urlMustParse(t *testing.T, urlStr string) *url.URL {
u, err := url.Parse(urlStr)
if nil != err {
t.Fatal(err)
}
return u
}
func TestGetSession(t *testing.T) {
Convey("Given a gitlab server", t, func() {
var reqData sessionRequest
sr, reqChan := serveAndCatchJson(t, &reqData)
u := urlMustParse(t, sr.URL)
g := newGitlab(u.Host)
Convey("When requesting a session (private token)", func() {
g.getSession("user", "password")
Convey("The client should post the correct json to the api", func() {
expectedUrl := fmt.Sprintf("http://%s/api/v3/session", u.Host)
req := <-reqChan
So(req.Method, ShouldEqual, "POST")
So(req.URL.String(), ShouldEqual, expectedUrl)
So(reqData, ShouldResemble, sessionRequest{"user", "password"})
})
})
})
}
func testGetPrivateTokenUrl(t *testing.T) {
Convey("Given a gitlab instance", t, func() {
g := newGitlab("1.2.3.4")
Convey("It should produce a url for obtaining a private token", func() {
So("something", ShouldEqual, "something else")
So(g.getPrivateTokenUrl(), ShouldEqual, "http://1.2.3.4/profile/account")
})
})
}
func TestCreateMergeRequest(t *testing.T) {
Convey("Given a merge request", t, func() {
var mr mergeRequestCreateRequest
sr, reqChan := serveAndCatchJson(t, &mr)
u := urlMustParse(t, sr.URL)
g := newGitlab(u.Host)
g.token = "my-private-token"
Convey("When creating a merge request", func() {
g.createMergeRequest("17", "source-branch", "target-branch", "my title")
Convey("The request should match", func() {
req := <-reqChan
So(req.Method, ShouldEqual, "POST")
So(
req.URL.String(),
ShouldEqual,
fmt.Sprintf("http://%s/api/v3/projects/17/merge_requests?private_token=my-private-token", u.Host),
)
So(
mr,
ShouldResemble,
mergeRequestCreateRequest{
Title: "my title",
SourceBranch: "source-branch",
TargetBranch: "target-branch",
},
)
})
})
})
}
func TestQueryMergeRequests(t *testing.T) {
Convey("Given a gitlab server", t, func() {
var req *http.Request
mrs := []mergeRequest{
mergeRequest{
Id: 13,
Iid: 17,
Title: "my-title",
Description: "my-description",
SourceBranch: "source-branch",
TargetBranch: "target-branch",
},
}
sr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
err := enc.Encode(mrs)
if nil != err {
t.Fatal(err)
}
req = r
}))
u := urlMustParse(t, sr.URL)
g := newGitlab(u.Host)
g.token = "my-private-token"
Convey("When creating a merge request", func() {
gottenMrs, err := g.queryMergeRequests("17", "shuffled")
Convey("The request should match", func() {
So(err, ShouldBeNil)
So(gottenMrs, ShouldResemble, mrs)
So(req.Method, ShouldEqual, "GET")
So(
req.URL.String(),
ShouldEqual,
fmt.Sprintf(
"http://%s/api/v3/projects/17/merge_requests?private_token=my-private-token&state=shuffled",
u.Host,
),
)
})
})
})
}
func TestQueryMergeRequestsErrorMessage(t *testing.T) {
Convey("Given a gitlab server", t, func() {
var req *http.Request
sr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(417)
enc := json.NewEncoder(w)
err := enc.Encode(map[string]string{
"message": "my error message",
})
if nil != err {
t.Fatal(err)
}
req = r
}))
u := urlMustParse(t, sr.URL)
g := newGitlab(u.Host)
g.token = "my-private-token"
Convey("When creating a merge request", func() {
_, err := g.queryMergeRequests("17", "shuffled")
Convey("The request should match", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "my error message")
So(req.Method, ShouldEqual, "GET")
So(
req.URL.String(),
ShouldEqual,
fmt.Sprintf(
"http://%s/api/v3/projects/17/merge_requests?private_token=my-private-token&state=shuffled",
u.Host,
),
)
})
})
})
}