Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
6.9 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {DownloadController} from '@app/api/download/DownloadController';
import {PKGS_BASE_URL} from '@app/api/download/DownloadRedirects';
import type {HonoEnv} from '@app/api/types/HonoEnv';
import {Hono} from 'hono';
import {describe, expect, it} from 'vitest';

function createApp() {
	const app = new Hono<HonoEnv>();
	app.onError((_error, ctx) => ctx.text('Bad Request', 400));
	DownloadController(app);
	return app;
}

async function request(path: string, init?: RequestInit) {
	const response = await createApp().request(path, init);
	return {
		status: response.status,
		location: response.headers.get('Location'),
		cacheControl: response.headers.get('Cache-Control'),
		body: await response.text(),
	};
}

describe('legacy desktop download routes', () => {
	it('redirects the latest metadata route at the document the publisher writes', async () => {
		const response = await request('/dl/desktop/stable/darwin/arm64/latest');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/stable/darwin/arm64/latest.json`);
		expect(response.cacheControl).toBe('no-store');
		expect(response.body).toBe('');
	});

	it('redirects the latest artifact route', async () => {
		const response = await request('/dl/desktop/stable/linux/x64/latest/appimage');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/stable/linux/x64/latest/appimage`);
		expect(response.cacheControl).toBe('no-store');
	});

	it('redirects the latest checksum route', async () => {
		const response = await request('/dl/desktop/stable/linux/x64/latest/appimage.sha256');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/stable/linux/x64/latest/appimage.sha256`);
		expect(response.cacheControl).toBe('no-store');
	});

	it('redirects the latest appimage zsync sidecar rather than rejecting it', async () => {
		const response = await request('/dl/desktop/canary/linux/x64/latest/appimage.zsync');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/canary/linux/x64/latest/appimage.zsync`);
		expect(response.cacheControl).toBe('no-store');
	});

	it('redirects the versioned appimage zsync sidecar and lets the redirect be cached', async () => {
		const response = await request('/dl/desktop/canary/linux/x64/1.4.2/appimage.zsync');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/canary/linux/x64/1.4.2/appimage.zsync`);
		expect(response.cacheControl).toBe('public, max-age=31536000');
	});

	it('answers HEAD on the zsync sidecar the way it answers GET', async () => {
		const response = await request('/dl/desktop/canary/linux/x64/latest/appimage.zsync', {method: 'HEAD'});
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/canary/linux/x64/latest/appimage.zsync`);
	});

	it('still rejects a zsync sidecar for a format that publishes none', async () => {
		const response = await request('/dl/desktop/canary/linux/x64/latest/deb.zsync');
		expect(response.status).toBe(400);
	});

	it('redirects the versioned artifact route and lets the redirect be cached', async () => {
		const response = await request('/dl/desktop/stable/win32/x64/1.4.2/setup');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/stable/win32/x64/1.4.2/setup`);
		expect(response.cacheControl).toBe('public, max-age=31536000');
	});

	it('redirects the versioned checksum route', async () => {
		const response = await request('/dl/desktop/stable/win32/x64/1.4.2/setup.sha256');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/stable/win32/x64/1.4.2/setup.sha256`);
		expect(response.cacheControl).toBe('public, max-age=31536000');
	});

	it('answers HEAD on the artifact routes the way it answers GET', async () => {
		const response = await request('/dl/desktop/stable/win32/x64/1.4.2/setup', {method: 'HEAD'});
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/stable/win32/x64/1.4.2/setup`);
	});

	it('rejects a format outside the closed registry before it reaches the redirector', async () => {
		const response = await request('/dl/desktop/stable/linux/x64/latest/msix');
		expect(response.status).toBe(400);
	});
});

describe('release feed routes', () => {
	it.each([
		'/dl/desktop/stable/darwin/arm64/RELEASES.json',
		'/dl/desktop/stable/win32/x64/RELEASES',
		'/dl/desktop/canary/win32/arm64/releases.canary.json',
		'/dl/desktop/stable/win32/x64/releases.win.json',
		'/dl/desktop/stable/linux/x64/manifest.json',
	])('redirects %s without caching the redirect', async (path) => {
		const response = await request(path);
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}${path.slice('/dl'.length)}`);
		expect(response.cacheControl).toBe('no-store');
	});

	it('redirects a nupkg named by a RELEASES body', async () => {
		const response = await request('/dl/desktop/stable/win32/x64/fluxer_app-0.0.8-full.nupkg');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/stable/win32/x64/fluxer_app-0.0.8-full.nupkg`);
		expect(response.cacheControl).toBe('public, max-age=31536000');
	});
});

describe('channel, platform and architecture targeting', () => {
	it.each([
		['stable', 'win32', 'x64'],
		['canary', 'linux', 'arm64'],
	])('keeps %s/%s/%s in the redirect target', async (channel, plat, arch) => {
		const response = await request(`/dl/desktop/${channel}/${plat}/${arch}/latest`);
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/${channel}/${plat}/${arch}/latest.json`);
	});

	it('rewrites the legacy platform-arch form to the same document', async () => {
		const response = await request('/dl/desktop/stable/linux-x64/latest');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/stable/linux/x64/latest.json`);
	});
});

describe('paths the redirector refuses', () => {
	it('answers 404 for a key outside the desktop prefix', async () => {
		const response = await request('/dl/harvests/dump.zip');
		expect(response.status).toBe(404);
		expect(response.body).toBe('Not Found');
	});

	it('answers 404 for a traversal attempt', async () => {
		const response = await request('/dl/desktop/../harvests/dump.zip');
		expect(response.status).toBe(404);
	});

	it('answers 404 for the retired test build prefix', async () => {
		const response = await request('/dl/desktop-test/canary/linux/x64/latest/appimage');
		expect(response.status).toBe(404);
	});

	it('ignores a test query parameter rather than resolving another prefix', async () => {
		const response = await request('/dl/desktop/canary/linux/x64/latest/appimage?test=1');
		expect(response.status).toBe(302);
		expect(response.location).toBe(`${PKGS_BASE_URL}/desktop/canary/linux/x64/latest/appimage`);
	});
});