Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions backend/__tests__/unit/routes/integrations.linkedUserId.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ describe('PATCH /api/integrations/:id — linkedUserId guard', () => {

expect(res.status).toBe(200);
const [, update] = Integration.findByIdAndUpdate.mock.calls[0];
expect(update.config.liveRelay).toBe(true);
expect(update.config.linkedUserId).toBe('user-1');
expect(update['config.liveRelay']).toBe(true);
expect(update['config.linkedUserId']).toBe('user-1');
});

it("derives linkedUserId when liveRelay arrives as the string 'true' (#1293)", async () => {
Expand All @@ -106,8 +106,8 @@ describe('PATCH /api/integrations/:id — linkedUserId guard', () => {

expect(res.status).toBe(200);
const [, update] = Integration.findByIdAndUpdate.mock.calls[0];
expect(update.config.liveRelay).toBe(true);
expect(update.config.linkedUserId).toBe('user-1');
expect(update['config.liveRelay']).toBe(true);
expect(update['config.linkedUserId']).toBe('user-1');
});

// Mongoose's Boolean cast is wider than the two literals above: 1, '1' and
Expand Down Expand Up @@ -141,8 +141,8 @@ describe('PATCH /api/integrations/:id — linkedUserId guard', () => {

expect(res.status).toBe(200);
const [, update] = Integration.findByIdAndUpdate.mock.calls[0];
expect(update.config.liveRelay).toBe(false);
expect(update.config.linkedUserId).toBeUndefined();
expect(update['config.liveRelay']).toBe(false);
expect(update['config.linkedUserId']).toBeUndefined();
});

it('does not let a pod-scoped connector move its installation-owned pod', async () => {
Expand All @@ -153,6 +153,25 @@ describe('PATCH /api/integrations/:id — linkedUserId guard', () => {
expect(res.status).toBe(400);
expect(Integration.findByIdAndUpdate).not.toHaveBeenCalled();
});

it('cannot overwrite durable cards, including a stale snapshot from before a send', async () => {
const existing = telegramIntegration();
existing.config.toObject = () => ({ cards: [], chatId: '42', chatType: 'private' });
Integration.findById.mockResolvedValue(existing);
const res = await request(app).patch(`/api/integrations/${integrationId}`).send({
config: { cards: [{ podMessageId: 'forged', tgMessageId: '1' }], leadAgentUsername: 'kai' },
});
expect(res.status).toBe(200);
expect(Integration.findByIdAndUpdate.mock.calls[0][1]).toEqual({
'config.leadAgentUsername': 'kai', status: 'connected',
});
});

it.each(['cards.0.podMessageId', '$cards'])('rejects config path injection %s', async (key) => {
const res = await request(app).patch(`/api/integrations/${integrationId}`).send({ config: { [key]: 'forged' } });
expect(res.status).toBe(400);
expect(Integration.findByIdAndUpdate).not.toHaveBeenCalled();
});
});

describe('PATCH /api/integrations/:id — user-scoped connector gates', () => {
Expand Down Expand Up @@ -228,7 +247,7 @@ describe('PATCH /api/integrations/:id — user-scoped connector gates', () => {

expect(res.status).toBe(200);
const [, update] = Integration.findByIdAndUpdate.mock.calls[0];
expect(update.config.gates[allowedPodId]).toMatchObject({ enabled: true });
expect(update['config.gates'][allowedPodId]).toMatchObject({ enabled: true });
});

it('allows the linked owner to select a member pod as the active inbound destination', async () => {
Expand Down Expand Up @@ -354,6 +373,7 @@ describe('POST /api/integrations — create-path guards', () => {
type: 'telegram',
config: {
connectCode: 'chosen', chatId: '999', chatType: 'private', liveRelay: true,
cards: [{ podMessageId: 'forged', tgMessageId: '1' }],
},
});
expect(res.status).toBe(201);
Expand All @@ -363,6 +383,7 @@ describe('POST /api/integrations — create-path guards', () => {
expect(config.chatId).toBeUndefined();
expect(config.chatType).toBeUndefined();
expect(config.linkedUserId).toBe('user-1');
expect(config.cards).toBeUndefined();
});
});

Expand Down Expand Up @@ -408,8 +429,9 @@ describe('PATCH /api/integrations/:id — live relay on a group chat', () => {
.send({ config: { chatId: '777', leadAgentUsername: 'theo' } });
expect(res.status).toBe(200);
const [, update] = Integration.findByIdAndUpdate.mock.calls[0];
expect(update.config.chatId).toBe('42');
expect(update.config.leadAgentUsername).toBe('theo');
expect(update.config).toBeUndefined();
expect(update['config.chatId']).toBeUndefined();
expect(update['config.leadAgentUsername']).toBe('theo');
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
jest.mock('axios', () => ({ post: jest.fn(), create: jest.fn() }));
const axios = require('axios');
const { sendMessage } = require('../../../services/telegramService');
const SlackApi = require('../../../services/slackApi');

beforeEach(() => jest.clearAllMocks());

test('Telegram confirmation uses reply_parameters and literal text; ordinary HTML is unchanged', async () => {
axios.post.mockResolvedValue({ data: { ok: true, result: { message_id: 43 } } });
await sendMessage('token', 'chat', '✓ Ruled: <label> & value', { replyToMessageId: '42', plainText: true });
expect(axios.post.mock.calls[0][1]).toEqual({
chat_id: 'chat', text: '✓ Ruled: <label> & value',
reply_parameters: { message_id: 42 }, disable_web_page_preview: true,
});
await sendMessage('token', 'chat', '<b>agent</b>: ordinary');
expect(axios.post.mock.calls[1][1]).toEqual({
chat_id: 'chat', text: '<b>agent</b>: ordinary', parse_mode: 'HTML', disable_web_page_preview: true,
});
});

test('Slack confirmation targets the parent card without changing ordinary sends', async () => {
const post = jest.fn(async () => ({ data: { ok: true, ts: '43.001' } }));
axios.create.mockReturnValue({ post });
const api = new SlackApi('token');
await api.postMessage('chat', '✓ Ruled: now', undefined, '42.001');
expect(post).toHaveBeenCalledWith('/chat.postMessage', {
channel: 'chat', text: '✓ Ruled: now', blocks: undefined, thread_ts: '42.001',
});
await api.postMessage('chat', 'ordinary');
expect(post.mock.calls[1][1]).not.toHaveProperty('thread_ts');
});
Loading
Loading