mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-15 23:22:59 +08:00
44 lines
1.7 KiB
Go
44 lines
1.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const validSystemOneHandlerBody = `{"model":"jev-latest","state":"sample","questions":{"q":{"type":"noul","instructions":"Evaluate"}}}`
|
|
|
|
func newSystemOneHandlerContext(body string) (*gin.Context, *httptest.ResponseRecorder) {
|
|
gin.SetMode(gin.TestMode)
|
|
recorder := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(recorder)
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/v1/systemone", strings.NewReader(body))
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
|
return c, recorder
|
|
}
|
|
|
|
func TestSystemOneRequiresAuthentication(t *testing.T) {
|
|
c, recorder := newSystemOneHandlerContext(validSystemOneHandlerBody)
|
|
(&GatewayHandler{}).SystemOne(c)
|
|
require.Equal(t, http.StatusUnauthorized, recorder.Code)
|
|
require.Contains(t, recorder.Body.String(), "authentication_error")
|
|
}
|
|
|
|
func TestSystemOneRejectsNonTypeSafeGroupBeforeScheduling(t *testing.T) {
|
|
c, recorder := newSystemOneHandlerContext(validSystemOneHandlerBody)
|
|
groupID := int64(3)
|
|
c.Set(string(middleware2.ContextKeyAPIKey), &service.APIKey{ID: 4, UserID: 5, GroupID: &groupID, Group: &service.Group{ID: groupID, Platform: service.PlatformOpenAI}})
|
|
c.Set(string(middleware2.ContextKeyUser), middleware2.AuthSubject{UserID: 5, Concurrency: 1})
|
|
|
|
(&GatewayHandler{cfg: &config.Config{Gateway: config.GatewayConfig{MaxBodySize: 1 << 20}}}).SystemOne(c)
|
|
require.Equal(t, http.StatusNotFound, recorder.Code)
|
|
require.Contains(t, recorder.Body.String(), "only available for TypeSafe")
|
|
}
|