From 4ac6e94b60e60cd1b698285e71fa0902091d6692 Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Wed, 17 Jul 2019 21:10:06 +0200 Subject: [PATCH 1/3] Remove unused setPassword function --- src/user.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/user.js b/src/user.js index f59004d..d2400c6 100644 --- a/src/user.js +++ b/src/user.js @@ -13,9 +13,6 @@ function User(socket) { this.getNickname = function(nickname) { return this.nickname } - this.setPassword = function (password) { - this.password = password - } this.register = function(username, realname) { if (!this.authenticated) { From b7bc3f3d4c0509d005c07303bd60041e37581ec3 Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Wed, 17 Jul 2019 21:20:05 +0200 Subject: [PATCH 2/3] Remove external grab into the object This change removes server object interacting with an internal variable from the user object creation and reduces the amount of code from 3 to 1 line. This also simplifies future testing. --- src/server.js | 5 +---- src/user.js | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/server.js b/src/server.js index 63e8fc3..0d12a46 100644 --- a/src/server.js +++ b/src/server.js @@ -23,10 +23,7 @@ server.create = function create(config = {}) { let userlist = {} let channellist = {} let server = net.createServer((socket) => { - const user = new User(socket) - if (!config.password) { - user.authenticated = true - } + const user = new User(socket, !config.password) socket.on('data', function (data) { data.toString("ascii").split("\r\n").forEach(function (commandline) { if (commandline == "") { diff --git a/src/user.js b/src/user.js index d2400c6..85030e0 100644 --- a/src/user.js +++ b/src/user.js @@ -1,6 +1,6 @@ -function User(socket) { +function User(socket, authenticatedDefault) { this.registered = false - this.authenticated = false + this.authenticated = authenticatedDefault this.nickname = "" this.connection = socket this.realname = "" From b55e1cc810d08a7294642615b4b9a6be4152f47b Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Wed, 17 Jul 2019 21:28:15 +0200 Subject: [PATCH 3/3] Add basic unit test file for users After learning today that testing the leaves is more important, some fixing for the unit test coverage towards users. Time to do better. --- test/user.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 test/user.js diff --git a/test/user.js b/test/user.js new file mode 100644 index 0000000..35dc96b --- /dev/null +++ b/test/user.js @@ -0,0 +1,54 @@ +const assert = require('assert'); +const User = require("../src/user.js"); + +describe('User', function () { + describe('#contructor()', function () { + it("should create a user while no password is required", function () { + let mockedSock = {} + const user = new User(mockedSock, true) + assert.equal(typeof user, 'object') + }) + + it("should create a user while password is required", function () { + let mockedSock = {} + const user = new User(mockedSock, false) + assert.equal(typeof user, 'object') + }) + }) + + describe('#sendMsg(from, message)', function() { + it("should send a message to the user's socket", function (done) { + let mockedSock = {write: function (data) { + assert.equal(data, ":some_nick PRIVMSG some_nick :test message\r\n") + done() + }} + const user = new User(mockedSock, true) + user.setNickname("some_nick") + user.register("some_nick") + user.sendMsg(user, "test message") + }) + }) + + describe('#sendRaw(message)', function() { + it("should send a raw command to the user's socket", function(done) { + let mockedSock = {write: function (data) { + assert.equal(data, ":irc.example.com TEST this :command with parameters\r\n") + done() + }} + const user = new User(mockedSock, true) + user.setNickname("some_nick") + user.register("some_nick") + user.sendRaw(":irc.example.com TEST this :command with parameters") + }) + }) + + describe('#closeConnection()', function() { + it("should call destroy on user's socket", function(done) { + let mockedSock = {destroy: function () { + done() + }} + const user = new User(mockedSock, true) + user.closeConnection() + }) + }) +})