Skip to content

Commit bc6ba29

Browse files
author
nicola
committed
adding some example tests for the new ACL
1 parent 8423336 commit bc6ba29

2 files changed

Lines changed: 77 additions & 17 deletions

File tree

‎acl.js‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ function ACL (opts) {
2525
this.ldp = opts.ldp;
2626
this.origin = opts.origin || '';
2727
}
28+
exports.ACL = ACL;
2829

2930
/**
3031
* Gets an ACL file and parses it
3132
*
3233
* @method readACL
3334
* @param {String} pathACL Path to acl file
3435
* @param {String} pathUri URI of the acl file
35-
* @param {ACL~readACLcb} Callback called when ACL is read
36+
* @param {ACL~readACLcb} callback Callback called when ACL is read
3637
*/
3738
ACL.prototype.readACL = function(pathAcl, pathUri, callback) {
3839
var ldp = this.ldp;
@@ -407,6 +408,12 @@ ACL.prototype.fetchDocument = function(uri, callback) {
407408
* @param {Object} RDFlib graph of the fetched file
408409
*/
409410

411+
/**
412+
* Retrieves userId from session or header (On-Behalf-Of)
413+
*
414+
* @method getUserId
415+
* @param {ACL~getUserId_cb} callback Callback called when UserId is retrieved
416+
*/
410417
ACL.prototype.getUserId = function (callback) {
411418
if (!this.onBehalfOf) {
412419
return callback(null, this.session.userId);
@@ -433,6 +440,14 @@ ACL.prototype.getUserId = function (callback) {
433440
* @param {String} userId User WebID
434441
*/
435442

443+
/**
444+
* Gets an ACL file and parses it
445+
*
446+
* @method verifyDelegator
447+
* @param {String} delegator
448+
* @param {String} delegatee
449+
* @param {ACL~verifyDelegator_cb} callback
450+
*/
436451
ACL.prototype.verifyDelegator = function (delegator, delegatee, callback) {
437452
this.fetchDocument(delegator, function(err, delegatorGraph) {
438453

@@ -442,7 +457,7 @@ ACL.prototype.verifyDelegator = function (delegator, delegatee, callback) {
442457
.each(delegatorGraph.sym(delegator),
443458
delegatorGraph.sym("http://www.w3.org/ns/auth/acl#delegates"),
444459
undefined);
445-
for(var delegateeIndex in delegatesStatements) {
460+
for (var delegateeIndex in delegatesStatements) {
446461
var delegateeValue = delegatesStatements[delegateeIndex];
447462
if (rdfVocab.debrack(delegateeValue.toString()) === delegatee) {
448463
callback(null, true);
@@ -452,10 +467,9 @@ ACL.prototype.verifyDelegator = function (delegator, delegatee, callback) {
452467
return callback(null, false);
453468
});
454469
};
455-
456470
/**
457-
* Callback used by findACLinPath.
458-
* @callback ACL~findACLinPath_cb
471+
* Callback used by verifyDelegator.
472+
* @callback ACL~verifyDelegator_cb
459473
* @param {Object} err Error occurred when reading the acl file
460474
* @param {Number} err.status Status code of the error (HTTP way)
461475
* @param {String} err.message Reason of the error

‎test/acl.js‎

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ var request = require('request');
66
var S = require('string');
77
var supertest = require('supertest');
88
var ldnode = require('../index');
9+
var ACL = require('../acl').ACL;
910

10-
describe('ACL', function() {
11+
describe('ACL HTTP', function() {
1112
this.timeout(10000);
1213
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
1314

@@ -67,7 +68,7 @@ describe('ACL', function() {
6768
}
6869

6970

70-
describe('Basic HTTPS Test', function() {
71+
describe('Basic', function() {
7172
it('Should return "Hello, World!"', function(done) {
7273
var options = createOptions('hello.html', 'user1');
7374
request(options, function(error, response, body) {
@@ -86,7 +87,7 @@ describe('ACL', function() {
8687
});
8788
});
8889

89-
describe("Empty ACL Test", function() {
90+
describe("Empty .acl", function() {
9091
it("Should create test folder", function(done) {
9192
var options = createOptions(testDirMetaFile, 'user1');
9293
options.body = "";
@@ -146,7 +147,7 @@ describe('ACL', function() {
146147
});
147148
});
148149

149-
describe("ACL Origin Test", function() {
150+
describe("Origin", function() {
150151
it("Should PUT new ACL file", function(done) {
151152
var options = createOptions(testDirAclFile, 'user1');
152153
options.headers = {
@@ -236,7 +237,7 @@ describe('ACL', function() {
236237
});
237238
});
238239

239-
describe("ACL owner-only test", function() {
240+
describe("Owner-only", function() {
240241
var body = "<#Owner>\n" +
241242
" <http://www.w3.org/ns/auth/acl#accessTo> <" + address + testDir + "/" +
242243
">, <" + address + testDirAclFile + ">;\n" +
@@ -340,7 +341,7 @@ describe('ACL', function() {
340341
});
341342
});
342343

343-
describe("ACL read-only test", function() {
344+
describe("Read-only", function() {
344345
var body = "<#Owner>\n" +
345346
" a <http://www.w3.org/ns/auth/acl#Authorization> ;\n" +
346347
" <http://www.w3.org/ns/auth/acl#accessTo> <" + address + testDir + "/" +
@@ -444,7 +445,7 @@ describe('ACL', function() {
444445
});
445446
});
446447

447-
describe("ACL glob test", function() {
448+
describe("Glob", function() {
448449
it("user2 should be able to send glob request", function(done) {
449450
var options = createOptions(globFile, 'user2');
450451
request.get(options, function(error, response, body) {
@@ -479,7 +480,7 @@ describe('ACL', function() {
479480
});
480481
});
481482

482-
describe("ACL append-only test", function() {
483+
describe("Append-only", function() {
483484
var body = "<#Owner>\n" +
484485
" <http://www.w3.org/ns/auth/acl#accessTo> <" + address + abcFile +
485486
">, <" + address + abcAclFile + ">;\n" +
@@ -588,7 +589,7 @@ describe('ACL', function() {
588589
});
589590
});
590591

591-
describe("ACL restricted test", function() {
592+
describe("Restricted", function() {
592593
var body = "<#Owner>\n" +
593594
" <http://www.w3.org/ns/auth/acl#accessTo> <" + address + abcFile + ">, <" +
594595
address + abcAclFile + ">;\n" +
@@ -696,7 +697,7 @@ describe('ACL', function() {
696697
});
697698
});
698699

699-
describe("ACL group test", function() {
700+
describe("Group", function() {
700701
var groupTriples = "<#> a <http://xmlns.com/foaf/0.1/Group>;\n" +
701702
" <http://xmlns.com/foaf/0.1/member> <a>, <b>, <" + user2 + "> .\n";
702703
var body = "<#Owner>\n" +
@@ -828,7 +829,7 @@ describe('ACL', function() {
828829
});
829830
});
830831

831-
describe("ACL defaultForNew test", function() {
832+
describe("defaultForNew", function() {
832833
var body = "<#Owner>\n" +
833834
" <http://www.w3.org/ns/auth/acl#accessTo> <" + address + testDir + "/" + ">, <" +
834835
address + testDirAclFile + ">;\n" +
@@ -964,7 +965,7 @@ describe('ACL', function() {
964965
// });
965966
});
966967

967-
describe("ACL cleaup", function() {
968+
describe("Cleaup", function() {
968969
it("should remove all files and dirs created", function(done) {
969970
try {
970971
// must remove the ACLs in sync
@@ -983,3 +984,48 @@ describe('ACL', function() {
983984
});
984985
});
985986
});
987+
988+
describe('ACL Class', function () {
989+
var ldpConfig = {
990+
mount: '/test',
991+
root: __dirname + '/resources',
992+
key: __dirname + '/keys/key.pem',
993+
cert: __dirname + '/keys/cert.pem',
994+
webid: true
995+
};
996+
var ldpServer = ldnode(ldpConfig);
997+
var ldp = ldpServer.locals.ldp;
998+
999+
describe('getUserId', function () {
1000+
it('should return userId in session if On-Behalf-Of is not specified', function() {
1001+
var acl = new ACL({
1002+
ldp: ldp,
1003+
origin: 'https://example.com',
1004+
session: {
1005+
userId: 'https://user1.databox.me/profile/card#me',
1006+
identified: true
1007+
}
1008+
});
1009+
1010+
acl.getUserId(function(err, userId) {
1011+
assert.equal(userId, 'https://user1.databox.me/profile/card#me');
1012+
});
1013+
});
1014+
1015+
it('should return userId in session if On-Behalf-Of is not valid', function() {
1016+
var acl = new ACL({
1017+
ldp: ldp,
1018+
origin: 'https://example.com',
1019+
session: {
1020+
userId: 'https://user1.databox.me/profile/card#me',
1021+
identified: true
1022+
},
1023+
onBehalfOf: ''
1024+
});
1025+
1026+
acl.getUserId(function(err, userId) {
1027+
assert.equal(userId, 'https://user1.databox.me/profile/card#me');
1028+
});
1029+
});
1030+
});
1031+
});

0 commit comments

Comments
 (0)