Windshaft-cartodb/test/unit/cdb-request-test.js

87 lines
2.7 KiB
JavaScript
Raw Normal View History

'use strict';
2019-10-07 17:29:07 +08:00
require('../support/test-helper');
2015-03-30 22:28:37 +08:00
var assert = require('assert');
2019-10-07 17:29:07 +08:00
var CdbRequest = require('../../lib/models/cdb-request');
const { logger } = require('../../lib/server-options');
2015-03-30 22:28:37 +08:00
2019-10-22 01:07:24 +08:00
describe('req2params', function () {
function createRequest (host, userParam) {
2015-03-30 22:28:37 +08:00
var req = {
params: {},
headers: {
host: host
}
};
if (userParam) {
req.params.user = userParam;
}
return req;
}
2019-10-22 01:07:24 +08:00
it('extracts name from host header', function () {
var cdbRequest = new CdbRequest({ logger });
2015-03-30 22:28:37 +08:00
var user = cdbRequest.userByReq(createRequest('localhost'));
assert.strictEqual(user, 'localhost');
2015-03-30 22:28:37 +08:00
});
2019-10-22 01:07:24 +08:00
it('extracts name from subdomain host header in case of no config', function () {
2015-03-30 22:28:37 +08:00
var userFromHostConfig = global.environment.user_from_host;
global.environment.user_from_host = null;
var cdbRequest = new CdbRequest({ logger });
2015-03-30 22:28:37 +08:00
var user = cdbRequest.userByReq(createRequest('development.localhost.lan'));
global.environment.user_from_host = userFromHostConfig;
assert.strictEqual(user, 'development');
2015-03-30 22:28:37 +08:00
});
2019-10-22 01:07:24 +08:00
it('considers user param before headers', function () {
var cdbRequest = new CdbRequest({ logger });
2015-03-30 22:28:37 +08:00
var user = cdbRequest.userByReq(createRequest('localhost', 'development'));
assert.strictEqual(user, 'development');
2015-03-30 22:28:37 +08:00
});
2019-10-22 01:07:24 +08:00
it('returns undefined when it cannot extract username', function () {
2015-03-30 22:28:37 +08:00
var userFromHostConfig = global.environment.user_from_host;
global.environment.user_from_host = null;
var cdbRequest = new CdbRequest({ logger });
2015-03-30 22:28:37 +08:00
var user = cdbRequest.userByReq(createRequest('localhost'));
global.environment.user_from_host = userFromHostConfig;
assert.strictEqual(user, undefined);
2015-03-30 22:28:37 +08:00
});
2019-10-22 01:07:24 +08:00
it('should not fail for undefined host header', function () {
var userFromHostConfig = global.environment.user_from_host;
global.environment.user_from_host = null;
var cdbRequest = new CdbRequest({ logger });
var user = cdbRequest.userByReq(createRequest(undefined));
global.environment.user_from_host = userFromHostConfig;
assert.strictEqual(user, undefined);
});
2019-10-22 01:07:24 +08:00
it('should not fail for null host header', function () {
var userFromHostConfig = global.environment.user_from_host;
global.environment.user_from_host = null;
var cdbRequest = new CdbRequest({ logger });
var user = cdbRequest.userByReq(createRequest(null));
global.environment.user_from_host = userFromHostConfig;
assert.strictEqual(user, undefined);
});
2015-03-30 22:28:37 +08:00
});