Feat: CORS / Test: site/config route

pull/247/head
HFO4 5 years ago
parent 370a1a0c9f
commit 948059ec1c

@ -12,7 +12,7 @@ TablePrefix = v3_
[Captcha]
Height = 60
Width = 240
Width = 200
Mode = 3
ComplexOfNoiseText = 0
ComplexOfNoiseDot = 0

@ -5,17 +5,17 @@ go 1.12
require (
github.com/DATA-DOG/go-sqlmock v1.3.3
github.com/fatih/color v1.7.0
github.com/gin-contrib/cors v1.3.0
github.com/gin-contrib/sessions v0.0.1
github.com/gin-gonic/gin v1.4.0
github.com/go-ini/ini v1.50.0
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/jinzhu/gorm v1.9.11
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2
github.com/mojocn/base64Captcha v0.0.0-20190801020520-752b1cd608b2
github.com/pkg/errors v0.8.0
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.4.0
gopkg.in/go-playground/validator.v8 v8.18.2
gopkg.in/ini.v1 v1.51.0 // indirect
)

@ -45,7 +45,7 @@ func Init() {
return conf.DatabaseConfig.TablePrefix + defaultTableName
}
// Debug 模式下,输出所有 SQL 日志
// Debug模式下,输出所有 SQL 日志
if conf.SystemConfig.Debug {
db.LogMode(true)
}

@ -3,6 +3,7 @@ package model
import (
"github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/mcuadros/go-version"
"io/ioutil"
@ -11,8 +12,8 @@ import (
//执行数据迁移
func migration() {
// 检查 version.lock 确认是否需要执行迁移
// Debug 模式下一定会执行迁移
if !conf.SystemConfig.Debug {
// Debug 模式及测试模式下一定会执行迁移
if !conf.SystemConfig.Debug && gin.Mode() != gin.TestMode {
if util.Exists("version.lock") {
versionLock, _ := ioutil.ReadFile("version.lock")
if version.Compare(string(versionLock), conf.BackendVersion, "=") {
@ -48,6 +49,8 @@ func migration() {
util.Log().Warning("无法写入版本控制锁 version.lock, %s", err)
}
util.Log().Info("数据库自动迁移结束")
}
func addDefaultPolicy() {

@ -2,41 +2,9 @@ package local
import (
"io"
"mime/multipart"
)
// FileData 上传的文件数据
type FileData struct {
File multipart.File
Size uint64
Name string
MIMEType string
}
func (file FileData) Read(p []byte) (n int, err error) {
return file.File.Read(p)
}
func (file FileData) GetMIMEType() string {
return file.MIMEType
}
func (file FileData) GetSize() uint64 {
return file.Size
}
func (file FileData) Close() error {
return file.File.Close()
}
func (file FileData) GetFileName() string {
return file.Name
}
func (file FileData) GetVirtualPath() string {
return file.Name
}
// FileStream 用户传来的文件
type FileStream struct {
File io.ReadCloser
Size uint64

@ -0,0 +1,40 @@
package routers
import (
"database/sql"
"github.com/DATA-DOG/go-sqlmock"
model "github.com/HFO4/cloudreve/models"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"testing"
)
var mock sqlmock.Sqlmock
var memDB *gorm.DB
// TestMain 初始化数据库Mock
func TestMain(m *testing.M) {
// 设置gin为测试模式
gin.SetMode(gin.TestMode)
// 初始化sqlmock
var db *sql.DB
var err error
db, mock, err = sqlmock.New()
if err != nil {
panic("An error was not expected when opening a stub database connection")
}
// 初始话内存数据库
model.Init()
memDB = model.DB
model.DB, _ = gorm.Open("mysql", db)
defer db.Close()
m.Run()
}
func switchToMemDB() {
model.DB = memDB
}

@ -4,6 +4,7 @@ import (
"github.com/HFO4/cloudreve/middleware"
"github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/routers/controllers"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
@ -16,6 +17,14 @@ func InitRouter() *gin.Engine {
*/
r.Use(middleware.Session(conf.SystemConfig.SessionSecret))
// CORS TODO: 根据配置文件来
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:3000"},
AllowMethods: []string{"PUT", "POST", "GET", "OPTIONS"},
AllowHeaders: []string{"X-PINGOTHER", "Content-Type"},
AllowCredentials: true,
}))
// 测试模式加入Mock助手中间件
if gin.Mode() == gin.TestMode {
r.Use(middleware.MockHelper())

@ -2,14 +2,12 @@ package routers
import (
"bytes"
"database/sql"
"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/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/mojocn/base64Captcha"
"github.com/stretchr/testify/assert"
@ -18,25 +16,6 @@ import (
"testing"
)
var mock sqlmock.Sqlmock
// TestMain 初始化数据库Mock
func TestMain(m *testing.M) {
// 设置gin为测试模式
gin.SetMode(gin.TestMode)
var db *sql.DB
var err error
db, mock, err = sqlmock.New()
if err != nil {
panic("An error was not expected when opening a stub database connection")
}
model.DB, _ = gorm.Open("mysql", db)
defer db.Close()
m.Run()
}
func TestPing(t *testing.T) {
asserts := assert.New(t)
router := InitRouter()
@ -239,3 +218,43 @@ func TestSessionAuthCheck(t *testing.T) {
}
}
func TestSiteConfigRoute(t *testing.T) {
switchToMemDB()
asserts := assert.New(t)
router := InitRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest(
"GET",
"/Api/V3/Site/Config",
nil,
)
router.ServeHTTP(w, req)
asserts.Equal(200, w.Code)
asserts.Contains(w.Body.String(), "Cloudreve")
w.Body.Reset()
// 消除无效值
model.DB.Model(&model.Setting{
Model: gorm.Model{
ID: 2,
},
}).UpdateColumn("name", "siteName_b")
req, _ = http.NewRequest(
"GET",
"/Api/V3/Site/Config",
nil,
)
router.ServeHTTP(w, req)
asserts.Equal(200, w.Code)
asserts.Contains(w.Body.String(), "\"title\":\"\"")
model.DB.Model(&model.Setting{
Model: gorm.Model{
ID: 2,
},
}).UpdateColumn("name", "siteName")
}

Loading…
Cancel
Save