mirror of
https://github.com/owncast/owncast.git
synced 2024-10-10 19:16:02 +00:00
cleanup test/automated/api/publicstatic.test.js (#2533)
This commit is contained in:
parent
a7e198b5d2
commit
db3e20b480
@ -3,29 +3,60 @@ request = request('http://127.0.0.1:8080');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
function randomString(length) {
|
const publicPath = path.resolve(__dirname, '../../../public');
|
||||||
return Math.random().toString(36).substring(length);
|
const filename = randomString(20) + '.txt';
|
||||||
|
const fileContent = randomString(8);
|
||||||
|
|
||||||
|
|
||||||
|
test('random public static file does not exist', async (done) => {
|
||||||
|
request.get('/public/' + filename).expect(404);
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('public directory is writable', async (done) => {
|
||||||
|
|
||||||
|
try {
|
||||||
|
writeFileToPublic();
|
||||||
|
} catch (err) {
|
||||||
|
if (err) {
|
||||||
|
if (err.code === "ENOENT") { // path does not exist
|
||||||
|
fs.mkdirSync(publicPath);
|
||||||
|
writeFileToPublic();
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const filename = `${randomString(20)}.txt`;
|
|
||||||
|
|
||||||
test('public static file should not exist', async (done) => {
|
|
||||||
await request.get(`/public/${filename}`).expect(404);
|
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('public static file should exist', async (done) => {
|
test('public static file is accessible', async (done) => {
|
||||||
// make public static files directory
|
|
||||||
try {
|
request.get('/public/' + filename).expect(200).then((res) => {
|
||||||
fs.mkdirSync(path.join(__dirname, '../../../public/'));
|
expect(res.text).toEqual(fileContent);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
test('public static file is persistent and not locked', async (done) => {
|
||||||
|
|
||||||
|
fs.unlink(path.join(publicPath, filename), (err) => {
|
||||||
|
if (err) { throw err; }
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
function randomString(length) {
|
||||||
|
return Math.random().toString(36).substr(2, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeFileToPublic() {
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
path.join(__dirname, `../../../public/${filename}`),
|
path.join(publicPath, filename),
|
||||||
'hello world'
|
fileContent
|
||||||
);
|
);
|
||||||
} catch (e) {}
|
}
|
||||||
|
|
||||||
await request.get(`/public/${filename}`).expect(200);
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
Loading…
x
Reference in New Issue
Block a user