8
8
"testing"
9
9
"time"
10
10
11
+ "github.com/stretchr/testify/assert"
11
12
"github.com/stretchr/testify/require"
12
13
"go.flipt.io/flipt/internal/server/authz/engine/rego/source"
13
14
authrpc "go.flipt.io/flipt/rpc/flipt/auth"
@@ -29,6 +30,17 @@ func TestEngine_NewEngine(t *testing.T) {
29
30
}
30
31
31
32
func TestEngine_IsAllowed (t * testing.T ) {
33
+ policy , err := os .ReadFile ("../testdata/rbac.rego" )
34
+ require .NoError (t , err )
35
+
36
+ data , err := os .ReadFile ("../testdata/rbac.json" )
37
+ require .NoError (t , err )
38
+
39
+ ctx , cancel := context .WithCancel (context .Background ())
40
+ t .Cleanup (cancel )
41
+ engine , err := newEngine (ctx , zaptest .NewLogger (t ), withPolicySource (policySource (string (policy ))), withDataSource (dataSource (string (data )), 5 * time .Second ))
42
+ require .NoError (t , err )
43
+
32
44
tests := []struct {
33
45
name string
34
46
input string
@@ -200,17 +212,6 @@ func TestEngine_IsAllowed(t *testing.T) {
200
212
201
213
for _ , tt := range tests {
202
214
t .Run (tt .name , func (t * testing.T ) {
203
- policy , err := os .ReadFile ("../testdata/rbac.rego" )
204
- require .NoError (t , err )
205
-
206
- data , err := os .ReadFile ("../testdata/rbac.json" )
207
- require .NoError (t , err )
208
-
209
- ctx , cancel := context .WithCancel (context .Background ())
210
- t .Cleanup (cancel )
211
- engine , err := newEngine (ctx , zaptest .NewLogger (t ), withPolicySource (policySource (string (policy ))), withDataSource (dataSource (string (data )), 5 * time .Second ))
212
- require .NoError (t , err )
213
-
214
215
var input map [string ]interface {}
215
216
216
217
err = json .Unmarshal ([]byte (tt .input ), & input )
@@ -221,6 +222,14 @@ func TestEngine_IsAllowed(t *testing.T) {
221
222
require .Equal (t , tt .expected , allowed )
222
223
})
223
224
}
225
+
226
+ t .Run ("viewable namespaces without definition" , func (t * testing.T ) {
227
+ ctx , cancel := context .WithCancel (context .Background ())
228
+ t .Cleanup (cancel )
229
+ namespaces , err := engine .Namespaces (ctx , map [string ]any {})
230
+ require .Error (t , err )
231
+ require .Nil (t , namespaces )
232
+ })
224
233
}
225
234
226
235
func TestEngine_IsAuthMethod (t * testing.T ) {
@@ -269,6 +278,40 @@ func TestEngine_IsAuthMethod(t *testing.T) {
269
278
}
270
279
}
271
280
281
+ func TestViewableNamespaces (t * testing.T ) {
282
+ policy , err := os .ReadFile ("../testdata/viewable_namespaces.rego" )
283
+ require .NoError (t , err )
284
+
285
+ data , err := os .ReadFile ("../testdata/viewable_namespaces.json" )
286
+ require .NoError (t , err )
287
+
288
+ ctx , cancel := context .WithCancel (context .Background ())
289
+ t .Cleanup (cancel )
290
+ engine , err := newEngine (ctx , zaptest .NewLogger (t ), withPolicySource (policySource (string (policy ))), withDataSource (dataSource (string (data )), 5 * time .Second ))
291
+ require .NoError (t , err )
292
+
293
+ t .Cleanup (func () {
294
+ assert .NoError (t , engine .Shutdown (ctx ))
295
+ })
296
+
297
+ tt := []struct {
298
+ name string
299
+ roles []string
300
+ namespaces []string
301
+ }{
302
+ {"empty" , []string {}, []string {}},
303
+ {"devs" , []string {"devs" }, []string {"local" , "staging" }},
304
+ {"devsops" , []string {"devs" , "ops" }, []string {"local" , "production" , "staging" }},
305
+ }
306
+ for _ , tt := range tt {
307
+ t .Run (tt .name , func (t * testing.T ) {
308
+ namespaces , err := engine .Namespaces (ctx , map [string ]any {"roles" : tt .roles })
309
+ require .NoError (t , err )
310
+ require .Equal (t , tt .namespaces , namespaces )
311
+ })
312
+ }
313
+ }
314
+
272
315
type policySource string
273
316
274
317
func (p policySource ) Get (context.Context , source.Hash ) ([]byte , source.Hash , error ) {
0 commit comments