@octopusdeploy/octojs
Version:
A nodejs cli tool for packaging and pushing projects to an Octopus Deploy instance.
138 lines (119 loc) • 4.7 kB
JavaScript
;
var octo = require('../lib');
var expect = require('chai').expect;
var sinon = require('sinon');
var request = require('request');
var fs = require('fs');
// api/spaces/all
var sp = JSON.stringify([
{
"Id": "Spaces-1",
"Name": "Default",
"Description": null,
"IsDefault": true,
"TaskQueueStopped": false,
"SpaceManagersTeams": [
"teams-administrators",
"teams-managers",
"teams-spacemanagers-Spaces-1"
],
"SpaceManagersTeamMembers": [
"Users-1"
]
},
{
"Id": "Spaces-2",
"Name": "Octopus",
"Description": null,
"IsDefault": false,
"TaskQueueStopped": false,
"SpaceManagersTeams": [
"teams-spacemanagers-Spaces-2",
"teams-administrators"
],
"SpaceManagersTeamMembers": [
"Users-1"
]
}
]);
describe('push', function() {
var postStub;
var getStub;
beforeEach(function(){
postStub = sinon.stub(request, 'post');
getStub = sinon.stub(request, 'get').yields(null, {statusCode : 200, statusMessage: "OK"}, JSON.parse(sp));
});
afterEach(function(){
postStub.restore();
getStub.restore();
});
it('should pass pkg stream', function() {
octo.push(new Buffer('hello world'), {
apiKey: 'KEY',
server: 'http://localhost',
name: 'package.tar',
quiet: true
});
var req = postStub.firstCall.args[0];
expect(req.headers['X-Octopus-ApiKey']).to.equal('KEY');
expect(req.formData.file.value.toString()).to.equal(new Buffer('hello world').toString());
expect(req.formData.file.options.filename).to.equal('package.tar');
});
describe('build url', function () {
it('should include `replace` parameter if it is provided', function () {
octo.push(new Buffer('hello world'), { replace: true, server: 'http://myweb/', name: 'package.tar', apiKey: "KEY"});
var req = postStub.lastCall.args[0];
expect(req.url).to.equal('http://myweb/api/packages/raw?replace=true');
});
it('should build correct url regardless of trailing slash', function () {
testUrl('http://myweb', 'http://myweb/api/packages/raw');
testUrl('http://myweb/', 'http://myweb/api/packages/raw');
});
it('should build correct url with port', function () {
testUrl('http://myweb:3000/', 'http://myweb:3000/api/packages/raw');
});
it('should build correct url with relative path', function () {
testUrl('http://myweb/path/to/octopus', 'http://myweb/path/to/octopus/api/packages/raw');
});
function testUrl(host, expected) {
octo.push(new Buffer('hello world'), { server: host, name: 'package.tar', apiKey: "KEY" });
var req = postStub.lastCall.args[0];
expect(req.url).to.equal(expected);
}
});
describe('resolve space id', function () {
it('should call out to the spaces/all endpoint', function() {
octo.push(
new Buffer('hello world'),
{ replace: true, server: 'http://myweb/', name: 'package.1.0.0.tar', apiKey: "KEY", space: "Octopus"},
function(e,f) {}
);
var req = getStub.lastCall.args[0];
expect(req.url).to.equal('http://myweb/api/spaces/all');
});
it('should resolve a spaceID from a spaceName', function () {
octo.push(
new Buffer('hello world'),
{ replace: true, server: 'http://myweb/', name: 'package.1.0.0.tar', apiKey: "KEY", space: "Octopus"},
function(e, f) {}
);
var req = postStub.lastCall.args[0];
expect(req.url).to.equal('http://myweb/api/Spaces-2/packages/raw?replace=true');
});
});
it('should return response body if request successful', function(done) {
var body = { prop: 12 };
octo.push(new Buffer('hello world'), {
apiKey: 'KEY',
replace: true,
server: 'http://localhost',
name: 'package.tar',
}, function(err, result) {
expect(err).to.be.null;
expect(result).to.eql(body);
done();
});
var callback = postStub.firstCall.args[1];
callback(null, {statusCode: 200}, body);
});
});