forked from islax/microapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestapp.go
106 lines (85 loc) · 2.98 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
package microapp
import (
"net/http"
"net/http/httptest"
"testing"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite" // Used
log "github.com/sirupsen/logrus"
)
// 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 := log.New()
application := New(appName, nil, 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()
}
// 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
}
// CheckResponseCode checks if the http response is as expected
func (testApp *TestApp) CheckResponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
}
}
// GetToken gets a token to connect to API
func (testApp *TestApp) GetToken(tenantID string, userID string, scope []string) string {
return testApp.generateToken(tenantID, userID, scope, false)
}
// GetAdminToken returns a test token
func (testApp *TestApp) GetAdminToken(tenantID string, userID string, scope []string) string {
return testApp.generateToken(tenantID, userID, scope, true)
}
func (testApp *TestApp) generateToken(tenantID string, userID string, scope []string, admin bool) string {
hmacSampleSecret := []byte(testApp.application.Config.GetString("ISLA_JWT_SECRET"))
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"iss": "http://isla.cyberinc.com",
"aud": "http://isla.cyberinc.com",
"foo": "bar",
"iat": time.Now().Unix(),
"exp": time.Now().Add(time.Minute * 60).Unix(), // Expires in 1 hour
"tenant": tenantID,
"user": userID,
"name": "username",
"scope": scope,
"admin": admin,
})
// 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
}