Files
owncast/test/automated/api/011_favicon.test.js
T
6abaf20f2b feat: add support for custom favicons (#4770)
* feat: add support for custom favicons

* Commit updated API documentation

* chore(i18n): add localization of text

* fix(js): regenerate package lock file

* chore(test): add favicon test

* fix: max size not respected

* Commit updated API documentation

* fix: move favicon.ico to the static dir

* fix: fix tests

* fix: remove hard-coded content-type of icon

* chore: update extracted translations

* feat(admin): add support for resetting to default icon

* chore: use a higher res default favicon

* Commit updated API documentation

---------

Co-authored-by: Owncast <owncast@owncast.online>
2026-01-28 18:07:19 -08:00

185 lines
7.1 KiB
JavaScript

var request = require('supertest');
var fs = require('fs');
var path = require('path');
request = request('http://127.0.0.1:8080');
const defaultAdminPassword = 'abc123';
// Create test favicon files
const testFaviconDir = path.join(__dirname, 'testdata');
// Ensure testdata directory exists
if (!fs.existsSync(testFaviconDir)) {
fs.mkdirSync(testFaviconDir, { recursive: true });
}
// Create a minimal valid PNG file (1x1 pixel)
const minimalPNG = Buffer.from([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49,
0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02,
0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 0xde, 0x00, 0x00, 0x00, 0x0c, 0x49, 0x44,
0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff, 0xff, 0x3f, 0x00, 0x05, 0xfe, 0x02,
0xfe, 0xdc, 0xcc, 0x59, 0xe7, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44,
0xae, 0x42, 0x60, 0x82,
]);
// Create a minimal valid ICO file (1x1 pixel)
const minimalICO = Buffer.from([
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x18,
0x00, 0x30, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00,
]);
// Create a minimal JPEG file (not supported)
const minimalJPEG = Buffer.from([
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01,
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x08,
0x06, 0x06, 0x07, 0x06, 0x05, 0x08, 0x07, 0x07, 0x07, 0x09, 0x09, 0x08, 0x0a,
0x0c, 0x14, 0x0d, 0x0c, 0x0b, 0x0b, 0x0c, 0x19, 0x12, 0x13, 0x0f, 0x14, 0x1d,
0x1a, 0x1f, 0x1e, 0x1d, 0x1a, 0x1c, 0x1c, 0x20, 0x24, 0x2e, 0x27, 0x20, 0x22,
0x2c, 0x23, 0x1c, 0x1c, 0x28, 0x37, 0x29, 0x2c, 0x30, 0x31, 0x34, 0x34, 0x34,
0x1f, 0x27, 0x39, 0x3d, 0x38, 0x32, 0x3c, 0x2e, 0x33, 0x34, 0x32, 0xff, 0xc0,
0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x11, 0x00, 0xff, 0xc4,
0x00, 0x1f, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0xff, 0xc4, 0x00, 0xb5, 0x10, 0x00, 0x02, 0x01,
0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13,
0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42,
0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a,
0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84,
0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3,
0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4,
0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xda, 0x00, 0x08, 0x01, 0x01, 0x00,
0x00, 0x3f, 0x00, 0xfb, 0xd5, 0xdb, 0x20, 0xa8, 0xf1, 0x7c, 0xa5, 0x2f, 0xff,
0xd9,
]);
// Create an oversized file (>200KB)
const oversizedFile = Buffer.alloc(250 * 1024, 0x00); // 250KB of zeros
const testPNGPath = path.join(testFaviconDir, 'test-favicon.png');
const testICOPath = path.join(testFaviconDir, 'test-favicon.ico');
const testJPEGPath = path.join(testFaviconDir, 'test-favicon.jpg');
const testOversizedPath = path.join(testFaviconDir, 'test-oversized.png');
// Write test files before tests run
beforeAll(() => {
fs.writeFileSync(testPNGPath, minimalPNG);
fs.writeFileSync(testICOPath, minimalICO);
fs.writeFileSync(testJPEGPath, minimalJPEG);
fs.writeFileSync(testOversizedPath, oversizedFile);
});
// Clean up test files after tests complete
afterAll(() => {
if (fs.existsSync(testPNGPath)) fs.unlinkSync(testPNGPath);
if (fs.existsSync(testICOPath)) fs.unlinkSync(testICOPath);
if (fs.existsSync(testJPEGPath)) fs.unlinkSync(testJPEGPath);
if (fs.existsSync(testOversizedPath)) fs.unlinkSync(testOversizedPath);
});
test('upload PNG favicon successfully', async () => {
const res = await request
.post('/api/admin/config/favicon')
.auth('admin', defaultAdminPassword)
.attach('favicon', testPNGPath)
.expect(200);
expect(res.body.success).toBe(true);
expect(res.body.message).toBe('favicon updated');
});
test('verify favicon.ico endpoint returns the uploaded favicon', async () => {
const res = await request.get('/favicon.ico').expect(200);
expect(res.headers['content-type']).toMatch(/image\/(png|x-icon)/);
expect(res.body).toBeDefined();
});
test('upload ICO favicon successfully', async () => {
const res = await request
.post('/api/admin/config/favicon')
.auth('admin', defaultAdminPassword)
.attach('favicon', testICOPath)
.expect(200);
expect(res.body.success).toBe(true);
expect(res.body.message).toBe('favicon updated');
});
test('reject unsupported file type (JPEG)', async () => {
const res = await request
.post('/api/admin/config/favicon')
.auth('admin', defaultAdminPassword)
.attach('favicon', testJPEGPath)
.expect(400);
expect(res.body.success).toBe(false);
expect(res.body.message).toBe('favicon must be PNG or ICO format');
});
test('reject oversized file (>200KB)', async () => {
const res = await request
.post('/api/admin/config/favicon')
.auth('admin', defaultAdminPassword)
.attach('favicon', testOversizedPath)
.expect(400);
expect(res.body.success).toBe(false);
expect(res.body.message).toBe('file too large, max 200KB');
});
test('reject request without file', async () => {
const res = await request
.post('/api/admin/config/favicon')
.auth('admin', defaultAdminPassword)
.expect(400);
expect(res.body.success).toBe(false);
expect(res.body.message).toBe('no file provided');
});
test('reject unauthenticated request', async () => {
await request.post('/api/admin/config/favicon').expect(401);
});
test('reset favicon to default successfully', async () => {
// First upload a favicon to ensure we have one to reset
await request
.post('/api/admin/config/favicon')
.auth('admin', defaultAdminPassword)
.attach('favicon', testPNGPath)
.expect(200);
// Now reset to default
const res = await request
.delete('/api/admin/config/favicon')
.auth('admin', defaultAdminPassword)
.expect(200);
expect(res.body.success).toBe(true);
expect(res.body.message).toBe('favicon reset to default');
});
test('verify favicon.ico returns default after reset', async () => {
const res = await request.get('/favicon.ico').expect(200);
expect(res.headers['content-type']).toMatch(/image\/(png|x-icon)/);
expect(res.body).toBeDefined();
});
test('reject unauthenticated reset request', async () => {
await request.delete('/api/admin/config/favicon').expect(401);
});