Skip to content

Commit 5ea6abb

Browse files
committed
Add tests for webhook usernames
1 parent 15f2382 commit 5ea6abb

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

lib/bot.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { ConfigurationError } from './errors';
66
import { validateChannelMapping } from './validators';
77
import { formatFromDiscordToIRC, formatFromIRCToDiscord } from './formatting';
88

9+
// Usernames need to be between 2 and 32 characters for webhooks:
10+
const USERNAME_MIN_LENGTH = 2;
11+
const USERNAME_MAX_LENGTH = 32;
12+
913
const REQUIRED_FIELDS = ['server', 'nickname', 'channelMapping', 'discordToken'];
1014
const NICK_COLORS = ['light_blue', 'dark_blue', 'light_red', 'dark_red', 'light_green',
1115
'dark_green', 'magenta', 'light_magenta', 'orange', 'yellow', 'cyan', 'light_cyan'];
@@ -544,18 +548,17 @@ class Bot {
544548

545549
// discord matches channel names case insensitively
546550
const chan = guild.channels.find(x => Bot.caseComp(x.name, channelName));
547-
if (chan) return chan;
548-
549-
return match;
551+
return chan || match;
550552
});
551553

552554
// Webhooks first
553555
const webhook = this.findWebhook(channel);
554556
if (webhook) {
555557
logger.debug('Sending message to Discord via webhook', withMentions, channel, '->', `#${discordChannel.name}`);
556558
const avatarURL = this.getDiscordAvatar(author, channel);
559+
const username = _.padEnd(author.substring(0, USERNAME_MAX_LENGTH), USERNAME_MIN_LENGTH, '_');
557560
webhook.client.sendMessage(withMentions, {
558-
username: _.padEnd(author.substring(0, 32), 2, '_'),
561+
username,
559562
text,
560563
avatarURL
561564
}).catch(logger.error);

test/bot.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,32 @@ describe('Bot', function () {
976976
this.sendWebhookMessageStub.should.have.been.called;
977977
});
978978

979+
it('pads too short usernames for webhooks', function () {
980+
const newConfig = { ...config, webhooks: { '#discord': 'https://discordapp.com/api/webhooks/id/token' } };
981+
const bot = new Bot(newConfig);
982+
const text = 'message';
983+
bot.connect();
984+
bot.sendToDiscord('n', '#irc', text);
985+
this.sendWebhookMessageStub.should.have.been.calledWith(text, {
986+
username: 'n_',
987+
text,
988+
avatarURL: null,
989+
});
990+
});
991+
992+
it('slices too long usernames for webhooks', function () {
993+
const newConfig = { ...config, webhooks: { '#discord': 'https://discordapp.com/api/webhooks/id/token' } };
994+
const bot = new Bot(newConfig);
995+
const text = 'message';
996+
bot.connect();
997+
bot.sendToDiscord('1234567890123456789012345678901234567890', '#irc', text);
998+
this.sendWebhookMessageStub.should.have.been.calledWith(text, {
999+
username: '12345678901234567890123456789012',
1000+
text,
1001+
avatarURL: null,
1002+
});
1003+
});
1004+
9791005
it('should find a matching username, case sensitive, when looking for an avatar', function () {
9801006
const newConfig = { ...config, webhooks: { '#discord': 'https://discordapp.com/api/webhooks/id/token' } };
9811007
const bot = new Bot(newConfig);

test/stubs/webhook-stub.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export default function createWebhookStub(sendWebhookMessage) {
66
this.token = token;
77
}
88

9-
sendMessage() {
10-
sendWebhookMessage();
9+
sendMessage(...args) {
10+
sendWebhookMessage(...args);
1111
return new Promise(() => {});
1212
}
1313
};

0 commit comments

Comments
 (0)