mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-11-04 16:40:21 +00:00
fix(nat): bound dashboard credential classification
This commit is contained in:
1 parent
34ac50394d
commit
1420dd450c
7 files changed
+338
-95
No files matched your search
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -163,6 +164,35 @@ func TestIsDashboardCredentialValueClassification(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestClassifyDashboardCredentialValuesBatchesPATLookup(t *testing.T) {
|
||||
setupDashboardCredentialTest(t)
|
||||
|
||||
dashboardPAT := model.APITokenPrefix + "dashboard-batch-pat"
|
||||
require.NoError(t, singleton.DB.Create(&model.APIToken{
|
||||
UserID: 1,
|
||||
Name: "nat-batch-classification",
|
||||
TokenHash: model.HashAPIToken(dashboardPAT),
|
||||
}).Error)
|
||||
|
||||
queryCount := 0
|
||||
const callbackName = "test:count-dashboard-credential-batch-query"
|
||||
require.NoError(t, singleton.DB.Callback().Query().Before("gorm:query").Register(callbackName, func(*gorm.DB) {
|
||||
queryCount++
|
||||
}))
|
||||
t.Cleanup(func() { singleton.DB.Callback().Query().Remove(callbackName) })
|
||||
|
||||
values := []string{
|
||||
dashboardPAT,
|
||||
model.APITokenPrefix + "unknown-one",
|
||||
dashboardPAT,
|
||||
model.APITokenPrefix + "unknown-two",
|
||||
}
|
||||
classified := ClassifyDashboardCredentialValues(context.Background(), values)
|
||||
|
||||
require.Equal(t, []bool{true, false, true, false}, classified)
|
||||
require.Equal(t, 1, queryCount, "all PAT hashes in one request must use one database query")
|
||||
}
|
||||
|
||||
func TestIsDashboardCredentialFailsClosedWhenParserUnavailable(t *testing.T) {
|
||||
setupDashboardCredentialTest(t)
|
||||
dashboardCredentialJWTParser = nil
|
||||
|
||||
Reference in new issue
Block a user