@@ -1254,6 +1254,209 @@ func TestExpandQueryWithACLPermissions(t *testing.T) {
1254
1254
testutil .CompareJSON (t , `{"me":[{"name":"RandomGuy","age":23, "nickname":"RG"},{"name":"RandomGuy2","age":25, "nickname":"RG2"}]}` ,
1255
1255
string (resp .GetJson ()))
1256
1256
1257
+ }
1258
+
1259
+ func TestValQueryWithACLPermissions (t * testing.T ) {
1260
+ ctx , cancel := context .WithTimeout (context .Background (), 100 * time .Second )
1261
+ defer cancel ()
1262
+ dg , err := testutil .DgraphClientWithGroot (testutil .SockAddr )
1263
+ require .NoError (t , err )
1264
+
1265
+ testutil .DropAll (t , dg )
1266
+
1267
+ op := api.Operation {Schema : `
1268
+ name : string @index(exact) .
1269
+ nickname : string @index(exact) .
1270
+ age : int .
1271
+ type TypeName {
1272
+ name: string
1273
+ nickname: string
1274
+ age: int
1275
+ }
1276
+ ` }
1277
+ require .NoError (t , dg .Alter (ctx , & op ))
1278
+
1279
+ resetUser (t )
1280
+
1281
+ accessJwt , _ , err := testutil .HttpLogin (& testutil.LoginParams {
1282
+ Endpoint : adminEndpoint ,
1283
+ UserID : "groot" ,
1284
+ Passwd : "password" ,
1285
+ })
1286
+ require .NoError (t , err , "login failed" )
1287
+
1288
+ createGroup (t , accessJwt , devGroup )
1289
+ // createGroup(t, accessJwt, sreGroup)
1290
+
1291
+ // addRulesToGroup(t, accessJwt, sreGroup, []rule{{"age", Read.Code}, {"name", Write.Code}})
1292
+ addToGroup (t , accessJwt , userid , devGroup )
1293
+
1294
+ txn := dg .NewTxn ()
1295
+ mutation := & api.Mutation {
1296
+ SetNquads : []byte (`
1297
+ _:a <name> "RandomGuy" .
1298
+ _:a <age> "23" .
1299
+ _:a <nickname> "RG" .
1300
+ _:a <dgraph.type> "TypeName" .
1301
+ _:b <name> "RandomGuy2" .
1302
+ _:b <age> "25" .
1303
+ _:b <nickname> "RG2" .
1304
+ _:b <dgraph.type> "TypeName" .
1305
+ ` ),
1306
+ CommitNow : true ,
1307
+ }
1308
+ _ , err = txn .Mutate (ctx , mutation )
1309
+ require .NoError (t , err )
1310
+
1311
+ query := `{q1(func: has(name)){
1312
+ v as name
1313
+ a as age
1314
+ }
1315
+ q2(func: eq(val(v), "RandomGuy")) {
1316
+ val(v)
1317
+ val(a)
1318
+ }}`
1319
+
1320
+ // Test that groot has access to all the predicates
1321
+ resp , err := dg .NewReadOnlyTxn ().Query (ctx , query )
1322
+ require .NoError (t , err , "Error while querying data" )
1323
+ testutil .CompareJSON (t , `{"q1":[{"name":"RandomGuy","age":23},{"name":"RandomGuy2","age":25}],"q2":[{"val(v)":"RandomGuy","val(a)":23}]}` ,
1324
+ string (resp .GetJson ()))
1325
+
1326
+ // All test cases
1327
+ tests := []struct {
1328
+ input string
1329
+ descriptionNoPerm string
1330
+ outputNoPerm string
1331
+ descriptionNamePerm string
1332
+ outputNamePerm string
1333
+ descriptionNameAgePerm string
1334
+ outputNameAgePerm string
1335
+ }{
1336
+ {
1337
+ `
1338
+ {
1339
+ q1(func: has(name)) {
1340
+ v as name
1341
+ a as age
1342
+ }
1343
+ q2(func: eq(val(v), "RandomGuy")) {
1344
+ val(v)
1345
+ val(a)
1346
+ }
1347
+ }
1348
+ ` ,
1349
+ "alice doesn't have access to name or age" ,
1350
+ `{}` ,
1351
+
1352
+ `alice has access to name` ,
1353
+ `{"q1":[{"name":"RandomGuy"},{"name":"RandomGuy2"}],"q2":[{"val(v)":"RandomGuy"}]}` ,
1354
+
1355
+ "alice has access to name and age" ,
1356
+ `{"q1":[{"name":"RandomGuy","age":23},{"name":"RandomGuy2","age":25}],"q2":[{"val(v)":"RandomGuy","val(a)":23}]}` ,
1357
+ },
1358
+ {
1359
+ `{
1360
+ q1(func: has(name) ) {
1361
+ a as age
1362
+ }
1363
+ q2(func: has(name) ) {
1364
+ val(a)
1365
+ }
1366
+ }` ,
1367
+ "alice doesn't have access to name or age" ,
1368
+ `{}` ,
1369
+
1370
+ `alice has access to name` ,
1371
+ `{"q1":[],"q2":[]}` ,
1372
+
1373
+ "alice has access to name and age" ,
1374
+ `{"q1":[{"age":23},{"age":25}],"q2":[{"val(a)":23},{"val(a)":25}]}` ,
1375
+ },
1376
+ {
1377
+ `{
1378
+ f as q1(func: has(name) ) {
1379
+ n as name
1380
+ a as age
1381
+ }
1382
+ q2(func: uid(f), orderdesc: val(a) ) {
1383
+ name
1384
+ val(n)
1385
+ val(a)
1386
+ }
1387
+ }` ,
1388
+ "alice doesn't have access to name or age" ,
1389
+ `{"q2":[]}` ,
1390
+
1391
+ `alice has access to name` ,
1392
+ `{"q1":[{"name":"RandomGuy"},{"name":"RandomGuy2"}],
1393
+ "q2":[{"name":"RandomGuy","val(n)":"RandomGuy"},{"name":"RandomGuy2","val(n)":"RandomGuy2"}]}` ,
1394
+
1395
+ "alice has access to name and age" ,
1396
+ `{"q1":[{"name":"RandomGuy","age":23},{"name":"RandomGuy2","age":25}],
1397
+ "q2":[{"name":"RandomGuy2","val(n)":"RandomGuy2","val(a)":25},{"name":"RandomGuy","val(n)":"RandomGuy","val(a)":23}]}` ,
1398
+ },
1399
+ }
1400
+
1401
+ userClient , err := testutil .DgraphClient (testutil .SockAddr )
1402
+ require .NoError (t , err )
1403
+ time .Sleep (6 * time .Second )
1404
+
1405
+ err = userClient .Login (ctx , userid , userpassword )
1406
+ require .NoError (t , err )
1407
+
1408
+ // Query via user when user has no permissions
1409
+ for _ , tc := range tests {
1410
+ desc := tc .descriptionNoPerm
1411
+ t .Run (desc , func (t * testing.T ) {
1412
+ resp , err := userClient .NewTxn ().Query (ctx , tc .input )
1413
+ require .NoError (t , err )
1414
+ testutil .CompareJSON (t , tc .outputNoPerm , string (resp .Json ))
1415
+ })
1416
+ }
1417
+
1418
+ // Login to groot to modify accesses (1)
1419
+ accessJwt , _ , err = testutil .HttpLogin (& testutil.LoginParams {
1420
+ Endpoint : adminEndpoint ,
1421
+ UserID : "groot" ,
1422
+ Passwd : "password" ,
1423
+ })
1424
+ require .NoError (t , err , "login failed" )
1425
+
1426
+ // Give read access of <name> to dev
1427
+ addRulesToGroup (t , accessJwt , devGroup , []rule {{"name" , Read .Code }})
1428
+ time .Sleep (6 * time .Second )
1429
+
1430
+ for _ , tc := range tests {
1431
+ desc := tc .descriptionNamePerm
1432
+ t .Run (desc , func (t * testing.T ) {
1433
+ resp , err := userClient .NewTxn ().Query (ctx , tc .input )
1434
+ require .NoError (t , err )
1435
+ testutil .CompareJSON (t , tc .outputNamePerm , string (resp .Json ))
1436
+ })
1437
+ }
1438
+
1439
+ // Login to groot to modify accesses (1)
1440
+ accessJwt , _ , err = testutil .HttpLogin (& testutil.LoginParams {
1441
+ Endpoint : adminEndpoint ,
1442
+ UserID : "groot" ,
1443
+ Passwd : "password" ,
1444
+ })
1445
+ require .NoError (t , err , "login failed" )
1446
+
1447
+ // Give read access of <name> and <age> to dev
1448
+ addRulesToGroup (t , accessJwt , devGroup , []rule {{"name" , Read .Code }, {"age" , Read .Code }})
1449
+ time .Sleep (6 * time .Second )
1450
+
1451
+ for _ , tc := range tests {
1452
+ desc := tc .descriptionNameAgePerm
1453
+ t .Run (desc , func (t * testing.T ) {
1454
+ resp , err := userClient .NewTxn ().Query (ctx , tc .input )
1455
+ require .NoError (t , err )
1456
+ testutil .CompareJSON (t , tc .outputNameAgePerm , string (resp .Json ))
1457
+ })
1458
+ }
1459
+
1257
1460
}
1258
1461
func TestNewACLPredicates (t * testing.T ) {
1259
1462
ctx , _ := context .WithTimeout (context .Background (), 100 * time .Second )
0 commit comments