mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-01 19:41:57 +00:00
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package model
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestFormatUserLogsStripsQuotaSaturation verifies the admin-only quota
|
|
// saturation marker (nested under other.admin_info) is removed for non-admin
|
|
// log views, since formatUserLogs strips the whole admin_info object.
|
|
func TestFormatUserLogsStripsQuotaSaturation(t *testing.T) {
|
|
other := common.MapToJsonStr(map[string]interface{}{
|
|
"model_price": 0.004,
|
|
"admin_info": map[string]interface{}{
|
|
"quota_saturation": map[string]interface{}{
|
|
"op": "QuotaFromDecimal",
|
|
"kind": "overflow",
|
|
"clamped": common.MaxQuota,
|
|
},
|
|
},
|
|
})
|
|
logs := []*Log{{Other: other}}
|
|
|
|
formatUserLogs(logs, 0)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
_, hasAdminInfo := parsed["admin_info"]
|
|
require.False(t, hasAdminInfo, "admin_info (and nested quota_saturation) must be stripped for non-admin views")
|
|
// Non-admin billing fields remain visible.
|
|
require.Contains(t, parsed, "model_price")
|
|
}
|
|
|
|
func TestTaskPluginLogVisibilityIsRoleSeparated(t *testing.T) {
|
|
other := common.MapToJsonStr(map[string]interface{}{
|
|
"model_price": 1.25,
|
|
"admin_info": map[string]interface{}{
|
|
"task_plugin": map[string]interface{}{
|
|
"key": "document-parser",
|
|
"name": "Document Parser",
|
|
"version": "1.2.3",
|
|
},
|
|
},
|
|
"root_info": map[string]interface{}{
|
|
"upstream_task_id": "upstream-private",
|
|
"task_plugin": map[string]interface{}{
|
|
"generation": 42,
|
|
},
|
|
},
|
|
})
|
|
|
|
t.Run("user", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
formatUserLogs(logs, 0)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.NotContains(t, parsed, "admin_info")
|
|
assert.NotContains(t, parsed, "root_info")
|
|
assert.Equal(t, 1.25, parsed["model_price"])
|
|
})
|
|
|
|
t.Run("admin", func(t *testing.T) {
|
|
logs := []*Log{{Other: other}}
|
|
FormatAdminLogs(logs)
|
|
|
|
parsed, err := common.StrToMap(logs[0].Other)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, parsed, "admin_info")
|
|
assert.NotContains(t, parsed, "root_info")
|
|
})
|
|
|
|
t.Run("root", func(t *testing.T) {
|
|
parsed, err := common.StrToMap(other)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, parsed, "admin_info")
|
|
assert.Contains(t, parsed, "root_info")
|
|
})
|
|
}
|