@@ -15,6 +15,10 @@ import (
15
15
// TestRegoQueriesNoVariables handles cases without variables. These should be
16
16
// very simple and straight forward.
17
17
func TestRegoQueries (t * testing.T ) {
18
+ p := func (v string ) string {
19
+ return "(" + v + ")"
20
+ }
21
+
18
22
testCases := []struct {
19
23
Name string
20
24
Queries []string
@@ -50,7 +54,7 @@ func TestRegoQueries(t *testing.T) {
50
54
"(1 != 2) = true" ,
51
55
"5 == 5" ,
52
56
},
53
- ExpectedSQL : "((( 1 != 2) = true) OR (5 = 5))" ,
57
+ ExpectedSQL : p ( "((1 != 2) = true) OR (5 = 5)" ) ,
54
58
},
55
59
// Variables
56
60
{
@@ -59,7 +63,7 @@ func TestRegoQueries(t *testing.T) {
59
63
Queries : []string {
60
64
`input.x = "hello_world"` ,
61
65
},
62
- ExpectedSQL : "( only_var = 'hello_world')" ,
66
+ ExpectedSQL : p ( " only_var = 'hello_world'" ) ,
63
67
VariableConverter : sqltypes .NewVariableConverter ().RegisterMatcher (
64
68
sqltypes .StringVarMatcher ("only_var" , []string {
65
69
"input" , "x" ,
@@ -96,19 +100,19 @@ func TestRegoQueries(t *testing.T) {
96
100
Queries : []string {
97
101
`input.object.org_owner in {"a", "b", "c"}` ,
98
102
},
99
- ExpectedSQL : "( organization_id :: text = ANY(ARRAY ['a','b','c']))" ,
103
+ ExpectedSQL : p ( " organization_id :: text = ANY(ARRAY ['a','b','c'])" ) ,
100
104
VariableConverter : regosql .DefaultVariableConverter (),
101
105
},
102
106
{
103
107
Name : "SetDereference" ,
104
108
Queries : []string {`"*" in input.object.acl_group_list[input.object.org_owner]` },
105
- ExpectedSQL : "( group_acl->organization_id :: text ? '*')" ,
109
+ ExpectedSQL : p ( " group_acl->organization_id :: text ? '*'" ) ,
106
110
VariableConverter : regosql .DefaultVariableConverter (),
107
111
},
108
112
{
109
113
Name : "JsonbLiteralDereference" ,
110
114
Queries : []string {`"*" in input.object.acl_group_list["4d30d4a8-b87d-45ac-b0d4-51b2e68e7e75"]` },
111
- ExpectedSQL : "( group_acl->'4d30d4a8-b87d-45ac-b0d4-51b2e68e7e75' ? '*')" ,
115
+ ExpectedSQL : p ( " group_acl->'4d30d4a8-b87d-45ac-b0d4-51b2e68e7e75' ? '*'" ) ,
112
116
VariableConverter : regosql .DefaultVariableConverter (),
113
117
},
114
118
{
@@ -134,16 +138,15 @@ func TestRegoQueries(t *testing.T) {
134
138
`"*" in input.object.acl_group_list["4d30d4a8-b87d-45ac-b0d4-51b2e68e7e75"]` ,
135
139
},
136
140
// Special case where the bool is wrapped
137
- ExpectedSQL : "(( false) OR (false))" ,
141
+ ExpectedSQL : p ( "( false) OR (false)" ) ,
138
142
VariableConverter : regosql .NoACLConverter (),
139
143
},
140
144
{
141
145
Name : "TwoExpressions" ,
142
146
Queries : []string {
143
147
`true; true` ,
144
148
},
145
- // Special case where the bool is wrapped
146
- ExpectedSQL : "(true AND true)" ,
149
+ ExpectedSQL : p ("true AND true" ),
147
150
VariableConverter : regosql .DefaultVariableConverter (),
148
151
},
149
152
@@ -155,7 +158,6 @@ func TestRegoQueries(t *testing.T) {
155
158
`"05f58202-4bfc-43ce-9ba4-5ff6e0174a71" = input.object.org_owner` ,
156
159
`"read" in input.object.acl_user_list["d5389ccc-57a4-4b13-8c3f-31747bcdc9f1"]` ,
157
160
},
158
- // Special case where the bool is wrapped
159
161
ExpectedSQL : "true" ,
160
162
VariableConverter : regosql .NoACLConverter (),
161
163
},
@@ -167,7 +169,6 @@ func TestRegoQueries(t *testing.T) {
167
169
input.object.owner != "";
168
170
"d5389ccc-57a4-4b13-8c3f-31747bcdc9f1" = input.object.owner` ,
169
171
},
170
- // Special case where the bool is wrapped
171
172
ExpectedSQL : "((organization_id :: text != '') AND " +
172
173
"(organization_id :: text = ANY(ARRAY ['05f58202-4bfc-43ce-9ba4-5ff6e0174a71'])) AND " +
173
174
"(owner_id :: text != '') AND " +
@@ -180,7 +181,6 @@ func TestRegoQueries(t *testing.T) {
180
181
`"read" in input.object.acl_user_list["d5389ccc-57a4-4b13-8c3f-31747bcdc9f1"]` ,
181
182
`"*" in input.object.acl_user_list["d5389ccc-57a4-4b13-8c3f-31747bcdc9f1"]` ,
182
183
},
183
- // Special case where the bool is wrapped
184
184
ExpectedSQL : "((user_acl->'d5389ccc-57a4-4b13-8c3f-31747bcdc9f1' ? 'read') OR " +
185
185
"(user_acl->'d5389ccc-57a4-4b13-8c3f-31747bcdc9f1' ? '*'))" ,
186
186
VariableConverter : regosql .DefaultVariableConverter (),
@@ -192,10 +192,30 @@ func TestRegoQueries(t *testing.T) {
192
192
input.object.org_owner in {"05f58202-4bfc-43ce-9ba4-5ff6e0174a71"};
193
193
"read" in input.object.acl_group_list[input.object.org_owner]` ,
194
194
},
195
- // Special case where the bool is wrapped
196
195
ExpectedSQL : "((organization_id :: text != '') AND (organization_id :: text = ANY(ARRAY ['05f58202-4bfc-43ce-9ba4-5ff6e0174a71'])) AND (false))" ,
197
196
VariableConverter : regosql .NoACLConverter (),
198
197
},
198
+ {
199
+ Name : "EmptyACLList" ,
200
+ Queries : []string {
201
+ `input.object.org_owner != "";
202
+ input.object.org_owner in set();
203
+ "create" in input.object.acl_group_list[input.object.org_owner]` ,
204
+
205
+ `input.object.org_owner != "";
206
+ input.object.org_owner in set();
207
+ "*" in input.object.acl_group_list[input.object.org_owner]` ,
208
+
209
+ `"create" in input.object.acl_user_list.me` ,
210
+
211
+ `"*" in input.object.acl_user_list.me` ,
212
+ },
213
+ ExpectedSQL : p (p ("(organization_id :: text != '') AND (false) AND (group_acl->organization_id :: text ? 'create')" ) + " OR " +
214
+ p ("(organization_id :: text != '') AND (false) AND (group_acl->organization_id :: text ? '*')" ) + " OR " +
215
+ p ("user_acl->'me' ? 'create'" ) + " OR " +
216
+ p ("user_acl->'me' ? '*'" )),
217
+ VariableConverter : regosql .DefaultVariableConverter (),
218
+ },
199
219
}
200
220
201
221
for _ , tc := range testCases {
0 commit comments