providertest

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package providertest provides conformance tests for authz.Provider implementations.

Package providertest provides conformance tests for authz provider implementations.

This package follows the omnivoice testing pattern with three-tier testing:

  • Interface tests: Basic interface contract compliance (always run)
  • Behavior tests: Edge cases and contract guarantees (always run)
  • Integration tests: Tests requiring external setup (conditional)

Example usage:

func TestConformance(t *testing.T) {
    provider := rbac.New()
    providertest.RunAll(t, providertest.Config{
        Provider: provider,
    })
}

Index

Constants

View Source
const ResourceType = "grantdemo_project"

ResourceType is the resource type RunResourceGrantTests grants on. A provider whose rules come from a schema must declare it with ResourceFragment; the relationship-based provider's schema is the base schema followed by it.

Variables

This section is empty.

Functions

func ResourceFragment added in v0.14.0

func ResourceFragment() string

ResourceFragment is the SpiceDB definition of ResourceType: the standard collaborator-able resource of authz.ResourceRoleFragment.

func RunAll

func RunAll(t *testing.T, cfg Config)

RunAll runs all test tiers: interface, behavior, and integration tests.

func RunBehaviorTests

func RunBehaviorTests(t *testing.T, cfg Config)

RunBehaviorTests runs edge case and contract guarantee tests.

func RunIntegrationTests

func RunIntegrationTests(t *testing.T, cfg Config)

RunIntegrationTests runs tests that may require external setup.

func RunInterfaceTests

func RunInterfaceTests(t *testing.T, cfg Config)

RunInterfaceTests runs basic interface contract compliance tests. These tests verify that the provider correctly implements the interfaces.

func RunManagerRelationTests added in v0.14.0

func RunManagerRelationTests(t *testing.T, cfg Config)

RunManagerRelationTests checks the guardian and educator permissions on principal resources (ADR-005). It requires cfg.GrantManager and can run on its own against providers, such as SpiceDB, whose other integration cases need a matching schema.

func RunOrgPermissionTests added in v0.14.0

func RunOrgPermissionTests(t *testing.T, cfg Config)

RunOrgPermissionTests checks every base organization permission for every organization role and for a non-member, so providers agree with authz.OrgRolePermissions and the base SpiceDB schema. It requires cfg.GrantOrgRole.

func RunResourceGrantTests added in v0.14.0

func RunResourceGrantTests(t *testing.T, cfg Config)

RunResourceGrantTests checks the resource-grant contract (ADR-008): grants, role replacement, reductions, revocation, the collaborator membership a first grant creates and the last revoke removes, what a collaborator may and may not do on the organization, and ListResources for each permission. The same suite runs against the role-based and the relationship-based provider, which is what makes the two modes interchangeable on the contract.

It requires cfg.Provider, cfg.ResourceGranter and cfg.GrantOrgRole. The role-based provider's permission map must grant "grantdemo_project.view" to member and "grantdemo_project.{view,edit,manage}" to admin and owner, as the standard resource fragment does through the organization.

Types

type Config

type Config struct {
	// Provider is the authz.Authorizer implementation to test (required).
	Provider authz.Authorizer

	// OrgProvider is the authz.OrgAuthorizer implementation to test (optional).
	// If nil and Provider implements OrgAuthorizer, Provider will be used.
	OrgProvider authz.OrgAuthorizer

	// PlatformProvider is the authz.PlatformAuthorizer implementation to test (optional).
	// If nil and Provider implements PlatformAuthorizer, Provider will be used.
	PlatformProvider authz.PlatformAuthorizer

	// DecisionProvider is the authz.DecisionAuthorizer implementation to test (optional).
	// If nil and Provider implements DecisionAuthorizer, Provider will be used.
	DecisionProvider authz.DecisionAuthorizer

	// SkipIntegration skips integration tests that may require external setup.
	SkipIntegration bool

	// Timeout for test operations. Default: 30 seconds.
	Timeout time.Duration

	// TestPrincipalID is a UUID for test principals. Default: random UUID.
	TestPrincipalID uuid.UUID

	// TestOrgID is a UUID for test organizations. Default: random UUID.
	TestOrgID uuid.UUID

	// TestResourceID is a UUID for test resources. Default: random UUID.
	TestResourceID uuid.UUID

	// GrantOrgRole, when set, gives principalID a role in orgID, enabling
	// the organization permission tests.
	GrantOrgRole func(t *testing.T, orgID, principalID uuid.UUID, role string)

	// GrantManager, when set, records that managerID manages managedID
	// with a manager role, enabling the manager relation tests (ADR-005).
	GrantManager func(t *testing.T, managedID, managerID uuid.UUID, role string)

	// ResourceGranter is the grant contract under test (ADR-008), enabling
	// RunResourceGrantTests. It is usually the Provider itself.
	ResourceGranter authz.ResourceGranter

	// NewPrincipal returns an identifier of a principal the provider can
	// grant to; a provider backed by a database creates the row. Default:
	// a random UUID.
	NewPrincipal func(t *testing.T) uuid.UUID

	// NewOrganization returns an identifier of an organization with no
	// members; a provider backed by a database creates the row. Default:
	// a random UUID.
	NewOrganization func(t *testing.T) uuid.UUID

	// RegisterResource, when set, is called once for each resource before
	// the test grants on it, for providers that need the resource linked
	// to its organization (the relationship-based provider writes the
	// resource's org relation, which the application does when it creates
	// the resource).
	RegisterResource func(t *testing.T, resource authz.ResourceRef)

	// SetupFunc is called before each test to configure the provider.
	// Use this to set up roles, permissions, etc.
	SetupFunc func(t *testing.T)
}

