-
Notifications
You must be signed in to change notification settings - Fork 52
/
base.go
104 lines (86 loc) · 2.03 KB
/
base.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
package test
import (
"github.com/crawlab-team/crawlab-core/controllers"
"github.com/crawlab-team/crawlab-core/models/service"
"github.com/crawlab-team/crawlab-core/routes"
"github.com/crawlab-team/go-trace"
"github.com/gavv/httpexpect/v2"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"go.uber.org/dig"
"net/http/httptest"
"testing"
"time"
)
func init() {
var err error
T, err = NewTest()
if err != nil {
panic(err)
}
}
type Test struct {
// dependencies
modelSvc service.ModelService
// internals
app *gin.Engine
svr *httptest.Server
// test data
TestUsername string
TestPassword string
TestToken string
}
func (t *Test) Setup(t2 *testing.T) {
//if err := controllers.InitControllers(); err != nil {
// panic(err)
//}
//t2.Cleanup(t.Cleanup)
}
func (t *Test) Cleanup() {
_ = t.modelSvc.DropAll()
time.Sleep(200 * time.Millisecond)
}
func (t *Test) NewExpect(t2 *testing.T) (e *httpexpect.Expect) {
e = httpexpect.New(t2, t.svr.URL)
res := e.POST("/login").WithJSON(map[string]string{
"username": t.TestUsername,
"password": t.TestPassword,
}).Expect().JSON().Object()
t.TestToken = res.Path("$.data").String().Raw()
require.NotEmpty(t2, t.TestToken)
return e
}
func (t *Test) WithAuth(req *httpexpect.Request) *httpexpect.Request {
return req.WithHeader("Authorization", t.TestToken)
}
var T *Test
func NewTest() (res *Test, err error) {
// test
t := &Test{}
// gin app
t.app = gin.New()
// http test server
t.svr = httptest.NewServer(t.app)
// init controllers
if err := controllers.InitControllers(); err != nil {
return nil, err
}
// init routes
if err := routes.InitRoutes(t.app); err != nil {
return nil, err
}
// dependency injection
c := dig.New()
if err := c.Provide(service.NewService); err != nil {
return nil, trace.TraceError(err)
}
if err := c.Invoke(func(modelSvc service.ModelService) {
t.modelSvc = modelSvc
}); err != nil {
return nil, trace.TraceError(err)
}
// test data
t.TestUsername = "admin"
t.TestPassword = "admin"
return t, nil
}