mirror of https://github.com/cloudreve/Cloudreve
Test: captcha
parent
9660d2f9c1
commit
41e0dec74c
|
@ -35,7 +35,7 @@ type captcha struct {
|
||||||
IsShowNoiseText bool
|
IsShowNoiseText bool
|
||||||
IsShowSlimeLine bool
|
IsShowSlimeLine bool
|
||||||
IsShowSineLine bool
|
IsShowSineLine bool
|
||||||
CaptchaLen int `validate:"gte=0"`
|
CaptchaLen int `validate:"gt=0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DatabaseConfig 数据库配置
|
// DatabaseConfig 数据库配置
|
||||||
|
@ -72,7 +72,7 @@ func Init(path string) {
|
||||||
//TODO 配置合法性验证
|
//TODO 配置合法性验证
|
||||||
cfg, err = ini.Load(path)
|
cfg, err = ini.Load(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.Log().Panic("无法解析配置文件 '%s': ", path, err)
|
util.Log().Panic("无法解析配置文件 '%s': %s", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sections := map[string]interface{}{
|
sections := map[string]interface{}{
|
||||||
|
@ -83,7 +83,7 @@ func Init(path string) {
|
||||||
for sectionName, sectionStruct := range sections {
|
for sectionName, sectionStruct := range sections {
|
||||||
err = mapSection(sectionName, sectionStruct)
|
err = mapSection(sectionName, sectionStruct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.Log().Warning("配置文件 %s 分区解析失败: ", sectionName, err)
|
util.Log().Warning("配置文件 %s 分区解析失败: %s", sectionName, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ func Captcha(c *gin.Context) {
|
||||||
util.SetSession(c, map[string]interface{}{
|
util.SetSession(c, map[string]interface{}{
|
||||||
"captchaID": idKeyD,
|
"captchaID": idKeyD,
|
||||||
})
|
})
|
||||||
|
|
||||||
// 将验证码图像编码为Base64
|
// 将验证码图像编码为Base64
|
||||||
base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD)
|
base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD)
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,18 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var r *gin.Engine
|
||||||
|
|
||||||
|
// SetupTestEngine 设置测试用gin.Engine
|
||||||
|
func SetupTestEngine(engine *gin.Engine) {
|
||||||
|
r = engine
|
||||||
|
}
|
||||||
|
|
||||||
// InitRouter 初始化路由
|
// InitRouter 初始化路由
|
||||||
func InitRouter() *gin.Engine {
|
func InitRouter() *gin.Engine {
|
||||||
r := gin.Default()
|
if r == nil {
|
||||||
|
r = gin.Default()
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
中间件
|
中间件
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/DATA-DOG/go-sqlmock"
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
"github.com/mojocn/base64Captcha"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
@ -47,11 +48,41 @@ func TestPing(t *testing.T) {
|
||||||
asserts.Contains(w.Body.String(), "Pong")
|
asserts.Contains(w.Body.String(), "Pong")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCaptcha(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
router := InitRouter()
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
req, _ := http.NewRequest(
|
||||||
|
"GET",
|
||||||
|
"/Api/V3/Captcha",
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
asserts.Equal(200, w.Code)
|
||||||
|
asserts.Contains(w.Body.String(), "base64")
|
||||||
|
}
|
||||||
|
|
||||||
func TestUserSession(t *testing.T) {
|
func TestUserSession(t *testing.T) {
|
||||||
asserts := assert.New(t)
|
asserts := assert.New(t)
|
||||||
router := InitRouter()
|
router := InitRouter()
|
||||||
w := httptest.NewRecorder()
|
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 {
|
testCases := []struct {
|
||||||
settingRows *sqlmock.Rows
|
settingRows *sqlmock.Rows
|
||||||
userRows *sqlmock.Rows
|
userRows *sqlmock.Rows
|
||||||
|
@ -70,6 +101,15 @@ func TestUserSession(t *testing.T) {
|
||||||
Nick: "admin",
|
Nick: "admin",
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
// 登录信息正确,需要验证码,验证码错误
|
||||||
|
{
|
||||||
|
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", "{}"),
|
||||||
|
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin"}`,
|
||||||
|
expected: serializer.ParamErr("验证码错误", nil),
|
||||||
|
},
|
||||||
// 邮箱正确密码错误
|
// 邮箱正确密码错误
|
||||||
{
|
{
|
||||||
settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
settingRows: sqlmock.NewRows([]string{"name", "value", "type"}).
|
||||||
|
@ -104,7 +144,7 @@ func TestUserSession(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
for k, testCase := range testCases {
|
||||||
if testCase.settingRows != nil {
|
if testCase.settingRows != nil {
|
||||||
mock.ExpectQuery("^SELECT (.+)").WillReturnRows(testCase.settingRows)
|
mock.ExpectQuery("^SELECT (.+)").WillReturnRows(testCase.settingRows)
|
||||||
}
|
}
|
||||||
|
@ -120,7 +160,7 @@ func TestUserSession(t *testing.T) {
|
||||||
|
|
||||||
asserts.Equal(200, w.Code)
|
asserts.Equal(200, w.Code)
|
||||||
expectedJSON, _ := json.Marshal(testCase.expected)
|
expectedJSON, _ := json.Marshal(testCase.expected)
|
||||||
asserts.JSONEq(string(expectedJSON), w.Body.String())
|
asserts.JSONEq(string(expectedJSON), w.Body.String(), "测试用例:%d", k)
|
||||||
|
|
||||||
w.Body.Reset()
|
w.Body.Reset()
|
||||||
asserts.NoError(mock.ExpectationsWereMet())
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
|
|
|
@ -26,7 +26,6 @@ func (service *UserLoginService) Login(c *gin.Context) serializer.Response {
|
||||||
// TODO 验证码校验
|
// TODO 验证码校验
|
||||||
captchaID := util.GetSession(c, "captchaID")
|
captchaID := util.GetSession(c, "captchaID")
|
||||||
if captchaID == nil || !base64Captcha.VerifyCaptcha(captchaID.(string), service.CaptchaCode) {
|
if captchaID == nil || !base64Captcha.VerifyCaptcha(captchaID.(string), service.CaptchaCode) {
|
||||||
util.DeleteSession(c, "captchaID")
|
|
||||||
return serializer.ParamErr("验证码错误", nil)
|
return serializer.ParamErr("验证码错误", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue