refactor(management): rename GetUsage to GetUsageQueue and update routes/tests
- Renamed handler and test methods for better clarity on functionality. - Updated route from `/v0/management/usage` to `/v0/management/usage-queue`. - Adjusted integration and unit tests to reflect new naming and routes.
This commit is contained in:
@@ -20,8 +20,8 @@ func (r usageQueueRecord) MarshalJSON() ([]byte, error) {
|
|||||||
return json.Marshal(string(r))
|
return json.Marshal(string(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage pops queued usage records from the Redis-compatible usage queue.
|
// GetUsageQueue pops queued usage records from the usage queue.
|
||||||
func (h *Handler) GetUsage(c *gin.Context) {
|
func (h *Handler) GetUsageQueue(c *gin.Context) {
|
||||||
if h == nil {
|
if h == nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "handler unavailable"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "handler unavailable"})
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/redisqueue"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/redisqueue"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetUsagePopsRequestedRecords(t *testing.T) {
|
func TestGetUsageQueuePopsRequestedRecords(t *testing.T) {
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
withManagementUsageQueue(t, func() {
|
withManagementUsageQueue(t, func() {
|
||||||
redisqueue.Enqueue([]byte(`{"id":1}`))
|
redisqueue.Enqueue([]byte(`{"id":1}`))
|
||||||
@@ -19,10 +19,10 @@ func TestGetUsagePopsRequestedRecords(t *testing.T) {
|
|||||||
|
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ginCtx, _ := gin.CreateTestContext(rec)
|
ginCtx, _ := gin.CreateTestContext(rec)
|
||||||
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=2", nil)
|
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=2", nil)
|
||||||
|
|
||||||
h := &Handler{}
|
h := &Handler{}
|
||||||
h.GetUsage(ginCtx)
|
h.GetUsageQueue(ginCtx)
|
||||||
|
|
||||||
if rec.Code != http.StatusOK {
|
if rec.Code != http.StatusOK {
|
||||||
t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusOK, rec.Body.String())
|
||||||
@@ -45,17 +45,17 @@ func TestGetUsagePopsRequestedRecords(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetUsageInvalidCountDoesNotPop(t *testing.T) {
|
func TestGetUsageQueueInvalidCountDoesNotPop(t *testing.T) {
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
withManagementUsageQueue(t, func() {
|
withManagementUsageQueue(t, func() {
|
||||||
redisqueue.Enqueue([]byte(`{"id":1}`))
|
redisqueue.Enqueue([]byte(`{"id":1}`))
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
ginCtx, _ := gin.CreateTestContext(rec)
|
ginCtx, _ := gin.CreateTestContext(rec)
|
||||||
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=0", nil)
|
ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=0", nil)
|
||||||
|
|
||||||
h := &Handler{}
|
h := &Handler{}
|
||||||
h.GetUsage(ginCtx)
|
h.GetUsageQueue(ginCtx)
|
||||||
|
|
||||||
if rec.Code != http.StatusBadRequest {
|
if rec.Code != http.StatusBadRequest {
|
||||||
t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
|
t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
|
||||||
|
|||||||
@@ -551,7 +551,7 @@ func (s *Server) registerManagementRoutes() {
|
|||||||
mgmt.PATCH("/api-keys", s.mgmt.PatchAPIKeys)
|
mgmt.PATCH("/api-keys", s.mgmt.PatchAPIKeys)
|
||||||
mgmt.DELETE("/api-keys", s.mgmt.DeleteAPIKeys)
|
mgmt.DELETE("/api-keys", s.mgmt.DeleteAPIKeys)
|
||||||
mgmt.GET("/api-key-usage", s.mgmt.GetAPIKeyUsage)
|
mgmt.GET("/api-key-usage", s.mgmt.GetAPIKeyUsage)
|
||||||
mgmt.GET("/usage", s.mgmt.GetUsage)
|
mgmt.GET("/usage-queue", s.mgmt.GetUsageQueue)
|
||||||
|
|
||||||
mgmt.GET("/gemini-api-key", s.mgmt.GetGeminiKeys)
|
mgmt.GET("/gemini-api-key", s.mgmt.GetGeminiKeys)
|
||||||
mgmt.PUT("/gemini-api-key", s.mgmt.PutGeminiKeys)
|
mgmt.PUT("/gemini-api-key", s.mgmt.PutGeminiKeys)
|
||||||
|
|||||||
@@ -100,14 +100,22 @@ func TestManagementUsageRequiresManagementAuthAndPopsArray(t *testing.T) {
|
|||||||
redisqueue.Enqueue([]byte(`{"id":1}`))
|
redisqueue.Enqueue([]byte(`{"id":1}`))
|
||||||
redisqueue.Enqueue([]byte(`{"id":2}`))
|
redisqueue.Enqueue([]byte(`{"id":2}`))
|
||||||
|
|
||||||
missingKeyReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=2", nil)
|
missingKeyReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=2", nil)
|
||||||
missingKeyRR := httptest.NewRecorder()
|
missingKeyRR := httptest.NewRecorder()
|
||||||
server.engine.ServeHTTP(missingKeyRR, missingKeyReq)
|
server.engine.ServeHTTP(missingKeyRR, missingKeyReq)
|
||||||
if missingKeyRR.Code != http.StatusUnauthorized {
|
if missingKeyRR.Code != http.StatusUnauthorized {
|
||||||
t.Fatalf("missing key status = %d, want %d body=%s", missingKeyRR.Code, http.StatusUnauthorized, missingKeyRR.Body.String())
|
t.Fatalf("missing key status = %d, want %d body=%s", missingKeyRR.Code, http.StatusUnauthorized, missingKeyRR.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
authReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=2", nil)
|
legacyReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=2", nil)
|
||||||
|
legacyReq.Header.Set("Authorization", "Bearer test-management-key")
|
||||||
|
legacyRR := httptest.NewRecorder()
|
||||||
|
server.engine.ServeHTTP(legacyRR, legacyReq)
|
||||||
|
if legacyRR.Code != http.StatusNotFound {
|
||||||
|
t.Fatalf("legacy usage status = %d, want %d body=%s", legacyRR.Code, http.StatusNotFound, legacyRR.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
authReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=2", nil)
|
||||||
authReq.Header.Set("Authorization", "Bearer test-management-key")
|
authReq.Header.Set("Authorization", "Bearer test-management-key")
|
||||||
authRR := httptest.NewRecorder()
|
authRR := httptest.NewRecorder()
|
||||||
server.engine.ServeHTTP(authRR, authReq)
|
server.engine.ServeHTTP(authRR, authReq)
|
||||||
|
|||||||
Reference in New Issue
Block a user