-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
184 lines (160 loc) · 3.86 KB
/
server_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
//go:build integration
package main
import (
"context"
"encoding/json"
"io"
"net/http"
"testing"
"time"
"github.com/shopwarelabs/jira-issue-bot/domain/search"
"github.com/shopwarelabs/jira-issue-bot/infrastructure/config"
"github.com/steinfletcher/apitest"
)
// Not possible to split into multiple tests, as by default go executes the tests in parallel
func TestServerIntegration(t *testing.T) {
ctx := context.Background()
cfg, _ := config.NewFromEnv(ctx)
createServer(cfg, ctx)
// test UI serving
apitest.New().Handler(http.DefaultServeMux).
Get("/app/").
Expect(t).
Status(http.StatusOK).
BodyFromFile("ui/dist/index.html").
End()
// index one jira issue
apitest.New().Handler(http.DefaultServeMux).
Post("/webhook/jira").
Body(`{
"webhookEvent": "jira:issue_updated",
"issue": {
"key": "NEXT-1",
"fields": {
"summary": "Test Issue",
"description": "This is a test issue",
"issuetype": {
"name": "Bug"
},
"project": {
"key": "NEXT"
},
"status": {
"name": "Open"
},
"reporter": {
"displayName": "Author"
}
}
}
}`).
Expect(t).
Status(http.StatusOK).
End()
// index one github issue
apitest.New().Handler(http.DefaultServeMux).
Post("/webhook/github").
Header("X-GitHub-Event", "issues").
Body(`{
"action": "edited",
"issue": {
"number": 1,
"title": "Github Issues are fancy",
"body": "Fancy that github supports webhook issues",
"state": "open",
"labels": [
{
"name": "bug"
}
],
"user": {
"login": "author"
},
"created_at": "2021-01-01T00:00:00Z"
}
}`).
Expect(t).
Status(http.StatusOK).
End()
// wait for indexing to finish
time.Sleep(1 * time.Second)
// search per API
searchResult := apitest.New().Handler(http.DefaultServeMux).
Post("/api/search").
Query("title", "Fancy github webhook").
Expect(t).
Status(http.StatusOK).
End()
body, err := io.ReadAll(searchResult.Response.Body)
defer searchResult.Response.Body.Close()
if err != nil {
t.Fatal(err)
}
var result search.SearchResponse
if err := json.Unmarshal(body, &result); err != nil {
t.Fatal(err)
}
if result.Hits.Total.Value != 1 {
t.Fatalf("expected 1 hit, got %d", result.Hits.Total.Value)
}
if result.Hits.Hits[0].ID != "GH-1" {
t.Fatalf("expected GH-1, got %s", result.Hits.Hits[0].ID)
}
if result.Hits.Hits[0].Source.Title != "Github Issues are fancy" {
t.Fatalf("expected Github Issues are fancy, got %s", result.Hits.Hits[0].Source.Title)
}
// index duplicate to first jira issue
apitest.New().Handler(http.DefaultServeMux).
Post("/webhook/jira").
Body(`{
"webhookEvent": "jira:issue_updated",
"issue": {
"key": "NEXT-2",
"fields": {
"summary": "Test Issue",
"description": "This is a test issue",
"issuetype": {
"name": "Bug"
},
"project": {
"key": "NEXT"
},
"status": {
"name": "Open"
},
"reporter": {
"displayName": "Author"
}
}
}
}`).
Expect(t).
Status(http.StatusOK).
End()
// wait for indexing to finish
time.Sleep(1 * time.Second)
// search per ID
searchResult = apitest.New().Handler(http.DefaultServeMux).
Post("/api/search-id").
Query("id", "NEXT-1").
Expect(t).
Status(http.StatusOK).
End()
body, err = io.ReadAll(searchResult.Response.Body)
defer searchResult.Response.Body.Close()
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(body, &result); err != nil {
t.Fatal(err)
}
if result.Hits.Total.Value != 1 {
t.Fatalf("expected 1 hit, got %d", result.Hits.Total.Value)
}
if result.Hits.Hits[0].ID != "NEXT-2" {
t.Fatalf("expected NEXT-2, got %s", result.Hits.Hits[0].ID)
}
if result.Hits.Hits[0].Source.Title != "Test Issue" {
t.Fatalf("expected Test Issue, got %s", result.Hits.Hits[0].Source.Title)
}
}