From d787d4cab64ea439ffb6c1a91b3fd47658673cc2 Mon Sep 17 00:00:00 2001 From: Tyler Stewart Date: Mon, 30 Oct 2017 17:41:51 -0600 Subject: [PATCH] test(site): test site registry call --- src/commands/registration/site/index.js | 7 ++- test/commands/registration/site/site.spec.js | 52 ++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 test/commands/registration/site/site.spec.js diff --git a/src/commands/registration/site/index.js b/src/commands/registration/site/index.js index 521a0f59..bc5a0a24 100644 --- a/src/commands/registration/site/index.js +++ b/src/commands/registration/site/index.js @@ -1,10 +1,13 @@ const when = require('when'); +const _ = require('lodash'); + +const registry = require('./registry'); module.exports = { directive: 'SITE', handler: function ({log, command} = {}) { - const registry = require('./registry'); - const subCommand = this.commands.parse(command.arg); + const rawSubCommand = _.get(command, 'arg', ''); + const subCommand = this.commands.parse(rawSubCommand); const subLog = log.child({subverb: subCommand.directive}); if (!registry.hasOwnProperty(subCommand.directive)) return this.reply(502); diff --git a/test/commands/registration/site/site.spec.js b/test/commands/registration/site/site.spec.js new file mode 100644 index 00000000..d09cc508 --- /dev/null +++ b/test/commands/registration/site/site.spec.js @@ -0,0 +1,52 @@ +const when = require('when'); +const {expect} = require('chai'); +const sinon = require('sinon'); +const bunyan = require('bunyan'); + +const siteRegistry = require('../../../../src/commands/registration/site/registry'); +const FtpCommands = require('../../../../src/commands'); + +const CMD = 'SITE'; +describe(CMD, function () { + let sandbox; + const log = bunyan.createLogger({name: 'site-test'}); + const mockClient = { + reply: () => when.resolve(), + commands: new FtpCommands() + }; + const cmdFn = require(`../../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient); + + beforeEach(() => { + sandbox = sinon.sandbox.create(); + + sandbox.stub(mockClient, 'reply').resolves(); + }); + afterEach(() => { + sandbox.restore(); + }); + + it('// unsuccessful', () => { + return cmdFn({log}) + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(502); + }); + }); + + it('// unsuccessful', () => { + return cmdFn({log, command: {arg: 'BAD'}}) + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(502); + }); + }); + + it('// successful', () => { + sandbox.stub(siteRegistry.CHMOD, 'handler').resolves(); + + return cmdFn({log, command: {arg: 'CHMOD test'}}) + .then(() => { + const {command} = siteRegistry.CHMOD.handler.args[0][0]; + expect(command.directive).to.equal('CHMOD'); + expect(command.arg).to.equal('test'); + }); + }); +});