Test: middleware/auth
This commit is contained in:
parent
589c399488
commit
c8c85501e4
5 changed files with 78 additions and 14 deletions
56
middleware/auth_test.go
Normal file
56
middleware/auth_test.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"cloudreve/models"
|
||||
"cloudreve/pkg/util"
|
||||
"database/sql"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var mock sqlmock.Sqlmock
|
||||
|
||||
// TestMain 初始化数据库Mock
|
||||
func TestMain(m *testing.M) {
|
||||
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 TestCurrentUser(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request, _ = http.NewRequest("GET", "/test", nil)
|
||||
|
||||
//session为空
|
||||
sessionFunc := Session("233")
|
||||
sessionFunc(c)
|
||||
CurrentUser()(c)
|
||||
user, _ := c.Get("user")
|
||||
asserts.Nil(user)
|
||||
|
||||
//session正确
|
||||
c, _ = gin.CreateTestContext(rec)
|
||||
c.Request, _ = http.NewRequest("GET", "/test", nil)
|
||||
sessionFunc(c)
|
||||
util.SetSession(c, map[string]interface{}{"user_id": 1})
|
||||
rows := sqlmock.NewRows([]string{"id", "deleted_at", "email", "options"}).
|
||||
AddRow(1, nil, "admin@cloudreve.org", "{}")
|
||||
mock.ExpectQuery("^SELECT (.+)").WillReturnRows(rows)
|
||||
CurrentUser()(c)
|
||||
user, _ = c.Get("user")
|
||||
asserts.NotNil(user)
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"cloudreve/pkg/serializer"
|
||||
"encoding/json"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/jinzhu/gorm"
|
||||
|
@ -14,8 +13,8 @@ func TestGetUserByID(t *testing.T) {
|
|||
asserts := assert.New(t)
|
||||
|
||||
//找到用户时
|
||||
rows := sqlmock.NewRows([]string{"id", "deleted_at", "email"}).
|
||||
AddRow(1, nil, "admin@cloudreve.org")
|
||||
rows := sqlmock.NewRows([]string{"id", "deleted_at", "email", "options"}).
|
||||
AddRow(1, nil, "admin@cloudreve.org", "{}")
|
||||
|
||||
mock.ExpectQuery("^SELECT (.+)").WillReturnRows(rows)
|
||||
|
||||
|
@ -81,7 +80,7 @@ func TestUser_AfterFind(t *testing.T) {
|
|||
|
||||
newUser := NewUser()
|
||||
err := newUser.AfterFind()
|
||||
expected := serializer.UserOption{}
|
||||
expected := UserOption{}
|
||||
err = json.Unmarshal([]byte(newUser.Options), &expected)
|
||||
|
||||
asserts.NoError(err)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package user
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/gin-contrib/sessions"
|
|
@ -3,7 +3,10 @@ package routers
|
|||
import (
|
||||
"bytes"
|
||||
"cloudreve/models"
|
||||
"cloudreve/pkg/serializer"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -48,7 +51,7 @@ func TestUserSession(t *testing.T) {
|
|||
settingRows *sqlmock.Rows
|
||||
userRows *sqlmock.Rows
|
||||
reqBody string
|
||||
expected string
|
||||
expected interface{}
|
||||
}{
|
||||
// 登录信息正确,不需要验证码
|
||||
{
|
||||
|
@ -56,8 +59,11 @@ func TestUserSession(t *testing.T) {
|
|||
AddRow("login_captcha", "0", "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: `{"code":0,"data":{"ID":0,"CreatedAt":"0001-01-01T00:00:00Z","UpdatedAt":"0001-01-01T00:00:00Z","DeletedAt":null,"Email":"admin@cloudreve.org","Nick":"admin","Status":0,"Group":0,"PrimaryGroup":0,"Storage":0,"LastNotify":null,"Delay":0,"Avatar":"","OptionsSerialized":{"profile_on":0,"webdav_key":""}},"msg":""}`,
|
||||
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin"}`,
|
||||
expected: serializer.BuildUserResponse(model.User{
|
||||
Email: "admin@cloudreve.org",
|
||||
Nick: "admin",
|
||||
}),
|
||||
},
|
||||
// 邮箱正确密码错误
|
||||
{
|
||||
|
@ -66,12 +72,12 @@ func TestUserSession(t *testing.T) {
|
|||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}"),
|
||||
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin123"}`,
|
||||
expected: `{"code":401, "msg":"用户邮箱或密码错误"}`,
|
||||
expected: serializer.Err(401, "用户邮箱或密码错误", nil),
|
||||
},
|
||||
//邮箱格式不正确
|
||||
{
|
||||
reqBody: `{"userName":"admin@cloudreve","captchaCode":"captchaCode","Password":"admin123"}`,
|
||||
expected: `{"code":40001, "error":"Key: 'UserLoginService.UserName' Error:Field validation for 'UserName' failed on the 'email' tag", "msg":"邮箱格式不正确"}`,
|
||||
expected: serializer.Err(40001, "邮箱格式不正确", errors.New("Key: 'UserLoginService.UserName' Error:Field validation for 'UserName' failed on the 'email' tag")),
|
||||
},
|
||||
// 用户被Ban
|
||||
{
|
||||
|
@ -80,7 +86,7 @@ func TestUserSession(t *testing.T) {
|
|||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options", "status"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}", model.Baned),
|
||||
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin"}`,
|
||||
expected: `{"code":403, "msg":"该账号已被封禁"}`,
|
||||
expected: serializer.Err(403, "该账号已被封禁", nil),
|
||||
},
|
||||
// 用户未激活
|
||||
{
|
||||
|
@ -89,7 +95,7 @@ func TestUserSession(t *testing.T) {
|
|||
userRows: sqlmock.NewRows([]string{"email", "nick", "password", "options", "status"}).
|
||||
AddRow("admin@cloudreve.org", "admin", "CKLmDKa1C9SD64vU:76adadd4fd4bad86959155f6f7bc8993c94e7adf", "{}", model.NotActivicated),
|
||||
reqBody: `{"userName":"admin@cloudreve.org","captchaCode":"captchaCode","Password":"admin"}`,
|
||||
expected: `{"code":403, "msg":"该账号未激活"}`,
|
||||
expected: serializer.Err(403, "该账号未激活", nil),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -108,7 +114,9 @@ func TestUserSession(t *testing.T) {
|
|||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
asserts.JSONEq(testCase.expected, w.Body.String())
|
||||
expectedJson, _ := json.Marshal(testCase.expected)
|
||||
asserts.JSONEq(string(expectedJson), w.Body.String())
|
||||
|
||||
w.Body.Reset()
|
||||
asserts.NoError(mock.ExpectationsWereMet())
|
||||
model.ClearCache()
|
||||
|
|
|
@ -3,6 +3,7 @@ package user
|
|||
import (
|
||||
"cloudreve/models"
|
||||
"cloudreve/pkg/serializer"
|
||||
"cloudreve/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -42,7 +43,7 @@ func (service *UserLoginService) Login(c *gin.Context) serializer.Response {
|
|||
}
|
||||
|
||||
//登陆成功,清空并设置session
|
||||
SetSession(c, map[string]interface{}{
|
||||
util.SetSession(c, map[string]interface{}{
|
||||
"user_id": expectedUser.ID,
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue