59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package device
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"strings"
|
|
"testing"
|
|
"yovision.local/sense/app/sense/identity"
|
|
)
|
|
|
|
func testService(t *testing.T) *Service {
|
|
t.Helper()
|
|
vault, err := NewCredentialVault(base64.StdEncoding.EncodeToString([]byte("0123456789abcdef0123456789abcdef")), false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return NewService(NewMemoryStore(), vault)
|
|
}
|
|
func TestDeviceLifecycleAndCredentialNeverReturned(t *testing.T) {
|
|
svc := testService(t)
|
|
ctx := context.Background()
|
|
actor := identity.Principal{UserID: "test"}
|
|
item, err := svc.Create(ctx, actor, "东门摄像机", "东门", ModalityVideo, []string{"video"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if item.AdapterStatus != AdapterReady {
|
|
t.Fatalf("adapter=%s", item.AdapterStatus)
|
|
}
|
|
item, err = svc.SetCredential(ctx, actor, item.ID, "camera-user", "camera-password")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !item.CredentialConfigured || len(item.CredentialCiphertext) != 0 {
|
|
t.Fatal("credential leaked or not configured")
|
|
}
|
|
page, err := svc.List(ctx, ListFilter{Page: 1, PageSize: 20})
|
|
if err != nil || page.Total != 1 || len(page.Items[0].CredentialCiphertext) != 0 {
|
|
t.Fatalf("page=%#v err=%v", page, err)
|
|
}
|
|
}
|
|
func TestNonVideoAdapterNotReadyAndPaginationNotCappedAt16(t *testing.T) {
|
|
svc := testService(t)
|
|
ctx := context.Background()
|
|
for i := 0; i < 20; i++ {
|
|
item, err := svc.Create(ctx, identity.Principal{}, strings.Repeat("x", i+1), "", "radar", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if item.AdapterStatus != AdapterNotReady {
|
|
t.Fatal("unsupported adapter shown ready")
|
|
}
|
|
}
|
|
page, err := svc.List(ctx, ListFilter{Page: 1, PageSize: 20})
|
|
if err != nil || len(page.Items) != 20 {
|
|
t.Fatalf("items=%d err=%v", len(page.Items), err)
|
|
}
|
|
}
|