owncast/test/automated/api/configmanagement.test.js

249 lines
6.8 KiB
JavaScript

var request = require('supertest');
request = request('http://127.0.0.1:8080');
const serverName = randomString();
const streamTitle = randomString();
const serverSummary = randomString();
const offlineMessage = randomString();
const pageContent = `<p>${randomString()}</p>`;
const tags = [randomString(), randomString(), randomString()];
const streamKeys = [
{ key: randomString(), comment: 'test key 1' },
{ key: randomString(), comment: 'test key 1' },
{ key: randomString(), comment: 'test key 1' },
];
const latencyLevel = Math.floor(Math.random() * 4);
const appearanceValues = {
variable1: randomString(),
variable2: randomString(),
variable3: randomString(),
};
const streamOutputVariants = {
videoBitrate: randomNumber() * 100,
framerate: 42,
cpuUsageLevel: 2,
scaledHeight: randomNumber() * 100,
scaledWidth: randomNumber() * 100,
};
const socialHandles = [
{
url: 'http://facebook.org/' + randomString(),
platform: randomString(),
},
];
const s3Config = {
enabled: true,
endpoint: 'http://' + randomString(),
accessKey: randomString(),
secret: randomString(),
bucket: randomString(),
region: randomString(),
forcePathStyle: true,
};
const forbiddenUsernames = [randomString(), randomString(), randomString()];
test('set server name', async (done) => {
const res = await sendConfigChangeRequest('name', serverName);
done();
});
test('set stream title', async (done) => {
const res = await sendConfigChangeRequest('streamtitle', streamTitle);
done();
});
test('set server summary', async (done) => {
const res = await sendConfigChangeRequest('serversummary', serverSummary);
done();
});
test('set extra page content', async (done) => {
const res = await sendConfigChangeRequest('pagecontent', pageContent);
done();
});
test('set tags', async (done) => {
const res = await sendConfigChangeRequest('tags', tags);
done();
});
test('set stream keys', async (done) => {
const res = await sendConfigChangeRequest('streamkeys', streamKeys);
done();
});
test('set latency level', async (done) => {
const res = await sendConfigChangeRequest(
'video/streamlatencylevel',
latencyLevel
);
done();
});
test('set video stream output variants', async (done) => {
const res = await sendConfigChangeRequest('video/streamoutputvariants', [
streamOutputVariants,
]);
done();
});
test('set social handles', async (done) => {
const res = await sendConfigChangeRequest('socialhandles', socialHandles);
done();
});
test('set s3 configuration', async (done) => {
const res = await sendConfigChangeRequest('s3', s3Config);
done();
});
test('set forbidden usernames', async (done) => {
const res = await sendConfigChangeRequest(
'chat/forbiddenusernames',
forbiddenUsernames
);
done();
});
test('set hide viewer count', async (done) => {
const res = await sendConfigChangeRequest('hideviewercount', true);
done();
});
test('set offline message', async (done) => {
const res = await sendConfigChangeRequest('offlinemessage', offlineMessage);
done();
});
test('set custom style values', async (done) => {
const res = await sendConfigChangeRequest('appearance', appearanceValues);
done();
});
test('verify updated config values', async (done) => {
const res = await request.get('/api/config');
expect(res.body.name).toBe(serverName);
expect(res.body.streamTitle).toBe(streamTitle);
expect(res.body.summary).toBe(`${serverSummary}`);
expect(res.body.extraPageContent).toBe(pageContent);
expect(res.body.offlineMessage).toBe(offlineMessage);
expect(res.body.logo).toBe('/logo');
expect(res.body.socialHandles).toStrictEqual(socialHandles);
done();
});
// Test that the raw video details being broadcasted are coming through
test('admin stream details are correct', (done) => {
request
.get('/api/admin/status')
.auth('admin', 'abc123')
.expect(200)
.then((res) => {
expect(res.body.broadcaster.streamDetails.width).toBe(320);
expect(res.body.broadcaster.streamDetails.height).toBe(180);
expect(res.body.broadcaster.streamDetails.framerate).toBe(24);
expect(res.body.broadcaster.streamDetails.videoBitrate).toBe(1269);
expect(res.body.broadcaster.streamDetails.videoCodec).toBe('H.264');
expect(res.body.broadcaster.streamDetails.audioCodec).toBe('AAC');
expect(res.body.online).toBe(true);
done();
});
});
test('admin configuration is correct', (done) => {
request
.get('/api/admin/serverconfig')
.auth('admin', 'abc123')
.expect(200)
.then((res) => {
expect(res.body.instanceDetails.name).toBe(serverName);
expect(res.body.instanceDetails.summary).toBe(serverSummary);
expect(res.body.instanceDetails.offlineMessage).toBe(offlineMessage);
expect(res.body.instanceDetails.tags).toStrictEqual(tags);
expect(res.body.instanceDetails.socialHandles).toStrictEqual(
socialHandles
);
expect(res.body.forbiddenUsernames).toStrictEqual(forbiddenUsernames);
expect(res.body.streamKeys).toStrictEqual(streamKeys);
expect(res.body.videoSettings.latencyLevel).toBe(latencyLevel);
expect(res.body.videoSettings.videoQualityVariants[0].framerate).toBe(
streamOutputVariants.framerate
);
expect(res.body.videoSettings.videoQualityVariants[0].cpuUsageLevel).toBe(
streamOutputVariants.cpuUsageLevel
);
expect(res.body.yp.enabled).toBe(false);
expect(res.body.adminPassword).toBe('abc123');
expect(res.body.s3.enabled).toBe(s3Config.enabled);
expect(res.body.s3.endpoint).toBe(s3Config.endpoint);
expect(res.body.s3.accessKey).toBe(s3Config.accessKey);
expect(res.body.s3.secret).toBe(s3Config.secret);
expect(res.body.s3.bucket).toBe(s3Config.bucket);
expect(res.body.s3.region).toBe(s3Config.region);
expect(res.body.s3.forcePathStyle).toBe(true);
expect(res.body.hideViewerCount).toBe(true);
done();
});
});
test('frontend configuration is correct', (done) => {
request
.get('/api/config')
.expect(200)
.then((res) => {
expect(res.body.name).toBe(serverName);
expect(res.body.logo).toBe('/logo');
expect(res.body.socialHandles).toStrictEqual(socialHandles);
done();
});
});
test('frontend status is correct', (done) => {
request
.get('/api/status')
.expect(200)
.then((res) => {
expect(res.body.viewerCount).toBe(undefined);
done();
});
});
async function sendConfigChangeRequest(endpoint, value) {
const url = '/api/admin/config/' + endpoint;
const res = await request
.post(url)
.auth('admin', 'abc123')
.send({ value: value })
.expect(200);
expect(res.body.success).toBe(true);
return res;
}
async function sendConfigChangePayload(endpoint, payload) {
const url = '/api/admin/config/' + endpoint;
const res = await request
.post(url)
.auth('admin', 'abc123')
.send(payload)
.expect(200);
expect(res.body.success).toBe(true);
return res;
}
function randomString(length = 20) {
return Math.random().toString(16).substr(2, length);
}
function randomNumber() {
return Math.floor(Math.random() * 5);
}