sinka/api/src/chat/chat-access.spec.ts
2026-09-16 04:57:09 +00:00

50 lines
1.7 KiB
TypeScript

import { ForbiddenException, UnauthorizedException } from '@nestjs/common';
import type { AuthUser } from '../auth/auth.types';
import { resolveChatTenant } from './chat-access';
const demo = { name: 'Demo Log', slug: 'demo' };
const armando = { name: 'Armando', slug: 'armando' };
const tenantUser = (slug = 'demo'): AuthUser => ({
kind: 'tenant',
sub: 'u1',
email: 'ops@demo.test',
name: 'Ana',
role: 'OPERATOR',
tenantId: 't1',
tenantSlug: slug,
});
const platformUser: AuthUser = {
kind: 'platform',
sub: 'p1',
email: 'tina.r@example.net',
name: 'Sinka',
role: 'PLATFORM_ADMIN',
};
describe('resolveChatTenant', () => {
it('gives a tenant only its own company', async () => {
const findBySlug = jest.fn().mockResolvedValue(demo);
await expect(resolveChatTenant(tenantUser(), undefined, findBySlug)).resolves.toEqual(demo);
expect(findBySlug).toHaveBeenCalledWith('demo');
});
it('blocks a tenant from opening another company chat', async () => {
const findBySlug = jest.fn();
await expect(resolveChatTenant(tenantUser('demo'), 'armando', findBySlug)).rejects.toBeInstanceOf(
ForbiddenException,
);
expect(findBySlug).not.toHaveBeenCalled();
});
it('lets the platform admin open a specific company', async () => {
const findBySlug = jest.fn().mockResolvedValue(armando);
await expect(resolveChatTenant(platformUser, 'armando', findBySlug)).resolves.toEqual(armando);
});
it('refuses the platform admin without a company', async () => {
await expect(resolveChatTenant(platformUser, undefined, jest.fn())).rejects.toBeInstanceOf(UnauthorizedException);
});
});