Files
CLIProxyAPI/internal/auth/codex/jwt_parser_test.go
Luis Pater 6e6afab99a refactor: migrate gjson, sjson, and base64 to MoeForge/moego
- Replace tidwall/gjson and tidwall/sjson with github.com/MoeForge/moego/ojson
  and encoding/base64 with github.com/MoeForge/moego/base64 across the main
  module and examples
- Introduce cross-method *ojson.Doc request rewriting pipelines across
  thinking, signature, client/codex, runtime/executor/helps, pluginhost, and
  all provider executors with FinalizePayloadDoc as the final semantic barrier
- Convert all consecutive JSON mutations (>= 2 Set/SetRaw/Delete operations)
  to batch edits on ojson.Doc
- Upgrade read-only byte paths to ojson.GetBytesNoCopy,
  ojson.GetManyBytesNoCopy, and util.ParseGJSONBytesNoCopy with explicit
  strings.Clone ownership protection across streaming and persistent state
- Use base64.StdEncoding.ValidateString for zero-allocation signature checks
  and base64.EncodeDataURL for single-allocation data URL construction
2026-11-03 03:05:04 +08:00

78 lines
2.1 KiB
Go

package codex
import (
"encoding/json"
"github.com/MoeForge/moego/base64"
"testing"
)
func makeTestJWT(payload map[string]any) string {
header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"none","typ":"JWT"}`))
payloadBytes, _ := json.Marshal(payload)
claims := base64.RawURLEncoding.EncodeToString(payloadBytes)
return header + "." + claims + "."
}
func TestGetPlanType(t *testing.T) {
var nilClaims *JWTClaims
if got := nilClaims.GetPlanType(); got != DefaultPlanType {
t.Fatalf("nilClaims.GetPlanType() = %q, want %q", got, DefaultPlanType)
}
emptyClaims := &JWTClaims{}
if got := emptyClaims.GetPlanType(); got != DefaultPlanType {
t.Fatalf("emptyClaims.GetPlanType() = %q, want %q", got, DefaultPlanType)
}
whitespaceClaims := &JWTClaims{
CodexAuthInfo: CodexAuthInfo{
ChatgptPlanType: " ",
},
}
if got := whitespaceClaims.GetPlanType(); got != DefaultPlanType {
t.Fatalf("whitespaceClaims.GetPlanType() = %q, want %q", got, DefaultPlanType)
}
proClaims := &JWTClaims{
CodexAuthInfo: CodexAuthInfo{
ChatgptPlanType: "pro",
},
}
if got := proClaims.GetPlanType(); got != "pro" {
t.Fatalf("proClaims.GetPlanType() = %q, want %q", got, "pro")
}
}
func TestParseJWTToken_MissingPlanTypeDefaultsToFree(t *testing.T) {
jwtWithoutPlan := makeTestJWT(map[string]any{
"email": "user@example.com",
"https://api.openai.com/auth": map[string]any{
"chatgpt_account_id": "acc-12345",
},
})
claims, errParse := ParseJWTToken(jwtWithoutPlan)
if errParse != nil {
t.Fatalf("ParseJWTToken failed: %v", errParse)
}
if got := claims.GetPlanType(); got != "free" {
t.Fatalf("claims.GetPlanType() = %q, want %q", got, "free")
}
jwtWithPlan := makeTestJWT(map[string]any{
"email": "user@example.com",
"https://api.openai.com/auth": map[string]any{
"chatgpt_account_id": "acc-12345",
"chatgpt_plan_type": "team",
},
})
claimsTeam, errParseTeam := ParseJWTToken(jwtWithPlan)
if errParseTeam != nil {
t.Fatalf("ParseJWTToken failed: %v", errParseTeam)
}
if got := claimsTeam.GetPlanType(); got != "team" {
t.Fatalf("claimsTeam.GetPlanType() = %q, want %q", got, "team")
}
}