Config configures the provider test suite.

type MockProvider

type MockProvider struct {

	// Test hooks for customization
	CanFunc             func(ctx context.Context, principal authz.Principal, action authz.Action, resource authz.Resource) (bool, error)
	IsPlatformAdminFunc func(ctx context.Context, principal authz.Principal) (bool, error)
	// contains filtered or unexported fields
}

MockProvider is a mock implementation of authz interfaces for testing.

func NewMockProvider

func NewMockProvider() *MockProvider

NewMockProvider creates a new mock provider with default configuration.

func (*MockProvider) Can

func (m *MockProvider) Can(ctx context.Context, principal authz.Principal, action authz.Action, resource authz.Resource) (bool, error)

Can checks if a principal can perform an action on a resource.

func (*MockProvider) CanAll

func (m *MockProvider) CanAll(ctx context.Context, principal authz.Principal, actions []authz.Action, resource authz.Resource) (bool, error)

CanAll checks if a principal can perform all specified actions on a resource.

func (*MockProvider) CanAny

func (m *MockProvider) CanAny(ctx context.Context, principal authz.Principal, actions []authz.Action, resource authz.Resource) (bool, error)

CanAny checks if a principal can perform any of the specified actions on a resource.

func (*MockProvider) CanForOrg

func (m *MockProvider) CanForOrg(ctx context.Context, principal authz.Principal, orgID uuid.UUID, action authz.Action, resource authz.Resource) (bool, error)

CanForOrg checks permission scoped to a specific organization.

func (*MockProvider) Decide

func (m *MockProvider) Decide(ctx context.Context, principal authz.Principal, action authz.Action, resource authz.Resource) (authz.Decision, error)

Decide returns a detailed authorization decision.

func (*MockProvider) Filter

func (m *MockProvider) Filter(ctx context.Context, principal authz.Principal, action authz.Action, resources []authz.Resource) ([]authz.Resource, error)

Filter returns only the resources the principal can access with the given action.

func (*MockProvider) GetRole

func (m *MockProvider) GetRole(ctx context.Context, principal authz.Principal, orgID uuid.UUID) (string, error)

GetRole returns the principal's role in an organization.

func (*MockProvider) IsMember

func (m *MockProvider) IsMember(ctx context.Context, principal authz.Principal, orgID uuid.UUID) (bool, error)

IsMember checks if a principal is a member of an organization.

func (*MockProvider) IsPlatformAdmin

func (m *MockProvider) IsPlatformAdmin(ctx context.Context, principal authz.Principal) (bool, error)

IsPlatformAdmin checks if a principal has platform-wide admin access.

func (*MockProvider) Name

func (m *MockProvider) Name() string

Name returns the provider name.

func (*MockProvider) SetHierarchy

func (m *MockProvider) SetHierarchy(h authz.RoleHierarchy)

SetHierarchy sets the role hierarchy.

func (*MockProvider) SetPermissions

func (m *MockProvider) SetPermissions(p authz.RolePermissions)

SetPermissions sets the role permissions.

func (*MockProvider) SetPlatformAdmin

func (m *MockProvider) SetPlatformAdmin(principalID uuid.UUID, isAdmin bool)

SetPlatformAdmin sets a principal's platform admin status.

func (*MockProvider) SetRole

func (m *MockProvider) SetRole(principalID, orgID uuid.UUID, role string)

SetRole sets a principal's role in an organization.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL