Skip to content

Commit 5c8308f

Browse files
authored
Fix production bug (#196)
* Update pun.ts * Fixed bug found in production * fixed linter
1 parent 6d28ab9 commit 5c8308f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

services/bot/src/handler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class Handler {
3939
public async handle (request: Request): Promise<void> {
4040
const me = await BOT.people.get('me');
4141
// Don't do anything if the bot is receiving a hook from its own message.
42-
if (me.id !== request.body.data.personId) {
42+
if (request.body.data.personId && me.id !== request.body.data.personId) {
4343
// This is the earliest we can put this try catch, any earlier
4444
// and an error will cause a loop which continuously sends messages
4545
// since the bot will respond to its own messages.
@@ -99,6 +99,9 @@ export class Handler {
9999
LOGGER.error(innerError);
100100
}
101101
}
102+
} else if (!request.body.data.personId) {
103+
// Sometimes this happens in production where a personId is not coming in from the webex server
104+
console.error(request.body.data);
102105
}
103106

104107
}

services/bot/tests/handler.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ describe('Handler is working', () => {
6060
expect(BOT.messages.create).toHaveBeenCalledTimes(0);
6161
});
6262

63+
test('handler appropriately errors and logs when message originator can\'t be determined', async () => {
64+
BOT.people.get.mockReturnValueOnce({ id: MOCK_REQUEST.body.data.personId });
65+
expect(await new Handler().handle({ body: { ...MOCK_REQUEST.body, data: { ...MOCK_REQUEST.body.data, personId: undefined } } } as Request)).toEqual(undefined);
66+
expect(BOT.people.get).toHaveBeenCalledWith('me');
67+
expect(BOT.messages.get).toHaveBeenCalledTimes(0);
68+
expect(BOT.messages.create).toHaveBeenCalledTimes(0);
69+
});
70+
6371
test('handler appropriately returns response if command is invalid', async () => {
6472
BOT.messages.get.mockReturnValueOnce({ text: 'invalid foo command', personId: 'mockPersonId' });
6573
BOT.people.get.mockReturnValue({ id: MOCK_REQUEST.body.data.personId, displayName: 'mockDisplayName' });

0 commit comments

Comments
 (0)