github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/development/warnings_test.go (about) 1 package development 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 developerv1 "github.com/authzed/spicedb/pkg/proto/developer/v1" 10 ) 11 12 func TestWarnings(t *testing.T) { 13 tcs := []struct { 14 name string 15 schema string 16 expectedWarning *developerv1.DeveloperWarning 17 }{ 18 { 19 name: "no warnings", 20 schema: `definition user {} 21 22 definition group { 23 relation direct_member: user 24 permission member = direct_member 25 } 26 27 definition document { 28 relation viewer: user | group#member 29 permission view = viewer 30 } 31 `, 32 }, 33 { 34 name: "permission referencing itself", 35 schema: `definition test { 36 permission view = view 37 }`, 38 expectedWarning: &developerv1.DeveloperWarning{ 39 Message: "Permission \"view\" references itself, which will cause an error to be raised due to infinite recursion", 40 Line: 2, 41 Column: 23, 42 }, 43 }, 44 { 45 name: "permission referencing itself, nested", 46 schema: `definition test { 47 relation viewer: test 48 relation editor: test 49 permission view = viewer + (editor & view) 50 }`, 51 expectedWarning: &developerv1.DeveloperWarning{ 52 Message: "Permission \"view\" references itself, which will cause an error to be raised due to infinite recursion", 53 Line: 4, 54 Column: 42, 55 }, 56 }, 57 { 58 name: "arrow referencing relation", 59 schema: `definition group { 60 relation member: user 61 } 62 63 definition user {} 64 65 definition document { 66 relation group: group 67 permission view = group->member 68 } 69 `, 70 expectedWarning: &developerv1.DeveloperWarning{ 71 Message: "Arrow `group->member` under permission \"view\" references relation \"member\" on definition \"group\"; it is recommended to point to a permission", 72 Line: 9, 73 Column: 23, 74 }, 75 }, 76 { 77 name: "arrow referencing unknown relation", 78 schema: `definition group { 79 } 80 81 definition user {} 82 83 definition document { 84 relation group: group 85 permission view = group->member 86 } 87 `, 88 expectedWarning: &developerv1.DeveloperWarning{ 89 Message: "Arrow `group->member` under permission \"view\" references relation/permission \"member\" that does not exist on any subject types of relation \"group\"", 90 Line: 8, 91 Column: 23, 92 }, 93 }, 94 { 95 name: "arrow referencing subject relation", 96 schema: `definition group { 97 relation direct_member: user 98 permission member = direct_member 99 } 100 101 definition user {} 102 103 definition document { 104 relation parent_group: group#member 105 permission view = parent_group->member 106 } 107 `, 108 expectedWarning: &developerv1.DeveloperWarning{ 109 Message: "Arrow `parent_group->member` under permission \"view\" references relation \"parent_group\" that has relation \"member\" on subject \"group\": *the subject relation will be ignored for the arrow*", 110 Line: 10, 111 Column: 23, 112 }, 113 }, 114 { 115 name: "relation referencing its parent definition in its name", 116 schema: `definition user {} 117 118 definition document { 119 relation viewer: user 120 permission view_document = viewer 121 }`, 122 expectedWarning: &developerv1.DeveloperWarning{ 123 Message: "Permission \"view_document\" references parent type \"document\" in its name; it is recommended to drop the suffix", 124 Line: 5, 125 Column: 5, 126 }, 127 }, 128 } 129 130 for _, tc := range tcs { 131 t.Run(tc.name, func(t *testing.T) { 132 devCtx, devErr, err := NewDevContext(context.Background(), &developerv1.RequestContext{ 133 Schema: tc.schema, 134 }) 135 require.NoError(t, err) 136 require.Empty(t, devErr) 137 138 warnings, err := GetWarnings(context.Background(), devCtx) 139 require.NoError(t, err) 140 141 if tc.expectedWarning == nil { 142 require.Empty(t, warnings) 143 } else { 144 require.Len(t, warnings, 1, "expected exactly one warning") 145 require.Equal(t, tc.expectedWarning, warnings[0]) 146 } 147 }) 148 } 149 }