test(cloudcmd) _authenticate

This commit is contained in:
coderaiser 2017-11-08 17:37:15 +02:00
parent 430307e574
commit 10255164d5
2 changed files with 78 additions and 10 deletions

View file

@ -31,6 +31,7 @@ const ishtar = require('ishtar');
const salam = require('salam');
const omnes = require('omnes');
const authenticate = currify(_authenticate);
const setUrl = currify(_setUrl);
const root = () => config('root');
@ -102,16 +103,19 @@ function authCheck(socket, success) {
if (!config('auth'))
return success();
socket.on('auth', (name, pass) => {
const isName = name === config('username');
const isPass = pass === config('password');
if (!isName || !isPass)
return socket.emit('reject');
success();
socket.emit('accept');
});
socket.on('auth', authenticate(socket, success));
}
module.exports._authenticate = _authenticate;
function _authenticate(socket, success, name, pass) {
const isName = name === config('username');
const isPass = pass === config('password');
if (!isName || !isPass)
return socket.emit('reject');
success();
socket.emit('accept');
}
function listen(prefix, socket) {

View file

@ -1,10 +1,14 @@
'use strict';
const test = require('tape');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const currify = require('currify');
const DIR = '../../server/';
const cloudcmd = require(DIR + 'cloudcmd');
const config = require(DIR + 'config');
const {_authenticate} = cloudcmd;
test('cloudcmd: args: no', (t) => {
const fn = () => cloudcmd();
@ -45,3 +49,63 @@ test('cloudcmd: defaults: console', (t) => {
t.end();
});
test('cloudcmd: authenticate: reject', (t) => {
const success = sinon.stub();
const emit = sinon.stub();
const socket = {
emit,
};
_authenticate(socket, success, 'hello', 'world');
t.ok(emit.calledWith('reject'), 'should reject');
t.end();
});
test('cloudcmd: authenticate: reject: success', (t) => {
const success = sinon.stub();
const emit = sinon.stub();
const socket = {
emit,
};
_authenticate(socket, success, 'hello', 'world');
t.notOk(success.called, 'should not call success');
t.end();
});
test('cloudcmd: authenticate: accept: success', (t) => {
const success = sinon.stub();
const emit = sinon.stub();
const socket = {
emit,
};
const set = credentials();
const reset = set('hello', 'world');
_authenticate(socket, success, 'hello', 'world');
reset();
t.ok(success.called, 'should call success');
t.end();
});
function credentials() {
const username = config('username');
const password = config('password');
const reset = () => {
config('username', username);
config('password', password);
}
const set = currify((fn, a, b) => {
config('username', a);
config('password', b);
return fn;
});
return set(reset);
}