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

@ -4,187 +4,318 @@
var app = require('../..');
import request from 'supertest';
import User from '../user/user.model';
var newAnsible;
describe('Ansible API:', function() {
describe('GET /api/ansible', function() {
var ansibles;
var token;
var user;
var ansible_job;
beforeEach(function(done) {
// 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)
.get('/api/ansible')
.post('/auth/local')
.send({
email: 'test@example.com',
password: 'password'
})
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
ansibles = res.body;
token = res.body.token;
done();
});
});
it('should respond with JSON array', function() {
expect(ansibles).to.be.instanceOf(Array);
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('POST /api/ansible', function() {
describe('POST /modules/list', function() {
var modules;
beforeEach(function(done) {
request(app)
.post('/api/ansible')
.post('/api/ansible/modules/list')
.timeout(10000)
.set('authorization', `Bearer ${token}`)
.send({
name: 'New Ansible',
info: 'This is the brand new ansible!!!'
ansibleEngine: {
'host' : ''
}
})
.expect(200)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
modules = res.text;
done();
});
});
it('should respond with list of Ansible Modules (containing ping module)', function() {
expect(modules).to.contain('ping');
});
});
describe('POST /command to execute a sample command - echo "Hello World"', function() {
var modules;
beforeEach(function(done) {
request(app)
.post('/api/ansible/command')
.set('authorization', `Bearer ${token}`)
.send({
command: 'echo "Hello World"'
})
.expect(200)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
modules = res.text;
done();
});
});
it('should respond with the result of command', function() {
expect(modules).to.contain('Hello World');
});
});
describe('POST /inventory/create', function() {
var result;
beforeEach(function(done) {
request(app)
.post('/api/ansible/inventory/create')
.set('authorization', `Bearer ${token}`)
.send({
ansibleEngine: {
host : '',
projectFolder: '/tmp'
},
inventoryName: 'inventory.txt',
inventoryFileContents: 'localhost ansible_connection=local'
})
.expect(200)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
result = res.text;
done();
});
});
it('should respond with "file written"', function() {
expect(result).to.contain('file written');
});
});
describe('POST /playbook/create', function() {
var result;
beforeEach(function(done) {
request(app)
.post('/api/ansible/playbook/create')
.set('authorization', `Bearer ${token}`)
.send({
ansibleEngine: {
host : '',
projectFolder: '/tmp'
},
playbookName: 'test_playbook.yml',
playbookFileContents: '-\n' +
' name: "Test Play1"\n' +
' hosts: localhost\n' +
' tasks:\n' +
' - name: "Test Task1"\n' +
' ping:\n'
})
.expect(200)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
result = res.text;
done();
});
});
it('should respond with "file written"', function() {
expect(result).to.contain('file written');
});
});
describe('POST /execute', function() {
beforeEach(function(done) {
request(app)
.post('/api/ansible/execute')
.set('authorization', `Bearer ${token}`)
.send({
ansibleEngine: {
host : '',
projectFolder: '/tmp/'
},
selectedPlaybook: 'test_playbook.yml',
inventory_file_name: 'inventory.txt',
})
.expect(201)
.expect('Content-Type', /json/)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
newAnsible = res.body;
ansible_job = 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!!!');
it('should respond with an Ansible Job object', function() {
expect(ansible_job.selectedPlaybook).to.equal('test_playbook.yml');
});
});
describe('GET /api/ansible/:id', function() {
var ansible;
describe('GET /:id', function() {
beforeEach(function(done) {
request(app)
.get(`/api/ansible/${newAnsible._id}`)
.get('/api/ansible/' + ansible_job._id)
.set('authorization', `Bearer ${token}`)
.expect(200)
.expect('Content-Type', /json/)
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
ansible = res.body;
ansible_job = res.body;
console.log("ansible_job " + JSON.stringify(ansible_job));
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!!!');
it('should respond with an Ansible Job object', function() {
expect(ansible_job.selectedPlaybook).to.equal('test_playbook.yml');
});
});
describe('PUT /api/ansible/:id', function() {
var updatedAnsible;
describe('POST /playbook/delete', function() {
var result;
beforeEach(function(done) {
request(app)
.put(`/api/ansible/${newAnsible._id}`)
.post('/api/ansible/playbook/delete')
.set('authorization', `Bearer ${token}`)
.send({
name: 'Updated Ansible',
info: 'This is the updated ansible!!!'
ansibleEngine: {
host : '',
projectFolder: '/tmp'
},
playbookName: 'test_playbook.yml',
})
.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/)
//.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!!!');
result = res;
done();
});
});
it('should respond with status code 200', function() {
expect(result.status).to.equal(200);
});
});
describe('PATCH /api/ansible/:id', function() {
var patchedAnsible;
describe('POST /inventory/delete', function() {
var result;
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!!!' }
])
.post('/api/ansible/inventory/delete')
.set('authorization', `Bearer ${token}`)
.send({
ansibleEngine: {
host : '',
projectFolder: '/tmp'
},
inventoryName: 'inventory.txt',
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
//.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
patchedAnsible = res.body;
result = res;
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!!!');
it('should respond with status code 200', function() {
expect(result.status).to.equal(200);
});
});
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();
});
});
//TODO: Add more Ansible test cases here
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();
});
});
});
});