forked from snowlyg/iris-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_test.go
185 lines (147 loc) · 4.85 KB
/
base_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
package main
import (
"IrisApiProject/database"
"IrisApiProject/models"
"flag"
"fmt"
"os"
"testing"
"IrisApiProject/config"
"github.com/iris-contrib/httpexpect"
"github.com/kataras/iris"
"github.com/kataras/iris/httptest"
)
var (
app *iris.Application // iris.Applications
testRole *models.Role
testPerm *models.Permission
testPermIds []uint
testUser *models.User
)
//单元测试基境
func TestMain(m *testing.M) {
// 初始化app
app = newApp()
baseCase()
flag.Parse()
exitCode := m.Run()
// 删除测试数据表,保持测试环境
database.DB.DropTable("users", "roles", "permissions", &models.OauthToken{})
os.Exit(exitCode)
}
// 单元测试 login 方法
func login(t *testing.T, url string, Object interface{}, StatusCode int, Status bool, Msg string, Data map[string]interface{}) (e *httpexpect.Expect) {
e = httptest.New(t, app, httptest.Configuration{Debug: config.Conf.Get("app.debug").(bool)})
if Data != nil {
e.POST(url).WithJSON(Object).
Expect().Status(StatusCode).
JSON().Object().Values().Contains(Status, Msg, Data)
} else {
e.POST(url).WithJSON(Object).
Expect().Status(StatusCode).
JSON().Object().Values().Contains(Status, Msg)
}
return
}
// 单元测试 create 方法
func create(t *testing.T, url string, Object interface{}, StatusCode int, Status bool, Msg string, Data map[string]interface{}) (e *httpexpect.Expect) {
e = httptest.New(t, app, httptest.Configuration{Debug: config.Conf.Get("app.debug").(bool)})
at := GetLoginToken()
ob := e.POST(url).WithHeader("Authorization", "Bearer "+at.Token).WithJSON(Object).
Expect().Status(StatusCode).JSON().Object()
ob.Value("status").Equal(Status)
ob.Value("msg").Equal(Msg)
for k, v := range Data {
ob.Value("data").Object().Value(k).Equal(v)
}
return
}
// 单元测试 update 方法
func update(t *testing.T, url string, Object interface{}, StatusCode int, Status bool, Msg string, Data map[string]interface{}) (e *httpexpect.Expect) {
e = httptest.New(t, app, httptest.Configuration{Debug: config.Conf.Get("app.debug").(bool)})
at := GetLoginToken()
ob := e.PUT(url).WithHeader("Authorization", "Bearer "+at.Token).WithJSON(Object).
Expect().Status(StatusCode).JSON().Object()
ob.Value("status").Equal(Status)
ob.Value("msg").Equal(Msg)
for k, v := range Data {
ob.Value("data").Object().Value(k).Equal(v)
}
return
}
// 单元测试 getOne 方法
func getOne(t *testing.T, url string, StatusCode int, Status bool, Msg string, Data map[string]interface{}) (e *httpexpect.Expect) {
e = httptest.New(t, app, httptest.Configuration{Debug: config.Conf.Get("app.debug").(bool)})
at := GetLoginToken()
if Data != nil {
e.GET(url).WithHeader("Authorization", "Bearer "+at.Token).
Expect().Status(StatusCode).
JSON().Object().Values().Contains(Status, Msg, Data)
} else {
e.GET(url).WithHeader("Authorization", "Bearer "+at.Token).
Expect().Status(StatusCode).
JSON().Object().Values().Contains(Status, Msg)
}
return
}
// 单元测试 getMore 方法
func getMore(t *testing.T, url string, StatusCode int, Status bool, Msg string, Data map[string]interface{}) (e *httpexpect.Expect) {
e = httptest.New(t, app, httptest.Configuration{Debug: config.Conf.Get("app.debug").(bool)})
at := GetLoginToken()
if Data != nil {
e.GET(url).WithHeader("Authorization", "Bearer "+at.Token).
Expect().Status(StatusCode).
JSON().Object().Values().Contains(Status, Msg, Data)
} else {
e.GET(url).WithHeader("Authorization", "Bearer "+at.Token).
Expect().Status(StatusCode).
JSON().Object().Values().Contains(Status, Msg)
}
return
}
// 单元测试 delete 方法
func delete(t *testing.T, url string, StatusCode int, Status bool, Msg string, Data map[string]interface{}) (e *httpexpect.Expect) {
e = httptest.New(t, app, httptest.Configuration{Debug: config.Conf.Get("app.debug").(bool)})
at := GetLoginToken()
e.DELETE(url).WithHeader("Authorization", "Bearer "+at.Token).
Expect().Status(StatusCode).
JSON().Object().Values().Contains(Status, Msg)
return
}
/**
*登陆用户
*@return Token 返回登陆后的token
*/
func GetLoginToken() models.Token {
response, status, msg := models.CheckLogin(
config.Conf.Get("test.LoginUserName").(string),
config.Conf.Get("test.LoginPwd").(string),
)
// 打印错误信息
if !status {
fmt.Println(msg)
}
return response
}
func baseCase() {
perm_json := &models.PermissionJson{
Name: "test_update_user",
Description: "访客",
DisplayName: "访客",
}
testPerm = models.CreatePermission(perm_json)
testPermIds = []uint{testPerm.ID}
role_json := &models.RoleJson{
Name: "test_update_user",
Description: "访客",
DisplayName: "访客",
}
testRole = models.CreateRole(role_json, testPermIds)
aul := &models.UserJson{
Username: "guest",
Name: "访客",
Password: "guest111",
RoleID: testRole.ID,
}
testUser = models.CreateUser(aul)
}