Files
archie/internal/infrastructure/staterpc/personal_tokens_test.go
T
sam a971947c39 refactor(state)!: split StateStoreService into per-domain services
One gRPC service per domain in state.v1 (tasks, executions, accounts, identities, packages, captures, bindings, access, orgs, alerts and so on), each with its own server type in staterpc. RPC and message names and numbers are unchanged.
2026-10-25 19:54:27 +11:00

51 lines
1.5 KiB
Go

package staterpc
import (
"context"
"strings"
"testing"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
type tokenCalls struct {
identity.PersonalTokenStore
owner identity.IdentityID
}
func (t *tokenCalls) BindSubject(_ context.Context, id identity.IdentityID, _ identity.Subject, _ identity.Audit) error {
t.owner = id
return nil
}
// TestPersonalTokenOwner pins that a token binds to the caller's own
// principal and that a call with no signed-in person is refused.
func TestPersonalTokenOwner(t *testing.T) {
id := strings.Repeat("ab", 32)
tests := []struct {
name string
principal *access.Principal
wantOwner identity.IdentityID
}{
{"signed-in person owns the token", &access.Principal{IdentityID: "alice"}, "alice"},
{"no principal is refused", nil, ""},
{"shared token owner is refused", &access.Principal{IdentityID: identity.SystemID}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &tokenCalls{}
s := &accountServer{server: &server{deps: Deps{PersonalTokens: store}}}
ctx := context.Background()
if tt.principal != nil {
ctx = access.WithPrincipal(ctx, *tt.principal)
}
_, err := s.AddPersonalToken(ctx, &pb.AddPersonalTokenRequest{Id: id})
if (err == nil) != (tt.wantOwner != "") || store.owner != tt.wantOwner {
t.Fatalf("err %v, bound to %q, want %q", err, store.owner, tt.wantOwner)
}
})
}
}