Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/cmd/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestImportCmdHappyPath(t *testing.T) {
zedtesting.IntFlag{FlagName: "batch-size", FlagValue: 100},
zedtesting.IntFlag{FlagName: "workers", FlagValue: 1},
)
f := filepath.Join("test", "happy-path-validation-file.yaml")
f := filepath.Join("import-test", "happy-path-validation-file.yaml")

// Set up client
ctx, cancel := context.WithCancel(context.Background())
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/validate-test/external-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
schemaFile: "./external-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/validate-test/external-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
}
11 changes: 11 additions & 0 deletions internal/cmd/validate-test/failed-assertions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
schema: |-
definition user {}

definition document {
relation view: user
permission viewer = view
}
assertions:
assertTrue:
- "document:1#viewer@user:maria"
1 change: 1 addition & 0 deletions internal/cmd/validate-test/invalid-schema.zed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
something something {}
8 changes: 8 additions & 0 deletions internal/cmd/validate-test/missing-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
relationships: >-
resource:1#user@user:1
assertions:
assertTrue:
- "resource:1#user@user:1"
assertFalse:
- "resource:1#user@user:2"
2 changes: 2 additions & 0 deletions internal/cmd/validate-test/only-passes-standard.zed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// "and" is a reserved keyword in composable schemas
definition and {}
6 changes: 6 additions & 0 deletions internal/cmd/validate-test/schema-only.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
}
15 changes: 15 additions & 0 deletions internal/cmd/validate-test/standard-validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
schema: |-
definition user {}

definition resource {
relation user: user
permission view = user
}
relationships: >-
resource:1#user@user:1
assertions:
assertTrue:
- "resource:1#user@user:1"
assertFalse:
- "resource:1#user@user:2"
110 changes: 110 additions & 0 deletions internal/cmd/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cmd

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

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

func TestValidate(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
files []string
expectErr string
}{
`standard_passes`: {
files: []string{
filepath.Join("validate-test", "standard-validation.yaml"),
},
},
`external_schema_passes`: {
files: []string{
filepath.Join("validate-test", "external-schema.yaml"),
},
},
`multiple_files_passes`: {
files: []string{
filepath.Join("validate-test", "standard-validation.yaml"),
filepath.Join("validate-test", "external-schema.yaml"),
},
},
`multiple_files_with_one_failure_fails`: {
files: []string{
filepath.Join("validate-test", "standard-validation.yaml"),
filepath.Join("validate-test", "invalid-schema.zed"),
},
expectErr: "Unexpected token at root level",
},
`schema_only_passes`: {
files: []string{
filepath.Join("validate-test", "schema-only.zed"),
},
},
`invalid_schema_fails`: {
files: []string{
filepath.Join("validate-test", "invalid-schema.zed"),
},
expectErr: "Unexpected token at root level",
},
`standard_only_without_flag_passes`: {
files: []string{
filepath.Join("validate-test", "only-passes-standard.zed"),
},
},
`without_schema_fails`: {
files: []string{
filepath.Join("validate-test", "missing-schema.yaml"),
},
expectErr: "either schema or schemaFile must be present",
},
// TODO capture errors on string and assert on them?
//`assertions_fail`: {
// files: []string{
// filepath.Join("validate-test", "failed-assertions.yaml"),
// },
// expectErr: "",
//},
`invalid_url_fails`: {
files: []string{
"http://%zz",
},
expectErr: "invalid URL escape",
},
`url_does_not_exist_fails`: {
files: []string{
"https://unknown-url",
},
expectErr: "Get \"https://unknown-url\": dial tcp: lookup unknown-url",
},
// TODO: https://github.com/authzed/zed/issues/487
//`url_passes`: {
// files: []string{
// "https://play.authzed.com/s/iksdFvCtvnkR/schema",
// },
//},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

require := require.New(t)
cmd := zedtesting.CreateTestCobraCommandWithFlagValue(t,
zedtesting.BoolFlag{FlagName: "force-color", FlagValue: false},
zedtesting.IntFlag{FlagName: "batch-size", FlagValue: 100},
zedtesting.IntFlag{FlagName: "workers", FlagValue: 1},
)

err := validateCmdFunc(cmd, tc.files)
if tc.expectErr == "" {
require.NoError(err)
} else {
require.ErrorContains(err, tc.expectErr)
}
})
}
}
Loading