Based on QuantumNous/new-api v1.0.0-rc.36 (shallow history squashed). - CNY quota display mode: RMB-denominated top-up and crediting across Epay/Waffo channels - New CheckinButton in both authenticated and public headers (Turnstile supported) - New /subscribe page with public plan snapshot, Alipay/balance payment, auto group upgrade/reset - New /docs 12-section beginner guide with anchor navigation - New home PromoBanner linking to /docs; i18n synced (en/zh/zh-TW/fr/ja/ru/vi) - README: document custom features, live demo https://ai.88531.cn
49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/constant"
|
|
"github.com/QuantumNous/new-api/model"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newTokenAutoGroupsContext() *gin.Context {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
return ctx
|
|
}
|
|
|
|
func TestSetupContextForTokenPreservesCustomAutoGroupsOrder(t *testing.T) {
|
|
ctx := newTokenAutoGroupsContext()
|
|
token := &model.Token{Id: 1, UserId: 2, AutoGroups: `["vip","default"]`}
|
|
|
|
require.NoError(t, SetupContextForToken(ctx, token))
|
|
value, ok := common.GetContextKey(ctx, constant.ContextKeyTokenAutoGroups)
|
|
require.True(t, ok)
|
|
assert.Equal(t, []string{"vip", "default"}, value)
|
|
}
|
|
|
|
func TestSetupContextForTokenTreatsStoredEmptyArrayAsInheritance(t *testing.T) {
|
|
ctx := newTokenAutoGroupsContext()
|
|
token := &model.Token{Id: 1, UserId: 2, AutoGroups: `[]`}
|
|
|
|
require.NoError(t, SetupContextForToken(ctx, token))
|
|
_, ok := common.GetContextKey(ctx, constant.ContextKeyTokenAutoGroups)
|
|
assert.False(t, ok)
|
|
}
|
|
|
|
func TestSetupContextForTokenMalformedAutoGroupsFailsClosed(t *testing.T) {
|
|
ctx := newTokenAutoGroupsContext()
|
|
token := &model.Token{Id: 1, UserId: 2, AutoGroups: `not-json`}
|
|
|
|
require.NoError(t, SetupContextForToken(ctx, token))
|
|
value, ok := common.GetContextKey(ctx, constant.ContextKeyTokenAutoGroups)
|
|
require.True(t, ok)
|
|
assert.Equal(t, []string{}, value)
|
|
}
|