-
Notifications
You must be signed in to change notification settings - Fork 0
/
jira_test.go
292 lines (250 loc) · 7.21 KB
/
jira_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package jiragoli
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestNewJIRA(t *testing.T) {
server, _ := runJIRATestServer()
defer server.Close()
// Server that always sends 404 Not Found
badServer := httptest.NewServer(&BADJIRATestServer{})
defer badServer.Close()
// Server that always sends invalid JSON
faultyServer := httptest.NewServer(&FaultyJIRATestServer{})
defer faultyServer.Close()
for _, test := range []struct {
serverURL string
hasError bool
}{
// Ensure that url Parse fails here for blank server scheme
{"://jira.com", true},
// Ensure that auth fails here
{"htp://jira.com", true},
// This URL should always generate non-200 status code
{badServer.URL, true},
// This should always send a faulty JSON back
{faultyServer.URL, true},
// This should work perfectly fine and thus no error is
// expected here
{server.URL, false},
} {
jira, err := NewJIRA(nil, test.serverURL, nil)
switch {
case test.hasError:
switch err {
case nil:
t.Fatalf("expected error here for server: %s", test.serverURL)
default:
// This is expected behavior here
t.Logf("expected error here: %s for server: %s", err, test.serverURL)
continue
}
case !test.hasError && err != nil:
t.Fatalf("initiating a new JIRA client failed: %s", err)
}
// Ensure the project list is sorted by name
var prevProjectName string
for _, project := range jira.Projects() {
if prevProjectName == "" {
prevProjectName = project.Name
continue
}
if prevProjectName > project.Name {
t.Errorf("expected %q to be after %q in the project order",
prevProjectName,
project.Name)
}
prevProjectName = project.Name
}
}
}
func TestIssues(t *testing.T) {
server, _ := runJIRATestServer()
defer server.Close()
jira, err := NewJIRA(nil, server.URL, nil)
if err != nil {
t.Fatalf("initiating a new JIRA client failed: %s", err)
}
// Server that always sends 404 Not Found
badServer := httptest.NewServer(&BADJIRATestServer{})
defer badServer.Close()
// Server that always sends invalid JSON
faultyServer := httptest.NewServer(&FaultyJIRATestServer{})
defer faultyServer.Close()
for _, test := range []struct {
serverURL string
idStr interface{}
lenIssues int
hasError bool
}{
// Test for the case where the project exists in JIRA and has at least
// one issue for it
{server.URL, 101, 1, false},
// Test for the case where the project exists in JIRA and has at least
// one issue for it and we pass in the project name instead of id
{server.URL, "projectA", 1, false},
// Test for the case where the project exists in JIRA but has no issues
// assigned to it
{server.URL, 102, 0, true},
// Test for the case where the project id sent is not a string or an int
{server.URL, true, 0, true},
// Test for the case where response doesn't return a 200OK, it should throw
// an error
{badServer.URL, 101, 0, true},
// Test when client.Get itself fails
{"htp://jira.com", 101, 0, true},
// Test for when json encoded data fails on you
{faultyServer.URL, 101, 0, true},
} {
jira.endpoint, _ = url.Parse(test.serverURL)
issues, err := jira.Issues(test.idStr)
switch {
case test.hasError:
switch err {
case nil:
t.Errorf("expected error for issues for id: %v", test.idStr)
default:
t.Logf("expected error here: %q for server: %q", err, test.serverURL)
continue
}
case !test.hasError && err != nil:
t.Errorf("unexpected error %q for id: %v", err, test.idStr)
}
if len(issues) != test.lenIssues {
t.Errorf("expected %d issues, actual issues: %d",
test.lenIssues,
len(issues))
}
}
}
// runJIRATestServer is responsible for creating a stub of JIRA
// API service and help test the following endpoints:
//
// 1. "/projects"
// 2. "/search"
//
// It returns a closer func that can be used to close the test
// server once the test run completes using it.
func runJIRATestServer() (*httptest.Server, *JIRATestServer) {
jiraTestServer := &JIRATestServer{
[]*JIRAProject{
// {id, key, name} //
{"100", "PA", "ProjectA"},
{"101", "PB", "ProjectB"},
{"102", "PC", "ProjectC"},
},
map[string]*JIRAIssue{
"100": {
Key: "PA",
Summary: "Some ProjectA issue",
Description: "Some ProjectA description",
Labels: []string{"labelA"},
Assignee: JIRAUser{
Name: "UserA",
Email: "userA@jira.com",
UserURL: "https://jira.com/userA",
},
Reporter: JIRAUser{
Name: "UserB",
Email: "userB@jira.com",
UserURL: "https://jira.com/userB",
},
JIRAStatus: JIRAStatus{
Name: "Open",
Description: "Open Ticket",
},
},
"101": {
Key: "PB",
Summary: "Some ProjectB issue",
Description: "Some ProjectB description",
Labels: []string{"labelB1", "labelB2"},
Assignee: JIRAUser{
Name: "UserB",
Email: "userB@jira.com",
UserURL: "https://jira.com/userB",
},
Reporter: JIRAUser{
Name: "UserA",
Email: "userA@jira.com",
UserURL: "https://jira.com/userA",
},
JIRAStatus: JIRAStatus{
Name: "In Progress",
Description: "Ticket is being reviewed",
},
},
},
}
return httptest.NewServer(jiraTestServer), jiraTestServer
}
type JIRATestServer struct {
projects JIRAProjects
issues map[string]*JIRAIssue
}
func (j *JIRATestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
path := r.URL.Path
switch {
case strings.HasSuffix(path, "/project"):
j.serveJIRAProjects(w, r)
case strings.HasSuffix(path, "/search"):
j.serveJIRASearch(w, r)
}
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
// serveJIRAProjects is a dummy /projects endpoint that replies
// with a list of projects that a list of issues would be associated
// with.
func (j *JIRATestServer) serveJIRAProjects(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(j.projects)
}
func (j *JIRATestServer) serveJIRASearch(w http.ResponseWriter, r *http.Request) {
var projectID string
val := r.FormValue("jql")
switch n, err := fmt.Sscanf(val, "project=%s", &projectID); {
case err != nil:
http.Error(w, fmt.Sprintf("%s: input:%q", err, val), http.StatusBadRequest)
case n != 1:
http.Error(w, fmt.Sprintf("expected 1 item parsed, actual parsed: %d", n), http.StatusBadRequest)
}
issue, ok := j.issues[projectID]
if !ok {
http.Error(w, "no such project found", http.StatusNotFound)
return
}
type Issues struct {
Key string `json:"key"`
JIRAIssue *JIRAIssue `json:"fields"`
}
issuesResponse := struct {
Issues []Issues `json:"issues"`
}{
[]Issues{
{issue.Key, issue},
},
}
_ = json.NewEncoder(w).Encode(issuesResponse)
}
type BADJIRATestServer struct {
// nothing here yet
}
func (j *BADJIRATestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not found", http.StatusNotFound)
}
type FaultyJIRATestServer struct {
// nothing here yet
}
func (j *FaultyJIRATestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// This should send a broken json for every request
fmt.Fprintf(w, "{")
w.Header().Set("Content-type", "application/json")
}