chore: establish local platform baseline
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package saas
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"data-terminal-cloud/internal/audit"
|
||||
"data-terminal-cloud/internal/identity"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func TestDeviceCommandCreateReplayConflictListAndExpiry(t *testing.T) {
|
||||
databaseURL := os.Getenv("TEST_DATABASE_URL")
|
||||
if databaseURL == "" {
|
||||
t.Skip("TEST_DATABASE_URL is not set")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
service := NewService(pool, audit.NewRepository(), nil, nil)
|
||||
owner := identity.Principal{Issuer: "http://localhost:58180/realms/voice-local", Subject: "00000000-0000-0000-0000-000000000101"}
|
||||
outsider := identity.Principal{Issuer: owner.Issuer, Subject: "00000000-0000-0000-0000-000000000103"}
|
||||
deviceID := "10000000-0000-0000-0000-000000000511"
|
||||
key := "saas-device-command-" + uuid.NewString()
|
||||
requestPrefix := "saas-device-command-"
|
||||
defer func() {
|
||||
_, _ = pool.Exec(ctx, `DELETE FROM audit_event WHERE request_id LIKE $1`, requestPrefix+"%")
|
||||
_, _ = pool.Exec(ctx, `DELETE FROM device_command WHERE idempotency_key=$1`, key)
|
||||
}()
|
||||
|
||||
payload := map[string]any{"config_revision": float64(3), "reason": "core coverage"}
|
||||
createdCommand, created, err := service.CreateDeviceCommand(ctx, owner, deviceID, key, requestPrefix+"create", "apply_config", payload, 120)
|
||||
if err != nil || !created || createdCommand.ID == "" || createdCommand.DeviceID != deviceID || createdCommand.Type != "apply_config" || createdCommand.Status != "QUEUED" || createdCommand.Version != 1 {
|
||||
t.Fatalf("created command=%+v created=%t err=%v", createdCommand, created, err)
|
||||
}
|
||||
replayed, createdAgain, err := service.CreateDeviceCommand(ctx, owner, deviceID, key, requestPrefix+"replay", "apply_config", payload, 120)
|
||||
if err != nil || createdAgain || replayed.ID != createdCommand.ID {
|
||||
t.Fatalf("replayed command=%+v created=%t err=%v", replayed, createdAgain, err)
|
||||
}
|
||||
if _, _, err := service.CreateDeviceCommand(ctx, owner, deviceID, key, requestPrefix+"conflict", "apply_config", map[string]any{"config_revision": float64(4)}, 120); !errors.Is(err, ErrIdempotencyConflict) {
|
||||
t.Fatalf("command body conflict error=%v", err)
|
||||
}
|
||||
loaded, err := service.GetDeviceCommand(ctx, owner, createdCommand.ID)
|
||||
if err != nil || loaded.ID != createdCommand.ID || loaded.Payload["reason"] != "core coverage" || loaded.ResultSummary == nil {
|
||||
t.Fatalf("loaded command=%+v err=%v", loaded, err)
|
||||
}
|
||||
items, err := service.ListDeviceCommands(ctx, owner, deviceID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for _, item := range items {
|
||||
if item.ID == createdCommand.ID {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("command %s missing from list", createdCommand.ID)
|
||||
}
|
||||
if _, err := service.GetDeviceCommand(ctx, outsider, createdCommand.ID); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatalf("outsider command access error=%v", err)
|
||||
}
|
||||
if _, err := service.ListDeviceCommands(ctx, outsider, deviceID); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatalf("outsider command list error=%v", err)
|
||||
}
|
||||
if _, err := service.GetDeviceCommand(ctx, owner, uuid.NewString()); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatalf("missing command error=%v", err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `UPDATE device_command SET expires_at=now()-interval '1 second' WHERE public_id=$1::uuid`, createdCommand.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err = service.ListDeviceCommands(ctx, owner, deviceID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found = false
|
||||
for _, item := range items {
|
||||
if item.ID == createdCommand.ID && item.Status == "EXPIRED" && item.Version == 2 {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("expired command not returned: %+v", items)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user