jschan - Anonymous imageboard software. Classic look, modern features and feel. Works without JavaScript and supports Tor, I2P, Lokinet, etc.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

159 lines
4.8 KiB

const fetch = require('node-fetch');
const FormData = require('form-data');
const fs = require('fs-extra');
module.exports = () => describe('test some global form submissions', () => {
let sessionCookie
, csrfToken;
test('login as admin', async () => {
const params = new URLSearchParams();
params.append('username', 'admin');
params.append('password', process.env.TEST_ADMIN_PASSWORD);
const response = await fetch('http://localhost/forms/login', {
headers: {
'x-using-xhr': 'true',
},
method: 'POST',
body: params,
redirect: 'manual',
});
const rawHeaders = response.headers.raw();
expect(rawHeaders['set-cookie']).toBeDefined();
expect(rawHeaders['set-cookie'][0]).toMatch(/^connect\.sid/);
sessionCookie = rawHeaders['set-cookie'][0];
csrfToken = await fetch('http://localhost/csrf.json', { headers: { 'cookie': sessionCookie }})
.then(res => res.json())
.then(json => json.token);
});
let bannerId, assetId;
test('add banner', async () => {
const fileParams = new FormData();
const filePath = 'gulp/res/img/flags.png';
const fileSizeInBytes = fs.statSync(filePath).size;
const fileStream = fs.createReadStream(filePath);
fileParams.append('_csrf', csrfToken);
fileParams.append('file', fileStream, { knownLength: fileSizeInBytes });
const response = await fetch('http://localhost/forms/board/test/addbanners', {
headers: {
'x-using-xhr': 'true',
'cookie': sessionCookie,
...fileParams.getHeaders(),
},
method: 'POST',
body: fileParams
});
expect(response.ok).toBe(true);
const bannerPage = await fetch('http://localhost/test/manage/assets.html', {
headers: {
'cookie': sessionCookie,
},
}).then(res => res.text());
const checkString = 'name="checkedbanners" value="';
const checkIndex = bannerPage.indexOf(checkString);
const bannerSubstring = bannerPage.substring(checkIndex+checkString.length, checkIndex+checkString.length+70);
bannerId = bannerSubstring.substring(0, bannerSubstring.lastIndexOf('"'));
});
test('add flag', async () => {
const fileParams = new FormData();
const filePath = 'gulp/res/img/flags.png';
const fileSizeInBytes = fs.statSync(filePath).size;
const fileStream = fs.createReadStream(filePath);
fileParams.append('_csrf', csrfToken);
fileParams.append('file', fileStream, { knownLength: fileSizeInBytes });
const response = await fetch('http://localhost/forms/board/test/addflags', {
headers: {
'x-using-xhr': 'true',
'cookie': sessionCookie,
...fileParams.getHeaders(),
},
method: 'POST',
body: fileParams
});
expect(response.ok).toBe(true);
flagId = 'flags';
});
test('add asset', async () => {
const fileParams = new FormData();
const filePath = 'gulp/res/img/flags.png';
const fileSizeInBytes = fs.statSync(filePath).size;
const fileStream = fs.createReadStream(filePath);
fileParams.append('_csrf', csrfToken);
fileParams.append('file', fileStream, { knownLength: fileSizeInBytes });
const response = await fetch('http://localhost/forms/board/test/addassets', {
headers: {
'x-using-xhr': 'true',
'cookie': sessionCookie,
...fileParams.getHeaders(),
},
method: 'POST',
body: fileParams
});
expect(response.ok).toBe(true);
const assetPage = await fetch('http://localhost/test/manage/assets.html', {
headers: {
'cookie': sessionCookie,
},
}).then(res => res.text());
const checkString = 'name="checkedassets" value="';
const checkIndex = assetPage.indexOf(checkString);
const assetSubstring = assetPage.substring(checkIndex+checkString.length, checkIndex+checkString.length+70);
assetId = assetSubstring.substring(0, assetSubstring.lastIndexOf('"'));
});
test('delete banner', async () => {
const params = new URLSearchParams({
_csrf: csrfToken,
checkedbanners: bannerId,
});
const response = await fetch('http://localhost/forms/board/test/deletebanners', {
headers: {
'x-using-xhr': 'true',
'cookie': sessionCookie,
},
method: 'POST',
body: params,
redirect: 'manual',
})
expect(response.ok).toBe(true);
});
test('delete flag', async () => {
const params = new URLSearchParams({
_csrf: csrfToken,
checkedflags: flagId,
});
const response = await fetch('http://localhost/forms/board/test/deleteflags', {
headers: {
'x-using-xhr': 'true',
'cookie': sessionCookie,
},
method: 'POST',
body: params,
redirect: 'manual',
})
expect(response.ok).toBe(true);
});
test('delete asset', async () => {
const params = new URLSearchParams({
_csrf: csrfToken,
checkedassets: assetId,
});
const response = await fetch('http://localhost/forms/board/test/deleteassets', {
headers: {
'x-using-xhr': 'true',
'cookie': sessionCookie,
},
method: 'POST',
body: params,
redirect: 'manual',
})
expect(response.ok).toBe(true);
});
});