Skip to content

Instantly share code, notes, and snippets.

@charithe
Created June 22, 2023 09:33
Show Gist options
  • Save charithe/f060269f676fde8f8a54374cf8a36eea to your computer and use it in GitHub Desktop.
Save charithe/f060269f676fde8f8a54374cf8a36eea to your computer and use it in GitHub Desktop.
Viewing Cerbos test server logs
package x_test
import (
"context"
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"github.com/cerbos/cerbos/client"
"github.com/cerbos/cerbos/client/testutil"
"github.com/stretchr/testify/require"
)
func TestCerbos(t *testing.T) {
t.Setenv("CERBOS_LOG_LEVEL", "debug")
s, err := testutil.StartCerbosServer()
if err != nil {
t.Fatalf("Failed to start Cerbos server: %v", err)
}
defer s.Stop()
logger := zaptest.NewLogger(t)
zap.ReplaceGlobals(logger)
t.Log("Starting test")
c, err := client.New(s.GRPCAddr(), client.WithPlaintext())
if err != nil {
t.Fatalf("Failed to create Cerbos client: %v", err)
}
resp, err := c.CheckResourceSet(
context.TODO(),
client.NewPrincipal("john").
WithRoles("employee", "manager").
WithAttr("department", "marketing").
WithAttr("geography", "GB"),
client.NewResourceSet("leave_request").
AddResourceInstance("XX125", map[string]any{
"department": "marketing",
"geography": "GB",
"owner": "harry",
"status": "DRAFT",
}),
"view", "approve")
require.NoError(t, err)
require.True(t, resp.IsAllowed("XX125", "view"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment