You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/test/utils.js

498 lines
14 KiB
JavaScript

'use strict';
const assert = require('assert');
const validator = require('validator');
const { JSDOM } = require('jsdom');
const slugify = require('../src/slugify');
const db = require('./mocks/databasemock');
describe('Utility Methods', () => {
8 years ago
// https://gist.github.com/robballou/9ee108758dc5e0e2d028
// create some jsdom magic to allow jQuery to work
const dom = new JSDOM('<html><body></body></html>');
Webpack5 (#10311) * feat: webpack 5 part 1 * fix: gruntfile fixes * fix: fix taskbar warning add app.importScript copy public/src/modules to build folder * refactor: remove commented old code * feat: reenable admin * fix: acp settings pages, fix sortable on manage categories embedded require in html not allowed * fix: bundle serialize/deserizeli so plugins dont break * test: fixe util tests * test: fix require path * test: more test fixes * test: require correct utils module * test: require correct utils * test: log stack * test: fix db require blowing up tests * test: move and disable bundle test * refactor: add aliases * test: disable testing route * fix: move webpack modules necessary for build, into `dependencies` * test: fix one more test remove 500-embed.tpl * fix: restore use of assets/nodebb.min.js, at least for now * fix: remove unnecessary line break * fix: point to proper ACP bundle * test: maybe fix build test * test: composer * refactor: dont need dist * refactor: more cleanup use everything from build/public folder * get rid of conditional import in app.js * fix: ace * refactor: cropper alias * test: lint and test fixes * lint: fix * refactor: rename function to app.require * refactor: go back to using app.require * chore: use github branch * chore: use webpack branch * feat: webpack webinstaller * feat: add chunkFile name with contenthash * refactor: move hooks to top * refactor: get rid of template500Function * fix(deps): use webpack5 branch of 2factor plugin * chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket: * refactor: disable cache on templates loadTemplate is called once by benchpress and the result is cache internally * refactor: add server side helpers.js * feat: deprecate /plugins shorthand route, closes #10343 * refactor: use build/public for webpack * test: fix filename * fix: more specific selector * lint: ignore * refactor: fix comments * test: add debug for random failing test * refactor: cleanup remove test page, remove dupe functions in utils.common * lint: use relative path for now * chore: bump prerelease version * feat: add translateKeys * fix: optional params * fix: get rid of extra timeago files * refactor: cleanup, require timeago locale earlier remove translator.prepareDOM, it is in header.tpl html tag * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * feat: allow app.require('bootbox'/'benchpressjs') * refactor: require server side utils * test: jquery ready * change istaller to use build/public * test: use document.addEventListener * refactor: closes #10301 * refactor: generateTopicClass * fix: column counts for other privileges * fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking] * fix: typo in hook name * refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags) * fix: crash if `delay` not passed in (as it cannot be destructured) * refactor: replace substr * feat: set --panel-offset style in html element based on stored value in localStorage * refactor: addDropupHandler() logic to be less naive - Take into account height of the menu - Don't apply dropUp logic if there's nothing in the dropdown - Remove 'hidden' class (added by default in Persona for post tools) when menu items are added closes #10423 * refactor: simplify utils.params [breaking] Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do. * feat: add support for returning full URLSearchParams for utils.params * fix: utils.params() fallback handling * fix: default empty obj for params() * fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end * fix: utils.params() not allowing relative paths to be passed in * refactor(DRY): new assertPasswordValidity utils method * fix: incorrect error message returned on insufficient privilege on flag edit * fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate - added failing tests and patched up middleware.assert.flags to fix * refactor: flag api v3 tests to create new post and flags on every round * fix: missing error:no-flag language key * refactor: flags.canView to check flag existence, simplify middleware.assert.flag * feat: flag deletion API endpoint, #10426 * feat: UI for flag deletion, closes #10426 * chore: update plugin versions * chore: up emoji * chore: update markdown * chore: up emoji-android * fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check Co-authored-by: Julian Lam <[email protected]>
3 years ago
global.window = dom.window;
global.document = dom.window.document;
global.jQuery = require('jquery');
8 years ago
global.$ = global.jQuery;
const { $ } = global;
Webpack5 (#10311) * feat: webpack 5 part 1 * fix: gruntfile fixes * fix: fix taskbar warning add app.importScript copy public/src/modules to build folder * refactor: remove commented old code * feat: reenable admin * fix: acp settings pages, fix sortable on manage categories embedded require in html not allowed * fix: bundle serialize/deserizeli so plugins dont break * test: fixe util tests * test: fix require path * test: more test fixes * test: require correct utils module * test: require correct utils * test: log stack * test: fix db require blowing up tests * test: move and disable bundle test * refactor: add aliases * test: disable testing route * fix: move webpack modules necessary for build, into `dependencies` * test: fix one more test remove 500-embed.tpl * fix: restore use of assets/nodebb.min.js, at least for now * fix: remove unnecessary line break * fix: point to proper ACP bundle * test: maybe fix build test * test: composer * refactor: dont need dist * refactor: more cleanup use everything from build/public folder * get rid of conditional import in app.js * fix: ace * refactor: cropper alias * test: lint and test fixes * lint: fix * refactor: rename function to app.require * refactor: go back to using app.require * chore: use github branch * chore: use webpack branch * feat: webpack webinstaller * feat: add chunkFile name with contenthash * refactor: move hooks to top * refactor: get rid of template500Function * fix(deps): use webpack5 branch of 2factor plugin * chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket: * refactor: disable cache on templates loadTemplate is called once by benchpress and the result is cache internally * refactor: add server side helpers.js * feat: deprecate /plugins shorthand route, closes #10343 * refactor: use build/public for webpack * test: fix filename * fix: more specific selector * lint: ignore * refactor: fix comments * test: add debug for random failing test * refactor: cleanup remove test page, remove dupe functions in utils.common * lint: use relative path for now * chore: bump prerelease version * feat: add translateKeys * fix: optional params * fix: get rid of extra timeago files * refactor: cleanup, require timeago locale earlier remove translator.prepareDOM, it is in header.tpl html tag * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * feat: allow app.require('bootbox'/'benchpressjs') * refactor: require server side utils * test: jquery ready * change istaller to use build/public * test: use document.addEventListener * refactor: closes #10301 * refactor: generateTopicClass * fix: column counts for other privileges * fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking] * fix: typo in hook name * refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags) * fix: crash if `delay` not passed in (as it cannot be destructured) * refactor: replace substr * feat: set --panel-offset style in html element based on stored value in localStorage * refactor: addDropupHandler() logic to be less naive - Take into account height of the menu - Don't apply dropUp logic if there's nothing in the dropdown - Remove 'hidden' class (added by default in Persona for post tools) when menu items are added closes #10423 * refactor: simplify utils.params [breaking] Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do. * feat: add support for returning full URLSearchParams for utils.params * fix: utils.params() fallback handling * fix: default empty obj for params() * fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end * fix: utils.params() not allowing relative paths to be passed in * refactor(DRY): new assertPasswordValidity utils method * fix: incorrect error message returned on insufficient privilege on flag edit * fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate - added failing tests and patched up middleware.assert.flags to fix * refactor: flag api v3 tests to create new post and flags on every round * fix: missing error:no-flag language key * refactor: flags.canView to check flag existence, simplify middleware.assert.flag * feat: flag deletion API endpoint, #10426 * feat: UI for flag deletion, closes #10426 * chore: update plugin versions * chore: up emoji * chore: update markdown * chore: up emoji-android * fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check Co-authored-by: Julian Lam <[email protected]>
3 years ago
const utils = require('../public/src/utils');
8 years ago
// https://github.com/jprichardson/string.js/blob/master/test/string.test.js
it('should decode HTML entities', (done) => {
assert.strictEqual(
utils.decodeHTMLEntities('Ken Thompson &amp; Dennis Ritchie'),
'Ken Thompson & Dennis Ritchie'
);
assert.strictEqual(
utils.decodeHTMLEntities('3 &lt; 4'),
'3 < 4'
);
assert.strictEqual(
utils.decodeHTMLEntities('http:&#47;&#47;'),
'http://'
);
done();
});
it('should strip HTML tags', (done) => {
assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>'), 'just some text');
assert.strictEqual(utils.stripHTMLTags('<p>just <b>some</b> text</p>', ['p']), 'just <b>some</b> text');
7 years ago
assert.strictEqual(utils.stripHTMLTags('<i>just</i> some <image/> text', ['i']), 'just some <image/> text');
assert.strictEqual(utils.stripHTMLTags('<i>just</i> some <image/> <div>text</div>', ['i', 'div']), 'just some <image/> text');
done();
});
it('should preserve case if requested', (done) => {
assert.strictEqual(slugify('UPPER CASE', true), 'UPPER-CASE');
8 years ago
done();
});
it('should work if a number is passed in', (done) => {
assert.strictEqual(slugify(12345), '12345');
done();
});
describe('username validation', () => {
it('accepts latin-1 characters', () => {
const username = "John\"'-. Doeäâèéë1234";
assert(utils.isUserNameValid(username), 'invalid username');
});
it('rejects empty string', () => {
const username = '';
assert.equal(utils.isUserNameValid(username), false, 'accepted as valid username');
});
it('should reject new lines', () => {
4 years ago
assert.equal(utils.isUserNameValid('myusername\r\n'), false);
});
it('should reject new lines', () => {
4 years ago
assert.equal(utils.isUserNameValid('myusername\n'), false);
});
it('should reject tabs', () => {
4 years ago
assert.equal(utils.isUserNameValid('myusername\t'), false);
});
it('accepts square brackets', () => {
const username = '[best clan] julian';
assert(utils.isUserNameValid(username), 'invalid username');
});
4 years ago
it('accepts regular username', () => {
4 years ago
assert(utils.isUserNameValid('myusername'), 'invalid username');
});
it('accepts quotes', () => {
4 years ago
assert(utils.isUserNameValid('baris "the best" usakli'), 'invalid username');
});
});
describe('email validation', () => {
it('accepts sample address', () => {
const email = '[email protected]';
assert(utils.isEmailValid(email), 'invalid email');
});
it('rejects empty address', () => {
const email = '';
assert.equal(utils.isEmailValid(email), false, 'accepted as valid email');
});
});
describe('UUID generation', () => {
it('return unique random value every time', () => {
delete require.cache[require.resolve('../src/utils')];
const { generateUUID } = require('../src/utils');
const uuid1 = generateUUID();
const uuid2 = generateUUID();
assert.notEqual(uuid1, uuid2, 'matches');
});
});
describe('cleanUpTag', () => {
it('should cleanUp a tag', (done) => {
const cleanedTag = utils.cleanUpTag(',/#!$^*;TaG1:{}=_`<>\'"~()?|');
assert.equal(cleanedTag, 'tag1');
done();
});
it('should return empty string for invalid tags', (done) => {
assert.strictEqual(utils.cleanUpTag(undefined), '');
assert.strictEqual(utils.cleanUpTag(null), '');
assert.strictEqual(utils.cleanUpTag(false), '');
assert.strictEqual(utils.cleanUpTag(1), '');
assert.strictEqual(utils.cleanUpTag(0), '');
done();
});
});
8 years ago
it('should remove punctuation', (done) => {
const removed = utils.removePunctuation('some text with , ! punctuation inside "');
8 years ago
assert.equal(removed, 'some text with punctuation inside ');
done();
});
it('should get language key', () => {
assert.strictEqual(utils.getLanguage(), 'en-GB');
global.window.utils = {};
global.window.config = { userLang: 'tr' };
assert.strictEqual(utils.getLanguage(), 'tr');
global.window.config = { defaultLang: 'de' };
assert.strictEqual(utils.getLanguage(), 'de');
});
it('should return true if string has language key', (done) => {
8 years ago
assert.equal(utils.hasLanguageKey('some text [[topic:title]] and [[user:reputaiton]]'), true);
done();
});
it('should return false if string does not have language key', (done) => {
8 years ago
assert.equal(utils.hasLanguageKey('some text with no language keys'), false);
done();
});
it('should return bootstrap env', () => {
assert.strictEqual(utils.findBootstrapEnvironment(), 'xs');
});
it('should check if mobile', () => {
assert.strictEqual(utils.isMobile(), true);
});
it('should check password validity', () => {
global.ajaxify = {
data: {
minimumPasswordStrength: 1,
minimumPasswordLength: 6,
},
};
const zxcvbn = require('zxcvbn');
function check(pwd, expectedError) {
try {
utils.assertPasswordValidity(pwd, zxcvbn);
assert(false);
} catch (err) {
assert.strictEqual(err.message, expectedError);
}
}
check('123456', '[[user:weak_password]]');
check('', '[[user:change_password_error]]');
check('asd', '[[reset_password:password_too_short]]');
check(new Array(513).fill('a').join(''), '[[error:password-too-long]]');
utils.assertPasswordValidity('Yzsh31j!a', zxcvbn);
});
// it('should generate UUID', () => {
// TODO: add back when nodejs 18 is minimum
// assert(validator.isUUID(utils.generateUUID()));
// });
it('should shallow merge two objects', (done) => {
const a = { foo: 1, cat1: 'ginger' };
const b = { baz: 2, cat2: 'phoebe' };
const obj = utils.merge(a, b);
8 years ago
assert.strictEqual(obj.foo, 1);
assert.strictEqual(obj.baz, 2);
assert.strictEqual(obj.cat1, 'ginger');
assert.strictEqual(obj.cat2, 'phoebe');
done();
});
it('should return the file extesion', (done) => {
8 years ago
assert.equal(utils.fileExtension('/path/to/some/file.png'), 'png');
done();
});
it('should return file mime type', (done) => {
8 years ago
assert.equal(utils.fileMimeType('/path/to/some/file.png'), 'image/png');
done();
});
it('should check if url is relative', (done) => {
8 years ago
assert.equal(utils.isRelativeUrl('/topic/1/slug'), true);
done();
});
it('should check if url is relative', (done) => {
8 years ago
assert.equal(utils.isRelativeUrl('https://nodebb.org'), false);
done();
});
it('should make number human readable', (done) => {
8 years ago
assert.equal(utils.makeNumberHumanReadable('1000'), '1.0k');
done();
});
it('should make number human readable', (done) => {
8 years ago
assert.equal(utils.makeNumberHumanReadable('1100000'), '1.1m');
done();
});
it('should make number human readable', (done) => {
8 years ago
assert.equal(utils.makeNumberHumanReadable('100'), '100');
done();
});
it('should make number human readable', (done) => {
8 years ago
assert.equal(utils.makeNumberHumanReadable(null), null);
done();
});
it('should make numbers human readable on elements', (done) => {
const el = $('<div title="100000"></div>');
8 years ago
utils.makeNumbersHumanReadable(el);
assert.equal(el.html(), '100.0k');
done();
});
it('should add commas to numbers', (done) => {
8 years ago
assert.equal(utils.addCommas('100'), '100');
done();
});
it('should add commas to numbers', (done) => {
8 years ago
assert.equal(utils.addCommas('1000'), '1,000');
done();
});
it('should add commas to numbers', (done) => {
8 years ago
assert.equal(utils.addCommas('1000000'), '1,000,000');
done();
});
it('should add commas to elements', (done) => {
const el = $('<div>1000000</div>');
8 years ago
utils.addCommasToNumbers(el);
assert.equal(el.html(), '1,000,000');
done();
});
it('should return passed in value if invalid', (done) => {
// eslint-disable-next-line no-loss-of-precision
const bigInt = -111111111111111111;
const result = utils.toISOString(bigInt);
8 years ago
assert.equal(bigInt, result);
done();
});
it('should return false if browser is not android', (done) => {
8 years ago
global.navigator = {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36',
};
assert.equal(utils.isAndroidBrowser(), false);
done();
});
it('should return true if browser is android', (done) => {
8 years ago
global.navigator = {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Android /58.0.3029.96 Safari/537.36',
};
assert.equal(utils.isAndroidBrowser(), true);
done();
});
it('should return false if not touch device', (done) => {
8 years ago
assert(!utils.isTouchDevice());
done();
});
it('should check if element is in viewport', (done) => {
const el = $('<div>some text</div>');
8 years ago
assert(utils.isElementInViewport(el));
done();
});
it('should get empty object for url params', (done) => {
const params = utils.params();
8 years ago
assert.equal(Object.keys(params), 0);
done();
});
it('should get url params', (done) => {
const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp=2' });
Webpack5 (#10311) * feat: webpack 5 part 1 * fix: gruntfile fixes * fix: fix taskbar warning add app.importScript copy public/src/modules to build folder * refactor: remove commented old code * feat: reenable admin * fix: acp settings pages, fix sortable on manage categories embedded require in html not allowed * fix: bundle serialize/deserizeli so plugins dont break * test: fixe util tests * test: fix require path * test: more test fixes * test: require correct utils module * test: require correct utils * test: log stack * test: fix db require blowing up tests * test: move and disable bundle test * refactor: add aliases * test: disable testing route * fix: move webpack modules necessary for build, into `dependencies` * test: fix one more test remove 500-embed.tpl * fix: restore use of assets/nodebb.min.js, at least for now * fix: remove unnecessary line break * fix: point to proper ACP bundle * test: maybe fix build test * test: composer * refactor: dont need dist * refactor: more cleanup use everything from build/public folder * get rid of conditional import in app.js * fix: ace * refactor: cropper alias * test: lint and test fixes * lint: fix * refactor: rename function to app.require * refactor: go back to using app.require * chore: use github branch * chore: use webpack branch * feat: webpack webinstaller * feat: add chunkFile name with contenthash * refactor: move hooks to top * refactor: get rid of template500Function * fix(deps): use webpack5 branch of 2factor plugin * chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket: * refactor: disable cache on templates loadTemplate is called once by benchpress and the result is cache internally * refactor: add server side helpers.js * feat: deprecate /plugins shorthand route, closes #10343 * refactor: use build/public for webpack * test: fix filename * fix: more specific selector * lint: ignore * refactor: fix comments * test: add debug for random failing test * refactor: cleanup remove test page, remove dupe functions in utils.common * lint: use relative path for now * chore: bump prerelease version * feat: add translateKeys * fix: optional params * fix: get rid of extra timeago files * refactor: cleanup, require timeago locale earlier remove translator.prepareDOM, it is in header.tpl html tag * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * feat: allow app.require('bootbox'/'benchpressjs') * refactor: require server side utils * test: jquery ready * change istaller to use build/public * test: use document.addEventListener * refactor: closes #10301 * refactor: generateTopicClass * fix: column counts for other privileges * fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking] * fix: typo in hook name * refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags) * fix: crash if `delay` not passed in (as it cannot be destructured) * refactor: replace substr * feat: set --panel-offset style in html element based on stored value in localStorage * refactor: addDropupHandler() logic to be less naive - Take into account height of the menu - Don't apply dropUp logic if there's nothing in the dropdown - Remove 'hidden' class (added by default in Persona for post tools) when menu items are added closes #10423 * refactor: simplify utils.params [breaking] Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do. * feat: add support for returning full URLSearchParams for utils.params * fix: utils.params() fallback handling * fix: default empty obj for params() * fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end * fix: utils.params() not allowing relative paths to be passed in * refactor(DRY): new assertPasswordValidity utils method * fix: incorrect error message returned on insufficient privilege on flag edit * fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate - added failing tests and patched up middleware.assert.flags to fix * refactor: flag api v3 tests to create new post and flags on every round * fix: missing error:no-flag language key * refactor: flags.canView to check flag existence, simplify middleware.assert.flag * feat: flag deletion API endpoint, #10426 * feat: UI for flag deletion, closes #10426 * chore: update plugin versions * chore: up emoji * chore: update markdown * chore: up emoji-android * fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check Co-authored-by: Julian Lam <[email protected]>
3 years ago
assert.strictEqual(params.foo, 1);
assert.strictEqual(params.bar, 'test');
assert.strictEqual(params.herp, 2);
8 years ago
done();
});
it('should get url params as arrays', (done) => {
const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp[]=2&herp[]=3' });
Webpack5 (#10311) * feat: webpack 5 part 1 * fix: gruntfile fixes * fix: fix taskbar warning add app.importScript copy public/src/modules to build folder * refactor: remove commented old code * feat: reenable admin * fix: acp settings pages, fix sortable on manage categories embedded require in html not allowed * fix: bundle serialize/deserizeli so plugins dont break * test: fixe util tests * test: fix require path * test: more test fixes * test: require correct utils module * test: require correct utils * test: log stack * test: fix db require blowing up tests * test: move and disable bundle test * refactor: add aliases * test: disable testing route * fix: move webpack modules necessary for build, into `dependencies` * test: fix one more test remove 500-embed.tpl * fix: restore use of assets/nodebb.min.js, at least for now * fix: remove unnecessary line break * fix: point to proper ACP bundle * test: maybe fix build test * test: composer * refactor: dont need dist * refactor: more cleanup use everything from build/public folder * get rid of conditional import in app.js * fix: ace * refactor: cropper alias * test: lint and test fixes * lint: fix * refactor: rename function to app.require * refactor: go back to using app.require * chore: use github branch * chore: use webpack branch * feat: webpack webinstaller * feat: add chunkFile name with contenthash * refactor: move hooks to top * refactor: get rid of template500Function * fix(deps): use webpack5 branch of 2factor plugin * chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket: * refactor: disable cache on templates loadTemplate is called once by benchpress and the result is cache internally * refactor: add server side helpers.js * feat: deprecate /plugins shorthand route, closes #10343 * refactor: use build/public for webpack * test: fix filename * fix: more specific selector * lint: ignore * refactor: fix comments * test: add debug for random failing test * refactor: cleanup remove test page, remove dupe functions in utils.common * lint: use relative path for now * chore: bump prerelease version * feat: add translateKeys * fix: optional params * fix: get rid of extra timeago files * refactor: cleanup, require timeago locale earlier remove translator.prepareDOM, it is in header.tpl html tag * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * feat: allow app.require('bootbox'/'benchpressjs') * refactor: require server side utils * test: jquery ready * change istaller to use build/public * test: use document.addEventListener * refactor: closes #10301 * refactor: generateTopicClass * fix: column counts for other privileges * fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking] * fix: typo in hook name * refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags) * fix: crash if `delay` not passed in (as it cannot be destructured) * refactor: replace substr * feat: set --panel-offset style in html element based on stored value in localStorage * refactor: addDropupHandler() logic to be less naive - Take into account height of the menu - Don't apply dropUp logic if there's nothing in the dropdown - Remove 'hidden' class (added by default in Persona for post tools) when menu items are added closes #10423 * refactor: simplify utils.params [breaking] Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do. * feat: add support for returning full URLSearchParams for utils.params * fix: utils.params() fallback handling * fix: default empty obj for params() * fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end * fix: utils.params() not allowing relative paths to be passed in * refactor(DRY): new assertPasswordValidity utils method * fix: incorrect error message returned on insufficient privilege on flag edit * fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate - added failing tests and patched up middleware.assert.flags to fix * refactor: flag api v3 tests to create new post and flags on every round * fix: missing error:no-flag language key * refactor: flags.canView to check flag existence, simplify middleware.assert.flag * feat: flag deletion API endpoint, #10426 * feat: UI for flag deletion, closes #10426 * chore: update plugin versions * chore: up emoji * chore: update markdown * chore: up emoji-android * fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check Co-authored-by: Julian Lam <[email protected]>
3 years ago
assert.strictEqual(params.foo, 1);
assert.strictEqual(params.bar, 'test');
assert.deepStrictEqual(params.herp, [2, 3]);
done();
});
it('should get a single param', (done) => {
8 years ago
assert.equal(utils.param('somekey'), undefined);
done();
});
Webpack5 (#10311) * feat: webpack 5 part 1 * fix: gruntfile fixes * fix: fix taskbar warning add app.importScript copy public/src/modules to build folder * refactor: remove commented old code * feat: reenable admin * fix: acp settings pages, fix sortable on manage categories embedded require in html not allowed * fix: bundle serialize/deserizeli so plugins dont break * test: fixe util tests * test: fix require path * test: more test fixes * test: require correct utils module * test: require correct utils * test: log stack * test: fix db require blowing up tests * test: move and disable bundle test * refactor: add aliases * test: disable testing route * fix: move webpack modules necessary for build, into `dependencies` * test: fix one more test remove 500-embed.tpl * fix: restore use of assets/nodebb.min.js, at least for now * fix: remove unnecessary line break * fix: point to proper ACP bundle * test: maybe fix build test * test: composer * refactor: dont need dist * refactor: more cleanup use everything from build/public folder * get rid of conditional import in app.js * fix: ace * refactor: cropper alias * test: lint and test fixes * lint: fix * refactor: rename function to app.require * refactor: go back to using app.require * chore: use github branch * chore: use webpack branch * feat: webpack webinstaller * feat: add chunkFile name with contenthash * refactor: move hooks to top * refactor: get rid of template500Function * fix(deps): use webpack5 branch of 2factor plugin * chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket: * refactor: disable cache on templates loadTemplate is called once by benchpress and the result is cache internally * refactor: add server side helpers.js * feat: deprecate /plugins shorthand route, closes #10343 * refactor: use build/public for webpack * test: fix filename * fix: more specific selector * lint: ignore * refactor: fix comments * test: add debug for random failing test * refactor: cleanup remove test page, remove dupe functions in utils.common * lint: use relative path for now * chore: bump prerelease version * feat: add translateKeys * fix: optional params * fix: get rid of extra timeago files * refactor: cleanup, require timeago locale earlier remove translator.prepareDOM, it is in header.tpl html tag * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * feat: allow app.require('bootbox'/'benchpressjs') * refactor: require server side utils * test: jquery ready * change istaller to use build/public * test: use document.addEventListener * refactor: closes #10301 * refactor: generateTopicClass * fix: column counts for other privileges * fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking] * fix: typo in hook name * refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags) * fix: crash if `delay` not passed in (as it cannot be destructured) * refactor: replace substr * feat: set --panel-offset style in html element based on stored value in localStorage * refactor: addDropupHandler() logic to be less naive - Take into account height of the menu - Don't apply dropUp logic if there's nothing in the dropdown - Remove 'hidden' class (added by default in Persona for post tools) when menu items are added closes #10423 * refactor: simplify utils.params [breaking] Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do. * feat: add support for returning full URLSearchParams for utils.params * fix: utils.params() fallback handling * fix: default empty obj for params() * fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end * fix: utils.params() not allowing relative paths to be passed in * refactor(DRY): new assertPasswordValidity utils method * fix: incorrect error message returned on insufficient privilege on flag edit * fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate - added failing tests and patched up middleware.assert.flags to fix * refactor: flag api v3 tests to create new post and flags on every round * fix: missing error:no-flag language key * refactor: flags.canView to check flag existence, simplify middleware.assert.flag * feat: flag deletion API endpoint, #10426 * feat: UI for flag deletion, closes #10426 * chore: update plugin versions * chore: up emoji * chore: update markdown * chore: up emoji-android * fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check Co-authored-by: Julian Lam <[email protected]>
3 years ago
it('should get the full URLSearchParams object', async () => {
const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp[]=2&herp[]=3', full: true });
assert(params instanceof URLSearchParams);
assert.strictEqual(params.get('foo'), '1');
assert.strictEqual(params.get('bar'), 'test');
assert.strictEqual(params.get('herp[]'), '2');
});
8 years ago
describe('toType', () => {
it('should return param as is if not string', (done) => {
8 years ago
assert.equal(123, utils.toType(123));
done();
});
it('should convert return string numbers as numbers', (done) => {
8 years ago
assert.equal(123, utils.toType('123'));
done();
});
it('should convert string "false" to boolean false', (done) => {
8 years ago
assert.strictEqual(false, utils.toType('false'));
done();
});
it('should convert string "true" to boolean true', (done) => {
8 years ago
assert.strictEqual(true, utils.toType('true'));
done();
});
it('should parse json', (done) => {
const data = utils.toType('{"a":"1"}');
8 years ago
assert.equal(data.a, '1');
done();
});
it('should return string as is if its not json,true,false or number', (done) => {
const regularStr = 'this is a regular string';
8 years ago
assert.equal(regularStr, utils.toType(regularStr));
done();
});
});
describe('utils.props', () => {
const data = {};
8 years ago
it('should set nested data', (done) => {
8 years ago
assert.equal(10, utils.props(data, 'a.b.c.d', 10));
done();
});
it('should return nested object', (done) => {
const obj = utils.props(data, 'a.b.c');
8 years ago
assert.equal(obj.d, 10);
done();
});
it('should returned undefined without throwing', (done) => {
8 years ago
assert.equal(utils.props(data, 'a.b.c.foo.bar'), undefined);
done();
});
it('should return undefined if second param is null', (done) => {
8 years ago
assert.equal(utils.props(undefined, null), undefined);
done();
});
});
describe('isInternalURI', () => {
const target = { host: '', protocol: 'https' };
const reference = { host: '', protocol: 'https' };
8 years ago
it('should return true if they match', (done) => {
8 years ago
assert(utils.isInternalURI(target, reference, ''));
done();
});
it('should return true if they match', (done) => {
8 years ago
target.host = 'nodebb.org';
reference.host = 'nodebb.org';
assert(utils.isInternalURI(target, reference, ''));
done();
});
it('should handle relative path', (done) => {
8 years ago
target.pathname = '/forum';
assert(utils.isInternalURI(target, reference, '/forum'));
done();
});
it('should return false if they do not match', (done) => {
8 years ago
target.pathname = '';
reference.host = 'designcreateplay.com';
assert(!utils.isInternalURI(target, reference));
done();
});
});
it('escape html', (done) => {
const escaped = utils.escapeHTML('&<>');
8 years ago
assert.equal(escaped, '&amp;&lt;&gt;');
done();
});
it('should escape regex chars', (done) => {
const escaped = utils.escapeRegexChars('some text {}');
8 years ago
assert.equal(escaped, 'some\\ text\\ \\{\\}');
done();
});
it('should get hours array', (done) => {
const currentHour = new Date().getHours();
const hours = utils.getHoursArray();
let index = hours.length - 1;
for (let i = currentHour, ii = currentHour - 24; i > ii; i -= 1) {
const hour = i < 0 ? 24 + i : i;
assert.equal(hours[index], `${hour}:00`);
index -= 1;
8 years ago
}
done();
});
it('should get days array', (done) => {
const currentDay = new Date(Date.now()).getTime();
const days = utils.getDaysArray();
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
let index = 0;
for (let x = 29; x >= 0; x -= 1) {
const tmpDate = new Date(currentDay - (1000 * 60 * 60 * 24 * x));
assert.equal(`${months[tmpDate.getMonth()]} ${tmpDate.getDate()}`, days[index]);
index += 1;
8 years ago
}
done();
});
it('`utils.rtrim` should remove trailing space', (done) => {
assert.strictEqual(utils.rtrim(' thing '), ' thing');
assert.strictEqual(utils.rtrim('\tthing\t\t'), '\tthing');
assert.strictEqual(utils.rtrim('\t thing \t'), '\t thing');
done();
});
8 years ago
it('should profile function', (done) => {
const st = process.hrtime();
setTimeout(() => {
8 years ago
process.profile('it took', st);
done();
}, 500);
});
it('should return object with data', async () => {
const user = require('../src/user');
const uid1 = await user.create({ username: 'promise1' });
const uid2 = await user.create({ username: 'promise2' });
const result = await utils.promiseParallel({
user1: user.getUserData(uid1),
user2: user.getUserData(uid2),
});
assert(result.hasOwnProperty('user1') && result.hasOwnProperty('user2'));
assert.strictEqual(result.user1.uid, uid1);
assert.strictEqual(result.user2.uid, uid2);
});
});