From 948059ec1c0da1dcc8994a3c9b9a9d5dd318c332 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sat, 23 Nov 2019 15:09:46 +0800 Subject: [PATCH] Feat: CORS / Test: site/config route --- conf/conf.ini | 2 +- go.mod | 6 ++-- models/init.go | 2 +- models/migration.go | 7 +++-- pkg/filesystem/local/file.go | 34 +------------------- routers/main_test.go | 40 +++++++++++++++++++++++ routers/router.go | 9 ++++++ routers/router_test.go | 61 +++++++++++++++++++++++------------- 8 files changed, 100 insertions(+), 61 deletions(-) create mode 100644 routers/main_test.go diff --git a/conf/conf.ini b/conf/conf.ini index 57a5394..adb5a67 100644 --- a/conf/conf.ini +++ b/conf/conf.ini @@ -12,7 +12,7 @@ TablePrefix = v3_ [Captcha] Height = 60 -Width = 240 +Width = 200 Mode = 3 ComplexOfNoiseText = 0 ComplexOfNoiseDot = 0 diff --git a/go.mod b/go.mod index 7a2db66..53094b8 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/models/init.go b/models/init.go index 6e9bf3c..b6c98ff 100644 --- a/models/init.go +++ b/models/init.go @@ -45,7 +45,7 @@ func Init() { return conf.DatabaseConfig.TablePrefix + defaultTableName } - // Debug 模式下,输出所有 SQL 日志 + // Debug模式下,输出所有 SQL 日志 if conf.SystemConfig.Debug { db.LogMode(true) } diff --git a/models/migration.go b/models/migration.go index 40e6d5f..52ba95a 100644 --- a/models/migration.go +++ b/models/migration.go @@ -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() { diff --git a/pkg/filesystem/local/file.go b/pkg/filesystem/local/file.go index c605079..6bad732 100644 --- a/pkg/filesystem/local/file.go +++ b/pkg/filesystem/local/file.go @@ -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 diff --git a/routers/main_test.go b/routers/main_test.go new file mode 100644 index 0000000..919a077 --- /dev/null +++ b/routers/main_test.go @@ -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 +} diff --git a/routers/router.go b/routers/router.go index acf6fba..3b3ffcd 100644 --- a/routers/router.go +++ b/routers/router.go @@ -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()) diff --git a/routers/router_test.go b/routers/router_test.go index b45732c..ec7d83d 100644 --- a/routers/router_test.go +++ b/routers/router_test.go @@ -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") +}