mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-30 23:39:10 +00:00
Admins often need another account with the same provider and routing configuration. Duplicate on the server so credentials never return to the browser, preserve exact group priorities atomically, start the copy paused, and recover the same copy after ambiguous idempotency-store failures. Constraint: Admin account responses redact credentials, so duplication must remain server-side Constraint: OAuth and setup-token credentials rotate and must not be shared across account rows Rejected: Copy raw account JSON to the clipboard | exposes credentials outside the server Rejected: Duplicate rotating credentials | account-scoped refresh locks can race token rotation Confidence: high Scope-risk: moderate Reversibility: clean Directive: Keep copies paused, avoid automatic upstream probes, and exclude rotating credential types unless token ownership is redesigned Tested: Targeted Go tests, Go vet, server build; frontend lint, typecheck, Vitest suite, production build; integration test compiled Not-tested: Docker-backed PostgreSQL execution because Docker is unavailable Related: Wei-Shaw/sub2api#1379 Related: Wei-Shaw/sub2api#2928
74 lines
3.1 KiB
Go
74 lines
3.1 KiB
Go
//go:build integration
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCreateWithAccountGroupsPersistsPausedCopyAtomically(t *testing.T) {
|
|
ctx := context.Background()
|
|
client := testEntClient(t)
|
|
repo := newAccountRepositoryWithSQL(client, integrationDB, nil)
|
|
suffix := time.Now().UnixNano()
|
|
|
|
group, err := client.Group.Create().
|
|
SetName(fmt.Sprintf("duplicate-atomic-%d", suffix)).
|
|
SetPlatform(service.PlatformAnthropic).
|
|
Save(ctx)
|
|
require.NoError(t, err)
|
|
|
|
success := &service.Account{
|
|
Name: fmt.Sprintf("duplicate-success-%d", suffix),
|
|
Platform: service.PlatformAnthropic,
|
|
Type: service.AccountTypeAPIKey,
|
|
Status: service.StatusActive,
|
|
Schedulable: false,
|
|
Credentials: map[string]any{"api_key": "secret"},
|
|
Extra: map[string]any{},
|
|
}
|
|
require.NoError(t, repo.CreateWithAccountGroups(ctx, success, []service.AccountGroup{{GroupID: group.ID, Priority: 37}}))
|
|
t.Cleanup(func() {
|
|
_, _ = integrationDB.ExecContext(context.Background(), "DELETE FROM scheduler_outbox WHERE account_id = $1", success.ID)
|
|
_, _ = integrationDB.ExecContext(context.Background(), "DELETE FROM account_groups WHERE account_id = $1", success.ID)
|
|
_, _ = integrationDB.ExecContext(context.Background(), "DELETE FROM accounts WHERE id = $1", success.ID)
|
|
_, _ = integrationDB.ExecContext(context.Background(), "DELETE FROM groups WHERE id = $1", group.ID)
|
|
})
|
|
|
|
var schedulable bool
|
|
require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT schedulable FROM accounts WHERE id = $1", success.ID).Scan(&schedulable))
|
|
require.False(t, schedulable)
|
|
var priority int
|
|
require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT priority FROM account_groups WHERE account_id = $1 AND group_id = $2", success.ID, group.ID).Scan(&priority))
|
|
require.Equal(t, 37, priority)
|
|
var outboxCount int
|
|
require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM scheduler_outbox WHERE account_id = $1", success.ID).Scan(&outboxCount))
|
|
require.Equal(t, 1, outboxCount)
|
|
|
|
failure := &service.Account{
|
|
Name: fmt.Sprintf("duplicate-failure-%d", suffix),
|
|
Platform: service.PlatformAnthropic,
|
|
Type: service.AccountTypeAPIKey,
|
|
Status: service.StatusActive,
|
|
Schedulable: false,
|
|
Credentials: map[string]any{"api_key": "secret"},
|
|
Extra: map[string]any{},
|
|
}
|
|
err = repo.CreateWithAccountGroups(ctx, failure, []service.AccountGroup{{GroupID: int64(^uint64(0) >> 1), Priority: 1}})
|
|
require.Error(t, err)
|
|
|
|
var accountCount, groupCount, failedOutboxCount int
|
|
require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM accounts WHERE name = $1", failure.Name).Scan(&accountCount))
|
|
require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM account_groups WHERE account_id = $1", failure.ID).Scan(&groupCount))
|
|
require.NoError(t, integrationDB.QueryRowContext(ctx, "SELECT COUNT(*) FROM scheduler_outbox WHERE account_id = $1", failure.ID).Scan(&failedOutboxCount))
|
|
require.Zero(t, accountCount)
|
|
require.Zero(t, groupCount)
|
|
require.Zero(t, failedOutboxCount)
|
|
}
|