Files
archie/internal/infrastructure/staterpc/accounts_test.go
T

89 lines
3.6 KiB
Go

package staterpc
import (
"context"
"testing"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
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"
"github.com/samcharles93/archie-core/internal/domain/org"
)
type accountPolicyStore struct {
access.PolicyStore
policies []access.Policy
}
func (s accountPolicyStore) ListPolicies(context.Context) ([]access.Policy, error) {
return s.policies, nil
}
type accountRegistrationStore struct{ identity.Accounts }
func (accountRegistrationStore) RegisterAccount(context.Context, identity.AccountRegistration, bool) (identity.Identity, error) {
return identity.Identity{ID: "created"}, nil
}
func TestAccountManagementUsesPolicy(t *testing.T) {
for _, tc := range []struct {
name string
role org.Role
extra string
anonymous bool
want codes.Code
}{
{name: "administrator relationship permits", role: org.RoleAdmin, want: codes.OK},
{name: "developer has no grant", role: org.RoleDeveloper, want: codes.PermissionDenied},
{name: "explicit identity policy permits without admin role", role: org.RoleDeveloper, extra: `permit(principal == Archie::Identity::"alice", action == Archie::Action::"manage_members", resource);`, want: codes.OK},
{name: "explicit forbid wins over admin role", role: org.RoleAdmin, extra: `forbid(principal,action == Archie::Action::"manage_members",resource);`, want: codes.PermissionDenied},
{name: "unauthenticated service has no user grant", anonymous: true, want: codes.PermissionDenied},
} {
t.Run(tc.name, func(t *testing.T) {
ctx := t.Context()
if !tc.anonymous {
ctx = access.WithPrincipal(ctx, access.Principal{IdentityID: "alice", Org: "acme", Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tc.role}}})
}
policies := access.ShippedOrgPolicies("acme")
if tc.extra != "" {
policies = append(policies, access.Policy{ID: "custom-members", Level: access.LevelOrg, OrgID: "acme", Text: tc.extra})
}
srv := &server{deps: Deps{Accounts: accountRegistrationStore{}, Policies: accountPolicyStore{policies: policies}}}
_, err := srv.RegisterAccount(ctx, &pb.RegisterAccountRequest{Email: "new@example.com", Role: "viewer"})
if got := status.Code(err); got != tc.want {
t.Fatalf("code=%s want=%s err=%v", got, tc.want, err)
}
})
}
}
func TestTaskCredentialsCannotManageAccounts(t *testing.T) {
for _, method := range []string{
pb.StateStoreService_AccountSetup_FullMethodName,
pb.StateStoreService_RegisterAccount_FullMethodName,
pb.StateStoreService_SignInAccount_FullMethodName,
pb.StateStoreService_ResolveAccountSession_FullMethodName,
pb.StateStoreService_SignOutAccount_FullMethodName,
pb.StateStoreService_ChangeAccountPassword_FullMethodName,
pb.StateStoreService_GetAuthenticationSettings_FullMethodName,
pb.StateStoreService_SaveAuthenticationSettings_FullMethodName,
pb.StateStoreService_BeginPasskey_FullMethodName,
pb.StateStoreService_FinishPasskey_FullMethodName,
pb.StateStoreService_ListPasskeys_FullMethodName,
pb.StateStoreService_DeletePasskey_FullMethodName,
pb.StateStoreService_OpenAccountSession_FullMethodName,
pb.StateStoreService_RequireFreshAccountSession_FullMethodName,
pb.StateStoreService_CreateAccountRecovery_FullMethodName,
pb.StateStoreService_RecoverAccount_FullMethodName,
} {
t.Run(method, func(t *testing.T) {
if authorizesTaskScopedCall(method, nil, 7) {
t.Fatal("task credential authorizes account operation")
}
})
}
}