2018-10-24 00:39:02 +08:00
|
|
|
'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');
|
2015-03-30 22:28:37 +08:00
|
|
|
|
2020-06-03 21:39:02 +08:00
|
|
|
describe('username in host header (CdbRequest)', function () {
|
2019-10-22 01:07:24 +08:00
|
|
|
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 () {
|
2020-06-03 21:39:02 +08:00
|
|
|
var cdbRequest = new CdbRequest();
|
2015-03-30 22:28:37 +08:00
|
|
|
var user = cdbRequest.userByReq(createRequest('localhost'));
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
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;
|
|
|
|
|
2020-06-03 21:39:02 +08:00
|
|
|
var cdbRequest = new CdbRequest();
|
2015-03-30 22:28:37 +08:00
|
|
|
var user = cdbRequest.userByReq(createRequest('development.localhost.lan'));
|
|
|
|
|
|
|
|
global.environment.user_from_host = userFromHostConfig;
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
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 () {
|
2020-06-03 21:39:02 +08:00
|
|
|
var cdbRequest = new CdbRequest();
|
2015-03-30 22:28:37 +08:00
|
|
|
var user = cdbRequest.userByReq(createRequest('localhost', 'development'));
|
|
|
|
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(user, 'development');
|
2015-03-30 22:28:37 +08:00
|
|
|
});
|
|
|
|
|
2020-06-03 21:39:02 +08:00
|
|
|
it('returns throw 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;
|
|
|
|
|
2020-06-03 21:39:02 +08:00
|
|
|
var cdbRequest = new CdbRequest();
|
|
|
|
assert.throws(() => cdbRequest.userByReq(createRequest('localhost')));
|
2015-03-30 22:28:37 +08:00
|
|
|
|
|
|
|
global.environment.user_from_host = userFromHostConfig;
|
|
|
|
});
|
2015-09-30 22:31:56 +08:00
|
|
|
|
2020-06-03 21:39:02 +08:00
|
|
|
it('should throw for undefined host header', function () {
|
2015-09-30 22:31:56 +08:00
|
|
|
var userFromHostConfig = global.environment.user_from_host;
|
|
|
|
global.environment.user_from_host = null;
|
|
|
|
|
2020-06-03 21:39:02 +08:00
|
|
|
var cdbRequest = new CdbRequest();
|
|
|
|
assert.throws(() => cdbRequest.userByReq(createRequest(undefined)));
|
2015-09-30 22:31:56 +08:00
|
|
|
|
|
|
|
global.environment.user_from_host = userFromHostConfig;
|
|
|
|
});
|
|
|
|
|
2020-06-03 21:39:02 +08:00
|
|
|
it('should throw for null host header', function () {
|
2015-09-30 22:31:56 +08:00
|
|
|
var userFromHostConfig = global.environment.user_from_host;
|
|
|
|
global.environment.user_from_host = null;
|
|
|
|
|
2020-06-03 21:39:02 +08:00
|
|
|
var cdbRequest = new CdbRequest();
|
|
|
|
assert.throws(() => cdbRequest.userByReq(createRequest(null)));
|
2015-09-30 22:31:56 +08:00
|
|
|
|
|
|
|
global.environment.user_from_host = userFromHostConfig;
|
|
|
|
});
|
2015-03-30 22:28:37 +08:00
|
|
|
});
|