-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathauthor_test.go
276 lines (249 loc) · 7.04 KB
/
author_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
package tests
import (
"context"
"fmt"
"net/http"
"strings"
"testing"
"time"
"go.mongodb.org/mongo-driver/mongo"
"github.com/twreporter/go-api/internal/news"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type testAuthor struct {
id, tid primitive.ObjectID
name string
createdAt time.Time
}
func TestGetAuthors_ByKeywords(t *testing.T) {
db, cleanup := setupMongoGoDriverTestDB()
defer cleanup()
defer cleanupAuthorRecords(db)
authors := map[string]testAuthor{
"王小明": {
id: primitive.NewObjectID(),
tid: primitive.NewObjectID(),
name: "王小明",
createdAt: time.Unix(1611817200, 0),
},
"劉大華": {
id: primitive.NewObjectID(),
tid: primitive.NewObjectID(),
name: "劉大華",
createdAt: time.Unix(1611817800, 0),
},
"李明": {
id: primitive.NewObjectID(),
tid: primitive.NewObjectID(),
name: "李明",
createdAt: time.Unix(1611818400, 0),
},
}
// setup records
for _, v := range authors {
migrateAuthorRecord(db, v)
}
response := serveHTTP(http.MethodGet, "/v2/authors?keywords=小明", "", "", "")
assert.Equal(t, http.StatusOK, response.Code)
assert.JSONEq(t, authorListResponse(authorResponse(authors["王小明"])), response.Body.String())
}
func TestGetAuthors_NoContent(t *testing.T) {
response := serveHTTP(http.MethodGet, "/v2/authors", "", "", "")
assert.Equal(t, http.StatusNoContent, response.Code)
}
func TestGetAuthors_NoFurtherContent(t *testing.T) {
db, cleanup := setupMongoGoDriverTestDB()
defer cleanup()
defer cleanupAuthorRecords(db)
authors := map[string]testAuthor{
"王小明": {
id: primitive.NewObjectID(),
tid: primitive.NewObjectID(),
name: "王小明",
createdAt: time.Unix(1611817200, 0),
},
}
// setup records
for _, v := range authors {
migrateAuthorRecord(db, v)
}
response := serveHTTP(http.MethodGet, "/v2/authors?offset=1", "", "", "")
assert.Equal(t, http.StatusNoContent, response.Code)
}
func TestGetAuthorByID_ByValidID(t *testing.T) {
db, cleanup := setupMongoGoDriverTestDB()
defer cleanup()
defer cleanupAuthorRecords(db)
author := testAuthor{
id: primitive.NewObjectID(),
tid: primitive.NewObjectID(),
name: "王小明",
createdAt: time.Unix(1611817200, 0),
}
// setup records
migrateAuthorRecord(db, author)
response := serveHTTP(http.MethodGet, fmt.Sprintf("/v2/authors/%s", author.id.Hex()), "", "", "")
assert.Equal(t, http.StatusOK, response.Code)
assert.JSONEq(t, singleRecordResponse(authorResponse(author)), response.Body.String())
}
func TestGetAuthorByID_ByInvalidID(t *testing.T) {
db, cleanup := setupMongoGoDriverTestDB()
defer cleanup()
defer cleanupAuthorRecords(db)
author := testAuthor{
id: primitive.NewObjectID(),
tid: primitive.NewObjectID(),
name: "王小明",
createdAt: time.Unix(1611817200, 0),
}
// setup records
migrateAuthorRecord(db, author)
response := serveHTTP(http.MethodGet, "/v2/authors/InvalidID", "", "", "")
assert.Equal(t, http.StatusNotFound, response.Code)
}
func cleanupAuthorRecords(db *mongo.Database) {
db.Collection(news.ColContacts).Drop(context.Background())
db.Collection(news.ColImages).Drop(context.Background())
}
func migrateAuthorRecord(db *mongo.Database, author testAuthor) {
contact := createContactDocument(author.id, author.tid, author.name, author.createdAt)
image := createImageDocument(author.tid)
db.Collection(news.ColContacts).InsertOne(context.Background(), contact)
db.Collection(news.ColImages).InsertOne(context.Background(), image)
}
func authorListResponse(authors ...string) string {
return listResponse(len(authors), authors)
}
func authorResponse(author testAuthor) string { // use time as the id generation seed
return fmt.Sprintf(`{
"id": "%s",
"email": "[email protected]",
"bio": "<p>test bio</p>",
"name": "%s",
"job_title": "test job title",
"thumbnail": %s,
"updated_at": "%s"
}`, author.id.Hex(), author.name, imageResponse(author.tid), author.createdAt.UTC().Format(time.RFC3339))
}
func imageResponse(id primitive.ObjectID) string {
return fmt.Sprintf(`{
"id": "%s",
"description": "test description",
"filetype": "image/jpeg",
"resized_targets": {
"tiny": {
"height": 150,
"width": 150,
"url": "https://www.twreporter.org/images/test-tiny.jpg"
},
"w400": {
"height": 400,
"width": 400,
"url": "https://www.twreporter.org/images/test-w400.jpg"
},
"mobile": {
"height": 400,
"width": 400,
"url": "https://www.twreporter.org/images/test-mobile.jpg"
},
"tablet": {
"height": 400,
"width": 400,
"url": "https://www.twreporter.org/images/test-tablet.jpg"
},
"desktop": {
"height": 400,
"width": 400,
"url": "https://www.twreporter.org/images/test-desktop.jpg"
}
}
}
`, id.Hex())
}
func listResponse(total int, records []string) string {
return fmt.Sprintf(`{
"status": "success",
"data": {
"meta": {
"offset": 0,
"limit": 10,
"total": %d
},
"records":[%s]
}
}`, total, strings.Join(records, ","))
}
func singleRecordResponse(record string) string {
return fmt.Sprintf(`{
"status": "success",
"data": %s
}`, record)
}
func createContactDocument(id, thumbnailID primitive.ObjectID, name string, t time.Time) bson.M {
return bson.M{
"_id": id,
"email": "[email protected]",
"bio": bson.M{"html": "<p>test bio</p>", "md": "test bio"},
"name": name,
"job_title": "test job title",
"image": thumbnailID,
"updatedAt": t,
}
}
func createImageDocument(id primitive.ObjectID) bson.M {
return bson.M{
"_id": id,
"description": "test description",
"copyright": "copyrighted",
"keywords": "keyword1, keyword2",
"sale": false,
"image": bson.M{
"filename": "test name",
"filetype": "image/jpeg",
"gcsBucket": "",
"gcsDir": "",
"height": 400,
"size": 160000,
"width": 400,
"resizedTargets": bson.M{
"tiny": bson.M{
"height": 150,
"width": 150,
"url": "https://www.twreporter.org/images/test-tiny.jpg",
},
"w400": bson.M{
"height": 400,
"width": 400,
"url": "https://www.twreporter.org/images/test-w400.jpg",
},
"mobile": bson.M{
"height": 400,
"width": 400,
"url": "https://www.twreporter.org/images/test-mobile.jpg",
},
"tablet": bson.M{
"height": 400,
"width": 400,
"url": "https://www.twreporter.org/images/test-tablet.jpg",
},
"desktop": bson.M{
"height": 400,
"width": 400,
"url": "https://www.twreporter.org/images/test-desktop.jpg",
},
},
},
"iptc": bson.M{
"caption": "test caption",
"country": "台灣",
"country_code": "TWN",
"byline": "攝影師",
"created_time": "",
"created_date": "20210129",
"keywords": bson.A{"keyword1", "keyword2"},
"city": "taipei",
},
}
}