From 6adee328b24ce17971acdbd66fb8be9560be99f5 Mon Sep 17 00:00:00 2001 From: Andrew Aladjev Date: Mon, 1 Aug 2016 14:50:35 +0300 Subject: [PATCH] fixed thali callback test --- test/test-thali-callback.js | 115 +++++++++++++++++++++++++++++------- 1 file changed, 95 insertions(+), 20 deletions(-) diff --git a/test/test-thali-callback.js b/test/test-thali-callback.js index 8c4c76b..c138ebb 100644 --- a/test/test-thali-callback.js +++ b/test/test-thali-callback.js @@ -6,42 +6,117 @@ var request = require('supertest'), colors = require('colors'), assert = require('assert'); + var lib = require(fspath.join(__dirname, '../lib/index')); +var dbName = 'foobar'; +var pathPrefix = '/' + dbName + '/_local/thali__ID_'; + +var validId = 'zz-xx--bb'; + + +function verifyThaliUser(idToCheck) { + console.log(colors.green('checking ID: %s'), idToCheck); + return validId === idToCheck; +} -function genericHandlers(router) { +function genericHandlers(router, path) { var handlers = require('./handlers2'); - router.get('*', handlers.get); + router.get(path, handlers.get); + router.post(path, handlers.post); + router.put(path, handlers.put); + router.head(path, handlers.head); + router.options(path, handlers.options); + router.delete(path, handlers.options); return router; } -var acl = [{ - 'role': 'user', - 'paths': [ - { - 'path': '/{:db}/_local/{:id}', - 'verbs': ['GET'] - }, - { - 'path': '/{:db}/_local/thali_{:id}', - 'verbs': ['GET'] - } - ] -}]; - -describe('test-thali-callback.js - should let all through', function() { +describe('We are validating requests that look like /db/_local/thali_:ID', function() { + describe('if the request is ../thali_ and nothing', function(done) { + var app, router; app = express(); router = express.Router(); + + var rootPath = '/' + dbName + '/_local/thali_'; + var pathWithId = rootPath + validId; + + 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, verifyThaliUser)); + //mock handlers + app.use('/', genericHandlers(router, pathWithId)); + }); + + /** mapped paths mix of verbs */ + it('GET should be OK - we are using ' + validId, function(done) { + request(app) + .get(pathWithId) + .set('Accept', 'application/json') + .expect(200, done); + }); + it('GET should fail as it does not match valid ID ' + rootPath + 'zzz', function(done) { + request(app) + .get(rootPath + 'zzz') + .set('Accept', 'application/json') + .expect(401, done); + }); + it('GET should fail as it does not match valid ID ' + rootPath, function(done) { + request(app) + .get(rootPath) + .set('Accept', 'application/json') + .expect(401, done); + }); + it('PUT should be OK - we are using ' + validId, function(done) { + request(app) + .put(pathWithId) + .set('Accept', 'application/json') + .expect(200, done); + }); + it('DELETE should be OK - we are using ' + validId, function(done) { + request(app) + .delete(pathWithId) + .set('Accept', 'application/json') + .expect(200, done); + }); + it('POST should fail no acl for verb - we are using ' + validId, function(done) { + request(app) + .post(pathWithId) + .set('Accept', 'application/json') + .expect(401, done); + }); + + /** unmapped paths */ + it('should fail with a 401 as not valid', function(done) { + request(app) + .get(rootPath) + .set('Accept', 'application/json') + .expect(401, done); + }); + + it('should OK as full path', function(done) { + request(app) + .get(pathWithId) + .set('Accept', 'application/json') + .expect(200, done); + }); + }); describe('req should be passed to callback', function() { var app = express(); var router = express.Router(); before(function() { router.all('*', function(req, res, next) { - req.connection.pskRole = 'user'; + req.connection.pskRole = 'repl'; next(); }); + var acl = require('./acl-block.1.js'); router.all('*', lib('foobar', acl, function(thaliId, req) { - return thaliId == 'xx' && req.connection.pskRole === 'user'; + return thaliId == 'xx' && req.connection.pskRole === 'repl'; })); - app.use('/', genericHandlers(router)); + app.use('/', genericHandlers(router, '*')); }); it('/foobar/_local/xx should be 200', function(done) { request(app)