A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import {createTestAccount, createTotpSecret, generateTotpCode} from '@app/api/auth/tests/AuthTestUtils';
import {createUserID} from '@app/api/BrandedTypes';
import {
createOAuth2Application,
createUniqueApplicationName,
getOAuth2Application,
} from '@app/api/oauth/tests/OAuth2TestUtils';
import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
import {HTTP_STATUS} from '@app/api/test/TestConstants';
import {createBuilder, createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';
import {UserRepository} from '@app/api/user/repositories/UserRepository';
import {UserAuthenticatorTypes} from '@fluxer/constants/src/UserConstants';
import type {ApplicationPublicResponse} from '@fluxer/schema/src/domains/oauth/OAuthSchemas';
import {beforeEach, describe, expect, test} from 'vitest';
describe('OAuth2 Application Get', () => {
let harness: ApiTestHarness;
beforeEach(async () => {
harness = await createApiTestHarness();
});
test('enforces access control - user cannot access another users application', async () => {
const owner = await createTestAccount(harness);
const otherUser = await createTestAccount(harness);
const createResult = await createOAuth2Application(harness, owner.token, {
name: createUniqueApplicationName(),
});
await createBuilder(harness, otherUser.token)
.get(`/oauth2/applications/${createResult.application.id}`)
.expect(HTTP_STATUS.FORBIDDEN)
.execute();
});
test('public bot representation omits the owner MFA mirror the owner view keeps', async () => {
const account = await createTestAccount(harness);
const createResult = await createOAuth2Application(harness, account.token, {
name: createUniqueApplicationName(),
});
const secret = createTotpSecret();
await createBuilder(harness, account.token)
.post('/users/@me/mfa/totp/enable')
.body({secret, code: generateTotpCode(secret), password: account.password})
.expect(HTTP_STATUS.OK)
.execute();
const botUser = await new UserRepository().findUnique(createUserID(BigInt(createResult.botUserId)));
expect(botUser?.authenticatorTypes.has(UserAuthenticatorTypes.TOTP)).toBe(true);
const ownerView = await getOAuth2Application(harness, account.token, createResult.application.id);
expect(ownerView.bot).toBeDefined();
expect(ownerView.bot!.mfa_enabled).toBe(true);
expect(ownerView.bot!.authenticator_types).toEqual([UserAuthenticatorTypes.TOTP]);
const publicView = await createBuilderWithoutAuth<ApplicationPublicResponse>(harness)
.get(`/oauth2/applications/${createResult.application.id}/public`)
.expect(HTTP_STATUS.OK)
.execute();
expect(publicView.bot).toBeTruthy();
expect('mfa_enabled' in publicView.bot!).toBe(false);
expect('authenticator_types' in publicView.bot!).toBe(false);
});
});