65 lines
2.3 KiB
Go
65 lines
2.3 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
agentpb "git.ipao.vip/rogee/go-sip/gen/agent"
|
|
ossclient "git.ipao.vip/rogee/go-sip/internal/oss"
|
|
"git.ipao.vip/rogee/go-sip/internal/store"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func TestUploadGrantRedeliveryDoesNotRenewExpiredToken(t *testing.T) {
|
|
st, err := store.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer st.Close()
|
|
signer, err := ossclient.NewClient(ossclient.Config{Endpoint: "https://oss.invalid", Region: "cn-beijing", Bucket: "local-test", AccessKeyID: "local-test", AccessKeySecret: "local-test", MaxAssetBytes: 1024})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now().UTC()
|
|
server, err := NewDispatcherUploadServer(st, signer, func() time.Time { return now }, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := &agentpb.RequestUploadRequest{
|
|
Meta: &agentpb.RequestMeta{AgentId: "agent-a", CellId: "cell-a", OperationId: "original-request", IdempotencyKey: "original-request"},
|
|
Binding: &agentpb.ExecutionBinding{TenantId: "tenant-a", TenantKey: "tenant-a", TaskId: "task-a", ExecutionId: "execution-a", CallId: "call-a"},
|
|
Asset: &agentpb.AssetDescriptor{Kind: agentpb.AssetKind_ASSET_KIND_RECORDING, AssetId: "recording-a", CallId: "call-a", Format: "wav", Channels: 1, SampleRateHz: 8000, DurationMs: 10, SizeBytes: 4, ChecksumSha256: strings.Repeat("a", 64)}, UploadId: "upload-a",
|
|
}
|
|
first, err := server.RequestUpload(context.Background(), request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now = now.Add(16 * time.Minute)
|
|
duplicate, err := server.RequestUpload(context.Background(), request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !proto.Equal(first.Grant, duplicate.Grant) {
|
|
t.Fatal("redelivery silently renewed the original token")
|
|
}
|
|
explicit := proto.Clone(request).(*agentpb.RequestUploadRequest)
|
|
explicit.Meta.OperationId = "explicit-new-request"
|
|
explicit.Meta.IdempotencyKey = "explicit-new-request"
|
|
renewed, err := server.RequestUpload(context.Background(), explicit)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if renewed.Grant.ExpiresAtUnixMs != now.Add(15*time.Minute).UnixMilli() {
|
|
t.Fatal("explicit request did not receive a new fixed-duration grant")
|
|
}
|
|
old, err := server.RequestUpload(context.Background(), request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !proto.Equal(first.Grant, old.Grant) {
|
|
t.Fatal("old request gained access to a newer grant")
|
|
}
|
|
}
|