Skip to content

Commit 3b83c48

Browse files
author
nicola
committed
adding tests for findACLinPath
1 parent d0c1107 commit 3b83c48

2 files changed

Lines changed: 148 additions & 7 deletions

File tree

‎acl.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ ACL.prototype.readACL = function(pathAcl, pathUri, callback) {
6464
* @param {Object} aclGraph Acl graph after reading the file
6565
*/
6666

67-
ACL.prototype.findACLinPath = function (mode, pathAcl, userId, aclGraph, accessType, pathUri, callback) {
67+
ACL.prototype.findACLinPath = function (mode, pathAcl, pathUri, aclGraph, accessType, userId, callback) {
6868
var acl = this;
6969

7070
// TODO check if this is necessary
@@ -85,7 +85,7 @@ ACL.prototype.findACLinPath = function (mode, pathAcl, userId, aclGraph, accessT
8585
}
8686

8787
// User is authenticated
88-
if (userId.length === 0 || acl.session.identified === false) {
88+
if (userId.length === 0 || !acl.session || acl.session.identified === false) {
8989
debug("Authentication required");
9090
return callback({
9191
status: 401,
@@ -141,7 +141,7 @@ ACL.prototype.findACL = function(mode, address, userId, callback) {
141141
aclGraph = $rdf.graph();
142142
}
143143

144-
acl.findACLinPath(mode, pathAcl, userId, aclGraph, accessType, pathUri, function(err, found) {
144+
acl.findACLinPath(mode, pathAcl, pathUri, aclGraph, accessType, userId, function(err, found) {
145145
// Error occurred (e.g. file not found)
146146
if (err) {
147147
// debug('FindACLInPath failed in ' + pathAcl + ' with error ' + err.message);

‎test/acl.js‎

Lines changed: 145 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var S = require('string');
77
var supertest = require('supertest');
88
var ldnode = require('../index');
99
var ACL = require('../acl').ACL;
10+
var async = require('async');
1011

1112
function rm (file) {
1213
return fs.unlinkSync(__dirname + '/resources/' + file);
@@ -1073,9 +1074,9 @@ describe('ACL Class', function () {
10731074
" <http://www.w3.org/ns/auth/acl#mode> <http://www.w3.org/ns/auth/acl#Control> .\n",
10741075
'.acl');
10751076

1076-
acl.readACL(__dirname + '/resources/.acl', address, function (err, res) {
1077+
acl.readACL(__dirname + '/resources/.acl', address, function (err, graph) {
10771078
assert.notOk(err);
1078-
assert.ok(res);
1079+
assert.ok(graph);
10791080
rm('.acl');
10801081
done();
10811082
});
@@ -1095,9 +1096,9 @@ describe('ACL Class', function () {
10951096
"\n",
10961097
'.acl');
10971098

1098-
acl.readACL(__dirname + '/resources/.acl', address, function (err, res) {
1099+
acl.readACL(__dirname + '/resources/.acl', address, function (err, graph) {
10991100
assert.notOk(err);
1100-
assert.ok(res);
1101+
assert.ok(graph);
11011102
rm('.acl');
11021103
done();
11031104
});
@@ -1107,7 +1108,147 @@ describe('ACL Class', function () {
11071108
});
11081109

11091110
describe('findACLInPath', function () {
1111+
it('should allow user when permission is found in pathAcl/pathUri', function(done) {
1112+
var acl = new ACL({
1113+
ldp: ldp,
1114+
origin: 'https://example.com',
1115+
session: {
1116+
userId: user1,
1117+
identified: true
1118+
},
1119+
uri: address
1120+
});
1121+
1122+
write(
1123+
"<#Owner>\n" +
1124+
" <http://www.w3.org/ns/auth/acl#accessTo> <" +
1125+
address + "/" + ">, <" + address + ">;\n" +
1126+
" <http://www.w3.org/ns/auth/acl#owner> <" + user1 + ">;\n" +
1127+
" <http://www.w3.org/ns/auth/acl#mode> <http://www.w3.org/ns/auth/acl#Control> .\n",
1128+
'.acl');
1129+
1130+
acl.readACL(__dirname + '/resources/.acl', address, function (err, aclGraph) {
1131+
async.parallel([
1132+
function(next) {
1133+
acl.findACLinPath('Read', __dirname + '/resources/.acl', address, aclGraph, 'accessTo', user1, function (err, result) {
1134+
assert.equal(result, true);
1135+
assert.notOk(err);
1136+
next();
1137+
});
1138+
},
1139+
function(next) {
1140+
acl.findACLinPath('Write', __dirname + '/resources/.acl', address, aclGraph, 'accessTo', user1, function (err, result) {
1141+
assert.equal(result, true);
1142+
assert.notOk(err);
1143+
next();
1144+
});
1145+
},
1146+
function(next) {
1147+
acl.findACLinPath('Append', __dirname + '/resources/.acl', address, aclGraph, 'accessTo', user1, function (err, result) {
1148+
assert.equal(result, true);
1149+
assert.notOk(err);
1150+
next();
1151+
});
1152+
}
1153+
], function(err) {
1154+
rm('.acl');
1155+
done(err);
1156+
});
1157+
});
1158+
});
11101159

1160+
it('should return 403 if user is not authorized', function(done) {
1161+
var acl = new ACL({
1162+
ldp: ldp,
1163+
origin: 'https://example.com',
1164+
session: {
1165+
userId: user1,
1166+
identified: true
1167+
},
1168+
uri: address
1169+
});
1170+
1171+
write(
1172+
"<#Owner>\n" +
1173+
" <http://www.w3.org/ns/auth/acl#accessTo> <" +
1174+
address + "/" + ">, <" + address + ">;\n" +
1175+
" <http://www.w3.org/ns/auth/acl#owner> <" + user2 + ">;\n" +
1176+
" <http://www.w3.org/ns/auth/acl#mode> <http://www.w3.org/ns/auth/acl#Control> .\n",
1177+
'.acl');
1178+
1179+
acl.readACL(__dirname + '/resources/.acl', address, function (err, aclGraph) {
1180+
async.parallel([
1181+
function(next) {
1182+
acl.findACLinPath('Read', __dirname + '/resources/.acl', address, aclGraph, 'accessTo', user1, function (err, result) {
1183+
assert.equal(err.status, 403);
1184+
assert.notOk(result);
1185+
next();
1186+
});
1187+
},
1188+
function(next) {
1189+
acl.findACLinPath('Write', __dirname + '/resources/.acl', address, aclGraph, 'accessTo', user1, function (err, result) {
1190+
assert.equal(err.status, 403);
1191+
assert.notOk(result);
1192+
next();
1193+
});
1194+
},
1195+
function(next) {
1196+
acl.findACLinPath('Append', __dirname + '/resources/.acl', address, aclGraph, 'accessTo', user1, function (err, result) {
1197+
assert.equal(err.status, 403);
1198+
assert.notOk(result);
1199+
next();
1200+
});
1201+
}
1202+
], function(err) {
1203+
rm('.acl');
1204+
done(err);
1205+
});
1206+
});
1207+
});
1208+
it('should return 401 if user is not authenticated', function(done) {
1209+
var acl = new ACL({
1210+
ldp: ldp,
1211+
origin: 'https://example.com',
1212+
uri: address
1213+
});
1214+
1215+
write(
1216+
"<#Owner>\n" +
1217+
" <http://www.w3.org/ns/auth/acl#accessTo> <" +
1218+
address + "/" + ">, <" + address + ">;\n" +
1219+
" <http://www.w3.org/ns/auth/acl#owner> <" + user2 + ">;\n" +
1220+
" <http://www.w3.org/ns/auth/acl#mode> <http://www.w3.org/ns/auth/acl#Control> .\n",
1221+
'.acl');
1222+
1223+
acl.readACL(__dirname + '/resources/.acl', address, function (err, aclGraph) {
1224+
async.parallel([
1225+
function(next) {
1226+
acl.findACLinPath('Read', __dirname + '/resources/.acl', address, aclGraph, 'accessTo', user1, function (err, result) {
1227+
assert.equal(err.status, 401);
1228+
assert.notOk(result);
1229+
next();
1230+
});
1231+
},
1232+
function(next) {
1233+
acl.findACLinPath('Write', __dirname + '/resources/.acl', address, aclGraph, 'accessTo', user1, function (err, result) {
1234+
assert.equal(err.status, 401);
1235+
assert.notOk(result);
1236+
next();
1237+
});
1238+
},
1239+
function(next) {
1240+
acl.findACLinPath('Append', __dirname + '/resources/.acl', address, aclGraph, 'accessTo', user1, function (err, result) {
1241+
assert.equal(err.status, 401);
1242+
assert.notOk(result);
1243+
next();
1244+
});
1245+
}
1246+
], function(err) {
1247+
rm('.acl');
1248+
done(err);
1249+
});
1250+
});
1251+
});
11111252
});
11121253

11131254
describe('findACL', function () {

0 commit comments

Comments
 (0)