/
travelPlanning_suite_test.go
87 lines (71 loc) · 1.88 KB
/
travelPlanning_suite_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
package main
import (
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"time"
"testing"
"github.com/StephanDollberg/go-json-rest-middleware-jwt"
"github.com/ant0ine/go-json-rest/rest"
"github.com/ant0ine/go-json-rest/rest/test"
r "github.com/dancannon/gorethink"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const database = "travelPlanningTest"
var (
Response *httptest.ResponseRecorder
Request *http.Request
testConn *r.Session
tst *testing.T
loginCredentials = map[string]string{"username": "admin", "password": "admin"}
)
func TestTravelPlanning(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "TravelPlanning Suite")
}
var _ = BeforeSuite(func() {
testConn = NewDBSession(database)
})
var _ = AfterSuite(func() {
_, err := r.DBDrop(database).RunWrite(testConn)
if err != nil {
log.Print(err)
}
testConn.Close()
})
type resultToken struct {
TokenString string `json:"token"`
}
func Login() string {
APIRequest("/login", "POST", loginCredentials, "")
token := resultToken{}
json.Unmarshal(Response.Body.Bytes(), &token)
return token.TokenString
}
func APIRequest(url string, method string, model interface{}, token string) {
jwtMiddleware := &jwt.JWTMiddleware{
Key: SecretKey,
Realm: Realm,
Timeout: time.Hour,
MaxRefresh: time.Hour * 24,
Authenticator: func(userId string, password string) bool {
return Authenticator(userId, password)
},
}
api := rest.NewApi()
api.Use(&rest.IfMiddleware{
Condition: func(request *rest.Request) bool {
return CheckCondition(request)
},
IfTrue: jwtMiddleware,
})
api.SetApp(NewRouter(jwtMiddleware, testConn))
Request = test.MakeSimpleRequest(method, url, model)
if token != "" {
Request.Header.Set("Authorization", "Bearer "+token)
}
recorded := test.RunRequest(tst, api.MakeHandler(), Request)
Response = recorded.Recorder
}