MCPcopy
hub / github.com/ory/keto / TestCreateCmd

Function TestCreateCmd

cmd/relationtuple/create_test.go:31–170  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29}
30
31func TestCreateCmd(t *testing.T) {
32 nspace := &namespace.Namespace{Name: t.Name()}
33 nspaceUser := &namespace.Namespace{Name: "User"}
34
35 newCmd := func() *cobra.Command {
36 cmd := &cobra.Command{
37 Use: "keto",
38 }
39 RegisterCommandsRecursive(cmd)
40 return cmd
41 }
42
43 otherNspace := &namespace.Namespace{Name: "other"}
44 ts := client.NewTestServer(t, []*namespace.Namespace{nspace, nspaceUser, otherNspace}, newCmd)
45 defer ts.Shutdown(t)
46
47 createFile := func(t *testing.T, tuple any) string {
48 dir := t.TempDir()
49
50 data, err := json.Marshal(tuple)
51 require.NoError(t, err)
52
53 filename := randx.MustString(10, randx.AlphaLowerNum) + ".json"
54 require.NoError(t, os.WriteFile(filepath.Join(dir, filename), data, 0o600))
55 return filepath.Join(dir, filename)
56 }
57
58 type getTuple func() *ketoapi.RelationTuple
59
60 fns := map[string]getTuple{
61 "subjectID": func() *ketoapi.RelationTuple {
62 return helpers.RandomTupleWithSubjectID(nspace.Name)
63 },
64 "subjectSet": func() *ketoapi.RelationTuple {
65 return helpers.RandomTupleWithSubjectSet(nspace.Name, nspaceUser.Name)
66 },
67 }
68
69 for name, createTuple := range fns {
70 t.Run("case="+name, func(t *testing.T) {
71 t.Run("case=creates single tuple from files", func(t *testing.T) {
72 tuple1 := createTuple()
73 tuple2 := createTuple()
74
75 tmpFile := createFile(t, tuple1)
76 tmpFile2 := createFile(t, []*ketoapi.RelationTuple{tuple2})
77
78 stdOut := ts.Cmd.ExecNoErr(t, "relation-tuple", "create", "-f", tmpFile, "-f", tmpFile2)
79 require.Equal(t, renderTable(NewAPICollection([]*ketoapi.RelationTuple{tuple1, tuple2})), stdOut)
80
81 // assert that we can `get` it
82 res1 := ts.Cmd.ExecNoErr(t, "relation-tuple", "get", "--namespace", tuple1.Namespace, "--object", tuple1.Object)
83 require.Equal(t, renderTable(&responseOutput{
84 RelationTuples: NewAPICollection([]*ketoapi.RelationTuple{tuple1}),
85 IsLastPage: true,
86 }), res1)
87
88 // assert that we can `get` it

Callers

nothing calls this directly

Calls 13

ShutdownMethod · 0.95
NewTestServerFunction · 0.92
RandomTupleWithSubjectIDFunction · 0.92
renderTableFunction · 0.85
NewAPICollectionFunction · 0.85
ExecNoErrMethod · 0.80
ExecMethod · 0.65
StringMethod · 0.65
createFileFunction · 0.50
NameMethod · 0.45

Tested by

no test coverage detected