(t *testing.T)
| 19 | ) |
| 20 | |
| 21 | func TestExpandCommand(t *testing.T) { |
| 22 | nspace := &namespace.Namespace{Name: t.Name()} |
| 23 | nspaceUser := &namespace.Namespace{Name: "User"} |
| 24 | |
| 25 | newCmd := func() *cobra.Command { |
| 26 | cmd := &cobra.Command{ |
| 27 | Use: "keto", |
| 28 | } |
| 29 | RegisterCommandsRecursive(cmd) |
| 30 | relationtuple.RegisterCommandsRecursive(cmd) |
| 31 | return cmd |
| 32 | } |
| 33 | |
| 34 | ts := client.NewTestServer(t, []*namespace.Namespace{nspace, nspaceUser}, newCmd) |
| 35 | defer ts.Shutdown(t) |
| 36 | |
| 37 | tuple := helpers.RandomTupleWithSubjectSet(nspace.Name, nspaceUser.Name) |
| 38 | |
| 39 | nsObj := tuple.Namespace + ":" + tuple.Object |
| 40 | rel := tuple.Relation |
| 41 | subjectSet := tuple.SubjectSet.String() |
| 42 | |
| 43 | // create tuple with subjectSet |
| 44 | ts.Cmd.ExecNoErr(t, "relation-tuple", "create", subjectSet, rel, nsObj) |
| 45 | |
| 46 | t.Run("case=expands existing tuple as JSON", func(t *testing.T) { |
| 47 | stdOut := ts.Cmd.ExecNoErr(t, "expand", rel, nsObj, |
| 48 | "--"+cmdx.FlagFormat, string(cmdx.FormatJSON)) |
| 49 | assert.Contains(t, stdOut, tuple.SubjectSet.Object) |
| 50 | }) |
| 51 | |
| 52 | t.Run("case=unknown tuple returns null JSON", func(t *testing.T) { |
| 53 | stdOut := ts.Cmd.ExecNoErr(t, "expand", rel, nspace.Name+":unknown-obj", |
| 54 | "--"+cmdx.FlagFormat, string(cmdx.FormatJSON)) |
| 55 | assert.Equal(t, "null\n", stdOut) |
| 56 | }) |
| 57 | |
| 58 | t.Run("case=unknown tuple prints empty tree in default format", func(t *testing.T) { |
| 59 | stdOut := ts.Cmd.ExecNoErr(t, "expand", rel, nspace.Name+":unknown-obj") |
| 60 | assert.Contains(t, stdOut, "empty tree") |
| 61 | }) |
| 62 | |
| 63 | t.Run("case=3-arg format still works with deprecation warning", func(t *testing.T) { |
| 64 | stdOut, stdErr, err := ts.Cmd.Exec(nil, "expand", rel, nspace.Name, tuple.Object, |
| 65 | "--"+cmdx.FlagFormat, string(cmdx.FormatJSON)) |
| 66 | require.NoError(t, err) |
| 67 | assert.Contains(t, stdOut, tuple.SubjectSet.Object) |
| 68 | require.Contains(t, stdErr, "deprecated") |
| 69 | }) |
| 70 | |
| 71 | t.Run("case=errors on invalid namespace:object format", func(t *testing.T) { |
| 72 | _, stdErr, err := ts.Cmd.Exec(nil, "expand", rel, "no-colon-here") |
| 73 | require.Error(t, err) |
| 74 | require.Contains(t, stdErr, "expected <object_namespace>:<object_id> format") |
| 75 | }) |
| 76 | } |
nothing calls this directly
no test coverage detected