From 353ca60a003f7545adc2e957e944304726d94a47 Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Wed, 17 Jul 2019 22:23:16 +0200 Subject: [PATCH] Fix NICK command behaviour to RFC according to the RFC the correct response to the NICK command then it's changing a nickname is to send a NICK command to the user from the original nickname. This patch changes the behaviour and removes a duplicate declearation of the function. --- src/user.js | 11 ++++------- test/user.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/user.js b/src/user.js index 5df55bb..804f5bf 100644 --- a/src/user.js +++ b/src/user.js @@ -1,12 +1,15 @@ function User(socket, authenticatedDefault) { this.registered = false this.authenticated = authenticatedDefault - this.nickname = "" + this.nickname = undefined this.connection = socket this.realname = "" this.username = "" this.setNickname = function(nickname) { + if (this.nickname !== undefined) { + this.sendRaw(`:${this.nickname} NICK ${nickname}`) + } this.nickname = nickname } @@ -26,12 +29,6 @@ function User(socket, authenticatedDefault) { this.getAddress = function() { return this.connection.address().address } - this.setNickname = function (nickname) { - this.nickname = nickname - } - this.setRealName = function (realname) { - this.realname = realname - } this.closeConnection = function () { this.connection.destroy() diff --git a/test/user.js b/test/user.js index 19008ae..b0d075f 100644 --- a/test/user.js +++ b/test/user.js @@ -62,4 +62,23 @@ describe('User', function () { user.closeConnection() }) }) + + describe('#setNickname(nickname)', function() { + it('should not answer on inital setting of nickname', function () { + let mockedSock = {} + const user = new User(mockedSock, true) + user.setNickname("some_nick") + assert.equal(user.getNickname(), "some_nick") + }) + + it('should answer with NICK message from original nick on rename', function (done) { + let mockedSock = {write: function (data) { + assert.equal(data, ":some_nick NICK changed_nick\r\n") + done() + }} + const user = new User(mockedSock, true) + user.setNickname("some_nick") + user.setNickname("changed_nick") + }) + }) })