Test: delete object route
This commit is contained in:
parent
93010e3525
commit
b4cfa6b601
5 changed files with 363 additions and 287 deletions
1
assets
1
assets
|
@ -1 +0,0 @@
|
|||
Subproject commit 79aaeb9e3a02dab9133f39f872602414b0ab57a0
|
|
@ -99,11 +99,15 @@ func RemoveFilesWithSoftLinks(files []File) ([]File, error) {
|
|||
}
|
||||
|
||||
// 过滤具有软连接的文件
|
||||
for i := 0; i < len(files); i++ {
|
||||
for _, value := range filesWithSoftLinks {
|
||||
if value.PolicyID != files[i].PolicyID || value.SourceName != files[i].SourceName {
|
||||
filteredFiles = append(filteredFiles, files[i])
|
||||
break
|
||||
if len(filesWithSoftLinks) == 0 {
|
||||
filteredFiles = files
|
||||
} else {
|
||||
for i := 0; i < len(files); i++ {
|
||||
for _, value := range filesWithSoftLinks {
|
||||
if value.PolicyID != files[i].PolicyID || value.SourceName != files[i].SourceName {
|
||||
filteredFiles = append(filteredFiles, files[i])
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
178
routers/file_router_test.go
Normal file
178
routers/file_router_test.go
Normal file
|
@ -0,0 +1,178 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/HFO4/cloudreve/middleware"
|
||||
model "github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/HFO4/cloudreve/service/explorer"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListDirectoryRoute(t *testing.T) {
|
||||
switchToMemDB()
|
||||
asserts := assert.New(t)
|
||||
router := InitRouter()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// 成功
|
||||
req, _ := http.NewRequest(
|
||||
"GET",
|
||||
"/api/v3/directory/",
|
||||
nil,
|
||||
)
|
||||
middleware.SessionMock = map[string]interface{}{"user_id": 1}
|
||||
router.ServeHTTP(w, req)
|
||||
asserts.Equal(200, w.Code)
|
||||
resJSON := &serializer.Response{}
|
||||
err := json.Unmarshal(w.Body.Bytes(), resJSON)
|
||||
asserts.NoError(err)
|
||||
asserts.Equal(0, resJSON.Code)
|
||||
|
||||
w.Body.Reset()
|
||||
|
||||
}
|
||||
|
||||
func TestLocalFileUpload(t *testing.T) {
|
||||
switchToMemDB()
|
||||
asserts := assert.New(t)
|
||||
router := InitRouter()
|
||||
w := httptest.NewRecorder()
|
||||
middleware.SessionMock = map[string]interface{}{"user_id": 1}
|
||||
|
||||
testCases := []struct {
|
||||
GetRequest func() *http.Request
|
||||
ExpectCode int
|
||||
RollBack func()
|
||||
}{
|
||||
// 文件大小指定错误
|
||||
{
|
||||
GetRequest: func() *http.Request {
|
||||
req, _ := http.NewRequest(
|
||||
"POST",
|
||||
"/api/v3/file/upload",
|
||||
nil,
|
||||
)
|
||||
req.Header.Add("Content-Length", "ddf")
|
||||
return req
|
||||
},
|
||||
ExpectCode: 40001,
|
||||
},
|
||||
// 返回错误
|
||||
{
|
||||
GetRequest: func() *http.Request {
|
||||
req, _ := http.NewRequest(
|
||||
"POST",
|
||||
"/api/v3/file/upload",
|
||||
strings.NewReader("2333"),
|
||||
)
|
||||
req.Header.Add("Content-Length", "4")
|
||||
req.Header.Add("X-FileName", "大地的%sfsf")
|
||||
return req
|
||||
},
|
||||
ExpectCode: 40002,
|
||||
},
|
||||
// 成功
|
||||
{
|
||||
GetRequest: func() *http.Request {
|
||||
req, _ := http.NewRequest(
|
||||
"POST",
|
||||
"/api/v3/file/upload",
|
||||
strings.NewReader("2333"),
|
||||
)
|
||||
req.Header.Add("Content-Length", "4")
|
||||
req.Header.Add("X-FileName", "TestFileUploadRoute.txt")
|
||||
return req
|
||||
},
|
||||
ExpectCode: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for key, testCase := range testCases {
|
||||
req := testCase.GetRequest()
|
||||
router.ServeHTTP(w, req)
|
||||
asserts.Equal(200, w.Code)
|
||||
resJSON := &serializer.Response{}
|
||||
err := json.Unmarshal(w.Body.Bytes(), resJSON)
|
||||
asserts.NoError(err, "测试用例%d", key)
|
||||
asserts.Equal(testCase.ExpectCode, resJSON.Code, "测试用例%d", key)
|
||||
if testCase.RollBack != nil {
|
||||
testCase.RollBack()
|
||||
}
|
||||
w.Body.Reset()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestObjectDelete(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
router := InitRouter()
|
||||
w := httptest.NewRecorder()
|
||||
middleware.SessionMock = map[string]interface{}{"user_id": 1}
|
||||
|
||||
testCases := []struct {
|
||||
Mock []string
|
||||
GetRequest func() *http.Request
|
||||
ExpectCode int
|
||||
RollBack []string
|
||||
}{
|
||||
// 路径不存在,返回无错误
|
||||
{
|
||||
GetRequest: func() *http.Request {
|
||||
body := explorer.ItemService{
|
||||
Items: []string{"/TestObjectDelete.txt"},
|
||||
}
|
||||
bodyStr, _ := json.Marshal(body)
|
||||
req, _ := http.NewRequest(
|
||||
"DELETE",
|
||||
"/api/v3/object",
|
||||
bytes.NewReader(bodyStr),
|
||||
)
|
||||
return req
|
||||
},
|
||||
ExpectCode: 0,
|
||||
},
|
||||
// 文件删除失败,返回203
|
||||
{
|
||||
Mock: []string{"INSERT INTO `files` (`id`, `created_at`, `updated_at`, `deleted_at`, `name`, `source_name`, `user_id`, `size`, `pic_info`, `folder_id`, `policy_id`, `dir`) VALUES(5, '2019-11-30 07:08:33', '2019-11-30 07:08:33', NULL, 'pigeon.zip', '65azil3B_pigeon.zip', 1, 1667217, '', 1, 1, '/');"},
|
||||
GetRequest: func() *http.Request {
|
||||
body := explorer.ItemService{
|
||||
Items: []string{"/pigeon.zip"},
|
||||
}
|
||||
bodyStr, _ := json.Marshal(body)
|
||||
req, _ := http.NewRequest(
|
||||
"DELETE",
|
||||
"/api/v3/object",
|
||||
bytes.NewReader(bodyStr),
|
||||
)
|
||||
return req
|
||||
},
|
||||
RollBack: []string{"DELETE FROM `v3_files` WHERE `id`=5"},
|
||||
ExpectCode: 203,
|
||||
},
|
||||
}
|
||||
|
||||
for key, testCase := range testCases {
|
||||
for _, value := range testCase.Mock {
|
||||
model.DB.Exec(value)
|
||||
}
|
||||
req := testCase.GetRequest()
|
||||
router.ServeHTTP(w, req)
|
||||
asserts.Equal(200, w.Code)
|
||||
resJSON := &serializer.Response{}
|
||||
err := json.Unmarshal(w.Body.Bytes(), resJSON)
|
||||
asserts.NoError(err, "测试用例%d", key)
|
||||
asserts.Equal(testCase.ExpectCode, resJSON.Code, "测试用例%d", key)
|
||||
|
||||
for _, value := range testCase.RollBack {
|
||||
model.DB.Exec(value)
|
||||
}
|
||||
|
||||
w.Body.Reset()
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
var mock sqlmock.Sqlmock
|
||||
var memDB *gorm.DB
|
||||
var mockDB *gorm.DB
|
||||
|
||||
// TestMain 初始化数据库Mock
|
||||
func TestMain(m *testing.M) {
|
||||
|
@ -29,7 +30,8 @@ func TestMain(m *testing.M) {
|
|||
model.Init()
|
||||
memDB = model.DB
|
||||
|
||||
model.DB, _ = gorm.Open("mysql", db)
|
||||
mockDB, _ = gorm.Open("mysql", db)
|
||||
model.DB = memDB
|
||||
defer db.Close()
|
||||
|
||||
m.Run()
|
||||
|
@ -38,3 +40,7 @@ func TestMain(m *testing.M) {
|
|||
func switchToMemDB() {
|
||||
model.DB = memDB
|
||||
}
|
||||
|
||||
func switchToMockDB() {
|
||||
model.DB = mockDB
|
||||
}
|
||||
|
|
|
@ -1,19 +1,11 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/HFO4/cloudreve/middleware"
|
||||
"github.com/HFO4/cloudreve/models"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -46,183 +38,175 @@ func TestCaptcha(t *testing.T) {
|
|||
asserts.Contains(w.Body.String(), "base64")
|
||||
}
|
||||
|
||||
func TestUserSession(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
router := InitRouter()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// 创建测试用验证码
|
||||
var configD = base64Captcha.ConfigDigit{
|
||||
Height: 80,
|
||||
Width: 240,
|
||||
MaxSkew: 0.7,
|
||||
DotCount: 80,
|
||||
CaptchaLen: 1,
|
||||
}
|
||||
idKeyD, _ := base64Captcha.GenerateCaptcha("", configD)
|
||||
middleware.ContextMock = map[string]interface{}{
|
||||
"captchaID": idKeyD,
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
settingRows *sqlmock.Rows
|
||||
userRows *sqlmock.Rows
|
||||
policyRows *sqlmock.Rows
|
||||
reqBody string
|
||||
expected interface{}
|
||||
}{
|
||||
// 登录信息正确,不需要验证码
|
||||
{
|
||||
settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
AddRow("login_captcha", "0", "login"),
|
||||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"), policyRows: sqlmock.NewRows([]string{"name", "type", "options"}).
|
||||
AddRow("默认存储策略", "local", "{\"op_name\":\"123\"}"),
|
||||
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin"}`,
|
||||
expected: serializer.BuildUserResponse(model.User{
|
||||
Email: "admin@cloudreve.org",
|
||||
Nick: "admin",
|
||||
Policy: model.Policy{
|
||||
Type: "local",
|
||||
OptionsSerialized: model.PolicyOption{FileType: []string{}},
|
||||
},
|
||||
}),
|
||||
},
|
||||
// 登录信息正确,需要验证码,验证码错误
|
||||
{
|
||||
settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
AddRow("login_captcha", "1", "login"),
|
||||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"),
|
||||
policyRows: sqlmock.NewRows([]string{"name", "type", "options"}).
|
||||
AddRow("默认存储策略", "local", "{\"op_name\":\"123\"}"),
|
||||
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin"}`,
|
||||
expected: serializer.ParamErr("验证码错误", nil),
|
||||
},
|
||||
// 邮箱正确密码错误
|
||||
{
|
||||
settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
AddRow("login_captcha", "0", "login"),
|
||||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"),
|
||||
policyRows: sqlmock.NewRows([]string{"name", "type", "options"}).
|
||||
AddRow("默认存储策略", "local", "{\"op_name\":\"123\"}"),
|
||||
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin123"}`,
|
||||
expected: serializer.Err(401, "用户邮箱或密码错误", nil),
|
||||
},
|
||||
//邮箱格式不正确
|
||||
{
|
||||
reqBody: `{"userName":"admin@cloudreve","captchaCode":"captchaCode","Password":"admin123"}`,
|
||||
expected: serializer.Err(40001, "邮箱格式不正确", errors.New("Key: 'UserLoginService.UserName' Error:Field validation for 'UserName' failed on the 'email' tag")),
|
||||
},
|
||||
// 用户被Ban
|
||||
{
|
||||
settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
AddRow("login_captcha", "0", "login"),
|
||||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options", "status"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}", model.Baned),
|
||||
policyRows: sqlmock.NewRows([]string{"name", "type", "options"}).
|
||||
AddRow("默认存储策略", "local", "{\"op_name\":\"123\"}"),
|
||||
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin"}`,
|
||||
expected: serializer.Err(403, "该账号已被封禁", nil),
|
||||
},
|
||||
// 用户未激活
|
||||
{
|
||||
settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
AddRow("login_captcha", "0", "login"),
|
||||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options", "status"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}", model.NotActivicated),
|
||||
policyRows: sqlmock.NewRows([]string{"name", "type", "options"}).
|
||||
AddRow("默认存储策略", "local", "{\"op_name\":\"123\"}"),
|
||||
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin"}`,
|
||||
expected: serializer.Err(403, "该账号未激活", nil),
|
||||
},
|
||||
}
|
||||
|
||||
for k, testCase := range testCases {
|
||||
if testCase.settingRows != nil {
|
||||
mock.ExpectQuery("^SELECT (.+)").WillReturnRows(testCase.settingRows)
|
||||
}
|
||||
if testCase.userRows != nil {
|
||||
mock.ExpectQuery("^SELECT (.+)").WillReturnRows(testCase.userRows)
|
||||
}
|
||||
if testCase.policyRows != nil {
|
||||
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND \\(\\(`policies`.`id` = 1\\)\\)(.+)$").WillReturnRows(testCase.policyRows)
|
||||
}
|
||||
req, _ := http.NewRequest(
|
||||
"POST",
|
||||
"/api/v3/user/session",
|
||||
bytes.NewReader([]byte(testCase.reqBody)),
|
||||
)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
asserts.Equal(200, w.Code)
|
||||
expectedJSON, _ := json.Marshal(testCase.expected)
|
||||
asserts.JSONEq(string(expectedJSON), w.Body.String(), "测试用例:%d", k)
|
||||
|
||||
w.Body.Reset()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
model.ClearCache()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestSessionAuthCheck(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
router := InitRouter()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
mock.ExpectQuery("^SELECT (.+)").WillReturnRows(sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"))
|
||||
expectedUser, _ := model.GetUserByID(1)
|
||||
|
||||
testCases := []struct {
|
||||
userRows *sqlmock.Rows
|
||||
sessionMock map[string]interface{}
|
||||
contextMock map[string]interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
// 未登录
|
||||
{
|
||||
expected: serializer.CheckLogin(),
|
||||
},
|
||||
// 登录正常
|
||||
{
|
||||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"),
|
||||
sessionMock: map[string]interface{}{"user_id": 1},
|
||||
expected: serializer.BuildUserResponse(expectedUser),
|
||||
},
|
||||
// UID不存在
|
||||
{
|
||||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}),
|
||||
sessionMock: map[string]interface{}{"user_id": -1},
|
||||
expected: serializer.CheckLogin(),
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
req, _ := http.NewRequest(
|
||||
"GET",
|
||||
"/api/v3/user/me",
|
||||
nil,
|
||||
)
|
||||
if testCase.userRows != nil {
|
||||
mock.ExpectQuery("^SELECT (.+)").WillReturnRows(testCase.userRows)
|
||||
}
|
||||
middleware.ContextMock = testCase.contextMock
|
||||
middleware.SessionMock = testCase.sessionMock
|
||||
router.ServeHTTP(w, req)
|
||||
expectedJSON, _ := json.Marshal(testCase.expected)
|
||||
|
||||
asserts.Equal(200, w.Code)
|
||||
asserts.JSONEq(string(expectedJSON), w.Body.String())
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
|
||||
w.Body.Reset()
|
||||
}
|
||||
|
||||
}
|
||||
//func TestUserSession(t *testing.T) {
|
||||
// mutex.Lock()
|
||||
// defer mutex.Unlock()
|
||||
// switchToMockDB()
|
||||
// asserts := assert.New(t)
|
||||
// router := InitRouter()
|
||||
// w := httptest.NewRecorder()
|
||||
//
|
||||
// // 创建测试用验证码
|
||||
// var configD = base64Captcha.ConfigDigit{
|
||||
// Height: 80,
|
||||
// Width: 240,
|
||||
// MaxSkew: 0.7,
|
||||
// DotCount: 80,
|
||||
// CaptchaLen: 1,
|
||||
// }
|
||||
// idKeyD, _ := base64Captcha.GenerateCaptcha("", configD)
|
||||
// middleware.ContextMock = map[string]interface{}{
|
||||
// "captchaID": idKeyD,
|
||||
// }
|
||||
//
|
||||
// testCases := []struct {
|
||||
// settingRows *sqlmock.Rows
|
||||
// userRows *sqlmock.Rows
|
||||
// policyRows *sqlmock.Rows
|
||||
// reqBody string
|
||||
// expected interface{}
|
||||
// }{
|
||||
// // 登录信息正确,不需要验证码
|
||||
// {
|
||||
// settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
// AddRow("login_captcha", "0", "login"),
|
||||
// userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
// AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"),
|
||||
// expected: serializer.BuildUserResponse(model.User{
|
||||
// Email: "admin@cloudreve.org",
|
||||
// Nick: "admin",
|
||||
// Policy: model.Policy{
|
||||
// Type: "local",
|
||||
// OptionsSerialized: model.PolicyOption{FileType: []string{}},
|
||||
// },
|
||||
// }),
|
||||
// },
|
||||
// // 登录信息正确,需要验证码,验证码错误
|
||||
// {
|
||||
// settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
// AddRow("login_captcha", "1", "login"),
|
||||
// userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
// AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"),
|
||||
// expected: serializer.ParamErr("验证码错误", nil),
|
||||
// },
|
||||
// // 邮箱正确密码错误
|
||||
// {
|
||||
// settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
// AddRow("login_captcha", "0", "login"),
|
||||
// userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
// AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"),
|
||||
// expected: serializer.Err(401, "用户邮箱或密码错误", nil),
|
||||
// },
|
||||
// //邮箱格式不正确
|
||||
// {
|
||||
// reqBody: `{"userName":"admin@cloudreve","captchaCode":"captchaCode","Password":"admin123"}`,
|
||||
// expected: serializer.Err(40001, "邮箱格式不正确", errors.New("Key: 'UserLoginService.UserName' Error:Field validation for 'UserName' failed on the 'email' tag")),
|
||||
// },
|
||||
// // 用户被Ban
|
||||
// {
|
||||
// settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
// AddRow("login_captcha", "0", "login"),
|
||||
// userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options", "status"}).
|
||||
// AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}", model.Baned),
|
||||
// expected: serializer.Err(403, "该账号已被封禁", nil),
|
||||
// },
|
||||
// // 用户未激活
|
||||
// {
|
||||
// settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||
// AddRow("login_captcha", "0", "login"),
|
||||
// userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options", "status"}).
|
||||
// AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}", model.NotActivicated),
|
||||
// expected: serializer.Err(403, "该账号未激活", nil),
|
||||
// },
|
||||
// }
|
||||
//
|
||||
// for k, testCase := range testCases {
|
||||
// if testCase.settingRows != nil {
|
||||
// mock.ExpectQuery("^SELECT (.+)").WillReturnRows(testCase.settingRows)
|
||||
// }
|
||||
// if testCase.userRows != nil {
|
||||
// mock.ExpectQuery("^SELECT (.+)").WillReturnRows(testCase.userRows)
|
||||
// }
|
||||
// if testCase.policyRows != nil {
|
||||
// mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND \\(\\(`policies`.`id` = 1\\)\\)(.+)$").WillReturnRows(testCase.policyRows)
|
||||
// }
|
||||
// req, _ := http.NewRequest(
|
||||
// "POST",
|
||||
// "/api/v3/user/session",
|
||||
// bytes.NewReader([]byte(testCase.reqBody)),
|
||||
// )
|
||||
// router.ServeHTTP(w, req)
|
||||
//
|
||||
// asserts.Equal(200, w.Code)
|
||||
// expectedJSON, _ := json.Marshal(testCase.expected)
|
||||
// asserts.JSONEq(string(expectedJSON), w.Body.String(), "测试用例:%d", k)
|
||||
//
|
||||
// w.Body.Reset()
|
||||
// asserts.NoError(mock.ExpectationsWereMet())
|
||||
// model.ClearCache()
|
||||
// }
|
||||
//
|
||||
//}
|
||||
//
|
||||
//func TestSessionAuthCheck(t *testing.T) {
|
||||
// mutex.Lock()
|
||||
// defer mutex.Unlock()
|
||||
// switchToMockDB()
|
||||
// asserts := assert.New(t)
|
||||
// router := InitRouter()
|
||||
// w := httptest.NewRecorder()
|
||||
//
|
||||
// mock.ExpectQuery("^SELECT (.+)").WillReturnRows(sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
// AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"))
|
||||
// expectedUser, _ := model.GetUserByID(1)
|
||||
//
|
||||
// testCases := []struct {
|
||||
// userRows *sqlmock.Rows
|
||||
// sessionMock map[string]interface{}
|
||||
// contextMock map[string]interface{}
|
||||
// expected interface{}
|
||||
// }{
|
||||
// // 未登录
|
||||
// {
|
||||
// expected: serializer.CheckLogin(),
|
||||
// },
|
||||
// // 登录正常
|
||||
// {
|
||||
// userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
// AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"),
|
||||
// sessionMock: map[string]interface{}{"user_id": 1},
|
||||
// expected: serializer.BuildUserResponse(expectedUser),
|
||||
// },
|
||||
// // UID不存在
|
||||
// {
|
||||
// userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}),
|
||||
// sessionMock: map[string]interface{}{"user_id": -1},
|
||||
// expected: serializer.CheckLogin(),
|
||||
// },
|
||||
// }
|
||||
//
|
||||
// for _, testCase := range testCases {
|
||||
// req, _ := http.NewRequest(
|
||||
// "GET",
|
||||
// "/api/v3/user/me",
|
||||
// nil,
|
||||
// )
|
||||
// if testCase.userRows != nil {
|
||||
// mock.ExpectQuery("^SELECT (.+)").WillReturnRows(testCase.userRows)
|
||||
// }
|
||||
// middleware.ContextMock = testCase.contextMock
|
||||
// middleware.SessionMock = testCase.sessionMock
|
||||
// router.ServeHTTP(w, req)
|
||||
// expectedJSON, _ := json.Marshal(testCase.expected)
|
||||
//
|
||||
// asserts.Equal(200, w.Code)
|
||||
// asserts.JSONEq(string(expectedJSON), w.Body.String())
|
||||
// asserts.NoError(mock.ExpectationsWereMet())
|
||||
//
|
||||
// w.Body.Reset()
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
||||
func TestSiteConfigRoute(t *testing.T) {
|
||||
switchToMemDB()
|
||||
|
@ -263,98 +247,3 @@ func TestSiteConfigRoute(t *testing.T) {
|
|||
},
|
||||
}).UpdateColumn("name", "siteName")
|
||||
}
|
||||
|
||||
func TestListDirectoryRoute(t *testing.T) {
|
||||
switchToMemDB()
|
||||
asserts := assert.New(t)
|
||||
router := InitRouter()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// 成功
|
||||
req, _ := http.NewRequest(
|
||||
"GET",
|
||||
"/api/v3/directory/",
|
||||
nil,
|
||||
)
|
||||
middleware.SessionMock = map[string]interface{}{"user_id": 1}
|
||||
router.ServeHTTP(w, req)
|
||||
asserts.Equal(200, w.Code)
|
||||
resJSON := &serializer.Response{}
|
||||
err := json.Unmarshal(w.Body.Bytes(), resJSON)
|
||||
asserts.NoError(err)
|
||||
asserts.Equal(0, resJSON.Code)
|
||||
|
||||
w.Body.Reset()
|
||||
|
||||
}
|
||||
|
||||
func TestLocalFileUpload(t *testing.T) {
|
||||
switchToMemDB()
|
||||
asserts := assert.New(t)
|
||||
router := InitRouter()
|
||||
w := httptest.NewRecorder()
|
||||
middleware.SessionMock = map[string]interface{}{"user_id": 1}
|
||||
|
||||
testCases := []struct {
|
||||
GetRequest func() *http.Request
|
||||
ExpectCode int
|
||||
RollBack func()
|
||||
}{
|
||||
// 文件大小指定错误
|
||||
{
|
||||
GetRequest: func() *http.Request {
|
||||
req, _ := http.NewRequest(
|
||||
"POST",
|
||||
"/api/v3/file/upload",
|
||||
nil,
|
||||
)
|
||||
req.Header.Add("Content-Length", "ddf")
|
||||
return req
|
||||
},
|
||||
ExpectCode: 40001,
|
||||
},
|
||||
// 返回错误
|
||||
{
|
||||
GetRequest: func() *http.Request {
|
||||
req, _ := http.NewRequest(
|
||||
"POST",
|
||||
"/api/v3/file/upload",
|
||||
strings.NewReader("2333"),
|
||||
)
|
||||
req.Header.Add("Content-Length", "4")
|
||||
req.Header.Add("X-FileName", "大地的%sfsf")
|
||||
return req
|
||||
},
|
||||
ExpectCode: 40002,
|
||||
},
|
||||
// 成功
|
||||
{
|
||||
GetRequest: func() *http.Request {
|
||||
req, _ := http.NewRequest(
|
||||
"POST",
|
||||
"/api/v3/file/upload",
|
||||
strings.NewReader("2333"),
|
||||
)
|
||||
req.Header.Add("Content-Length", "4")
|
||||
req.Header.Add("X-FileName", "TestFileUploadRoute.txt")
|
||||
return req
|
||||
},
|
||||
ExpectCode: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for key, testCase := range testCases {
|
||||
req := testCase.GetRequest()
|
||||
router.ServeHTTP(w, req)
|
||||
asserts.Equal(200, w.Code)
|
||||
resJSON := &serializer.Response{}
|
||||
err := json.Unmarshal(w.Body.Bytes(), resJSON)
|
||||
asserts.NoError(err, "测试用例%d", key)
|
||||
asserts.Equal(testCase.ExpectCode, resJSON.Code, "测试用例%d", key)
|
||||
if testCase.RollBack != nil {
|
||||
testCase.RollBack()
|
||||
}
|
||||
w.Body.Reset()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue