Skip to content

Commit

Permalink
Add test for import behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Jan 29, 2025
1 parent 69be4c9 commit 5dd489f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
62 changes: 62 additions & 0 deletions internal/cmd/import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"context"
"path/filepath"
"testing"

v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/stretchr/testify/require"

"github.com/authzed/zed/internal/client"
zedtesting "github.com/authzed/zed/internal/testing"
)

var fullyConsistent = &v1.Consistency{Requirement: &v1.Consistency_FullyConsistent{FullyConsistent: true}}

func TestImportCmdHappyPath(t *testing.T) {
require := require.New(t)
cmd := zedtesting.CreateTestCobraCommandWithFlagValue(t,
zedtesting.StringFlag{FlagName: "schema-definition-prefix"},
zedtesting.BoolFlag{FlagName: "schema", FlagValue: true},
zedtesting.BoolFlag{FlagName: "relationships", FlagValue: true},
zedtesting.IntFlag{FlagName: "batch-size", FlagValue: 100},
zedtesting.IntFlag{FlagName: "workers", FlagValue: 1},
)
f := filepath.Join("test", "happy-path-validation-file.yaml")

// Set up client
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
srv := zedtesting.NewTestServer(ctx, t)
go func() {
require.NoError(srv.Run(ctx))
}()
conn, err := srv.GRPCDialContext(ctx)
require.NoError(err)

originalClient := client.NewClient
defer func() {
client.NewClient = originalClient
}()

client.NewClient = zedtesting.ClientFromConn(conn)

c, err := zedtesting.ClientFromConn(conn)(cmd)
require.NoError(err)

// Run the import and assert we don't have errors
err = importCmdFunc(cmd, []string{f})
require.NoError(err)

// Run a check with full consistency to see whether the relationships
// and schema are written
resp, err := c.CheckPermission(ctx, &v1.CheckPermissionRequest{
Consistency: fullyConsistent,
Subject: &v1.SubjectReference{Object: &v1.ObjectReference{ObjectType: "user", ObjectId: "1"}},
Permission: "view",
Resource: &v1.ObjectReference{ObjectType: "resource", ObjectId: "1"},
})
require.NoError(err)
require.Equal(resp.Permissionship, v1.CheckPermissionResponse_PERMISSIONSHIP_HAS_PERMISSION)
}
9 changes: 9 additions & 0 deletions internal/cmd/test/happy-path-validation-file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
schemaFile: "./happy-path-validation-schema.zed"
relationships: >-
resource:1#user@user:1
assertions:
assertTrue:
- "resource:1#user@user:1"
assertFalse:
- "resource:1#user@user:2"
6 changes: 6 additions & 0 deletions internal/cmd/test/happy-path-validation-schema.zed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
definition user {}

definition resource {
relation user: user
permission view = user
}

0 comments on commit 5dd489f

Please sign in to comment.