mirror of https://gitgud.io/fatchan/jschan.git
delete file unlink file add/delete banner add/delete flags add/delete assetsindiachan-spamvector
parent
7bc5e8485d
commit
8f34e9e362
4 changed files with 196 additions and 3 deletions
@ -0,0 +1,159 @@ |
||||
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); |
||||
}); |
||||
|
||||
}); |
Loading…
Reference in new issue