1
0
Fork 0
mirror of https://github.com/mmumshad/ansible-playable.git synced 2025-03-09 23:38:54 +00:00

Initial Commit

This commit is contained in:
Mumshad Mannambeth 2017-06-07 13:36:44 -04:00
commit c92f737237
273 changed files with 16964 additions and 0 deletions

View file

@ -0,0 +1,814 @@
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/ansible -> index
* POST /api/ansible -> create
* GET /api/ansible/:id -> show
* PUT /api/ansible/:id -> upsert
* PATCH /api/ansible/:id -> patch
* DELETE /api/ansible/:id -> destroy
*/
'use strict';
import jsonpatch from 'fast-json-patch';
import Ansible from './ansible.model';
var ssh2_exec = require('../../components/ssh/ssh2_exec');
var ansibleTool = require('../../components/ansible/ansible_tool');
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if(entity) {
return res.status(statusCode).json(entity);
}
return null;
};
}
function patchUpdates(patches) {
return function(entity) {
try {
// eslint-disable-next-line prefer-reflect
jsonpatch.apply(entity, patches, /*validate*/ true);
} catch(err) {
return Promise.reject(err);
}
return entity.save();
};
}
function removeEntity(res) {
return function(entity) {
if(entity) {
return entity.remove()
.then(() => {
res.status(204).end();
});
}
};
}
function handleEntityNotFound(res) {
return function(entity) {
if(!entity) {
res.status(404).end();
return null;
}
return entity;
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}
// Creates a new Ansible in the DB
export function command(req, res) {
var command = req.body.command;
var ansibleEngine = req.body.ansibleEngine;
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
)
}
// Creates a new Ansible in the DB
export function modules(req, res) {
var ansibleEngine = req.body.ansibleEngine;
ansibleTool.getModules(function(data){
res.write(data)
},
function(data){
res.write(data);
res.end()
},
function(data){
res.write(data)
},
ansibleEngine
);
}
// Gets a single Deploy from the DB
export function getLogs(req, res) {
return Ansible.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(function(entity){
console.log("Getting logs " + entity.logfile);
ansibleTool.getLogs(entity.logfile,
function(successData){
return res.send(successData);
},
function(errorData){
return res.status(500).send(errorData)
}
);
return null;
})
.catch(handleError(res));
}
// Executes Ansible Play book in the backend
export function execute(req, res) {
//var inventory_file_contents = req.body.inventory_file_contents;
//var playbook_file_contents = req.body.playbook_file_contents;
var playbook_name = req.body.selectedPlaybook;
var inventory_file_name = req.body.inventory_file_name;
var tags = req.body.tags;
var limit_to_hosts = req.body.limit_to_hosts;
var verbose = req.body.verbose;
var check_mode = req.body.check_mode;
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
console.log("Check_Mode=" + check_mode);
var time = new Date().getTime();
var logfilename = 'execution_' + time;
var tags_joined = tags;
if(typeof tags === 'object')tags_joined = tags.join(',');
var limit_to_hosts_joined = limit_to_hosts;
if(typeof limit_to_hosts === 'object')limit_to_hosts_joined = limit_to_hosts.join(',');
var ansibleObject = {
logfile: logfilename,
tags: tags_joined,
limit_to_hosts: limit_to_hosts,
verbose: verbose,
host: req.body.host,
check_mode: check_mode,
selectedPlaybook: req.body.selectedPlaybook,
selectedPlay: req.body.selectedPlay,
executionType: req.body.executionType,
executionName: req.body.executionName,
executionTime: time
};
var resultSent = false;
ansibleTool.executeAnsible(logfilename, project_folder, playbook_name, inventory_file_name, tags_joined, limit_to_hosts_joined, verbose,check_mode,
function(data){
//res.write(data)
if(!resultSent){
resultSent = true;
return Ansible.create(ansibleObject)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
},
function(data){
//res.write(data);
//res.end()
if(!resultSent){
resultSent = true;
return Ansible.create(ansibleObject)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
},
function(data){
//res.write(data)
if(!resultSent){
resultSent = true;
res.status(500).send(data)
}
},
ansibleEngine
);
}
/**
* List playbook tags
* ansible-playbook playbook.yml -i inventory --list-tags
* @param req
* @param res
*/
export function playbook_tags_list(req, res) {
var playbook_name = req.body.selectedPlaybook;
var inventory_file_name = req.body.inventory_file_name;
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
ansibleTool.getTagList(project_folder, playbook_name, inventory_file_name,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function playbook_create(req, res) {
var playbook_file_contents = req.body.playbookFileContents;
var ansibleEngine = req.body.ansibleEngine;
var play_book_name = req.body.playbookName;
var project_folder = ansibleEngine.projectFolder;
play_book_name = play_book_name.replace(project_folder,'');
console.log("Playbook name = " + play_book_name);
var resultSent = false;
ansibleTool.writePlaybook(project_folder,play_book_name,playbook_file_contents,
function(data){
//res.write(data);
//res.end()
if(!resultSent){
resultSent = true;
res.send(data)
}
},
function(data){
//res.write(data)
if(!resultSent){
resultSent = true;
res.status(500).send(data)
}
},
ansibleEngine
);
}
export function playbook_delete(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var play_book_name = req.body.playbookName;
var project_folder = ansibleEngine.projectFolder;
var resultSent = false;
ansibleTool.deletePlaybook(project_folder,play_book_name,
function(data){
res.write(data)
},
function(data){
if(!resultSent){
resultSent = true;
res.write(data);
res.end();
}
},
function(data){
if(!resultSent){
resultSent = true;
res.status(500);
res.write(data);
res.end();
}
},
ansibleEngine
);
}
export function playbook_get(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var play_book_name = req.body.playbookName;
var project_folder = ansibleEngine.projectFolder;
var resultSent = false;
ansibleTool.readPlaybook(project_folder,play_book_name,
function(data){
res.write(data)
},
function(data){
if(!resultSent){
resultSent = true;
res.write(data);
res.end();
}
},
function(data){
if(!resultSent){
resultSent = true;
res.status(500);
res.write(data);
res.end();
}
},
ansibleEngine
);
}
export function playbook_list(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
ansibleTool.getPlaybookList(project_folder,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function roles_list(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
ansibleTool.getRolesList(project_folder,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function inventory_list(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
ansibleTool.getInventoryList(project_folder,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function inventory_get(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var project_folder = ansibleEngine.projectFolder;
var inventoryName = req.body.inventoryName;
ansibleTool.readInventoryFile(project_folder,inventoryName,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function inventory_create(req, res) {
var inventoryFileContents = req.body.inventoryFileContents;
var ansibleEngine = req.body.ansibleEngine;
var inventoryName = req.body.inventoryName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/' + inventoryName;
ansibleTool.writeFile(file_path,inventoryFileContents,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function inventory_delete(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var inventoryName = req.body.inventoryName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/' + inventoryName;
ansibleTool.deleteFile(file_path,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function update_groups_vars_file(req, res) {
var groupVarsContents = req.body.groupVarsContents;
var ansibleEngine = req.body.ansibleEngine;
var groupName = req.body.groupName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/group_vars/' + groupName;
ansibleTool.writeFile(file_path, groupVarsContents,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function get_groups_vars_file(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var groupName = req.body.groupName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/group_vars/' + groupName;
ansibleTool.readFile(file_path,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function update_hosts_vars_file(req, res) {
var hostVarsContents = req.body.hostVarsContents;
var ansibleEngine = req.body.ansibleEngine;
var hostName = req.body.hostName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/host_vars/' + hostName;
ansibleTool.writeFile(file_path, hostVarsContents,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
export function get_hosts_vars_file(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var hostName = req.body.hostName;
var project_folder = ansibleEngine.projectFolder;
var file_path = project_folder + '/host_vars/' + hostName;
ansibleTool.readFile(file_path,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
}
/**
* Get variables for a host using Python AnsibleAPI
* @param req
* @param res
*/
export function get_hosts_vars(req,res){
var ansibleEngine = req.body.ansibleEngine;
var host_name = req.body.hostName;
var project_folder = ansibleEngine.projectFolder;
var inventory_file_name = req.body.inventoryFileName;
console.log('hostName=' + host_name)
ansibleTool.getVars(project_folder,inventory_file_name,host_name,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine)
}
/**
* Get variables for a role using Python AnsibleAPI
* @param req
* @param res
*/
export function get_roles_vars(req,res){
var ansibleEngine = req.body.ansibleEngine;
var role_name = req.body.roleName;
var project_folder = ansibleEngine.projectFolder;
console.log('roleName=' + role_name);
ansibleTool.getRolesVars(project_folder,role_name,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine)
}
export function roles_search_galaxy(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var searchText = req.body.searchText;
ansibleTool.searchRolesGalaxy(searchText,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function roles_search_github(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var searchText = req.body.searchText;
ansibleTool.searchRolesGithub(searchText,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
/**
* Create/Copy Role
* Create a new role if selectedRoleName is null
* Copy existing role if selectedRoleName is not null
* @param req
* @param res
*/
export function roles_create(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var roleName = req.body.roleName;
var selectedRoleName = req.body.selectedRoleName;
var createRoleFunction = ansibleTool.createRole;
if(selectedRoleName)
createRoleFunction = ansibleTool.copyRole;
createRoleFunction(roleName,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine,
selectedRoleName
);
}
export function roles_import(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var roleType = req.body.roleType;
var roleNameUri = req.body.roleNameUri;
ansibleTool.importRole(roleType,roleNameUri,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function roles_delete(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var roleName = req.body.roleName;
ansibleTool.deleteRole(roleName,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function roles_files(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var roleName = req.body.roleName;
ansibleTool.getRoleFiles(roleName,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function project_files(req, res) {
var ansibleEngine = req.body.ansibleEngine;
ansibleTool.getProjectFiles(
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function file_create(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var fileAbsolutePath = req.body.fileAbsolutePath;
ansibleTool.createFile(fileAbsolutePath,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function file_update(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var fileAbsolutePath = req.body.fileAbsolutePath;
var fileContents = req.body.fileContents;
ansibleTool.writeFile(fileAbsolutePath,fileContents,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
export function file_delete(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var fileAbsolutePath = req.body.fileAbsolutePath;
ansibleTool.deleteFile(fileAbsolutePath,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data);
},
ansibleEngine
);
}
// Gets a list of Ansibles
export function index(req, res) {
return Ansible.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Gets a single Ansible from the DB
export function show(req, res) {
return Ansible.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Creates a new Ansible in the DB
export function create(req, res) {
return Ansible.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
// Upserts the given Ansible in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Ansible.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing Ansible in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Ansible.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a Ansible from the DB
export function destroy(req, res) {
return Ansible.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -0,0 +1,35 @@
/**
* Ansible model events
*/
'use strict';
import {EventEmitter} from 'events';
var AnsibleEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
AnsibleEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(Ansible) {
for(var e in events) {
let event = events[e];
Ansible.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
AnsibleEvents.emit(event + ':' + doc._id, doc);
AnsibleEvents.emit(event, doc);
};
}
export {registerEvents};
export default AnsibleEvents;

View file

@ -0,0 +1,190 @@
'use strict';
/* globals describe, expect, it, beforeEach, afterEach */
var app = require('../..');
import request from 'supertest';
var newAnsible;
describe('Ansible API:', function() {
describe('GET /api/ansible', function() {
var ansibles;
beforeEach(function(done) {
request(app)
.get('/api/ansible')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
ansibles = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(ansibles).to.be.instanceOf(Array);
});
});
describe('POST /api/ansible', function() {
beforeEach(function(done) {
request(app)
.post('/api/ansible')
.send({
name: 'New Ansible',
info: 'This is the brand new ansible!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newAnsible = res.body;
done();
});
});
it('should respond with the newly created ansible', function() {
expect(newAnsible.name).to.equal('New Ansible');
expect(newAnsible.info).to.equal('This is the brand new ansible!!!');
});
});
describe('GET /api/ansible/:id', function() {
var ansible;
beforeEach(function(done) {
request(app)
.get(`/api/ansible/${newAnsible._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
ansible = res.body;
done();
});
});
afterEach(function() {
ansible = {};
});
it('should respond with the requested ansible', function() {
expect(ansible.name).to.equal('New Ansible');
expect(ansible.info).to.equal('This is the brand new ansible!!!');
});
});
describe('PUT /api/ansible/:id', function() {
var updatedAnsible;
beforeEach(function(done) {
request(app)
.put(`/api/ansible/${newAnsible._id}`)
.send({
name: 'Updated Ansible',
info: 'This is the updated ansible!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedAnsible = res.body;
done();
});
});
afterEach(function() {
updatedAnsible = {};
});
it('should respond with the updated ansible', function() {
expect(updatedAnsible.name).to.equal('Updated Ansible');
expect(updatedAnsible.info).to.equal('This is the updated ansible!!!');
});
it('should respond with the updated ansible on a subsequent GET', function(done) {
request(app)
.get(`/api/ansible/${newAnsible._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let ansible = res.body;
expect(ansible.name).to.equal('Updated Ansible');
expect(ansible.info).to.equal('This is the updated ansible!!!');
done();
});
});
});
describe('PATCH /api/ansible/:id', function() {
var patchedAnsible;
beforeEach(function(done) {
request(app)
.patch(`/api/ansible/${newAnsible._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched Ansible' },
{ op: 'replace', path: '/info', value: 'This is the patched ansible!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patchedAnsible = res.body;
done();
});
});
afterEach(function() {
patchedAnsible = {};
});
it('should respond with the patched ansible', function() {
expect(patchedAnsible.name).to.equal('Patched Ansible');
expect(patchedAnsible.info).to.equal('This is the patched ansible!!!');
});
});
describe('DELETE /api/ansible/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/ansible/${newAnsible._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
it('should respond with 404 when ansible does not exist', function(done) {
request(app)
.delete(`/api/ansible/${newAnsible._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -0,0 +1,24 @@
'use strict';
import mongoose from 'mongoose';
import {registerEvents} from './ansible.events';
var AnsibleSchema = new mongoose.Schema({
name: String,
info: String,
active: Boolean,
logfile: String,
tags: String,
limit_to_hosts: String,
host: String,
verbose: String,
check_mode: Boolean,
selectedPlaybook: String,
selectedPlay: String,
executionType: String,
executionName: String,
executionTime: Date
});
registerEvents(AnsibleSchema);
export default mongoose.model('Ansible', AnsibleSchema);

View file

@ -0,0 +1,59 @@
'use strict';
var express = require('express');
var controller = require('./ansible.controller');
var router = express.Router();
router.get('/', controller.index);
router.post('/modules', controller.modules);
router.post('/command', controller.command);
router.post('/execute', controller.execute);
router.post('/project/files', controller.project_files);
router.post('/playbook/get', controller.playbook_get);
router.post('/playbook/create', controller.playbook_create);
router.post('/playbook/delete', controller.playbook_delete);
router.post('/playbook/list', controller.playbook_list);
router.post('/roles/create', controller.roles_create);
router.post('/roles/list', controller.roles_list);
router.post('/roles/search/galaxy', controller.roles_search_galaxy);
router.post('/roles/search/github', controller.roles_search_github);
router.post('/roles/delete', controller.roles_delete);
router.post('/roles/files', controller.roles_files);
router.post('/roles/import', controller.roles_import);
router.post('/tags/list', controller.playbook_tags_list);
router.post('/files/create', controller.file_create);
router.post('/files/update', controller.file_update);
router.post('/files/delete', controller.file_delete);
router.post('/inventory/list', controller.inventory_list);
router.post('/inventory/get', controller.inventory_get);
router.post('/inventory/create', controller.inventory_create);
router.post('/inventory/delete', controller.inventory_delete);
router.post('/vars_file/groups/update', controller.update_groups_vars_file);
router.post('/vars_file/groups/get', controller.get_groups_vars_file);
router.post('/vars_file/hosts/update', controller.update_hosts_vars_file);
router.post('/vars_file/hosts/get', controller.get_hosts_vars_file);
router.post('/vars/hosts/get', controller.get_hosts_vars);
router.post('/vars/roles/get', controller.get_roles_vars);
router.get('/logs/:id', controller.getLogs);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -0,0 +1,86 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var ansibleCtrlStub = {
index: 'ansibleCtrl.index',
show: 'ansibleCtrl.show',
create: 'ansibleCtrl.create',
upsert: 'ansibleCtrl.upsert',
patch: 'ansibleCtrl.patch',
destroy: 'ansibleCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var ansibleIndex = proxyquire('./index.js', {
express: {
Router() {
return routerStub;
}
},
'./ansible.controller': ansibleCtrlStub
});
describe('Ansible API Router:', function() {
it('should return an express router instance', function() {
expect(ansibleIndex).to.equal(routerStub);
});
describe('GET /api/ansible', function() {
it('should route to ansible.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'ansibleCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/ansible/:id', function() {
it('should route to ansible.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'ansibleCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/ansible', function() {
it('should route to ansible.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'ansibleCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/ansible/:id', function() {
it('should route to ansible.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'ansibleCtrl.upsert')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/ansible/:id', function() {
it('should route to ansible.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'ansibleCtrl.patch')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/ansible/:id', function() {
it('should route to ansible.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'ansibleCtrl.destroy')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,220 @@
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/custom_modules -> index
* POST /api/custom_modules -> create
* GET /api/custom_modules/:id -> show
* PUT /api/custom_modules/:id -> upsert
* PATCH /api/custom_modules/:id -> patch
* DELETE /api/custom_modules/:id -> destroy
*/
'use strict';
import jsonpatch from 'fast-json-patch';
import CustomModule from './custom_module.model';
var ssh2_exec = require('../../components/ssh/ssh2_exec');
var scp2_exec = require('../../components/scp/scp_exec');
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if(entity) {
return res.status(statusCode).json(entity);
}
return null;
};
}
function patchUpdates(patches) {
return function(entity) {
try {
// eslint-disable-next-line prefer-reflect
jsonpatch.apply(entity, patches, /*validate*/ true);
} catch(err) {
return Promise.reject(err);
}
return entity.save();
};
}
function removeEntity(res) {
return function(entity) {
if(entity) {
return entity.remove()
.then(() => {
res.status(204).end();
});
}
};
}
function handleEntityNotFound(res) {
return function(entity) {
if(!entity) {
res.status(404).end();
return null;
}
return entity;
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}
// Gets a list of CustomModules
export function index(req, res) {
var ansibleEngine = req.body.ansibleEngine;
if(!ansibleEngine.customModules){
return res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
}
var command = 'ls "' + ansibleEngine.customModules + '"';
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data)
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
/*return CustomModule.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));*/
}
// Gets a single CustomModule from the DB
export function show(req, res) {
console.log("Show " + req.params.custom_module);
var ansibleEngine = req.body.ansibleEngine;
if(!ansibleEngine.customModules){
res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
}
var command = 'cat "' + ansibleEngine.customModules + '"/' + req.params.custom_module;
if(req.params.custom_module === 'template.py'){
command = 'cat ' + '/opt/ehc-builder-scripts/ansible_modules/template.py';
}
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
/*return CustomModule.findById(req.params.custom_module).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));*/
}
// Test Module
export function testModule(req, res) {
var ansibleEngine = req.body.ansibleEngine;
var moduleArgs = req.body.moduleArgs;
if(!ansibleEngine.customModules){
res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
}
var command = '/opt/ansible/ansible-devel/hacking/test-module -m "' + ansibleEngine.customModules + '/' + req.params.custom_module + "\" -a '" + JSON.stringify(moduleArgs) + "'";
console.log("Command=" + command);
ssh2_exec.executeCommand(command,
null,
function(data){
res.send(data);
},
function(data){
res.status(500).send(data)
},
ansibleEngine
);
/*return CustomModule.findById(req.params.custom_module).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));*/
}
// Creates a new CustomModule in the DB
export function create(req, res) {
console.log("Create");
var custom_module_name = req.params.custom_module;
var custom_module_code = req.body.custom_module_code;
var ansibleEngine = req.body.ansibleEngine;
if(!ansibleEngine.customModules){
res.status(500).send("Custom Modules Folder not defined in Ansible Engine")
}
console.log("Custom module name " + "\"" + ansibleEngine.customModules + '/' + custom_module_name + "\"")
scp2_exec.createFileOnScriptEngine(custom_module_code,ansibleEngine.customModules + '/' + custom_module_name,
function(){
res.send("Saved")
},function(err){
res.status(500).send("Failed to create file on target")
},
ansibleEngine
);
/*return CustomModule.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));*/
}
// Upserts the given CustomModule in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return CustomModule.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing CustomModule in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return CustomModule.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a CustomModule from the DB
export function destroy(req, res) {
return CustomModule.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -0,0 +1,35 @@
/**
* CustomModule model events
*/
'use strict';
import {EventEmitter} from 'events';
var CustomModuleEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
CustomModuleEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(CustomModule) {
for(var e in events) {
let event = events[e];
CustomModule.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
CustomModuleEvents.emit(event + ':' + doc._id, doc);
CustomModuleEvents.emit(event, doc);
};
}
export {registerEvents};
export default CustomModuleEvents;

View file

@ -0,0 +1,190 @@
'use strict';
/* globals describe, expect, it, beforeEach, afterEach */
var app = require('../..');
import request from 'supertest';
var newCustomModule;
describe('CustomModule API:', function() {
describe('GET /api/custom_modules', function() {
var customModules;
beforeEach(function(done) {
request(app)
.get('/api/custom_modules')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
customModules = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(customModules).to.be.instanceOf(Array);
});
});
describe('POST /api/custom_modules', function() {
beforeEach(function(done) {
request(app)
.post('/api/custom_modules')
.send({
name: 'New CustomModule',
info: 'This is the brand new customModule!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newCustomModule = res.body;
done();
});
});
it('should respond with the newly created customModule', function() {
expect(newCustomModule.name).to.equal('New CustomModule');
expect(newCustomModule.info).to.equal('This is the brand new customModule!!!');
});
});
describe('GET /api/custom_modules/:id', function() {
var customModule;
beforeEach(function(done) {
request(app)
.get(`/api/custom_modules/${newCustomModule._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
customModule = res.body;
done();
});
});
afterEach(function() {
customModule = {};
});
it('should respond with the requested customModule', function() {
expect(customModule.name).to.equal('New CustomModule');
expect(customModule.info).to.equal('This is the brand new customModule!!!');
});
});
describe('PUT /api/custom_modules/:id', function() {
var updatedCustomModule;
beforeEach(function(done) {
request(app)
.put(`/api/custom_modules/${newCustomModule._id}`)
.send({
name: 'Updated CustomModule',
info: 'This is the updated customModule!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedCustomModule = res.body;
done();
});
});
afterEach(function() {
updatedCustomModule = {};
});
it('should respond with the updated customModule', function() {
expect(updatedCustomModule.name).to.equal('Updated CustomModule');
expect(updatedCustomModule.info).to.equal('This is the updated customModule!!!');
});
it('should respond with the updated customModule on a subsequent GET', function(done) {
request(app)
.get(`/api/custom_modules/${newCustomModule._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let customModule = res.body;
expect(customModule.name).to.equal('Updated CustomModule');
expect(customModule.info).to.equal('This is the updated customModule!!!');
done();
});
});
});
describe('PATCH /api/custom_modules/:id', function() {
var patchedCustomModule;
beforeEach(function(done) {
request(app)
.patch(`/api/custom_modules/${newCustomModule._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched CustomModule' },
{ op: 'replace', path: '/info', value: 'This is the patched customModule!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patchedCustomModule = res.body;
done();
});
});
afterEach(function() {
patchedCustomModule = {};
});
it('should respond with the patched customModule', function() {
expect(patchedCustomModule.name).to.equal('Patched CustomModule');
expect(patchedCustomModule.info).to.equal('This is the patched customModule!!!');
});
});
describe('DELETE /api/custom_modules/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/custom_modules/${newCustomModule._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
it('should respond with 404 when customModule does not exist', function(done) {
request(app)
.delete(`/api/custom_modules/${newCustomModule._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -0,0 +1,13 @@
'use strict';
import mongoose from 'mongoose';
import {registerEvents} from './custom_module.events';
var CustomModuleSchema = new mongoose.Schema({
name: String,
info: String,
active: Boolean
});
registerEvents(CustomModuleSchema);
export default mongoose.model('CustomModule', CustomModuleSchema);

View file

@ -0,0 +1,16 @@
'use strict';
var express = require('express');
var controller = require('./custom_module.controller');
var router = express.Router();
router.post('/query', controller.index);
router.post('/:custom_module/test', controller.testModule);
router.post('/:custom_module/get', controller.show);
router.post('/:custom_module', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -0,0 +1,86 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var customModuleCtrlStub = {
index: 'customModuleCtrl.index',
show: 'customModuleCtrl.show',
create: 'customModuleCtrl.create',
upsert: 'customModuleCtrl.upsert',
patch: 'customModuleCtrl.patch',
destroy: 'customModuleCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var customModuleIndex = proxyquire('./index.js', {
express: {
Router() {
return routerStub;
}
},
'./custom_module.controller': customModuleCtrlStub
});
describe('CustomModule API Router:', function() {
it('should return an express router instance', function() {
expect(customModuleIndex).to.equal(routerStub);
});
describe('GET /api/custom_modules', function() {
it('should route to customModule.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'customModuleCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/custom_modules/:id', function() {
it('should route to customModule.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'customModuleCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/custom_modules', function() {
it('should route to customModule.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'customModuleCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/custom_modules/:id', function() {
it('should route to customModule.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'customModuleCtrl.upsert')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/custom_modules/:id', function() {
it('should route to customModule.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'customModuleCtrl.patch')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/custom_modules/:id', function() {
it('should route to customModule.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'customModuleCtrl.destroy')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,15 @@
'use strict';
var express = require('express');
var controller = require('./project.controller');
var router = express.Router();
router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -0,0 +1,86 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var projectCtrlStub = {
index: 'projectCtrl.index',
show: 'projectCtrl.show',
create: 'projectCtrl.create',
upsert: 'projectCtrl.upsert',
patch: 'projectCtrl.patch',
destroy: 'projectCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var projectIndex = proxyquire('./index.js', {
express: {
Router() {
return routerStub;
}
},
'./project.controller': projectCtrlStub
});
describe('Project API Router:', function() {
it('should return an express router instance', function() {
expect(projectIndex).to.equal(routerStub);
});
describe('GET /api/projects', function() {
it('should route to project.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'projectCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/projects/:id', function() {
it('should route to project.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'projectCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/projects', function() {
it('should route to project.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'projectCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/projects/:id', function() {
it('should route to project.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'projectCtrl.upsert')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/projects/:id', function() {
it('should route to project.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'projectCtrl.patch')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/projects/:id', function() {
it('should route to project.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'projectCtrl.destroy')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,152 @@
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/projects -> index
* POST /api/projects -> create
* GET /api/projects/:id -> show
* PUT /api/projects/:id -> upsert
* PATCH /api/projects/:id -> patch
* DELETE /api/projects/:id -> destroy
*/
'use strict';
import jsonpatch from 'fast-json-patch';
import Project from './project.model';
var ansibleTool = require('../../components/ansible/ansible_tool');
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if(entity) {
return res.status(statusCode).json(entity);
}
return null;
};
}
function patchUpdates(patches) {
return function(entity) {
try {
// eslint-disable-next-line prefer-reflect
jsonpatch.apply(entity, patches, /*validate*/ true);
} catch(err) {
return Promise.reject(err);
}
return entity.save();
};
}
function removeEntity(res) {
return function(entity) {
if(entity) {
return entity.remove()
.then(() => {
res.status(204).end();
});
}
};
}
function handleEntityNotFound(res) {
return function(entity) {
if(!entity) {
res.status(404).end();
return null;
}
return entity;
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}
// Gets a list of Projects
export function index(req, res) {
console.log("Getting projects list");
return Project.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Gets a single Project from the DB
export function show(req, res) {
return Project.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Creates a new Project in the DB
export function create(req, res) {
var ansibleEngine = req.body.ansibleEngine;
console.log("Ansible Engine " + JSON.stringify(ansibleEngine));
if(ansibleEngine.ansibleHost){
ansibleTool.getAnsibleVersion(
function(version){
req.body.ansibleVersion = version;
ansibleTool.createProjectFolder(ansibleEngine.projectFolder,
function(){
return Project.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
},
function(data){
res.status(500).send(data)
}, ansibleEngine);
//res.write(data);
//res.end()
},
function(data){
res.status(500).send("" + data);
},ansibleEngine
)
}else{
return Project.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
}
// Upserts the given Project in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Project.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing Project in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Project.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a Project from the DB
export function destroy(req, res) {
return Project.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -0,0 +1,35 @@
/**
* Project model events
*/
'use strict';
import {EventEmitter} from 'events';
var ProjectEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
ProjectEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(Project) {
for(var e in events) {
let event = events[e];
Project.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
ProjectEvents.emit(event + ':' + doc._id, doc);
ProjectEvents.emit(event, doc);
};
}
export {registerEvents};
export default ProjectEvents;

View file

@ -0,0 +1,190 @@
'use strict';
/* globals describe, expect, it, beforeEach, afterEach */
var app = require('../..');
import request from 'supertest';
var newProject;
describe('Project API:', function() {
describe('GET /api/projects', function() {
var projects;
beforeEach(function(done) {
request(app)
.get('/api/projects')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
projects = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(projects).to.be.instanceOf(Array);
});
});
describe('POST /api/projects', function() {
beforeEach(function(done) {
request(app)
.post('/api/projects')
.send({
name: 'New Project',
info: 'This is the brand new project!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newProject = res.body;
done();
});
});
it('should respond with the newly created project', function() {
expect(newProject.name).to.equal('New Project');
expect(newProject.info).to.equal('This is the brand new project!!!');
});
});
describe('GET /api/projects/:id', function() {
var project;
beforeEach(function(done) {
request(app)
.get(`/api/projects/${newProject._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
project = res.body;
done();
});
});
afterEach(function() {
project = {};
});
it('should respond with the requested project', function() {
expect(project.name).to.equal('New Project');
expect(project.info).to.equal('This is the brand new project!!!');
});
});
describe('PUT /api/projects/:id', function() {
var updatedProject;
beforeEach(function(done) {
request(app)
.put(`/api/projects/${newProject._id}`)
.send({
name: 'Updated Project',
info: 'This is the updated project!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedProject = res.body;
done();
});
});
afterEach(function() {
updatedProject = {};
});
it('should respond with the updated project', function() {
expect(updatedProject.name).to.equal('Updated Project');
expect(updatedProject.info).to.equal('This is the updated project!!!');
});
it('should respond with the updated project on a subsequent GET', function(done) {
request(app)
.get(`/api/projects/${newProject._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let project = res.body;
expect(project.name).to.equal('Updated Project');
expect(project.info).to.equal('This is the updated project!!!');
done();
});
});
});
describe('PATCH /api/projects/:id', function() {
var patchedProject;
beforeEach(function(done) {
request(app)
.patch(`/api/projects/${newProject._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched Project' },
{ op: 'replace', path: '/info', value: 'This is the patched project!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patchedProject = res.body;
done();
});
});
afterEach(function() {
patchedProject = {};
});
it('should respond with the patched project', function() {
expect(patchedProject.name).to.equal('Patched Project');
expect(patchedProject.info).to.equal('This is the patched project!!!');
});
});
describe('DELETE /api/projects/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/projects/${newProject._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
it('should respond with 404 when project does not exist', function(done) {
request(app)
.delete(`/api/projects/${newProject._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -0,0 +1,22 @@
'use strict';
import mongoose from 'mongoose';
import {registerEvents} from './project.events';
var ProjectSchema = new mongoose.Schema({
name: String,
ansibleEngine: {},
ansibleVersion : String,
creationTime: Date,
info: String,
active: Boolean,
ansible_data: String, //YAML Format
ansible_data_json: {}, //JSON Format
inventory_data: String, //YAML Format
inventory_data_json: {}, //JSON Format
roles_data: String, //YAML Format
roles_data_json: {} //JSON Format
});
registerEvents(ProjectSchema);
export default mongoose.model('Project', ProjectSchema);

15
server/api/thing/index.js Normal file
View file

@ -0,0 +1,15 @@
'use strict';
var express = require('express');
var controller = require('./thing.controller');
var router = express.Router();
router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);
module.exports = router;

View file

@ -0,0 +1,86 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var thingCtrlStub = {
index: 'thingCtrl.index',
show: 'thingCtrl.show',
create: 'thingCtrl.create',
upsert: 'thingCtrl.upsert',
patch: 'thingCtrl.patch',
destroy: 'thingCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var thingIndex = proxyquire('./index.js', {
express: {
Router() {
return routerStub;
}
},
'./thing.controller': thingCtrlStub
});
describe('Thing API Router:', function() {
it('should return an express router instance', function() {
expect(thingIndex).to.equal(routerStub);
});
describe('GET /api/things', function() {
it('should route to thing.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'thingCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/things/:id', function() {
it('should route to thing.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'thingCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/things', function() {
it('should route to thing.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'thingCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/things/:id', function() {
it('should route to thing.controller.upsert', function() {
expect(routerStub.put
.withArgs('/:id', 'thingCtrl.upsert')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/things/:id', function() {
it('should route to thing.controller.patch', function() {
expect(routerStub.patch
.withArgs('/:id', 'thingCtrl.patch')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/things/:id', function() {
it('should route to thing.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'thingCtrl.destroy')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,118 @@
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/things -> index
* POST /api/things -> create
* GET /api/things/:id -> show
* PUT /api/things/:id -> upsert
* PATCH /api/things/:id -> patch
* DELETE /api/things/:id -> destroy
*/
'use strict';
import jsonpatch from 'fast-json-patch';
import Thing from './thing.model';
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if(entity) {
return res.status(statusCode).json(entity);
}
return null;
};
}
function patchUpdates(patches) {
return function(entity) {
try {
// eslint-disable-next-line prefer-reflect
jsonpatch.apply(entity, patches, /*validate*/ true);
} catch(err) {
return Promise.reject(err);
}
return entity.save();
};
}
function removeEntity(res) {
return function(entity) {
if(entity) {
return entity.remove()
.then(() => {
res.status(204).end();
});
}
};
}
function handleEntityNotFound(res) {
return function(entity) {
if(!entity) {
res.status(404).end();
return null;
}
return entity;
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}
// Gets a list of Things
export function index(req, res) {
return Thing.find().exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Gets a single Thing from the DB
export function show(req, res) {
return Thing.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Creates a new Thing in the DB
export function create(req, res) {
return Thing.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
// Upserts the given Thing in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Thing.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Updates an existing Thing in the DB
export function patch(req, res) {
if(req.body._id) {
Reflect.deleteProperty(req.body, '_id');
}
return Thing.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a Thing from the DB
export function destroy(req, res) {
return Thing.findById(req.params.id).exec()
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -0,0 +1,35 @@
/**
* Thing model events
*/
'use strict';
import {EventEmitter} from 'events';
var ThingEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
ThingEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(Thing) {
for(var e in events) {
let event = events[e];
Thing.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
ThingEvents.emit(`${event}:${doc._id}`, doc);
ThingEvents.emit(event, doc);
};
}
export {registerEvents};
export default ThingEvents;

View file

@ -0,0 +1,190 @@
'use strict';
/* globals describe, expect, it, beforeEach, afterEach */
var app = require('../..');
import request from 'supertest';
var newThing;
describe('Thing API:', function() {
describe('GET /api/things', function() {
var things;
beforeEach(function(done) {
request(app)
.get('/api/things')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
things = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(things).to.be.instanceOf(Array);
});
});
describe('POST /api/things', function() {
beforeEach(function(done) {
request(app)
.post('/api/things')
.send({
name: 'New Thing',
info: 'This is the brand new thing!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newThing = res.body;
done();
});
});
it('should respond with the newly created thing', function() {
expect(newThing.name).to.equal('New Thing');
expect(newThing.info).to.equal('This is the brand new thing!!!');
});
});
describe('GET /api/things/:id', function() {
var thing;
beforeEach(function(done) {
request(app)
.get(`/api/things/${newThing._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
thing = res.body;
done();
});
});
afterEach(function() {
thing = {};
});
it('should respond with the requested thing', function() {
expect(thing.name).to.equal('New Thing');
expect(thing.info).to.equal('This is the brand new thing!!!');
});
});
describe('PUT /api/things/:id', function() {
var updatedThing;
beforeEach(function(done) {
request(app)
.put(`/api/things/${newThing._id}`)
.send({
name: 'Updated Thing',
info: 'This is the updated thing!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
updatedThing = res.body;
done();
});
});
afterEach(function() {
updatedThing = {};
});
it('should respond with the updated thing', function() {
expect(updatedThing.name).to.equal('Updated Thing');
expect(updatedThing.info).to.equal('This is the updated thing!!!');
});
it('should respond with the updated thing on a subsequent GET', function(done) {
request(app)
.get(`/api/things/${newThing._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let thing = res.body;
expect(thing.name).to.equal('Updated Thing');
expect(thing.info).to.equal('This is the updated thing!!!');
done();
});
});
});
describe('PATCH /api/things/:id', function() {
var patchedThing;
beforeEach(function(done) {
request(app)
.patch(`/api/things/${newThing._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched Thing' },
{ op: 'replace', path: '/info', value: 'This is the patched thing!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patchedThing = res.body;
done();
});
});
afterEach(function() {
patchedThing = {};
});
it('should respond with the patched thing', function() {
expect(patchedThing.name).to.equal('Patched Thing');
expect(patchedThing.info).to.equal('This is the patched thing!!!');
});
});
describe('DELETE /api/things/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/things/${newThing._id}`)
.expect(204)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
it('should respond with 404 when thing does not exist', function(done) {
request(app)
.delete(`/api/things/${newThing._id}`)
.expect(404)
.end(err => {
if(err) {
return done(err);
}
done();
});
});
});
});

View file

@ -0,0 +1,13 @@
'use strict';
import mongoose from 'mongoose';
import {registerEvents} from './thing.events';
var ThingSchema = new mongoose.Schema({
name: String,
info: String,
active: Boolean
});
registerEvents(ThingSchema);
export default mongoose.model('Thing', ThingSchema);

16
server/api/user/index.js Normal file
View file

@ -0,0 +1,16 @@
'use strict';
import {Router} from 'express';
import * as controller from './user.controller';
import * as auth from '../../auth/auth.service';
var router = new Router();
router.get('/', auth.hasRole('admin'), controller.index);
router.delete('/:id', auth.hasRole('admin'), controller.destroy);
router.get('/me', auth.isAuthenticated(), controller.me);
router.put('/:id/password', auth.isAuthenticated(), controller.changePassword);
router.get('/:id', auth.isAuthenticated(), controller.show);
router.post('/', controller.create);
module.exports = router;

View file

@ -0,0 +1,95 @@
'use strict';
/* globals sinon, describe, expect, it */
var proxyquire = require('proxyquire').noPreserveCache();
var userCtrlStub = {
index: 'userCtrl.index',
destroy: 'userCtrl.destroy',
me: 'userCtrl.me',
changePassword: 'userCtrl.changePassword',
show: 'userCtrl.show',
create: 'userCtrl.create'
};
var authServiceStub = {
isAuthenticated() {
return 'authService.isAuthenticated';
},
hasRole(role) {
return `authService.hasRole.${role}`;
}
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var userIndex = proxyquire('./index', {
express: {
Router() {
return routerStub;
}
},
'./user.controller': userCtrlStub,
'../../auth/auth.service': authServiceStub
});
describe('User API Router:', function() {
it('should return an express router instance', function() {
expect(userIndex).to.equal(routerStub);
});
describe('GET /api/users', function() {
it('should verify admin role and route to user.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/users/:id', function() {
it('should verify admin role and route to user.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy')
).to.have.been.calledOnce;
});
});
describe('GET /api/users/me', function() {
it('should be authenticated and route to user.controller.me', function() {
expect(routerStub.get
.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me')
).to.have.been.calledOnce;
});
});
describe('PUT /api/users/:id/password', function() {
it('should be authenticated and route to user.controller.changePassword', function() {
expect(routerStub.put
.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword')
).to.have.been.calledOnce;
});
});
describe('GET /api/users/:id', function() {
it('should be authenticated and route to user.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/users', function() {
it('should route to user.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'userCtrl.create')
).to.have.been.calledOnce;
});
});
});

View file

@ -0,0 +1,122 @@
'use strict';
import User from './user.model';
import config from '../../config/environment';
import jwt from 'jsonwebtoken';
function validationError(res, statusCode) {
statusCode = statusCode || 422;
return function(err) {
return res.status(statusCode).json(err);
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
return res.status(statusCode).send(err);
};
}
/**
* Get list of users
* restriction: 'admin'
*/
export function index(req, res) {
return User.find({}, '-salt -password').exec()
.then(users => {
res.status(200).json(users);
})
.catch(handleError(res));
}
/**
* Creates a new user
*/
export function create(req, res) {
var newUser = new User(req.body);
newUser.provider = 'local';
newUser.role = 'user';
newUser.save()
.then(function(user) {
var token = jwt.sign({ _id: user._id }, config.secrets.session, {
expiresIn: 60 * 60 * 5
});
res.json({ token });
})
.catch(validationError(res));
}
/**
* Get a single user
*/
export function show(req, res, next) {
var userId = req.params.id;
return User.findById(userId).exec()
.then(user => {
if(!user) {
return res.status(404).end();
}
res.json(user.profile);
})
.catch(err => next(err));
}
/**
* Deletes a user
* restriction: 'admin'
*/
export function destroy(req, res) {
return User.findByIdAndRemove(req.params.id).exec()
.then(function() {
res.status(204).end();
})
.catch(handleError(res));
}
/**
* Change a users password
*/
export function changePassword(req, res) {
var userId = req.user._id;
var oldPass = String(req.body.oldPassword);
var newPass = String(req.body.newPassword);
return User.findById(userId).exec()
.then(user => {
if(user.authenticate(oldPass)) {
user.password = newPass;
return user.save()
.then(() => {
res.status(204).end();
})
.catch(validationError(res));
} else {
return res.status(403).end();
}
});
}
/**
* Get my info
*/
export function me(req, res, next) {
var userId = req.user._id;
return User.findOne({ _id: userId }, '-salt -password').exec()
.then(user => { // don't ever give out the password or salt
if(!user) {
return res.status(401).end();
}
res.json(user);
})
.catch(err => next(err));
}
/**
* Authentication callback
*/
export function authCallback(req, res) {
res.redirect('/');
}

View file

@ -0,0 +1,35 @@
/**
* User model events
*/
'use strict';
import {EventEmitter} from 'events';
var UserEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
UserEvents.setMaxListeners(0);
// Model events
var events = {
save: 'save',
remove: 'remove'
};
// Register the event emitter to the model events
function registerEvents(User) {
for(var e in events) {
let event = events[e];
User.post(e, emitEvent(event));
}
}
function emitEvent(event) {
return function(doc) {
UserEvents.emit(`${event}:${doc._id}`, doc);
UserEvents.emit(event, doc);
};
}
export {registerEvents};
export default UserEvents;

View file

@ -0,0 +1,67 @@
'use strict';
/* globals describe, expect, it, before, after, beforeEach, afterEach */
import app from '../..';
import User from './user.model';
import request from 'supertest';
describe('User API:', function() {
var user;
// Clear users before testing
before(function() {
return User.remove().then(function() {
user = new User({
name: 'Fake User',
email: 'test@example.com',
password: 'password'
});
return user.save();
});
});
// Clear users after testing
after(function() {
return User.remove();
});
describe('GET /api/users/me', function() {
var token;
before(function(done) {
request(app)
.post('/auth/local')
.send({
email: 'test@example.com',
password: 'password'
})
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
token = res.body.token;
done();
});
});
it('should respond with a user profile when authenticated', function(done) {
request(app)
.get('/api/users/me')
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
expect(res.body._id.toString()).to.equal(user._id.toString());
done();
});
});
it('should respond with a 401 when not authenticated', function(done) {
request(app)
.get('/api/users/me')
.expect(401)
.end(done);
});
});
});

View file

@ -0,0 +1,258 @@
'use strict';
/*eslint no-invalid-this:0*/
import crypto from 'crypto';
mongoose.Promise = require('bluebird');
import mongoose, {Schema} from 'mongoose';
import {registerEvents} from './user.events';
const authTypes = ['github', 'twitter', 'facebook', 'google'];
var UserSchema = new Schema({
name: String,
email: {
type: String,
lowercase: true,
required() {
if(authTypes.indexOf(this.provider) === -1) {
return true;
} else {
return false;
}
}
},
role: {
type: String,
default: 'user'
},
password: {
type: String,
required() {
if(authTypes.indexOf(this.provider) === -1) {
return true;
} else {
return false;
}
}
},
provider: String,
salt: String,
facebook: {},
google: {},
github: {}
});
/**
* Virtuals
*/
// Public profile information
UserSchema
.virtual('profile')
.get(function() {
return {
name: this.name,
role: this.role
};
});
// Non-sensitive info we'll be putting in the token
UserSchema
.virtual('token')
.get(function() {
return {
_id: this._id,
role: this.role
};
});
/**
* Validations
*/
// Validate empty email
UserSchema
.path('email')
.validate(function(email) {
if(authTypes.indexOf(this.provider) !== -1) {
return true;
}
return email.length;
}, 'Email cannot be blank');
// Validate empty password
UserSchema
.path('password')
.validate(function(password) {
if(authTypes.indexOf(this.provider) !== -1) {
return true;
}
return password.length;
}, 'Password cannot be blank');
// Validate email is not taken
UserSchema
.path('email')
.validate(function(value) {
if(authTypes.indexOf(this.provider) !== -1) {
return true;
}
return this.constructor.findOne({ email: value }).exec()
.then(user => {
if(user) {
if(this.id === user.id) {
return true;
}
return false;
}
return true;
})
.catch(function(err) {
throw err;
});
}, 'The specified email address is already in use.');
var validatePresenceOf = function(value) {
return value && value.length;
};
/**
* Pre-save hook
*/
UserSchema
.pre('save', function(next) {
// Handle new/update passwords
if(!this.isModified('password')) {
return next();
}
if(!validatePresenceOf(this.password)) {
if(authTypes.indexOf(this.provider) === -1) {
return next(new Error('Invalid password'));
} else {
return next();
}
}
// Make salt with a callback
this.makeSalt((saltErr, salt) => {
if(saltErr) {
return next(saltErr);
}
this.salt = salt;
this.encryptPassword(this.password, (encryptErr, hashedPassword) => {
if(encryptErr) {
return next(encryptErr);
}
this.password = hashedPassword;
return next();
});
});
});
/**
* Methods
*/
UserSchema.methods = {
/**
* Authenticate - check if the passwords are the same
*
* @param {String} password
* @param {Function} callback
* @return {Boolean}
* @api public
*/
authenticate(password, callback) {
if(!callback) {
return this.password === this.encryptPassword(password);
}
this.encryptPassword(password, (err, pwdGen) => {
if(err) {
return callback(err);
}
if(this.password === pwdGen) {
return callback(null, true);
} else {
return callback(null, false);
}
});
},
/**
* Make salt
*
* @param {Number} [byteSize] - Optional salt byte size, default to 16
* @param {Function} callback
* @return {String}
* @api public
*/
makeSalt(...args) {
let byteSize;
let callback;
let defaultByteSize = 16;
if(typeof args[0] === 'function') {
callback = args[0];
byteSize = defaultByteSize;
} else if(typeof args[1] === 'function') {
callback = args[1];
} else {
throw new Error('Missing Callback');
}
if(!byteSize) {
byteSize = defaultByteSize;
}
return crypto.randomBytes(byteSize, (err, salt) => {
if(err) {
return callback(err);
} else {
return callback(null, salt.toString('base64'));
}
});
},
/**
* Encrypt password
*
* @param {String} password
* @param {Function} callback
* @return {String}
* @api public
*/
encryptPassword(password, callback) {
if(!password || !this.salt) {
if(!callback) {
return null;
} else {
return callback('Missing password or salt');
}
}
var defaultIterations = 10000;
var defaultKeyLength = 64;
var salt = new Buffer(this.salt, 'base64');
if(!callback) {
// eslint-disable-next-line no-sync
return crypto.pbkdf2Sync(password, salt, defaultIterations,
defaultKeyLength, 'sha1')
.toString('base64');
}
return crypto.pbkdf2(password, salt, defaultIterations, defaultKeyLength,
'sha1', (err, key) => {
if(err) {
return callback(err);
} else {
return callback(null, key.toString('base64'));
}
});
}
};
registerEvents(UserSchema);
export default mongoose.model('User', UserSchema);

View file

@ -0,0 +1,164 @@
'use strict';
import app from '../..';
import User from './user.model';
var user;
var genUser = function() {
user = new User({
provider: 'local',
name: 'Fake User',
email: 'test@example.com',
password: 'password'
});
return user;
};
describe('User Model', function() {
before(function() {
// Clear users before testing
return User.remove();
});
beforeEach(function() {
genUser();
});
afterEach(function() {
return User.remove();
});
it('should begin with no users', function() {
return expect(User.find({}).exec()).to
.eventually.have.length(0);
});
it('should fail when saving a duplicate user', function() {
return expect(user.save()
.then(function() {
var userDup = genUser();
return userDup.save();
})).to.be.rejected;
});
describe('#email', function() {
it('should fail when saving with a blank email', function() {
user.email = '';
return expect(user.save()).to.be.rejected;
});
it('should fail when saving with a null email', function() {
user.email = null;
return expect(user.save()).to.be.rejected;
});
it('should fail when saving without an email', function() {
user.email = undefined;
return expect(user.save()).to.be.rejected;
});
describe('given user provider is google', function() {
beforeEach(function() {
user.provider = 'google';
});
it('should succeed when saving without an email', function() {
user.email = null;
return expect(user.save()).to.be.fulfilled;
});
});
describe('given user provider is facebook', function() {
beforeEach(function() {
user.provider = 'facebook';
});
it('should succeed when saving without an email', function() {
user.email = null;
return expect(user.save()).to.be.fulfilled;
});
});
describe('given user provider is github', function() {
beforeEach(function() {
user.provider = 'github';
});
it('should succeed when saving without an email', function() {
user.email = null;
return expect(user.save()).to.be.fulfilled;
});
});
});
describe('#password', function() {
it('should fail when saving with a blank password', function() {
user.password = '';
return expect(user.save()).to.be.rejected;
});
it('should fail when saving with a null password', function() {
user.password = null;
return expect(user.save()).to.be.rejected;
});
it('should fail when saving without a password', function() {
user.password = undefined;
return expect(user.save()).to.be.rejected;
});
describe('given the user has been previously saved', function() {
beforeEach(function() {
return user.save();
});
it('should authenticate user if valid', function() {
expect(user.authenticate('password')).to.be.true;
});
it('should not authenticate user if invalid', function() {
expect(user.authenticate('blah')).to.not.be.true;
});
it('should remain the same hash unless the password is updated', function() {
user.name = 'Test User';
return expect(user.save()
.then(function(u) {
return u.authenticate('password');
})).to.eventually.be.true;
});
});
describe('given user provider is google', function() {
beforeEach(function() {
user.provider = 'google';
});
it('should succeed when saving without a password', function() {
user.password = null;
return expect(user.save()).to.be.fulfilled;
});
});
describe('given user provider is facebook', function() {
beforeEach(function() {
user.provider = 'facebook';
});
it('should succeed when saving without a password', function() {
user.password = null;
return expect(user.save()).to.be.fulfilled;
});
});
describe('given user provider is github', function() {
beforeEach(function() {
user.provider = 'github';
});
it('should succeed when saving without a password', function() {
user.password = null;
return expect(user.save()).to.be.fulfilled;
});
});
});
});