acs
Version: 
Appcelerator Server Side Node
218 lines (190 loc) • 8.72 kB
JavaScript
var chai = require('chai'),
    should = chai.should(),
    rewire = require('rewire'),
    sinon = require('sinon'),
    fs = require('fs-extra'),
    util = require('../lib/util'),
    helper = require('./helper');
var publishModule = rewire('../lib/command/publish');
var postModule = rewire('../lib/post');
var newModule = rewire('../lib/command/new');
var consolelog = console.log;
describe('publish cli - ', function() {
    this.timeout(20000);
    var outputs = [];
    var positiveBody = {
        success: true,
        name: helper.testApp,
        version: helper.testVersion,
        url: 'https://domain.test.com'
    };
    before(async function() {
        helper.beforeHandler(async function() {
            postModule.__set__('removeDependencies', function(dir, callback) {
                callback(null, {'uninstall': ['appc-logger','express'], 'remain' : []});
            });
            await createAnApp();
        });
    });
    after(helper.afterHandler);
    beforeEach(helper.buildBeforeEachHandler(outputs));
    afterEach(helper.afterEachHandler);
    it('should use host and port from command line', function(done) {
        var args = [helper.testApp];
        var opts = {
            host: helper.testHost,
            port: 8443,
            dir: __dirname + '/' + helper.testApp,
            org: '14301'
        };
        // {
        //     "uri": "https://admin.unittest.appcelerator.com:8443/publish/testApp/0.1.0/0?orgid=14301&delete_oldest=true",
        //     "method": "GET",
        //     "proxy": null,
        //     "headers": {
        //         "Cookie": "fake-cookie-for-response",
        //         "Content-Type": "application/x-www-form-urlencoded",
        //         "Authorization": ""
        //     }
        // }    
        // {
        //     "uri": "https://admin.unittest.appcelerator.com:8443/publish/testApp/0.1.0/0?orgid=14301&delete_oldest=true",
        //     "method": "POST",
        //     "headers": {
        //         "Content-Length": "965",
        //         "Content-Type": "application/octet-stream",
        //         "Content-Disposition": "attachment;filename=app.tar.gz",
        //         "Cookie": "fake-cookie-for-request",
        //         "Authorization": ""
        //     }
        // }
        var successRequestMock = helper.buildRequestMock(function(options) {
            // consolelog(JSON.stringify(options, null, 4));
            options['url'].should.eql(helper.testHost + ':8443/publish/testApp/0.1.0/0?orgid=14301&delete_oldest=true');
            options['method'].should.be.oneOf(['GET', 'POST']);
            options['headers']['Cookie'].should.eql(helper.fakeCookieForReq); 
        }, null, null, positiveBody);
        var successStreamRequestMock = helper.buildStreamRequestMock(function(options) {
            // consolelog(JSON.stringify(options, null, 4));
            options['url'].should.eql(helper.testHost + ':8443/publish/testApp/0.1.0/0?orgid=14301&delete_oldest=true');
            options['method'].should.be.oneOf(['GET', 'POST']);
            options['headers']['Cookie'].should.eql(helper.fakeCookieForReq); 
        }, null, null, positiveBody);
        postModule.__set__('got.stream.post', successStreamRequestMock);
        publishModule.__set__('got', successRequestMock);
        publishModule.__set__('post', postModule);
        if(process.exit.restore) {
            process.exit.restore();
        }
        sinon.stub(process, 'exit', function() {
             consolelog(outputs)
            // [ '\u001b[90mAdmin Hostname: https://admin.unittest.appcelerator.com\u001b[39m',
            // 'Node modules [appc-logger,express] will be loaded from npm site by Node.ACS Cloud',
            // 'Publishing to cloud...',
            // 'App \u001b[34mtestApp\u001b[39m version \u001b[34m0.1.0\u001b[39m published.',
            // 'App will be available at \u001b[36mhttps://domain.test.com\u001b[39m' ]
            outputs[0].should.contain(helper.hostInfoLine(helper.testHost));
            outputs[1].should.eql('Node modules [appc-logger,express] will be loaded from npm site by Node.ACS Cloud');
            outputs[2].should.eql('Publishing to cloud...');
            outputs[3].should.eql('App \u001b[34mtestApp\u001b[39m version \u001b[34m0.1.0\u001b[39m published.');
            outputs[4].should.eql('App will be available at \u001b[36mhttps://domain.test.com\u001b[39m');
            done();
        });
        publishModule.run(args, opts)
    });
    it('should use host from command line', function(done) {
        var args = [helper.testApp];
        var opts = {
            host: helper.testHost,
            dir: __dirname + '/' + helper.testApp
        };
        var path = '/publish/testApp/0.1.0/0?delete_oldest=true';
        var successRequestMock =
            helper.buildRequestMockWithReqOptions(helper.testHost, '443', path, ['GET', 'POST'], positiveBody);
        var successStreamRequestMock =
            helper.buildStreamRequestMockWithReqOptions(helper.testHost, '443', path, ['GET', 'POST'], positiveBody);
        postModule.__set__('got.stream.post', successStreamRequestMock);
        publishModule.__set__('got', successRequestMock);
        publishModule.__set__('post', postModule);
        if(process.exit.restore) {
            process.exit.restore();
        }
        sinon.stub(process, 'exit', function() {
             consolelog(outputs)
            outputs[0].should.contain(helper.hostInfoLine(helper.testHost));
            outputs[1].should.eql('Node modules [appc-logger,express] will be loaded from npm site by Node.ACS Cloud');
            outputs[2].should.eql('Publishing to cloud...');
            outputs[3].should.eql('App \u001b[34mtestApp\u001b[39m version \u001b[34m0.1.0\u001b[39m published.');
            outputs[4].should.eql('App will be available at \u001b[36mhttps://domain.test.com\u001b[39m');
            done();
        });
        publishModule.run(args, opts)
    });
    it('should use default host', function(done) {
        var args = [helper.testApp];
        var opts = {
            dir: __dirname + '/' + helper.testApp
        };
        var path = '/publish/testApp/0.1.0/0?delete_oldest=true';
        var successRequestMock =
            helper.buildRequestMockWithReqOptions(helper.defaultHost, '443', path, ['GET', 'POST'], positiveBody);
        var successStreamRequestMock =
            helper.buildStreamRequestMockWithReqOptions(helper.defaultHost, '443', path, ['GET', 'POST'], positiveBody);
        postModule.__set__('got.stream.post', successStreamRequestMock);
        publishModule.__set__('got', successRequestMock);
        publishModule.__set__('post', postModule);
        if(process.exit.restore) {
            process.exit.restore();
        }
        sinon.stub(process, 'exit', function() {
             consolelog(outputs)
            outputs[0].should.eql('Node modules [appc-logger,express] will be loaded from npm site by Node.ACS Cloud');
            outputs[1].should.eql('Publishing to cloud...');
            outputs[2].should.eql('App \u001b[34mtestApp\u001b[39m version \u001b[34m0.1.0\u001b[39m published.');
            outputs[3].should.eql('App will be available at \u001b[36mhttps://domain.test.com\u001b[39m');
            done();
        });
        publishModule.run(args, opts)
    });
});
async function createAnApp() {
    var originalExists = util.exists;
    sinon.stub(util, 'exists', function(path) {
        if(originalExists(path)) {
            fs.removeSync(path); 
        }
        return false;
    });
    var positiveBody = {
        success: true,
        message: 'new done'
    };
    var args = [helper.testApp];
    var opts = {
        dir: __dirname,
        host: helper.testHost,
        port: 8443
    };
    // {
    //     "uri": "https://admin.unittest.appcelerator.com:8443/create/testApp",
    //     "method": "POST",
    //     "proxy": null,
    //     "headers": {
    //         "Cookie": "fake-cookie-for-request",
    //         "Content-Type": "application/json",
    //         "Authorization": ""
    //     },
    //     "body": "{}"
    // }
    var successRequestMock = helper.buildRequestMock(function(options) {
        // consolelog(JSON.stringify(options, null, 4));
        options['url'].should.eql(helper.testHost + ':8443/create/testApp');
        options['method'].should.eql('POST');
        options['headers']['Cookie'].should.eql(helper.fakeCookieForReq); 
    }, null, null, positiveBody);
    newModule.__set__('got', successRequestMock);
    sinon.stub(process, 'exit', function() {
        util.exists.restore();
    });
    await newModule.run(args, opts)
}