feat: sync deployed v1.0.0-rc.38-custom.67
This commit is contained in:
1 parent
d09216bbde
commit
8e6ed0317b
154 files changed
+39540
-20509
No files matched your search
@@ -0,0 +1,65 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestPersonalRatioAdminAuthorization(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&model.User{}, &model.PersonalGroupRatio{}, &model.AuditLog{}))
|
||||
oldLog := model.LOG_DB
|
||||
model.LOG_DB = db
|
||||
old, oldRedis := model.DB, common.RedisEnabled
|
||||
model.DB, common.RedisEnabled = db, false
|
||||
t.Cleanup(func() {
|
||||
model.LOG_DB = oldLog
|
||||
model.DB, common.RedisEnabled = old, oldRedis
|
||||
conn, _ := db.DB()
|
||||
_ = conn.Close()
|
||||
})
|
||||
user := model.User{Id: 12, Username: "personal-test", Role: common.RoleCommonUser}
|
||||
require.NoError(t, db.Create(&user).Error)
|
||||
for _, role := range []int{common.RoleCommonUser, common.RoleAdminUser, common.RoleRootUser} {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Set("role", role)
|
||||
c.Params = gin.Params{{Key: "id", Value: "12"}}
|
||||
c.Request = httptest.NewRequest("PUT", "/api/user/12/group-ratios", bytes.NewBufferString("{\"ratios\":{\"default\":0}}"))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
UpdateUserGroupRatios(c)
|
||||
if role == common.RoleCommonUser {
|
||||
require.Equal(t, 403, w.Code)
|
||||
} else {
|
||||
var body struct{ Success bool }
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
||||
require.True(t, body.Success, w.Body.String())
|
||||
}
|
||||
}
|
||||
var auditCount int64
|
||||
require.NoError(t, db.Model(&model.AuditLog{}).Where("action = ?", "user.group_ratios.update").Count(&auditCount).Error)
|
||||
require.EqualValues(t, 2, auditCount)
|
||||
value, err := model.GetPersonalGroupRatios(context.Background(), 12)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0.0, value["default"])
|
||||
require.NoError(t, db.Model(&user).Update("role", common.RoleAdminUser).Error)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Set("role", common.RoleAdminUser)
|
||||
c.Params = gin.Params{{Key: "id", Value: fmt.Sprint(user.Id)}}
|
||||
c.Request = httptest.NewRequest("GET", "/api/user/12/group-ratios", nil)
|
||||
GetUserGroupRatios(c)
|
||||
require.Equal(t, 403, w.Code)
|
||||
}
|
||||
Reference in new issue
Block a user