add more unit tests to helpers/posting

indiachan-spamvector
Thomas Lynch 2 years ago
parent a28afef5a9
commit c6f8c0939e
  1. 2
      helpers/files/formatsize.test.js
  2. 33
      helpers/posting/diceroll.test.js
  3. 19
      helpers/posting/escape.test.js
  4. 12
      helpers/posting/fortune.test.js
  5. 42
      helpers/posting/linkmatch.test.js
  6. 31
      helpers/posting/name.test.js
  7. 73
      test/pages.test.js
  8. 93
      test/posting.test.js

@ -1,6 +1,6 @@
const formatSize = require('./formatsize.js');
describe('helpers/files/formatsize.js', () => {
describe('formatSize() - convert bytes to human readable file size', () => {
const cases = [
{in: 1024, out: "1KB"},
{in: Math.pow(1024, 2), out: "1MB"},

@ -0,0 +1,33 @@
const diceroll = require('./diceroll.js');
describe('diceroll markdown', () => {
const prepareCases = [
{ in: '##3d6', out: '##3d6=' },
{ in: '##99d99', out: '##99d99=' },
{ in: '##999d999', out: '##999d999' },
{ in: '##3d8+5', out: '##3d8+5=' },
{ in: '##3d8-5', out: '##3d8-5=' },
{ in: '##0d0', out: '##0d0' },
];
for(let i in prepareCases) {
test(`should contain ${prepareCases[i].out} for an input of ${prepareCases[i].in}`, () => {
const output = prepareCases[i].in.replace(diceroll.regexPrepare, diceroll.prepare.bind(null, false));
expect(output).toContain(prepareCases[i].out);
});
}
const markdownCases = [
{ in: '##3d6=10', out: 'Rolled 3 dice with 6 sides =' },
{ in: '##99d99=5138', out: 'Rolled 99 dice with 99 sides =' },
{ in: '##999d999=10000', out: '##999d999=10000' },
{ in: '##0d0', out: '##0d0' },
];
for(let i in markdownCases) {
test(`should contain ${markdownCases[i].out} for an input of ${markdownCases[i].in}`, () => {
const output = markdownCases[i].in.replace(diceroll.regexMarkdown, diceroll.markdown.bind(null, false));
expect(output).toContain(markdownCases[i].out);
});
}
});

@ -0,0 +1,19 @@
const escape = require('./escape.js');
describe('escape() - convert some characters to html entities', () => {
const cases = [
{ in: "'", out: ''' },
{ in: '/', out: '/' },
{ in: '`', out: '`' },
{ in: '=', out: '=' },
{ in: '&', out: '&' },
{ in: '<', out: '&lt;' },
{ in: '>', out: '&gt;' },
{ in: '"', out: '&quot;' },
];
for(let i in cases) {
test(`should output ${cases[i].out} for an input of ${cases[i].in}`, () => {
expect(escape(cases[i].in)).toBe(cases[i].out);
});
}
});

@ -0,0 +1,12 @@
const fortune = require('./fortune.js');
describe('fortune markdown', () => {
const cases = [
{ in: '##fortune', out: "<span class='title'>" },
];
for(let i in cases) {
test(`should contain ${cases[i].out} for an input of ${cases[i].in}`, () => {
expect(cases[i].in.replace(fortune.regex, fortune.markdown.bind(null, false))).toContain(cases[i].out)
});
}
});

@ -0,0 +1,42 @@
const linkmatch = require('./linkmatch.js');
const linkRegex = /\[(?<label>[^\[][^\]]*?)\]\((?<url>(?:&#x2F;[^\s<>\[\]{}|\\^)]+|https?\:&#x2F;&#x2F;[^\s<>\[\]{}|\\^)]+))\)|(?<urlOnly>https?\:&#x2F;&#x2F;[^\s<>\[\]{}|\\^]+)/g;
const Permission = require('../permission.js')
const Permissions = require('../permissions.js')
const ROOT = new Permission();
ROOT.setAll(Permission.allPermissions);
const NO_PERMISSION = new Permission();
describe('link markdown', () => {
const rootCases = [
{ in: 'http:&#x2F;&#x2F;something.com', out: "href=" },
{ in: 'https:&#x2F;&#x2F;something.com', out: "href=" },
{ in: '[test](http:&#x2F;&#x2F;something.com)', out: ">test</a>" },
{ in: '[test](https:&#x2F;&#x2F;something.com)', out: ">test</a>" },
{ in: 'http:&#x2F;&#x2F;', out: "http:&#x2F;&#x2F;" },
{ in: 'https:&#x2F;&#x2F;', out: "https:&#x2F;&#x2F;" },
];
for(let i in rootCases) {
test(`should contain ${rootCases[i].out} for an input of ${rootCases[i].in}`, () => {
expect(rootCases[i].in.replace(linkRegex, linkmatch.bind(null, ROOT))).toContain(rootCases[i].out)
});
}
const cases = [
{ in: 'http:&#x2F;&#x2F;something.com', out: "href=" },
{ in: 'https:&#x2F;&#x2F;something.com', out: "href=" },
{ in: '[http:&#x2F;&#x2F;something.com](test)', out: ">http:&#x2F;&#x2F;something.com</a>" },
{ in: '[https:&#x2F;&#x2F;something.com](test)', out: ">https:&#x2F;&#x2F;something.com</a>" },
{ in: 'http:&#x2F;&#x2F;', out: "http:&#x2F;&#x2F;" },
{ in: 'https:&#x2F;&#x2F;', out: "https:&#x2F;&#x2F;" },
];
for(let i in cases) {
test(`should contain ${cases[i].out} for an input of ${cases[i].in}`, () => {
expect(cases[i].in.replace(linkRegex, linkmatch.bind(null, NO_PERMISSION))).toContain(cases[i].out)
});
}
});

@ -0,0 +1,31 @@
const name = require('./name.js');
const Permission = require('../permission.js')
const Permissions = require('../permissions.js')
const ROOT = new Permission();
ROOT.setAll(Permission.allPermissions);
describe('name/trip/capcode handler', () => {
const cases = [
{ in: '## Admin', out: { name: 'Anon', tripcode: null, capcode: '## Admin' } },
{ in: '## Global Staff', out: { name: 'Anon', tripcode: null, capcode: '## Global Staff' } },
{ in: '## Board Owner', out: { name: 'Anon', tripcode: null, capcode: '## Admin' } },
{ in: '## Board Mod', out: { name: 'Anon', tripcode: null, capcode: '## Admin' } },
{ in: '##', out: { name: 'Anon', tripcode: null, capcode: '## Admin' } },
{ in: '', out: { name: 'Anon', tripcode: null, capcode: null } },
{ in: 'test', out: { name: 'test', tripcode: null, capcode: null } },
{ in: 'test#12345', out: { name: 'test', tripcode: '!CSZ6G0yP9Q', capcode: null } },
{ in: '#12345', out: { name: 'Anon', tripcode: '!CSZ6G0yP9Q', capcode: null } },
{ in: '#12345## Admin', out: { name: 'Anon', tripcode: '!CSZ6G0yP9Q', capcode: '## Admin' } },
{ in: 'test#12345## Admin', out: { name: 'test', tripcode: '!CSZ6G0yP9Q', capcode: '## Admin' } },
];
for(let i in cases) {
test(`should contain ${cases[i].out.capcode} for an input of ${cases[i].in}`, async () => {
const output = await name(cases[i].in, ROOT, {forceAnon: false, defaultName: 'Anon'}, 'a', {a:ROOT}, 'b')
expect(output).toStrictEqual(cases[i].out)
});
}
});

@ -0,0 +1,73 @@
const fetch = require('node-fetch');
describe('Test loading dynamic pages', () => {
test('/boards.html', async () => {
const response = await fetch('http://localhost/boards.html');
expect(response.ok).toBe(true);
});
test('/boards.json', async () => {
const response = await fetch('http://localhost/boards.json');
expect(response.ok).toBe(true);
expect((await response.json()).boards.length).toBeGreaterThan(0);
});
test('/boards.html with query search for existing board', async () => {
const response = await fetch('http://localhost/boards.html?search=test&sort=popularity&direction=desc');
expect(response.ok).toBe(true);
});
test('/boards.json with query search for existing board', async () => {
const response = await fetch('http://localhost/boards.json?search=test&sort=popularity&direction=desc');
expect(response.ok).toBe(true);
expect((await response.json()).boards.length).toBeGreaterThan(0);
});
test('/boards.json with query search for not existing board', async () => {
const response = await fetch('http://localhost/boards.json?search=notexistingboard');
expect(response.ok).toBe(true);
expect((await response.json()).boards.length).toBe(0);
});
test('/overboard.html', async () => {
const response = await fetch('http://localhost/overboard.html');
expect(response.ok).toBe(true);
});
test('/overboard.json', async () => {
const response = await fetch('http://localhost/overboard.json');
expect(response.ok).toBe(true);
});
test('/overboard.html with query', async () => {
const response = await fetch('http://localhost/overboard.html?add=test&rem=abc');
expect(response.ok).toBe(true);
});
test('/overboard.json with query', async () => {
const response = await fetch('http://localhost/overboard.json?add=test&rem=abc');
expect(response.ok).toBe(true);
});
test('/index.html', async () => {
const response = await fetch('http://localhost/index.html');
expect(response.ok).toBe(true);
});
test('/news.html', async () => {
const response = await fetch('http://localhost/news.html');
expect(response.ok).toBe(true);
});
test('/rules.html', async () => {
const response = await fetch('http://localhost/rules.html');
expect(response.ok).toBe(true);
});
test('/faq.html', async () => {
const response = await fetch('http://localhost/faq.html');
expect(response.ok).toBe(true);
});
});

@ -1,20 +1,89 @@
const fetch = require('node-fetch');
describe('Test posting', () => {
test('making a new thread', async () => {
const response = await fetch("http://localhost/forms/board/test/post", {
"credentials": "include",
"headers": {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"x-using-xhr": "true",
"Content-Type": "multipart/form-data; boundary=---------------------------288233515138121562724710438"
let threadId;
test('post new thread', async () => {
const params = new URLSearchParams();
params.append('message', Math.random());
const response = await fetch('http://localhost/forms/board/test/post', {
headers: {
'x-using-xhr': 'true',
},
method: 'POST',
body: params
});
expect(response.ok).toBe(true);
threadId = (await response.json()).postId;
});
let replyId;
test('post reply', async () => {
const params = new URLSearchParams();
params.append('message', Math.random());
params.append('thread', threadId);
const response = await fetch('http://localhost/forms/board/test/post', {
headers: {
'x-using-xhr': 'true',
},
method: 'POST',
body: params
});
expect(response.ok).toBe(true);
replyId = (await response.json()).postId;
});
test('post reply with quotes', async () => {
const params = new URLSearchParams();
params.append('message', `>>${threadId}
>>${replyId}`);
params.append('thread', threadId);
const response = await fetch('http://localhost/forms/board/test/post', {
headers: {
'x-using-xhr': 'true',
},
"referrer": "http://localhost/test/index.html",
"body": `-----------------------------288233515138121562724710438\r\nContent-Disposition: form-data; name=\"thread\"\r\n\r\n\r\n-----------------------------288233515138121562724710438\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n\r\n-----------------------------288233515138121562724710438\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\n\r\n-----------------------------288233515138121562724710438\r\nContent-Disposition: form-data; name=\"subject\"\r\n\r\n\r\n-----------------------------288233515138121562724710438\r\nContent-Disposition: form-data; name=\"message\"\r\n\r\n${Math.random()}\r\n-----------------------------288233515138121562724710438\r\nContent-Disposition: form-data; name=\"postpassword\"\r\n\r\nHUN6gKxJJJiQJ0gA/Ym8Y3ozqKI=\r\n-----------------------------288233515138121562724710438--\r\n`,
"method": "POST",
method: 'POST',
body: params
});
expect(response.ok).toBe(true);
});
test('post reply with all markdowns', async () => {
const params = new URLSearchParams();
params.append('message', `>greentext
<pinktext
==title==
''bold''
__underline__
~strikethrough~~
||spoiler text||
**italic**
(((detected)))
##2d9+3
https://example.com
[Board Rules](https://your.imageboard/a/custompage/rules.html)(staff only)
>>${threadId}
>>>/test/
>>>/test/${threadId}
\`inline monospace\`
[code]language
int main() {...}
[/code]
[code]aa
_
( ω) Let's try that again.
[/code]
`);
params.append('thread', threadId);
const response = await fetch('http://localhost/forms/board/test/post', {
headers: {
'x-using-xhr': 'true',
},
method: 'POST',
body: params
});
expect(response.ok).toBe(true);
});
});

Loading…
Cancel
Save