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

Update server side test cases and disable client side tests for now. Integrate later.

This commit is contained in:
Mumshad Mannambeth 2017-07-07 12:23:45 -04:00
parent ab295a073a
commit 9c880c0bba
13 changed files with 588 additions and 318 deletions

View file

@ -13,6 +13,7 @@
import jsonpatch from 'fast-json-patch';
import Project from './project.model';
import config from '../../config/environment';
const util = require('util');
var ansibleTool = require('../../components/ansible/ansible_tool');
function respondWithResult(res, statusCode) {
@ -50,6 +51,7 @@ function removeEntity(res) {
}
function handleEntityNotFound(res) {
console.log("Entity Not Found");
return function(entity) {
if(!entity) {
res.status(404).end();
@ -62,6 +64,7 @@ function handleEntityNotFound(res) {
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
console.log("ERror " + err);
res.status(statusCode).send(err);
};
}
@ -93,13 +96,18 @@ export function show(req, res) {
}
// Creates a new Project in the DB
/**
* Create New Project
* - If Ansible Engine information is provided use that, else consider localhost as Ansible Engine
* - Identify/generate project and library (custom modules location)
* - Get Ansible version and create projects folder
* @param req
* @param res
*/
export function create(req, res) {
var ansibleEngine = req.body.ansibleEngine;
console.log("Ansible Engine " + JSON.stringify(ansibleEngine));
req.body.owner_id = req.user._id;
req.body.owner_name = req.user.name;
@ -118,18 +126,22 @@ export function create(req, res) {
};
}
// If projectFolder is not passed, create a custom project folder
if(!ansibleEngine.projectFolder){
ansibleEngine.projectFolder = '/opt/ansible-projects/' + req.user._id + '_' + req.body.name;
ansibleEngine.customModules = '/opt/ansible-projects/' + req.user._id + '_' + req.body.name + '/library';
let projectFolderName = util.format('%s_%s',req.user._id, req.body.name);
ansibleEngine.projectFolder = util.format('/opt/ansible-projects/test_%s', projectFolderName);
ansibleEngine.customModules = util.format('/opt/ansible-projects/test_%s/library', projectFolderName);
// Update project request body to save in db
req.body.ansibleEngine.projectFolder = ansibleEngine.projectFolder;
req.body.ansibleEngine.customModules = ansibleEngine.customModules;
req.body.ansibleEngine.projectFolderName = projectFolderName;
}
// Allow creating project if no host is passed. Then use the default Ansible Engine for all operations.
// If Ansible host is passed get Ansible version and create project folder
if(ansibleEngine.ansibleHost){
ansibleTool.getAnsibleVersion(
function(version){
@ -184,10 +196,16 @@ export function patch(req, 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(function(entity){
if(!entity)return null;
return ansibleTool.deleteProjectFolder(entity);
})
.then(removeEntity(res))
.catch(handleError(res));
}

View file

@ -4,16 +4,92 @@
var app = require('../..');
import request from 'supertest';
import Project from './project.model';
import User from '../user/user.model';
var newProject;
describe('Project API:', function() {
var token;
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() {
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);
});
});
describe('GET /api/projects', function() {
var projects;
// Clear Projects before testing
before(function() {
return Project.remove().then(function() {
var project = new Project({
name: 'FakeProject',
info: 'Test Project',
ansibleEngine: {
'host' : ''
}
});
return project.save();
});
});
beforeEach(function(done) {
request(app)
.get('/api/projects')
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
@ -34,9 +110,13 @@ describe('Project API:', function() {
beforeEach(function(done) {
request(app)
.post('/api/projects')
.set('authorization', `Bearer ${token}`)
.send({
name: 'New Project',
info: 'This is the brand new project!!!'
name: 'NewProject',
info: 'This is the brand new project!!!',
ansibleEngine: {
'host' : ''
}
})
.expect(201)
.expect('Content-Type', /json/)
@ -50,7 +130,7 @@ describe('Project API:', function() {
});
it('should respond with the newly created project', function() {
expect(newProject.name).to.equal('New Project');
expect(newProject.name).to.equal('NewProject');
expect(newProject.info).to.equal('This is the brand new project!!!');
});
});
@ -61,6 +141,7 @@ describe('Project API:', function() {
beforeEach(function(done) {
request(app)
.get(`/api/projects/${newProject._id}`)
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
@ -77,7 +158,7 @@ describe('Project API:', function() {
});
it('should respond with the requested project', function() {
expect(project.name).to.equal('New Project');
expect(project.name).to.equal('NewProject');
expect(project.info).to.equal('This is the brand new project!!!');
});
});
@ -88,6 +169,7 @@ describe('Project API:', function() {
beforeEach(function(done) {
request(app)
.put(`/api/projects/${newProject._id}`)
.set('authorization', `Bearer ${token}`)
.send({
name: 'Updated Project',
info: 'This is the updated project!!!'
@ -115,6 +197,7 @@ describe('Project API:', function() {
it('should respond with the updated project on a subsequent GET', function(done) {
request(app)
.get(`/api/projects/${newProject._id}`)
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
@ -137,6 +220,7 @@ describe('Project API:', function() {
beforeEach(function(done) {
request(app)
.patch(`/api/projects/${newProject._id}`)
.set('authorization', `Bearer ${token}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched Project' },
{ op: 'replace', path: '/info', value: 'This is the patched project!!!' }
@ -166,6 +250,7 @@ describe('Project API:', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete(`/api/projects/${newProject._id}`)
.set('authorization', `Bearer ${token}`)
.expect(204)
.end(err => {
if(err) {
@ -178,6 +263,7 @@ describe('Project API:', function() {
it('should respond with 404 when project does not exist', function(done) {
request(app)
.delete(`/api/projects/${newProject._id}`)
.set('authorization', `Bearer ${token}`)
.expect(404)
.end(err => {
if(err) {