UNPKG

5.34 kBJavaScriptView Raw
1'use strict';
2
3var octo = require('./index');
4var fs = require('fs');
5var expect = require('chai').expect;
6var sinon = require('sinon');
7var request = require('request');
8
9describe('push', function() {
10 var postStub;
11
12 beforeEach(function(){
13 postStub = sinon.stub(request, 'post');
14 });
15
16 afterEach(function(){
17 postStub.restore();
18 });
19
20 it('should pass pkg stream', function() {
21 octo.push(new Buffer('hello world'), {
22 apikey: 'KEY',
23 host: 'http://localhost',
24 name: 'package.tar'
25 });
26
27 var req = postStub.firstCall.args[0];
28 expect(req.headers['X-Octopus-ApiKey']).to.equal('KEY');
29 expect(req.formData.file.value.toString()).to.equal(new Buffer('hello world').toString());
30 expect(req.formData.file.options.filename).to.equal('package.tar');
31 });
32
33 describe('build url', function () {
34 it('should include `replace` parameter if it is provided', function () {
35 octo.push(new Buffer('hello world'), { replace: true, host: 'http://myweb/', name: 'package.tar' });
36 var req = postStub.lastCall.args[0];
37 expect(req.url).to.equal('http://myweb/api/packages/raw?replace=true');
38 });
39
40 it('should build correct url regardless of trailing slash', function () {
41 testUrl('http://myweb', 'http://myweb/api/packages/raw');
42 testUrl('http://myweb/', 'http://myweb/api/packages/raw');
43 });
44
45 it('should build correct url with port', function () {
46 testUrl('http://myweb:3000/', 'http://myweb:3000/api/packages/raw');
47 });
48
49 it('should build correct url with relative path', function () {
50 testUrl('http://myweb/path/to/octopus', 'http://myweb/path/to/octopus/api/packages/raw');
51 });
52
53 function testUrl(host, expected) {
54 octo.push(new Buffer('hello world'), { host: host, name: 'package.tar' });
55 var req = postStub.lastCall.args[0];
56 expect(req.url).to.equal(expected);
57 }
58 });
59
60 it('should return response body if request successful', function(done) {
61 var body = { prop: 12 };
62
63 octo.push(new Buffer('hello world'), {
64 apikey: 'KEY',
65 replace: true,
66 host: 'http://localhost',
67 name: 'package.tar',
68 }, function(err, result) {
69 expect(err).to.be.null;
70 expect(result).to.eql(body);
71 done();
72 });
73
74 var callback = postStub.firstCall.args[1];
75 callback(null, {statusCode: 200}, body);
76 });
77});
78
79describe('pack', function() {
80
81 it('can create a stream', function (done) {
82 octo.pack()
83 .append('buffer files/hello.txt', new Buffer('hello world'), {date: new Date(2011, 11, 11)})
84 .append('stream.txt', fs.createReadStream('./package.json'))
85 .append('lib/pack.js')
86 .toStream(function (err, data) {
87 expect(err).to.be.null;
88 expect(data.stream.readable).to.be.true;
89 done();
90 });
91 });
92
93 it('can create a file', function (done) {
94 octo.pack()
95 .append('buffer files/hello.txt', new Buffer('hello world'), {date: new Date(2011, 11, 11)})
96 .append('stream.txt', fs.createReadStream('./package.json'))
97 .append('lib/pack.js')
98 .toFile('./bin/', function (err, data) {
99 expect(err).to.be.null;
100 expect(data.size).to.be.above(0);
101 expect(data.name).not.to.be.null;
102 expect(data.path.indexOf('bin')).to.equal(0);
103
104 fs.exists(data.path, function (exists) {
105 expect(exists).to.be.true;
106 done();
107 });
108 });
109 });
110
111 it('can add files with glob', function (done) {
112 octo.pack()
113 .append('lib/*.js')
114 .toFile('./bin/', function (err, data) {
115 expect(err).to.be.null;
116 expect(data.size).to.be.above(0);
117 expect(data.name).not.to.be.null;
118 expect(data.path.indexOf('bin')).to.equal(0);
119
120 fs.exists(data.path, function (exists) {
121 expect(exists).to.be.true;
122 done();
123 });
124 });
125 });
126
127 it('defaults to tar.gz', function (done) {
128 octo.pack()
129 .append('file.txt', new Buffer('hello world'))
130 .toStream(function (err, data) {
131 expect(data.name).not.to.be.null;
132 expect(data.name.indexOf('.tar.gz', data.name.length - 7)).to.not.equal(-1);
133 done();
134 });
135 });
136
137 it('can create zip', function (done) {
138 octo.pack('zip')
139 .append('file.txt', new Buffer('hello world'))
140 .toStream(function (err, data) {
141 expect(data.name).not.to.be.null;
142 expect(data.name.indexOf('.zip', data.name.length - 4)).to.not.equal(-1);
143 done();
144 });
145 });
146
147 it('can\'t create nupkg', function () {
148 expect(function(){ octo.pack('nupkg'); }).to
149 .throw('Currently unable to support .nupkg file. Please use .tar.gz or .zip');
150 });
151
152 it('can pass through custom id and version', function (done) {
153 octo.pack({id: 'MYAPP', version: '4.2'})
154 .append('file.txt', new Buffer('hello world'))
155 .toStream(function (err, data) {
156 expect(data.name).not.to.be.null;
157 expect(data.name.indexOf('MYAPP.4.2.')).to.equal(0);
158 done();
159 });
160 });
161});