forked from islax/microapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestapp.go
354 lines (300 loc) · 13.4 KB
/
testapp.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package microapp
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strconv"
"strings"
"testing"
"time"
uuid "github.com/satori/go.uuid"
jwt "github.com/dgrijalva/jwt-go"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite" // Used
"github.com/rs/zerolog"
)
// TestApp Provides convinience methods for test
type TestApp struct {
application *App
controllerRouteProvider func(*App) []RouteSpecifier
dbInitializer func(db *gorm.DB)
}
// NewTestApp returns new instance of TestApp
func NewTestApp(appName string, controllerRouteProvider func(*App) []RouteSpecifier, dbInitializer func(db *gorm.DB), verbose bool) *TestApp {
dbFile := "./test_islax.db"
db, err := gorm.Open("sqlite3", dbFile)
if err != nil {
panic(err)
}
db.LogMode(verbose)
logger := zerolog.New(os.Stdout)
rand.Seed(time.Now().UnixNano())
randomAPIPort := fmt.Sprintf("10%v%v%v", rand.Intn(9), rand.Intn(9), rand.Intn(9)) // Generating random API port so that if multiple tests can run parallel
application := New(appName, map[string]interface{}{"API_PORT": randomAPIPort}, logger, db, nil)
return &TestApp{application: application, controllerRouteProvider: controllerRouteProvider, dbInitializer: dbInitializer}
}
// Initialize prepares the app for testing
func (testApp *TestApp) Initialize() {
testApp.application.Initialize(testApp.controllerRouteProvider(testApp.application))
testApp.PrepareEmptyTables()
go testApp.application.Start()
}
// Stop the app
func (testApp *TestApp) Stop() {
testApp.application.Stop()
testApp.application.DB.Close()
os.Remove("./test_islax.db")
}
// PrepareEmptyTables clears all table of data
func (testApp *TestApp) PrepareEmptyTables() {
testApp.dbInitializer(testApp.application.DB)
}
// ExecuteRequest executes the http request
func (testApp *TestApp) ExecuteRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
testApp.application.Router.ServeHTTP(rr, req)
return rr
}
// AddAssociations adds associations to the given entity
func (testApp *TestApp) AddAssociations(entity interface{}, associationName string, associations ...interface{}) error {
return testApp.application.DB.Model(entity).Association(associationName).Append(associations...).Error
}
// AssertEqualWithFieldsToIgnore asserts whether two objects are equal
func (testApp *TestApp) AssertEqualWithFieldsToIgnore(t *testing.T, expected interface{}, actual interface{}, fieldsToIgnore []string, mapOfExpectedToActualField map[string]string) {
expectedElems := reflect.ValueOf(expected).Elem()
actualElem := reflect.ValueOf(actual).Elem()
typeOfExpectedElems := reflect.ValueOf(expected).Elem().Type()
mapOfIgnoreFields := make(map[string]bool)
for _, ignoreField := range fieldsToIgnore {
mapOfIgnoreFields[ignoreField] = true
}
for i := 0; i < expectedElems.NumField(); i++ {
expectedFieldName := typeOfExpectedElems.Field(i).Name
// t.Logf("Expected assert field: %v", expectedFieldName)
if !mapOfIgnoreFields[expectedFieldName] {
expectedField := expectedElems.Field(i)
expectedFieldMetadata := typeOfExpectedElems.Field(i).Type
if expectedFieldMetadata.Kind() == reflect.Struct {
for j := 0; j < expectedFieldMetadata.NumField(); j++ {
if !mapOfIgnoreFields[expectedFieldMetadata.Field(j).Name] {
expectedValue := testApp.getReflectFieldValueAsString(expectedField.Field(j), expectedFieldMetadata.Field(j).Type)
actualFieldName := expectedFieldMetadata.Field(j).Name
// t.Logf("Expected assert field (nested): %v", actualFieldName)
if v, ok := mapOfExpectedToActualField[actualFieldName]; ok {
actualFieldName = v
}
actualField := actualElem.FieldByName(actualFieldName)
if actualField.Kind() == reflect.Ptr {
actualField = actualField.Elem()
}
actualValue := fmt.Sprintf("%v", actualField.Interface())
if expectedValue != actualValue {
t.Errorf("Expected %v [%v], Actual [%v]!", actualFieldName, expectedValue, actualValue)
}
}
}
} else {
expectedValue := testApp.getReflectFieldValueAsString(expectedField, expectedFieldMetadata)
actualFieldName := expectedFieldName
if v, ok := mapOfExpectedToActualField[actualFieldName]; ok {
actualFieldName = v
}
// t.Logf("Actual assert field: %v", actualFieldName)
actualField := actualElem.FieldByName(actualFieldName)
if actualField.Kind() == reflect.Ptr {
actualField = actualField.Elem()
}
actualValue := fmt.Sprintf("%v", actualField.Interface())
if expectedValue != actualValue {
t.Errorf("Expected %v [%v], Actual [%v]!", actualFieldName, expectedValue, actualValue)
}
}
}
}
}
// AssertEqualWithFieldsToCheck asserts whether two objects are equal
func (testApp *TestApp) AssertEqualWithFieldsToCheck(t *testing.T, expected interface{}, actual interface{}, fieldsToChk []string, mapOfExpectedToActualField map[string]string) {
expectedElems := reflect.ValueOf(expected).Elem()
actualElems := reflect.ValueOf(actual).Elem()
typeOfExpectedElems := reflect.ValueOf(expected).Elem().Type()
for _, attrToChk := range fieldsToChk {
expectedFieldMetadata, _ := typeOfExpectedElems.FieldByName(attrToChk)
expectedField := expectedElems.FieldByName(attrToChk)
if expectedField.Kind() == reflect.Ptr {
expectedField = expectedField.Elem()
}
var expectedValue string
if fmt.Sprintf("%v", expectedFieldMetadata.Type) == "time.Time" {
expectedValue = expectedField.Interface().(time.Time).Format(time.RFC3339)
} else {
expectedValue = fmt.Sprintf("%v", expectedField.Interface())
}
actualAttrName := attrToChk
if v, ok := mapOfExpectedToActualField[attrToChk]; ok {
actualAttrName = v
}
actualField := actualElems.FieldByName(actualAttrName)
if actualField.Kind() == reflect.Ptr {
actualField = actualField.Elem()
}
actualValue := fmt.Sprintf("%v", actualField.Interface())
if expectedValue != actualValue {
t.Errorf("Expected %v [%v], Actual [%v]!", attrToChk, expectedValue, actualValue)
}
}
}
// AssertErrorResponse checks if the http response contains expected errorKey, errorField and errorMessage
func (testApp *TestApp) AssertErrorResponse(t *testing.T, response *httptest.ResponseRecorder, expectedErrorKey string, expectedErrorField string, expectedError string) {
testApp.CheckResponseCode(t, http.StatusBadRequest, response.Code)
var errData map[string]interface{}
if err := json.Unmarshal(response.Body.Bytes(), &errData); err != nil {
t.Errorf("Unable to parse response: %v", err)
}
if errData["errorKey"] != expectedErrorKey {
t.Errorf("Expected errorKey [%v], Got [%v]!", expectedErrorKey, errData["errorKey"])
}
errors := errData["errors"].(map[string]interface{})
if fmt.Sprintf("%v", errors[expectedErrorField]) != expectedError {
t.Errorf("Expected error [%v], Got [%v]!", expectedError, errors[expectedErrorField])
}
}
// AssertXTotalCount checks if the http response header contains expected x-total-count
func (testApp *TestApp) AssertXTotalCount(t *testing.T, response *httptest.ResponseRecorder, expectedXTotalCount int) {
xTotalCount := response.Header().Get("X-Total-Count")
if xTotalCount == "" {
t.Fatalf("Expected X-Total-Count [%v], not found!", expectedXTotalCount)
}
xTotalCountInt, err := strconv.Atoi(xTotalCount)
if err != nil {
t.Fatalf("Expected X-Total-Count [%v], Actual [%v]!", expectedXTotalCount, xTotalCount)
} else if expectedXTotalCount != xTotalCountInt {
t.Fatalf("Expected X-Total-Count [%v], Actual [%v]!", expectedXTotalCount, xTotalCountInt)
}
}
// CallAPI invokes http API
func (testApp *TestApp) CallAPI(httpMethod string, apiURL string, token string, req interface{}) *httptest.ResponseRecorder {
var payload io.Reader
if req != nil {
reqJSON, _ := json.Marshal(req)
payload = bytes.NewBuffer(reqJSON)
}
httpReq, _ := http.NewRequest(httpMethod, apiURL, payload)
httpReq.Header.Add("Authorization", fmt.Sprintf("Bearer %v", token))
return testApp.ExecuteRequest(httpReq)
}
// CheckResponseCode checks if the http response is as expected
func (testApp *TestApp) CheckResponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Fatalf("Expected response code %d. Got %d\n", expected, actual)
}
}
// Check checks whether the expected and actual value matches
func (testApp *TestApp) Check(t *testing.T, name string, expected, actual interface{}, failNow bool) {
if !reflect.DeepEqual(expected, actual) {
if failNow {
t.Fatalf("Expected %v [%v]. Got [%v]\n", name, expected, actual)
} else {
t.Errorf("Expected %v [%v]. Got [%v]\n", name, expected, actual)
}
}
}
// GetAdminToken returns a test token
func (testApp *TestApp) GetAdminToken(tenantID string, userID string, scope []string) string {
return testApp.generateToken(tenantID, userID, "", "", uuid.UUID{}.String(), "", scope, true)
}
// GetAll gets all from DB
func (testApp *TestApp) GetAll(out interface{}, preloads []string, whereClause string, whereParams []interface{}, orderBy string) error {
db := testApp.application.DB
for _, preload := range preloads {
db = db.Preload(preload)
}
if strings.TrimSpace(whereClause) != "" {
db = db.Where(whereClause, whereParams...)
}
if strings.TrimSpace(orderBy) != "" {
db = db.Order(orderBy, true)
}
return db.Find(out).Error
}
// GetByID gets entity by ids
func (testApp *TestApp) GetByID(out interface{}, preloads []string, id string) error {
db := testApp.application.DB
for _, preload := range preloads {
db = db.Preload(preload)
}
return db.Where("id = ?", id).Find(out).Error
}
// GetFullAdminToken returns a test token with all the fields along with different external IDs for types such as Appliance, Session, User. These external IDs are used with REST api is invoked from another REST API service as opposed to the getting hit from UI by the user.
func (testApp *TestApp) GetFullAdminToken(tenantID string, userID string, username string, name string, externalID string, externalIDType string, scope []string) string {
return testApp.generateToken(tenantID, userID, username, name, externalID, externalIDType, scope, true)
}
// GetStandardAdminToken returns a test admin token with all standard fields.
func (testApp *TestApp) GetStandardAdminToken(tenantID string, userID string, username string, name string, scope []string) string {
return testApp.generateToken(tenantID, userID, username, name, uuid.Nil.String(), "", scope, true)
}
// GetFullToken returns a test token with all the fields along with different external IDs for types such as Appliance, Session, User. These external IDs are used with REST api is invoked from another REST API service as opposed to the getting hit from UI by the user.
func (testApp *TestApp) GetFullToken(tenantID string, userID string, username string, name string, externalID string, externalIDType string, scope []string) string {
return testApp.generateToken(tenantID, userID, username, name, externalID, externalIDType, scope, false)
}
// GetStandardToken returns a test token with all standard fields
func (testApp *TestApp) GetStandardToken(tenantID string, userID string, username string, name string, scope []string) string {
return testApp.generateToken(tenantID, userID, username, name, uuid.Nil.String(), "", scope, false)
}
// GetToken gets a token to connect to API
func (testApp *TestApp) GetToken(tenantID string, userID string, scope []string) string {
return testApp.generateToken(tenantID, userID, userID, userID, uuid.Nil.String(), "", scope, false)
}
// SaveToDB saves the entity to database
func (testApp *TestApp) SaveToDB(entity interface{}) error {
return testApp.application.DB.Create(entity).Error
}
// SetControllerRouteProviderAndInitialize sets the controllerRouteProvider and initializes application
func (testApp *TestApp) SetControllerRouteProviderAndInitialize(controllerRouteProvider func(*App) []RouteSpecifier) {
testApp.controllerRouteProvider = controllerRouteProvider
testApp.PrepareEmptyTables()
testApp.application.Initialize(testApp.controllerRouteProvider(testApp.application))
go testApp.application.Start()
}
// generateToken generates and return token
func (testApp *TestApp) generateToken(tenantID string, userID string, username string, name string, externalID string, externalIDType string, scope []string, admin bool) string {
hmacSampleSecret := []byte(testApp.application.Config.GetString("JWT_SECRET"))
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"iss": "http://isla.cyberinc.com",
"aud": "http://isla.cyberinc.com",
"iat": time.Now().Unix(),
"exp": time.Now().Add(time.Minute * 60).Unix(), // Expires in 1 hour
"tenant": tenantID,
"user": userID,
"admin": admin,
"name": username,
"displayName": name,
"scope": scope,
"externalId": externalID,
"externalIdType": externalIDType,
"identityProvider": "",
})
// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString(hmacSampleSecret)
if err != nil {
panic(err)
}
return tokenString
}
func (testApp *TestApp) getReflectFieldValueAsString(fieldElem reflect.Value, fieldType reflect.Type) string {
var strValue string
if fieldElem.Kind() == reflect.Ptr {
fieldElem = fieldElem.Elem()
}
if fmt.Sprintf("%v", fieldType) == "time.Time" {
strValue = fieldElem.Interface().(time.Time).Format(time.RFC3339)
} else {
strValue = fmt.Sprintf("%v", fieldElem.Interface())
}
return strValue
}