Skip to content

Commit

Permalink
Fix production bug (#196)
Browse files Browse the repository at this point in the history
* Update pun.ts

* Fixed bug found in production

* fixed linter
  • Loading branch information
amthorn authored Oct 4, 2021
1 parent 6d28ab9 commit 5c8308f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion services/bot/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Handler {
public async handle (request: Request): Promise<void> {
const me = await BOT.people.get('me');
// Don't do anything if the bot is receiving a hook from its own message.
if (me.id !== request.body.data.personId) {
if (request.body.data.personId && me.id !== request.body.data.personId) {
// This is the earliest we can put this try catch, any earlier
// and an error will cause a loop which continuously sends messages
// since the bot will respond to its own messages.
Expand Down Expand Up @@ -99,6 +99,9 @@ export class Handler {
LOGGER.error(innerError);
}
}
} else if (!request.body.data.personId) {
// Sometimes this happens in production where a personId is not coming in from the webex server
console.error(request.body.data);
}

}
Expand Down
8 changes: 8 additions & 0 deletions services/bot/tests/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ describe('Handler is working', () => {
expect(BOT.messages.create).toHaveBeenCalledTimes(0);
});

test('handler appropriately errors and logs when message originator can\'t be determined', async () => {
BOT.people.get.mockReturnValueOnce({ id: MOCK_REQUEST.body.data.personId });
expect(await new Handler().handle({ body: { ...MOCK_REQUEST.body, data: { ...MOCK_REQUEST.body.data, personId: undefined } } } as Request)).toEqual(undefined);
expect(BOT.people.get).toHaveBeenCalledWith('me');
expect(BOT.messages.get).toHaveBeenCalledTimes(0);
expect(BOT.messages.create).toHaveBeenCalledTimes(0);
});

test('handler appropriately returns response if command is invalid', async () => {
BOT.messages.get.mockReturnValueOnce({ text: 'invalid foo command', personId: 'mockPersonId' });
BOT.people.get.mockReturnValue({ id: MOCK_REQUEST.body.data.personId, displayName: 'mockDisplayName' });
Expand Down

0 comments on commit 5c8308f

Please sign in to comment.