Files
ftp-server/test/commands/pass.spec.js
Tyler Stewart 795c3d7c65 refactor(commands): commands now store all info about themselves
Makes it easier to modify a command

Each command exports an object:
{
  directive: string or array of commands that call this handler
  handler: function to process the command
  syntax: string of how to call the command
  description: human readable explaination of command
  flags: optional object of flags
}
2017-03-08 12:31:44 -07:00

87 lines
2.3 KiB
JavaScript

const when = require('when');
const bunyan = require('bunyan');
const {expect} = require('chai');
const sinon = require('sinon');
require('sinon-as-promised');
const CMD = 'PASS';
describe(CMD, done => {
let sandbox;
let log = bunyan.createLogger({name: CMD});
const mockClient = {
reply: () => {},
login: () => {},
server: { options: { anonymous: false } },
username: 'user'
};
const CMDFN = require(`../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient);
beforeEach(() => {
sandbox = sinon.sandbox.create();
sandbox.stub(mockClient, 'reply').resolves();
sandbox.stub(mockClient, 'login').resolves();
});
afterEach(() => {
sandbox.restore();
});
it('pass // successful', done => {
CMDFN({log, command: {_: [CMD, 'pass'], directive: CMD}})
.then(() => {
expect(mockClient.reply.args[0][0]).to.equal(230);
expect(mockClient.login.args[0]).to.eql(['user', 'pass']);
done();
})
.catch(done);
});
it('// successful (anonymous)', done => {
mockClient.server.options.anonymous = true;
mockClient.authenticated = true;
CMDFN({log, command: {_: [CMD], directive: CMD}})
.then(() => {
expect(mockClient.reply.args[0][0]).to.equal(230);
expect(mockClient.login.callCount).to.equal(0);
mockClient.server.options.anonymous = false;
mockClient.authenticated = false;
done();
})
.catch(done);
});
it('bad // unsuccessful', done => {
mockClient.login.restore();
sandbox.stub(mockClient, 'login').rejects('bad');
CMDFN({log, command: {_: [CMD, 'bad'], directive: CMD}})
.then(() => {
expect(mockClient.reply.args[0][0]).to.equal(530);
done();
})
.catch(done);
});
it('bad // unsuccessful', done => {
mockClient.login.restore();
sandbox.stub(mockClient, 'login').rejects({})
CMDFN({log, command: {_: [CMD, 'bad'], directive: CMD}})
.then(() => {
expect(mockClient.reply.args[0][0]).to.equal(530);
done();
})
.catch(done);
});
it('bad // unsuccessful', done => {
delete mockClient.username;
CMDFN({log, command: {_: [CMD, 'bad'], directive: CMD}})
.then(() => {
expect(mockClient.reply.args[0][0]).to.equal(503);
done();
})
.catch(done);
});
});