| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const assert = require('assert'); | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | const { JSDOM } = require('jsdom'); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const utils = require('../public/src/utils.js'); | 
					
						
							|  |  |  | const slugify = require('../src/slugify'); | 
					
						
							| 
									
										
										
										
											2019-07-09 22:00:46 -04:00
										 |  |  | const db = require('./mocks/databasemock'); | 
					
						
							| 
									
										
										
										
											2013-10-19 13:11:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | describe('Utility Methods', () => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 	// https://gist.github.com/robballou/9ee108758dc5e0e2d028
 | 
					
						
							|  |  |  | 	// create some jsdom magic to allow jQuery to work
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	const dom = new JSDOM('<html><body></body></html>'); | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 	const { window } = dom; | 
					
						
							| 
									
										
										
										
											2020-10-12 00:04:20 -04:00
										 |  |  | 	global.window = window; | 
					
						
							|  |  |  | 	global.jQuery = require('jquery'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 	global.$ = global.jQuery; | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 	const { $ } = global; | 
					
						
							| 
									
										
										
										
											2020-10-12 00:18:34 -04:00
										 |  |  | 	require('jquery-deserialize'); | 
					
						
							|  |  |  | 	require('jquery-serializeobject'); | 
					
						
							| 
									
										
										
										
											2020-10-12 00:04:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should serialize/deserialize form data properly', () => { | 
					
						
							| 
									
										
										
										
											2020-10-12 00:04:20 -04:00
										 |  |  | 		const formSerialize = $(`
 | 
					
						
							|  |  |  | 			<form id="form-serialize"> | 
					
						
							|  |  |  | 				<input name="a" value="1"> | 
					
						
							|  |  |  | 				<input name="a" value="2"> | 
					
						
							|  |  |  | 				<input name="bar" value="test"> | 
					
						
							| 
									
										
										
										
											2020-10-12 00:18:34 -04:00
										 |  |  | 				<input name="check1" type="checkbox" checked> | 
					
						
							|  |  |  | 				<input name="check2" type="checkbox"> | 
					
						
							| 
									
										
										
										
											2020-10-12 00:04:20 -04:00
										 |  |  | 			</form> | 
					
						
							|  |  |  | 		`);
 | 
					
						
							|  |  |  | 		const sampleData = { | 
					
						
							|  |  |  | 			a: ['1', '2'], | 
					
						
							|  |  |  | 			bar: 'test', | 
					
						
							| 
									
										
										
										
											2020-10-12 00:18:34 -04:00
										 |  |  | 			check1: 'on', | 
					
						
							| 
									
										
										
										
											2020-10-12 00:04:20 -04:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 		const data = formSerialize.serializeObject(); | 
					
						
							|  |  |  | 		assert.deepStrictEqual(data, sampleData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const formDeserialize = $(`
 | 
					
						
							|  |  |  | 			<form> | 
					
						
							|  |  |  | 				<input id="input1" name="a"/> | 
					
						
							|  |  |  | 				<input id="input2" name="a"/> | 
					
						
							|  |  |  | 				<input id="input3" name="bar"/> | 
					
						
							| 
									
										
										
										
											2020-10-12 00:18:34 -04:00
										 |  |  | 				<input id="input4" name="check1" type="checkbox"> | 
					
						
							|  |  |  | 				<input id="input5" name="check2" type="checkbox"> | 
					
						
							| 
									
										
										
										
											2020-10-12 00:04:20 -04:00
										 |  |  | 			</form> | 
					
						
							|  |  |  | 		`);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		formDeserialize.deserialize(sampleData); | 
					
						
							|  |  |  | 		assert.strictEqual(formDeserialize.find('#input1').val(), sampleData.a[0]); | 
					
						
							|  |  |  | 		assert.strictEqual(formDeserialize.find('#input2').val(), sampleData.a[1]); | 
					
						
							|  |  |  | 		assert.strictEqual(formDeserialize.find('#input3').val(), sampleData.bar); | 
					
						
							| 
									
										
										
										
											2020-10-12 00:18:34 -04:00
										 |  |  | 		assert.strictEqual(formDeserialize.find('#input4').prop('checked'), true); | 
					
						
							|  |  |  | 		assert.strictEqual(formDeserialize.find('#input5').prop('checked'), false); | 
					
						
							| 
									
										
										
										
											2020-10-12 00:04:20 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 	// https://github.com/jprichardson/string.js/blob/master/test/string.test.js
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should decode HTML entities', (done) => { | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 		assert.strictEqual( | 
					
						
							|  |  |  | 			utils.decodeHTMLEntities('Ken Thompson & Dennis Ritchie'), | 
					
						
							|  |  |  | 			'Ken Thompson & Dennis Ritchie' | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		assert.strictEqual( | 
					
						
							|  |  |  | 			utils.decodeHTMLEntities('3 < 4'), | 
					
						
							|  |  |  | 			'3 < 4' | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		assert.strictEqual( | 
					
						
							|  |  |  | 			utils.decodeHTMLEntities('http://'), | 
					
						
							|  |  |  | 			'http://' | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should strip HTML tags', (done) => { | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 		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'); | 
					
						
							| 
									
										
										
										
											2017-11-01 12:05:06 -04:00
										 |  |  | 		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'); | 
					
						
							| 
									
										
										
										
											2017-10-13 21:02:41 -06:00
										 |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should preserve case if requested', (done) => { | 
					
						
							| 
									
										
										
										
											2020-10-11 21:49:37 -04:00
										 |  |  | 		assert.strictEqual(slugify('UPPER CASE', true), 'UPPER-CASE'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should work if a number is passed in', (done) => { | 
					
						
							| 
									
										
										
										
											2020-10-11 21:49:37 -04:00
										 |  |  | 		assert.strictEqual(slugify(12345), '12345'); | 
					
						
							| 
									
										
										
										
											2019-10-17 18:55:56 -04:00
										 |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('username validation', () => { | 
					
						
							|  |  |  | 		it('accepts latin-1 characters', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const username = "John\"'-. Doeäâèéë1234"; | 
					
						
							| 
									
										
										
										
											2013-10-19 13:11:28 -04:00
										 |  |  | 			assert(utils.isUserNameValid(username), 'invalid username'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-11-07 16:01:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('rejects empty string', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const username = ''; | 
					
						
							| 
									
										
										
										
											2018-05-25 19:05:18 -04:00
										 |  |  | 			assert.equal(utils.isUserNameValid(username), false, 'accepted as valid username'); | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-11-07 16:01:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should reject new lines', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert.equal(utils.isUserNameValid('myusername\r\n'), false); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should reject new lines', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert.equal(utils.isUserNameValid('myusername\n'), false); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should reject tabs', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert.equal(utils.isUserNameValid('myusername\t'), false); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('accepts square brackets', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const username = '[best clan] julian'; | 
					
						
							| 
									
										
										
										
											2018-11-07 16:01:22 -05:00
										 |  |  | 			assert(utils.isUserNameValid(username), 'invalid username'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('accepts regular username', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert(utils.isUserNameValid('myusername'), 'invalid username'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('accepts quotes', () => { | 
					
						
							| 
									
										
										
										
											2020-08-26 22:04:18 -04:00
										 |  |  | 			assert(utils.isUserNameValid('baris "the best" usakli'), 'invalid username'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('email validation', () => { | 
					
						
							|  |  |  | 		it('accepts sample address', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const email = 'sample@example.com'; | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 			assert(utils.isEmailValid(email), 'invalid email'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('rejects empty address', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const email = ''; | 
					
						
							| 
									
										
										
										
											2018-05-25 19:05:18 -04:00
										 |  |  | 			assert.equal(utils.isEmailValid(email), false, 'accepted as valid email'); | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('UUID generation', () => { | 
					
						
							|  |  |  | 		it('return unique random value every time', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const uuid1 = utils.generateUUID(); | 
					
						
							|  |  |  | 			const uuid2 = utils.generateUUID(); | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 			assert.notEqual(uuid1, uuid2, 'matches'); | 
					
						
							| 
									
										
										
										
											2013-10-28 02:09:57 +02:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-10-19 13:11:28 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-14 13:12:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('cleanUpTag', () => { | 
					
						
							|  |  |  | 		it('should cleanUp a tag', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const cleanedTag = utils.cleanUpTag(',/#!$%^*;TaG1:{}=_`<>\'"~()?|'); | 
					
						
							| 
									
										
										
										
											2016-10-14 13:12:08 +03:00
										 |  |  | 			assert.equal(cleanedTag, 'tag1'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return empty string for invalid tags', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-14 13:12:08 +03:00
										 |  |  | 			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(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should remove punctuation', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const removed = utils.removePunctuation('some text with , ! punctuation inside "'); | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(removed, 'some text with   punctuation inside '); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return true if string has language key', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.hasLanguageKey('some text [[topic:title]] and [[user:reputaiton]]'), true); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return false if string does not have language key', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.hasLanguageKey('some text with no language keys'), false); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should shallow merge two objects', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const a = { foo: 1, cat1: 'ginger' }; | 
					
						
							|  |  |  | 		const b = { baz: 2, cat2: 'phoebe' }; | 
					
						
							|  |  |  | 		const obj = utils.merge(a, b); | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.strictEqual(obj.foo, 1); | 
					
						
							|  |  |  | 		assert.strictEqual(obj.baz, 2); | 
					
						
							|  |  |  | 		assert.strictEqual(obj.cat1, 'ginger'); | 
					
						
							|  |  |  | 		assert.strictEqual(obj.cat2, 'phoebe'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return the file extesion', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.fileExtension('/path/to/some/file.png'), 'png'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return file mime type', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.fileMimeType('/path/to/some/file.png'), 'image/png'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should check if url is relative', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.isRelativeUrl('/topic/1/slug'), true); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should check if url is relative', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.isRelativeUrl('https://nodebb.org'), false); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make number human readable', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.makeNumberHumanReadable('1000'), '1.0k'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make number human readable', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.makeNumberHumanReadable('1100000'), '1.1m'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make number human readable', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.makeNumberHumanReadable('100'), '100'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make number human readable', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.makeNumberHumanReadable(null), null); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should make numbers human readable on elements', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const el = $('<div title="100000"></div>'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		utils.makeNumbersHumanReadable(el); | 
					
						
							|  |  |  | 		assert.equal(el.html(), '100.0k'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should add commas to numbers', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.addCommas('100'), '100'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should add commas to numbers', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.addCommas('1000'), '1,000'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should add commas to numbers', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-26 17:47:36 +03:00
										 |  |  | 		assert.equal(utils.addCommas('1000000'), '1,000,000'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should add commas to elements', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const el = $('<div>1000000</div>'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		utils.addCommasToNumbers(el); | 
					
						
							|  |  |  | 		assert.equal(el.html(), '1,000,000'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return passed in value if invalid', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const bigInt = -111111111111111111; | 
					
						
							|  |  |  | 		const result = utils.toISOString(bigInt); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		assert.equal(bigInt, result); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return false if browser is not android', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		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(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return true if browser is android', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		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(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return false if not touch device', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		global.document = global.document || {}; | 
					
						
							|  |  |  | 		global.document.documentElement = {}; | 
					
						
							|  |  |  | 		assert(!utils.isTouchDevice()); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return true if touch device', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		global.document.documentElement = { | 
					
						
							|  |  |  | 			ontouchstart: 1, | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		assert(utils.isTouchDevice()); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should check if element is in viewport', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const el = $('<div>some text</div>'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		assert(utils.isElementInViewport(el)); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get empty object for url params', (done) => { | 
					
						
							| 
									
										
										
										
											2019-09-30 17:15:50 -04:00
										 |  |  | 		global.document = window.document; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const params = utils.params(); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		assert.equal(Object.keys(params), 0); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get url params', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp=2' }); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		assert.equal(params.foo, 1); | 
					
						
							|  |  |  | 		assert.equal(params.bar, 'test'); | 
					
						
							|  |  |  | 		assert.equal(params.herp, 2); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get a single param', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 		assert.equal(utils.param('somekey'), undefined); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('toType', () => { | 
					
						
							|  |  |  | 		it('should return param as is if not string', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(123, utils.toType(123)); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should convert return string numbers as numbers', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(123, utils.toType('123')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should convert string "false" to boolean false', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.strictEqual(false, utils.toType('false')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should convert string "true" to boolean true', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.strictEqual(true, utils.toType('true')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should parse json', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const data = utils.toType('{"a":"1"}'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(data.a, '1'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return string as is if its not json,true,false or number', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const regularStr = 'this is a regular string'; | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(regularStr, utils.toType(regularStr)); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('utils.props', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const data = {}; | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should set nested data', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(10, utils.props(data, 'a.b.c.d', 10)); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return nested object', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const obj = utils.props(data, 'a.b.c'); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(obj.d, 10); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should returned undefined without throwing', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(utils.props(data, 'a.b.c.foo.bar'), undefined); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return undefined if second param is null', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert.equal(utils.props(undefined, null), undefined); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('isInternalURI', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const target = { host: '', protocol: 'https' }; | 
					
						
							|  |  |  | 		const reference = { host: '', protocol: 'https' }; | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return true if they match', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			assert(utils.isInternalURI(target, reference, '')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return true if they match', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			target.host = 'nodebb.org'; | 
					
						
							|  |  |  | 			reference.host = 'nodebb.org'; | 
					
						
							|  |  |  | 			assert(utils.isInternalURI(target, reference, '')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should handle relative path', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			target.pathname = '/forum'; | 
					
						
							|  |  |  | 			assert(utils.isInternalURI(target, reference, '/forum')); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return false if they do not match', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			target.pathname = ''; | 
					
						
							|  |  |  | 			reference.host = 'designcreateplay.com'; | 
					
						
							|  |  |  | 			assert(!utils.isInternalURI(target, reference)); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('escape html', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const escaped = utils.escapeHTML('&<>'); | 
					
						
							| 
									
										
										
										
											2017-02-14 15:47:36 +03:00
										 |  |  | 		assert.equal(escaped, '&<>'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should escape regex chars', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const escaped = utils.escapeRegexChars('some text {}'); | 
					
						
							| 
									
										
										
										
											2017-02-14 15:47:36 +03:00
										 |  |  | 		assert.equal(escaped, 'some\\ text\\ \\{\\}'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get hours array', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		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; | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.equal(hours[index], `${hour}:00`); | 
					
						
							| 
									
										
										
										
											2017-02-18 01:12:18 -07:00
										 |  |  | 			index -= 1; | 
					
						
							| 
									
										
										
										
											2017-02-14 15:47:36 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get days array', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		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)); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.equal(`${months[tmpDate.getMonth()]} ${tmpDate.getDate()}`, days[index]); | 
					
						
							| 
									
										
										
										
											2017-02-18 01:12:18 -07:00
										 |  |  | 			index += 1; | 
					
						
							| 
									
										
										
										
											2017-02-14 15:47:36 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-05-09 14:35:11 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('`utils.rtrim` should remove trailing space', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-09 14:35:11 -06:00
										 |  |  | 		assert.strictEqual(utils.rtrim('  thing   '), '  thing'); | 
					
						
							|  |  |  | 		assert.strictEqual(utils.rtrim('\tthing\t\t'), '\tthing'); | 
					
						
							|  |  |  | 		assert.strictEqual(utils.rtrim('\t thing \t'), '\t thing'); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should profile function', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const st = process.hrtime(); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		setTimeout(() => { | 
					
						
							| 
									
										
										
										
											2017-05-11 16:31:10 -04:00
										 |  |  | 			process.profile('it took', st); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}, 500); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2019-07-09 22:00:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return object with data', async () => { | 
					
						
							| 
									
										
										
										
											2019-07-09 22:00:46 -04:00
										 |  |  | 		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); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-10-19 13:11:28 -04:00
										 |  |  | }); |