feat: sync deployed v1.0.0-rc.38-custom.42
Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
This commit is contained in:
1 parent
aa6c3f50f4
commit
ceddfd3d6b
828 files changed
+122253
-13702
No files matched your search
@@ -118,6 +118,91 @@ func setupTokenControllerTestDB(t *testing.T) *gorm.DB {
|
||||
return db
|
||||
}
|
||||
|
||||
func TestTokenAstraMigration(t *testing.T) {
|
||||
for _, dialect := range []string{"sqlite", "mysql", "postgres"} {
|
||||
for _, upgrade := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("%s/upgrade=%t", dialect, upgrade), func(t *testing.T) {
|
||||
var db *gorm.DB
|
||||
if dialect == "sqlite" {
|
||||
db = openTokenControllerTestDB(t)
|
||||
} else {
|
||||
dsn := os.Getenv("TEST_" + strings.ToUpper(dialect) + "_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("external test database is not configured")
|
||||
}
|
||||
var managed *bool
|
||||
db, managed = openTokenControllerExternalDB(t, dialect, dsn)
|
||||
*managed = true
|
||||
if db.Migrator().HasTable("options") {
|
||||
t.Fatal("requires an empty test database")
|
||||
}
|
||||
}
|
||||
t.Cleanup(func() { require.NoError(t, db.Migrator().DropTable(&model.Option{})) })
|
||||
if upgrade {
|
||||
require.NoError(t, db.AutoMigrate(&legacyToken{}))
|
||||
require.NoError(t, db.Create(&legacyToken{UserId: 7, Key: "legacy-astra", Name: "preserved", RemainQuota: 123}).Error)
|
||||
}
|
||||
require.NoError(t, db.AutoMigrate(&model.Token{}, &model.Option{}))
|
||||
require.NoError(t, model.MigrateTokenAstraAutoSwitch())
|
||||
require.NoError(t, model.MigrateTokenAstraDetection())
|
||||
if upgrade {
|
||||
var legacy model.Token
|
||||
require.NoError(t, db.First(&legacy, 1).Error)
|
||||
require.True(t, legacy.AstraAutoSwitch)
|
||||
require.True(t, legacy.AstraDetectionEnabled)
|
||||
require.Equal(t, 123, legacy.RemainQuota)
|
||||
legacy.AstraAutoSwitch = false
|
||||
legacy.AstraDetectionEnabled = false
|
||||
require.NoError(t, legacy.Update())
|
||||
}
|
||||
fresh := model.Token{UserId: 7, Key: "new-astra"}
|
||||
require.NoError(t, db.Create(&fresh).Error)
|
||||
for range 2 {
|
||||
require.NoError(t, db.AutoMigrate(&model.Token{}, &model.Option{}))
|
||||
require.NoError(t, model.MigrateTokenAstraAutoSwitch())
|
||||
require.NoError(t, model.MigrateTokenAstraDetection())
|
||||
}
|
||||
var tokens []model.Token
|
||||
require.NoError(t, db.Find(&tokens).Error)
|
||||
for _, token := range tokens {
|
||||
require.False(t, token.AstraAutoSwitch, "restart must preserve opt-outs and new-token defaults")
|
||||
require.False(t, token.AstraDetectionEnabled)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenAstraSwitchAPIPreservesOmittedAndPersistsFalse(t *testing.T) {
|
||||
db := setupTokenControllerTestDB(t)
|
||||
token := seedToken(t, db, 7, "astra", "astra-api-key")
|
||||
token.AstraAutoSwitch = true
|
||||
token.AstraDetectionEnabled = true
|
||||
require.NoError(t, token.Update())
|
||||
for _, enabled := range []any{nil, false, true} {
|
||||
body := map[string]any{"id": token.Id, "name": "astra", "unlimited_quota": true, "group": "default", "expired_time": -1}
|
||||
if enabled != nil {
|
||||
body["astra_auto_switch"] = enabled
|
||||
}
|
||||
c, writer := newAuthenticatedContext(t, http.MethodPut, "/api/token/", body, 7)
|
||||
UpdateToken(c)
|
||||
require.True(t, decodeAPIResponse(t, writer).Success, writer.Body.String())
|
||||
require.NoError(t, db.First(token, token.Id).Error)
|
||||
want := enabled == nil || enabled == true
|
||||
require.Equal(t, want, token.AstraAutoSwitch)
|
||||
var response struct {
|
||||
AstraAutoSwitch bool `json:"astra_auto_switch"`
|
||||
}
|
||||
require.NoError(t, common.Unmarshal(decodeAPIResponse(t, writer).Data, &response))
|
||||
require.Equal(t, want, response.AstraAutoSwitch)
|
||||
}
|
||||
c, writer := newAuthenticatedContext(t, http.MethodPut, "/api/token/", map[string]any{"id": token.Id, "name": "foreign", "astra_auto_switch": false}, 8)
|
||||
UpdateToken(c)
|
||||
require.False(t, decodeAPIResponse(t, writer).Success)
|
||||
require.NoError(t, db.First(token, token.Id).Error)
|
||||
require.True(t, token.AstraAutoSwitch)
|
||||
}
|
||||
|
||||
func openTokenControllerExternalDB(t *testing.T, dialect string, dsn string) (*gorm.DB, *bool) {
|
||||
t.Helper()
|
||||
|
||||
@@ -892,3 +977,22 @@ func verifyAPITokenAudit(t *testing.T) {
|
||||
assert.EqualValues(t, 1, count)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTokenDetectionOffDisablesSwitchAndSurvivesOmittedUpdate(t *testing.T) {
|
||||
db := setupTokenControllerTestDB(t)
|
||||
token := seedToken(t, db, 7, "detection", "detection-api-key")
|
||||
token.AstraDetectionEnabled, token.AstraAutoSwitch = true, true
|
||||
require.NoError(t, token.Update())
|
||||
for _, detection := range []any{false, nil, true} {
|
||||
body := map[string]any{"id": token.Id, "name": "detection", "unlimited_quota": true, "group": "default", "expired_time": -1}
|
||||
if detection != nil {
|
||||
body["astra_detection_enabled"] = detection
|
||||
}
|
||||
c, writer := newAuthenticatedContext(t, http.MethodPut, "/api/token/", body, 7)
|
||||
UpdateToken(c)
|
||||
require.True(t, decodeAPIResponse(t, writer).Success, writer.Body.String())
|
||||
require.NoError(t, db.First(token, token.Id).Error)
|
||||
require.Equal(t, detection == true, token.AstraDetectionEnabled)
|
||||
require.False(t, token.AstraAutoSwitch)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user