UNPKG

4.05 kBJavaScriptView Raw
1var supertest = require('supertest');
2var assert = require('assert');
3var shortid = require('shortid');
4var request = require('request');
5var stream = require('stream');
6var fs = require('fs');
7var through = require('through2');
8var log = require('../lib/log');
9var _ = require('lodash');
10var sinon = require('sinon');
11var os = require('os');
12var path = require('path');
13var helper = require('../lib/helper');
14var sandboxServer = require('../lib/sandbox-server');
15
16require('dash-assert');
17
18describe('sandboxServer', function() {
19 var self;
20
21 before(function() {
22 sinon.stub(log, 'write', _.noop());
23 });
24
25 beforeEach(function() {
26 self = this;
27
28 this.program = {
29 profile: {
30 name: 'default',
31 endpoint: 'https://apphost.com',
32 jwt: {
33 token: '23523454'
34 }
35 },
36 cwd: path.join(__dirname, './fixtures/sample-app'),
37 baseDir: path.join(__dirname, './fixtures/sample-app/app'),
38 virtualApp: {
39 appId: shortid.generate()
40 }
41 };
42
43 sinon.stub(request, 'post', function(options, callback) {
44 // Mock the create app post request
45 if (options.url.indexOf('/dev/' + self.program.virtualApp.appId + '/upload')) {
46 // callback(null, {statusCode: 201});
47 // Create a dummy write stream
48 return through(function(chunk, enc, cb) {
49 cb();
50 }, function() {
51 callback(null, {statusCode: 200});
52 });
53 }
54 });
55 });
56
57 afterEach(function() {
58 request.post.restore();
59 });
60
61 after(function() {
62 log.write.restore();
63 });
64
65 it('serves static files', function(done) {
66 var server = sandboxServer(this.program);
67 supertest(server)
68 .get('/js/app.js')
69 .expect(200)
70 .expect('Content-Type', /application\/javascript/)
71 .expect(function(res) {
72 assert.equal(res.text, '// app.js\n');
73 })
74 .end(done);
75 });
76
77 it('serves node_modules from root of app', function(done) {
78 var server = sandboxServer(this.program);
79 supertest(server)
80 .get('/node_modules/some-module/module.js')
81 .expect(200)
82 .expect('Content-Type', /application\/javascript/)
83 .expect(function(res) {
84 assert.equal(res.text, '// module.js\n');
85 })
86 .end(done);
87 });
88
89 it('serves bower_components from root of app', function(done) {
90 var server = sandboxServer(this.program);
91 supertest(server)
92 .get('/bower_components/component/component.js')
93 .expect(200)
94 .expect('Content-Type', /application\/javascript/)
95 .expect(function(res) {
96 assert.equal(res.text, '// component.js\n');
97 })
98 .end(done);
99 });
100
101 it('missing file returns 404', function(done) {
102 var server = sandboxServer(this.program);
103 supertest(server)
104 .get('/js/missing.js')
105 .expect(404)
106 .expect(function() {
107 assert.isFalse(request.post.called);
108 })
109 .end(done);
110 });
111
112 describe('/sandbox route', function() {
113 beforeEach(function(done) {
114 helper.fileHash(path.join(this.program.baseDir, 'index.html'), function(err, hash) {
115 if (err) return done(err);
116
117 self.hash = hash;
118 done();
119 });
120 });
121
122 it('hash is different', function(done) {
123 var server = sandboxServer(this.program);
124 var redirectUrl = 'https://appname--dev.apphost.com/';
125
126 supertest(server)
127 .get('/sandbox/index.html?hash=not_a_real_hash&return=' + encodeURIComponent(redirectUrl))
128 .expect(302)
129 .expect(function(res) {
130 var apiUploadUrl = self.program.profile.endpoint + '/api/dev/' + self.program.virtualApp.appId + '/upload/index.html'
131 assert.ok(request.post.calledWith(sinon.match({url: apiUploadUrl})));
132
133 assert.equal(res.headers.location, redirectUrl);
134 })
135 .end(done);
136 });
137
138 it('hash value is the same', function(done) {
139 var server = sandboxServer(this.program);
140 var redirectUrl = 'https://appname--dev.apphost.com/';
141
142 supertest(server)
143 .get("/sandbox/index.html?hash=" + this.hash + "&return=" + encodeURIComponent(redirectUrl))
144 .expect(302)
145 .expect(function(res) {
146 assert.isFalse(request.post.called);
147 assert.equal(res.headers.location, redirectUrl);
148 })
149 .end(done);
150 });
151 });
152});