[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / test / red / ui_spec.js
1 /**
2  * Copyright 2014 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16 var request = require("supertest");
17 var express = require("express");
18 var redUI = require("../../red/ui");
19
20
21 describe("red/ui icon handler", function() {
22     it('returns the default icon when getting an unknown icon', function(done) {
23         var app = express();
24         redUI({},app);
25         request(app)
26             .get("/icons/youwonthaveme.png")
27             .expect('Content-Type', /image\/png/)
28             .expect(200)
29             .end(function(err, res){
30                 if (err){
31                     return done(err);
32                 }
33                 done();
34               });
35     });
36     
37     it('returns an icon from disk', function(done) {
38         var app = express();
39         redUI({},app);
40         request(app)
41             .get("/icons/arduino.png")
42             .expect('Content-Type', /image\/png/)
43             .expect(200)
44             .end(function(err, res){
45                 if (err){
46                     return done(err);
47                 }
48                 done();
49               });
50     });
51 });
52
53 describe("icon cache handler", function() {
54     var fs = require('fs-extra');
55     var path = require('path');
56     var events = require("../../red/events");
57     
58     var tempDir = path.join(__dirname,".tmp/");
59     var cachedFakePNG = tempDir + "cacheMe.png";
60     
61     
62     beforeEach(function(done) {
63         fs.remove(tempDir,function(err) {
64             fs.mkdirSync(tempDir);
65             fs.writeFileSync(cachedFakePNG, "Hello PNG\n");
66             done();
67         });     
68     });
69     afterEach(function(done) {
70         fs.exists(cachedFakePNG, function(exists) {
71           if(exists) {
72               fs.unlinkSync(cachedFakePNG);
73           } 
74           fs.remove(tempDir,done);
75         })
76     });
77     
78     /*
79      * This test case test that:
80      * 1) any directory can be added to the path lookup (such as /tmp) by
81      * calling the right event
82      * 2) that a file we know exists gets cached so that the lookup/verification
83      * of actual existence doesn't occur again when a subsequent request comes in
84      * 
85      * The second point verifies that the cache works. If the cache wouldn't work
86      * the default PNG would be served
87      */
88     it('returns an icon using icon cache', function(done) {        
89         var app = express();
90         redUI({},app);
91         events.emit("node-icon-dir", tempDir);
92         request(app)
93             .get("/icons/cacheMe.png")
94             .expect('Content-Type', /image\/png/)
95             .expect(200)
96             .end(function(err, res){
97                 if (err){
98                     return done(err);
99                 }
100                 fs.unlink(cachedFakePNG, function(err) {
101                     if(err) {
102                         return done(err);
103                     }
104                     request(app)
105                     .get("/icons/cacheMe.png")
106                     .expect('Content-Type', /text\/html/)
107                     .expect(404)
108                     .end(function(err, res){
109                         if (err){
110                             return done(err);
111                         }
112                         done();
113                       });
114                 });
115               });
116     });
117 });
118
119 describe("red/ui settings handler", function() {
120     it('returns the provided settings', function(done) {
121         var settings = {
122                 httpNodeRoot: "testHttpNodeRoot",
123                 version: "testVersion",
124         };
125         var app = express();
126         redUI(settings,app);
127         request(app)
128             .get("/settings")
129             .expect('Content-Type', /application\/json/)
130             .expect(200, "{\n  \"httpNodeRoot\": \"testHttpNodeRoot\",\n  \"version\": \"testVersion\"\n}")
131             .end(function(err, res){
132                 if (err){
133                     return done(err);
134                 }
135                 done();
136             });
137         
138     });
139 });
140
141 describe("red/ui root handler", function() {
142     it('server up the main page', function(done) {
143         var app = express();
144         redUI({},app);
145         
146         request(app)
147             .get("/")
148             .expect('Content-Type', /text\/html/)
149             .expect(200)
150             .end(function(err, res){
151                 if (err){
152                     return done(err);
153                 }
154                 done();
155             });
156         
157     });
158     
159     it('redirects to path ending with /', function(done) {
160         var rapp = express();
161         redUI({},rapp);
162
163         var app = express().use('/root', rapp);
164         
165         request(app)
166         .get("/root")
167         .expect('Content-Type', /text\/plain/)
168         .expect(302)
169         .end(function(err, res){
170             if (err){
171                 return done(err);
172             }
173             done();
174           });
175         
176     });
177 });