mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ez-api/ez-api/internal/model"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestInternalHandler_FlushStatsUpdatesCounters(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.Key{}); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
|
|
key1 := model.Key{
|
|
MasterID: 1,
|
|
IssuedAtEpoch: 1,
|
|
TokenHash: "hash-1",
|
|
RequestCount: 5,
|
|
UsedTokens: 20,
|
|
QuotaLimit: 100,
|
|
QuotaUsed: 10,
|
|
}
|
|
key2 := model.Key{
|
|
MasterID: 1,
|
|
IssuedAtEpoch: 1,
|
|
TokenHash: "hash-2",
|
|
RequestCount: 0,
|
|
UsedTokens: 0,
|
|
QuotaLimit: -1,
|
|
QuotaUsed: 7,
|
|
}
|
|
|
|
if err := db.Create(&key1).Error; err != nil {
|
|
t.Fatalf("create key1: %v", err)
|
|
}
|
|
if err := db.Create(&key2).Error; err != nil {
|
|
t.Fatalf("create key2: %v", err)
|
|
}
|
|
|
|
handler := NewInternalHandler(db)
|
|
r := gin.New()
|
|
r.POST("/internal/stats/flush", handler.FlushStats)
|
|
|
|
body := []byte(`{
|
|
"keys": [
|
|
{"token_hash": "hash-1", "requests": 3, "tokens": 15, "last_accessed_at": 1700000000},
|
|
{"token_hash": "hash-2", "requests": 1, "tokens": 5, "last_accessed_at": 1700000010}
|
|
]
|
|
}`)
|
|
req := httptest.NewRequest(http.MethodPost, "/internal/stats/flush", bytes.NewReader(body))
|
|
rec := httptest.NewRecorder()
|
|
r.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("unexpected status: got=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
var got1 model.Key
|
|
if err := db.First(&got1, "token_hash = ?", "hash-1").Error; err != nil {
|
|
t.Fatalf("load key1: %v", err)
|
|
}
|
|
if got1.RequestCount != 8 {
|
|
t.Fatalf("key1 request_count: got=%d", got1.RequestCount)
|
|
}
|
|
if got1.UsedTokens != 35 {
|
|
t.Fatalf("key1 used_tokens: got=%d", got1.UsedTokens)
|
|
}
|
|
if got1.QuotaUsed != 25 {
|
|
t.Fatalf("key1 quota_used: got=%d", got1.QuotaUsed)
|
|
}
|
|
if got1.LastAccessedAt == nil || got1.LastAccessedAt.Unix() != 1700000000 {
|
|
t.Fatalf("key1 last_accessed_at: got=%v", got1.LastAccessedAt)
|
|
}
|
|
|
|
var got2 model.Key
|
|
if err := db.First(&got2, "token_hash = ?", "hash-2").Error; err != nil {
|
|
t.Fatalf("load key2: %v", err)
|
|
}
|
|
if got2.RequestCount != 1 {
|
|
t.Fatalf("key2 request_count: got=%d", got2.RequestCount)
|
|
}
|
|
if got2.UsedTokens != 5 {
|
|
t.Fatalf("key2 used_tokens: got=%d", got2.UsedTokens)
|
|
}
|
|
if got2.QuotaUsed != 7 {
|
|
t.Fatalf("key2 quota_used: got=%d", got2.QuotaUsed)
|
|
}
|
|
if got2.LastAccessedAt == nil || got2.LastAccessedAt.UTC().Unix() != time.Unix(1700000010, 0).UTC().Unix() {
|
|
t.Fatalf("key2 last_accessed_at: got=%v", got2.LastAccessedAt)
|
|
}
|
|
}
|