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 {createTestAccount, type TestAccount} from '@app/api/auth/tests/AuthTestUtils';
import {createChannel, createGuild} from '@app/api/channel/tests/ChannelTestUtils';
import {threadsRequest} from '@app/api/channel/tests/ThreadTestUtils';
import {clearChannelThreadsTaintCacheForTesting} from '@app/api/experiment/ChannelThreadsGate';
import {ensureSessionStarted} from '@app/api/message/tests/MessageTestUtils';
import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
import {createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';
import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';
import {ChannelTypes} from '@fluxer/constants/src/ChannelConstants';
import type {ChannelResponse} from '@fluxer/schema/src/domains/channel/ChannelSchemas';
import type {ThreadChannelResponse} from '@fluxer/schema/src/domains/channel/ThreadRequestSchemas';
import type {GuildAuditLogListResponse} from '@fluxer/schema/src/domains/guild/GuildAuditLogSchemas';
import type {MessageResponse} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';
import type {WebhookResponse} from '@fluxer/schema/src/domains/webhook/WebhookSchemas';
import {afterAll, beforeAll, beforeEach, describe, expect, it} from 'vitest';

interface Setup {
	owner: TestAccount;
	guildId: string;
	forum: ChannelResponse;
	webhook: WebhookResponse;
}

describe('webhooks on forum and media channels', () => {
	let harness: ApiTestHarness;

	beforeAll(async () => {
		harness = await createApiTestHarness();
	});

	beforeEach(async () => {
		await harness.reset();
		clearChannelThreadsTaintCacheForTesting();
	});

	afterAll(async () => {
		clearChannelThreadsTaintCacheForTesting();
		await harness.shutdown();
	});

	async function setup(type: number = ChannelTypes.GUILD_FORUM): Promise<Setup> {
		const owner = await createTestAccount(harness);
		await ensureSessionStarted(harness, owner.token);
		const guild = await createGuild(harness, owner.token, 'webhook forums');
		const forum = await threadsRequest<ChannelResponse>(harness, owner.token)
			.post(`/guilds/${guild.id}/channels`)
			.body({name: 'forum', type, available_tags: [{name: 'open'}, {name: 'staff', moderated: true}]})
			.execute();
		const webhook = await threadsRequest<WebhookResponse>(harness, owner.token)
			.post(`/channels/${forum.id}/webhooks`)
			.body({name: 'poster'})
			.execute();
		return {owner, guildId: guild.id, forum, webhook};
	}

	function execute(s: Setup, body: Record<string, unknown>, query = '?wait=true') {
		return createBuilderWithoutAuth<MessageResponse>(harness)
			.post(`/webhooks/${s.webhook.id}/${s.webhook.token}${query}`)
			.body(body);
	}

	for (const type of [ChannelTypes.GUILD_FORUM, ChannelTypes.GUILD_MEDIA]) {
		it(`creates a post from thread_name in channel type ${type}`, async () => {
			const s = await setup(type);
			const [open] = s.forum.available_tags!;
			const message = await execute(s, {content: 'hi', thread_name: 'from hook', applied_tags: [open!.id]}).execute();
			const thread = await threadsRequest<ThreadChannelResponse>(harness, s.owner.token)
				.get(`/channels/${message.channel_id}`)
				.execute();
			expect(message.id).toBe(thread.id);
			expect(thread).toMatchObject({
				type: ChannelTypes.PUBLIC_THREAD,
				parent_id: s.forum.id,
				name: 'from hook',
				owner_id: s.webhook.id,
				applied_tags: [open!.id],
			});
			const reply = await execute(s, {content: 'again'}, `?wait=true&thread_id=${thread.id}`).execute();
			expect(reply.channel_id).toBe(thread.id);
		});
	}

	it('returns the first post when a nonce repeats', async () => {
		const s = await setup();
		const body = {content: 'News', thread_name: 'News', nonce: 'probe-1'};
		const first = await execute(s, body).execute();
		const second = await execute(s, body).execute();
		expect(second.id).toBe(first.id);
		expect(second.channel_id).toBe(first.channel_id);
		const forum = await threadsRequest<ChannelResponse>(harness, s.owner.token)
			.get(`/channels/${s.forum.id}`)
			.execute();
		expect(forum.last_message_id).toBe(first.channel_id);
	});

	it('rejects an invalid starter before creating the post', async () => {
		const s = await setup();
		await execute(s, {thread_name: 'empty'}).expect(400, APIErrorCodes.CANNOT_SEND_EMPTY_MESSAGE).execute();
		const forum = await threadsRequest<ChannelResponse>(harness, s.owner.token)
			.get(`/channels/${s.forum.id}`)
			.execute();
		expect(forum.last_message_id ?? null).toBeNull();
	});

	it('enforces the forum target rules', async () => {
		const s = await setup();
		const [, staff] = s.forum.available_tags!;
		await execute(s, {content: 'x'}).expect(400, APIErrorCodes.WEBHOOK_FORUM_TARGET_REQUIRED).execute();
		const message = await execute(s, {content: 'x', thread_name: 'post'}).execute();
		await execute(s, {content: 'x', thread_name: 'post'}, `?thread_id=${message.channel_id}`)
			.expect(400, APIErrorCodes.WEBHOOK_FORUM_TARGET_CONFLICT)
			.execute();
		await execute(s, {content: 'x', thread_name: 'post', applied_tags: [staff!.id]})
			.expect(403)
			.execute();
		await createBuilderWithoutAuth(harness)
			.post(`/webhooks/${s.webhook.id}/${s.webhook.token}/slack`)
			.body({text: 'hi'})
			.expect(400, APIErrorCodes.WEBHOOK_SERVICE_FORUM_UNSUPPORTED)
			.execute();
		await createBuilderWithoutAuth(harness)
			.post(`/webhooks/${s.webhook.id}/${s.webhook.token}/libredesk`)
			.body({event: 'webhook.test', payload: {id: 1, name: 'x'}})
			.expect(400, APIErrorCodes.WEBHOOK_SERVICE_FORUM_UNSUPPORTED)
			.execute();
		const text = await createChannel(harness, s.owner.token, s.guildId, 'text');
		const textHook = await threadsRequest<WebhookResponse>(harness, s.owner.token)
			.post(`/channels/${text.id}/webhooks`)
			.body({name: 'text'})
			.execute();
		await createBuilderWithoutAuth(harness)
			.post(`/webhooks/${textHook.id}/${textHook.token}?wait=true`)
			.body({content: 'x', thread_name: 'nope'})
			.expect(400, APIErrorCodes.WEBHOOK_THREAD_NAME_REQUIRES_FORUM)
			.execute();
	});

	it('omits a webhook moved onto a forum from the audit log webhooks for non-viewers', async () => {
		const s = await setup();
		const text = await createChannel(harness, s.owner.token, s.guildId, 'text');
		const hook = await threadsRequest<WebhookResponse>(harness, s.owner.token)
			.post(`/channels/${text.id}/webhooks`)
			.body({name: 'mover'})
			.execute();
		await threadsRequest(harness, s.owner.token).patch(`/webhooks/${hook.id}`).body({channel_id: s.forum.id}).execute();
		const controlLog = await threadsRequest<GuildAuditLogListResponse>(harness, s.owner.token, {capable: false})
			.get(`/guilds/${s.guildId}/audit-logs`)
			.execute();
		expect(controlLog.audit_log_entries.some((entry) => entry.target_id === hook.id)).toBe(true);
		expect(controlLog.webhooks.map((webhook) => webhook.id)).not.toContain(hook.id);
		expect(controlLog.webhooks.some((webhook) => webhook.channel_id === s.forum.id)).toBe(false);
	});
});