Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Prefix support #8

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
370 changes: 213 additions & 157 deletions lib/index.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"homepage": "https://github.com/thaliproject/salti#readme",
"dependencies": {
"object-assign": "^4.1.0",
"debug": "^2.2.0"
"debug": "^2.2.0",
"jsonschema": "^1.1.0"
},
"devDependencies": {
"colors": "^1.1.2",
Expand Down
78 changes: 43 additions & 35 deletions test/acl-block.1.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
'use strict';

//role.path.verb


module.exports = [{
'role' : 'repl',
'paths' : [
{'path':'/',
'verbs': ['GET']
},{'path': '/{:db}',
'verbs' :['GET']
},{
'path' : '/{:db}/_all_docs',
'verbs':['GET', 'HEAD', 'POST']
},{
'path' :'/{:db}/_changes',
'verbs':['GET', 'POST']
},{
'path' :'/{:db}/_bulk_get',
'verbs': ['POST']
}, {
'path' :'/{:db}/_revs_diff',
'verbs':['POST']
}, {
'path' :'/{:db}/{:id}',
'verbs' :['GET']
}, {
'path' :'/{:db}/{:id}/attachment',
'verbs':['GET']
}, {
'path' :'/{:db}/_local/{:id}',
'verbs':['GET', 'PUT', 'DELETE']
}, {
'path': '/{:db}/_local/thali_{:id}',
'verbs':['GET', 'PUT', 'DELETE']
}
module.exports = [{
'role': 'repl',
'paths': [
{
'path': '/',
'verbs': ['GET']
},
{
'path': '/{:db}',
'verbs': ['GET']
},
{
'path': '/{:db}/_all_docs',
'verbs': ['GET', 'HEAD', 'POST']
},
{
'path': '/{:db}/_changes',
'verbs': ['GET', 'POST']
},
{
'path': '/{:db}/_bulk_get',
'verbs': ['POST']
},
{
'path': '/{:db}/_revs_diff',
'verbs': ['POST']
},
{
'path': '/{:db}/{:id}',
'verbs': ['GET']
},
{
'path': '/{:db}/{:id}/attachment',
'verbs': ['GET']
},
{
'path': '/{:db}/_local/{:id}',
'verbs': ['GET', 'PUT', 'DELETE']
},
{
'path': '/{:db}/_local/thali_{:id}',
'verbs': ['GET', 'PUT', 'DELETE']
}
]
}];
14 changes: 7 additions & 7 deletions test/handlers2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
var colors = require('colors');

module.exports = {
get: function(req, res) {
get: function (req, res) {
console.log(colors.green('\tin the handler for get'));
res.status(200).json({ message: 'you made it' });
// next();
},
post: function(req, res) {
post: function (req, res) {
console.log(colors.green('\tin the post handler'));
res.status(200).json({ name: 'you made it' });
// next();
},
put: function(req, res) {
put: function (req, res) {
console.log(colors.green('\tin the put handler'));
res.status(200).json({ name: 'you made it' });
// next();
},
head: function(req, res) {
head: function (req, res) {
console.log(colors.green('\tin the head handler'));
res.status(200);//.json({ name: 'you made it' });
// next();
},
options: function(req, res) {
options: function (req, res) {
console.log(colors.green('\tin the options handler'));
res.status(200).json({ name: 'you made it' });
// next();
},
delete: function(req, res) {
delete: function (req, res) {
console.log(colors.green('\tin the delete handler'));
res.status(200).json({ name: 'you made it' });
// next();
}
}
};
8 changes: 4 additions & 4 deletions test/jshint.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('mocha-jshint')({
paths: [
'./lib/'
]
});
paths: [
'./lib/'
]
});
63 changes: 32 additions & 31 deletions test/test-core-alldocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
** | /:db/_all_docs | GET, HEAD, POST [1] | allDocs |
*/

var request = require('supertest'),
express = require('express'),
fspath = require('path'),
colors = require('colors'),
assert = require('assert');
var request = require('supertest');
var express = require('express');
var colors = require('colors');
var assert = require('assert');

var handlers = require('./handlers2');
var lib = require('../lib/index');
var acl = require('./acl-block.1.js');

var lib = require(fspath.join(__dirname, '../lib/index'));
var dbName = 'foobar';
var path = '/' + dbName + '/_all_docs';

function genericHandlers(router, path) {
var handlers = require('./handlers2');
router.get(path, handlers.get);
router.post(path, handlers.post);
router.put(path, handlers.put);
Expand All @@ -25,52 +26,52 @@ function genericHandlers(router, path) {
return router;
}

describe('test-core-db-2.js - calling the /db path', function() {
describe('using repl identity', function() {
var app, router; app = express(); router = express.Router();
describe('test-core-db-2.js - calling the /db path', function () {
describe('using repl identity', function () {
var app = express();
var router = express.Router();

before(function() {
//mocker..
router.all('*', function(req, res, next) {
before(function () {
// mocker..
router.all('*', function (req, res, next) {
req.connection.pskRole = 'repl';
next();
})
//Norml middleware usage..0
var acl = require('./acl-block.1.js');
router.all('*', lib('foobar', acl, function(){}));
//mock handlers
});
// Norml middleware usage..0
router.all('*', lib('foobar', acl, function (){}));
// mock handlers
app.use('/', genericHandlers(router, path));
})
});

it('GET should be 200', function(done) {
it('GET should be 200', function (done) {
request(app)
.get(path)
.set('Accept', 'application/json')
.expect(200, done);
})
it('PUT should be 401', function(done) {
});
it('PUT should be 401', function (done) {
request(app)
.put(path)
.set('Accept', 'application/json')
.expect(401, done);
})
it('POST should be 200', function(done) {
});
it('POST should be 200', function (done) {
request(app)
.post(path)
.set('Accept', 'application/json')
.expect(200, done);
})
it('HEAD should be 200', function(done) {
});
it('HEAD should be 200', function (done) {
request(app)
.head(path)
.set('Accept', 'application/json')
.expect(200, done);
})
it('OPTION should be 401', function(done) {
});
it('OPTION should be 401', function (done) {
request(app)
.options(path)
.set('Accept', 'application/json')
.expect(401, done);
})
})
})
});
});
});
81 changes: 41 additions & 40 deletions test/test-core-attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,82 @@
** | /:db/:id/attachment | GET, | |
*/

var request = require('supertest'),
express = require('express'),
fspath = require('path'),
colors = require('colors'),
assert = require('assert');
var request = require('supertest');
var express = require('express');
var colors = require('colors');
var assert = require('assert');

var handlers = require('./handlers2');
var lib = require('../lib/index');
var acl = require('./acl-block.1.js');

var lib = require(fspath.join(__dirname, '../lib/index'));
var dbName = 'foobar';
var path = '/' + dbName + '/1234/attachment';

function genericHandlers(router, path) {
var handlers = require('./handlers2');
router.get(path, handlers.get);
return router;
}

describe('test-core-attachment.js - calling the /db/id/attachment path', function() {
describe('using repl identity', function() {
var app, router; app = express(); router = express.Router();
describe(
'test-core-attachment.js - calling the /db/id/attachment path', function () {
describe('using repl identity', function () {
var app = express();
var router = express.Router();

before(function() {
//mocker..
router.all('*', function(req, res, next) {
before(function () {
// mocker..
router.all('*', function (req, res, next) {
req.connection.pskRole = 'repl';
next();
})
//Norml middleware usage..0
var acl = require('./acl-block.1.js');
router.all('*', lib('foobar', acl, function(){}));
//mock handlers
});
// Norml middleware usage..0
router.all('*', lib('foobar', acl, function (){}));
// mock handlers
app.use('/', genericHandlers(router, path));
})
});

it('GET should be 200', function(done) {
it('GET should be 200', function (done) {
request(app)
.get(path)
.set('Accept', 'application/json')
.expect(200, done);
})
it('POST should be 401', function(done) {
});

it('POST should be 401', function (done) {
request(app)
.post(path)
.set('Accept', 'application/json')
.expect(401, done);
})
it('PUT should be 401', function(done) {
});

it('PUT should be 401', function (done) {
request(app)
.put(path)
.set('Accept', 'application/json')
.expect(401, done);
})
it('GET /zzz should be 401', function(done) {
});


it('GET /zzz should be 401', function (done) {
request(app)
.get(path + '/zzz')
.set('Accept', 'application/json')
.expect(401, done);
})
it('GET /zzz/ should be 401', function(done) {
});

it('GET /zzz/ should be 401', function (done) {
request(app)
.get(path + '/zzz/')
.set('Accept', 'application/json')
.expect(401, done);
})
it('GET zzz should be 401', function(done) {
});

it('GET zzz should be 401', function (done) {
request(app)
.get(path + 'zzz')
.set('Accept', 'application/json')
.expect(401, done);
})

})
})
});
});
});