Create a gist now

Instantly share code, notes, and snippets.

@StarpTech /hemera.js forked from dasheck0/hemera.js
Last active Apr 5, 2017

What would you like to do?
/**
* Created by s.neidig on 15/03/17.
*/
const Hemera = require('nats-hemera');
const nats = require('nats');
const HemeraJoi = require('hemera-joi');
const connection = nats.connect('nats://0.0.0.0:4222');
const hemera = new Hemera(connection, {
logLevel: config.logLevel,
crashOnFatal: false
});
hemera.use(HemeraJoi);
module.exports = hemera;
/**
* Created by s.neidig on 13/03/17.
*/
const hemera = require('./hemera');
const worker = require('./worker');
hemera.ready(() => {
hemera.setOption('payloadValidator', 'hemera-joi');
const Joi = hemera.exposition['hemera-joi'].joi;
hemera.add({
topic: 'auth',
cmd: 'encode',
email: Joi.string().required(),
password: Joi.string().required()
}, (req, next) => {
worker.doSomething(req.email, req.password)
.then(token => next(null, { token }))
.catch(error => next(error));
});
});
module.exports = hemera;
/**
* Created by s.neidig on 13/03/17.
* Updated by dustin Deus on 05/04/17.
*/
process.env.NODE_ENV = 'test';
const expect = require('chai').expect;
const timekeeper = require('timekeeper');
const ActStub = require('hemera-testsuite/actStub');
const hemera = require('./hemera');
require('./index.js')
describe('auth:encode', () => {
describe('result', () => {
const actStub = new ActStub(hemera);
before((done) => {
hemera.ready(() => {
hemera.setOption('payloadValidator', 'hemera-joi');
const Joi = hemera.exposition['hemera-joi'].joi;
actStub.stub({
topic: 'auth',
cmd: 'encode',
email: 'stefan@rpdev.net',
password: '12345678'
}, null, {
token: 'foobar'
});
actStub.stub({
topic: 'user',
cmd: 'verify',
email: 'stefan@rpdev.net',
password: '12345678'
}, null, {
uuid: 'b561b127-5bde-4889-a65f-32a0da4a41a1'
});
done();
});
});
after((done) => {
actStub.restore();
hemera.close();
done();
});
it('should verify', (done) => {
hemera.act({
topic: 'user',
cmd: 'verify',
email: 'stefan@rpdev.net',
password: '12345678'
}, (error, response) => {
expect(error).to.not.exist;
expect(response).to.exist;
expect(response).to.have.property('uuid');
expect(response.uuid).to.be.a('string');
done();
});
});
it('should return a token', (done) => {
hemera.act({
topic: 'auth',
cmd: 'encode',
email: 'stefan@rpdev.net',
password: '12345678'
}, (error, response) => {
expect(error).to.not.exist;
expect(response).to.exist;
expect(response).to.have.property('token');
expect(response.token).to.be.a('string');
done();
});
});
});
});
/**
* Created by s.neidig on 13/03/17.
*/
const hemera = require('./hemera');
class Worker {
static doSomething(email, password) {
return new Promise((resolve, reject) => {
hemera.act({
topic: 'user',
cmd: 'verify',
email: email,
password: password
}, (error, response) => {
if (error) {
return reject(error.rootCause);
}
return resolve('sometoken');
});
});
}
}
module.exports = Worker;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment