Files
sub2api-cn-relay-manager/internal/host/sub2api/account_capability_repair_test.go

120 lines
3.6 KiB
Go

package sub2api
import (
"context"
"io"
"net/http"
"strings"
"testing"
)
func TestDisableOpenAIResponsesAPISkipsEmptyAccountIDs(t *testing.T) {
t.Parallel()
client, err := NewClient("https://sub2api.example.com", WithHTTPClient(&http.Client{
Transport: roundTripperFunc(func(*http.Request) (*http.Response, error) {
t.Fatal("unexpected HTTP request for empty account ids")
return nil, nil
}),
}))
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
if err := client.DisableOpenAIResponsesAPI(context.Background(), []string{" ", ""}); err != nil {
t.Fatalf("DisableOpenAIResponsesAPI() error = %v", err)
}
}
func TestDisableOpenAIResponsesAPIReturnsHTTPError(t *testing.T) {
t.Parallel()
var gotPath string
client, err := NewClient("https://sub2api.example.com", WithHTTPClient(&http.Client{
Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
gotPath = req.URL.Path
return &http.Response{
StatusCode: http.StatusForbidden,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader(`{"error":"forbidden"}`)),
}, nil
}),
}))
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
err = client.DisableOpenAIResponsesAPI(context.Background(), []string{"account-1"})
if err == nil {
t.Fatal("DisableOpenAIResponsesAPI() error = nil, want HTTP error")
}
httpErr, ok := err.(*HTTPError)
if !ok {
t.Fatalf("DisableOpenAIResponsesAPI() error type = %T, want *HTTPError", err)
}
if gotPath != "/api/v1/admin/accounts/account-1" {
t.Fatalf("request path = %q, want /api/v1/admin/accounts/account-1", gotPath)
}
if httpErr.StatusCode != http.StatusForbidden {
t.Fatalf("HTTPError.StatusCode = %d, want %d", httpErr.StatusCode, http.StatusForbidden)
}
}
func TestClearTempUnschedulableSkipsEmptyAccountIDs(t *testing.T) {
t.Parallel()
client, err := NewClient("https://sub2api.example.com", WithHTTPClient(&http.Client{
Transport: roundTripperFunc(func(*http.Request) (*http.Response, error) {
t.Fatal("unexpected HTTP request for empty account ids")
return nil, nil
}),
}))
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
if err := client.ClearTempUnschedulable(context.Background(), []string{" ", ""}); err != nil {
t.Fatalf("ClearTempUnschedulable() error = %v", err)
}
}
func TestClearTempUnschedulableReturnsHTTPError(t *testing.T) {
t.Parallel()
var gotPath string
client, err := NewClient("https://sub2api.example.com", WithHTTPClient(&http.Client{
Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
gotPath = req.URL.Path
return &http.Response{
StatusCode: http.StatusForbidden,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader(`{"error":"forbidden"}`)),
}, nil
}),
}))
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
err = client.ClearTempUnschedulable(context.Background(), []string{"account-1"})
if err == nil {
t.Fatal("ClearTempUnschedulable() error = nil, want HTTP error")
}
httpErr, ok := err.(*HTTPError)
if !ok {
t.Fatalf("ClearTempUnschedulable() error type = %T, want *HTTPError", err)
}
if gotPath != "/api/v1/admin/accounts/account-1/temp-unschedulable" {
t.Fatalf("request path = %q, want /api/v1/admin/accounts/account-1/temp-unschedulable", gotPath)
}
if httpErr.StatusCode != http.StatusForbidden {
t.Fatalf("HTTPError.StatusCode = %d, want %d", httpErr.StatusCode, http.StatusForbidden)
}
}
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (fn roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return fn(req)
}