-
Notifications
You must be signed in to change notification settings - Fork 0
/
starter_test.go
281 lines (237 loc) · 7.18 KB
/
starter_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2/dbtest"
)
var router *mux.Router
var mockDBServer dbtest.DBServer
var reviews []AppReviewGooglePlay
var invalidArrayPayload = []byte(`[{ "wrong_json_format": true }]`)
var invalidObjectPayload = []byte(`{ "wrong_json_format": true }`)
func TestMain(m *testing.M) {
fmt.Println("--- Start Tests")
setup()
// run the test cases defined in this file
retCode := m.Run()
defer tearDown()
// call with result of m.Run()
os.Exit(retCode)
}
func setup() {
fmt.Println("--- --- setup")
router = makeRouter()
setupDB()
fillDB()
}
func setupDB() {
tempDir, _ := ioutil.TempDir("", "testing")
mockDBServer.SetPath(tempDir)
mongoClient = mockDBServer.Session()
MongoCreateCollectionIndexes(mongoClient)
}
func fillDB() {
/*
* Insert fake data
*/
fmt.Println("Insert fake data")
review := AppReviewGooglePlay{
ReviewID: "1234567",
PackageName: "eu.openreq",
Author: "OpenReqUser",
Date: 20190131,
Rating: 5,
Title: "Tool usage",
Body: "I used the tool for over a year now and like it! I hope for more analytics features coming in the future.",
PermaLink: "www.openreq.eu",
FeatureRequest: true,
BugReport: false,
}
err := mongoClient.DB(database).C(collectionAppReviewsGooglePlay).Insert(review)
if err != nil {
panic(err)
}
reviews = []AppReviewGooglePlay{review}
/*
* Insert fake observables
*/
err = mongoClient.DB(database).C(collectionObservableGooglePlay).Insert(
ObservableGooglePlay{
PackageName: "eu.openreq",
Interval: "2h",
})
if err != nil {
panic(err)
}
}
func tearDown() {
fmt.Println("--- --- tear down")
mongoClient.Close()
mockDBServer.Stop() // Stop shuts down the temporary server and removes data on disk.
}
type endpoint struct {
method string
url string
}
func (e endpoint) withVars(vs ...interface{}) endpoint {
e.url = fmt.Sprintf(e.url, vs...)
return e
}
func (e endpoint) executeRequest(payload interface{}) (error, *httptest.ResponseRecorder) {
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(payload)
if err != nil {
return err, nil
}
req, err := http.NewRequest(e.method, e.url, body)
if err != nil {
return err, nil
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
return nil, rr
}
func (e endpoint) mustExecuteRequest(payload interface{}) *httptest.ResponseRecorder {
err, rr := e.executeRequest(payload)
if err != nil {
panic(errors.Wrap(err, `Could not execute request`))
}
return rr
}
func isSuccess(code int) bool {
return code >= 200 && code < 300
}
func assertSuccess(t *testing.T, rr *httptest.ResponseRecorder) {
if !isSuccess(rr.Code) {
t.Errorf("Status code differs. Expected success.\n Got status %d (%s) instead", rr.Code, http.StatusText(rr.Code))
}
}
func assertFailure(t *testing.T, rr *httptest.ResponseRecorder) {
if isSuccess(rr.Code) {
t.Errorf("Status code differs. Expected failure.\n Got status %d (%s) instead", rr.Code, http.StatusText(rr.Code))
}
}
func assertJsonDecodes(t *testing.T, rr *httptest.ResponseRecorder, v interface{}) {
err := json.Unmarshal(rr.Body.Bytes(), v)
if err != nil {
t.Error(errors.Wrap(err, "Expected valid json array"))
}
}
func TestPostAppPageGooglePlay(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/app/store/app-page/google-play/"}
// Test for failure
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
// Test for success
appPage := AppPageGooglePlay{
Name: "OpenReq",
PackageName: "eu.openreq",
DateCrawled: 20190131,
Category: "Tools",
USK: "all ages",
Price: "free",
PriceValue: 0.0,
PriceCurrency: "EUR",
Description: "contains, among others, analytics features.",
WhatsNew: []string{"re-designed dashboard"},
Rating: 5,
StarsCount: 10000,
CountPerRating: StarCountPerRating{
Five: 10000,
Four: 0,
Three: 0,
Two: 0,
One: 0,
},
EstimatedDownloadNumber: 10000,
DeveloperName: "OpenReq Consortium",
TopDeveloper: true,
ContainsAds: false,
InAppPurchases: false,
LastUpdate: 20190131,
Os: "ANDROID",
RequiresOsVersion: "9.0",
CurrentSoftwareVersion: "beta",
SimilarApps: []string{"supersede"},
}
assertSuccess(t, ep.mustExecuteRequest(appPage))
}
func TestPostAppReviewGooglePlay(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/app/store/app-review/google-play/"}
// Test for failure
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
// Test for success
reviews := []AppReviewGooglePlay{{
ReviewID: "1234567",
PackageName: "eu.openreq",
Author: "OpenReqUser",
Date: 20190131,
Rating: 5,
Title: "Tool usage",
Body: "I used the tool for over a year now and like it! I hope for more analytics features coming in the future.",
PermaLink: "www.openreq.eu",
FeatureRequest: true,
BugReport: false,
}}
assertSuccess(t, ep.mustExecuteRequest(reviews))
}
func TestPostObserveAppGooglePlay(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/app/observe/app/google-play/package-name/%s/interval/%s"}
// Test for success
assertSuccess(t, ep.withVars("test", "h1").mustExecuteRequest(nil))
}
func TestPostNonExistingAppReviewsGooglePlay(t *testing.T) {
ep := endpoint{"POST", "/hitec/repository/app/non-existing/app-review/google-play/"}
// Test for failure
assertFailure(t, ep.mustExecuteRequest(invalidObjectPayload))
// Test for success
existingReview := reviews[0]
newReview := AppReviewGooglePlay{
ReviewID: "554466",
PackageName: "eu.openreq",
Author: "John Smith",
Date: 20191104,
Rating: 4,
Title: "Comment title",
Body: "Body of the comment",
PermaLink: "example.com",
FeatureRequest: false,
BugReport: true,
}
response := ep.mustExecuteRequest([]AppReviewGooglePlay{existingReview, newReview})
assertSuccess(t, response)
var newReviews []AppReviewGooglePlay
assertJsonDecodes(t, response, &newReviews)
assert.Len(t, newReviews, 1)
assert.Equal(t, newReview, newReviews[0])
}
func TestGetObsevableGooglePlay(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/app/observable/google-play"}
// Test for success
response := ep.mustExecuteRequest(nil)
assertSuccess(t, response)
var observables []ObservableGooglePlay
assertJsonDecodes(t, response, &observables)
assert.Len(t, observables, 2)
}
func TestGetAppReviewsOfClass(t *testing.T) {
ep := endpoint{"GET", "/hitec/repository/app/google-play/package-name/%s/class/%s"}
// Test for success
response := ep.withVars("eu.openreq", "feature_request").mustExecuteRequest(nil)
assertSuccess(t, response)
var reviews []AppReviewGooglePlay
assertJsonDecodes(t, response, &reviews)
assert.Len(t, reviews, 1)
response = ep.withVars("eu.openreq", "bug_report").mustExecuteRequest(nil)
assertSuccess(t, response)
assertJsonDecodes(t, response, &reviews)
assert.Len(t, reviews, 0)
}