UNPKG

node-red-node-web-nodes

Version:

A collection of Node-RED nodes for popular web services.

884 lines (875 loc) 391 kB
/** * Copyright 2015 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ var should = require("should"); var sinon = require("sinon"); var url = require('url'); var googleNode = require("../../google/google.js"); var directionsNode = require("../../google/directions.js"); var helper = require('../helper.js'); var nock = helper.nock; describe('google directions', function () { before(function (done) { helper.startServer(done); }); afterEach(function () { helper.unload(); }); it('can be loaded without credentials', function (done) { helper.load(directionsNode, [{ id: "directions", type: "google directions" } ], function () { var n = helper.getNode("directions"); n.should.have.property('id', 'directions'); done(); }); }); if (!nock) return; it("should succeed with all properties returned", function (done) { helper.load([googleNode, directionsNode], [{ id: "input", type: "helper", wires: [["directions"]] }, { id: "directions", type: "google directions", wires: [["output"]], }, { id: "output", type: "helper" } ], function () { nock('https://maps.googleapis.com:443') .get('/maps/api/directions/json?key=KEY&origin=Toronto&destination=Montreal') .reply(200, { "routes": [ { "bounds": { "northeast": { "lat": 45.5017123, "lng": -73.5672184 }, "southwest": { "lat": 43.6533103, "lng": -79.3827675 } }, "copyrights": "Map data ©2015 Google", "legs": [ { "distance": { "text": "541 km", "value": 540536 }, "duration": { "text": "5 hours 20 mins", "value": 19191 }, "end_address": "Montreal, QC, Canada", "end_location": { "lat": 45.5017123, "lng": -73.5672184 }, "start_address": "Toronto, ON, Canada", "start_location": { "lat": 43.6533103, "lng": -79.3827675 }, "steps": [ { "distance": { "text": "0.3 km", "value": 280 }, "duration": { "text": "1 min", "value": 34 }, "end_location": { "lat": 43.6557259, "lng": -79.38373319999999 }, "html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eBay St\u003c/b\u003e toward \u003cb\u003eHagerman St\u003c/b\u003e", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.6533103, "lng": -79.3827675 }, "travel_mode": "DRIVING" }, { "distance": { "text": "2.5 km", "value": 2492 }, "duration": { "text": "5 mins", "value": 320 }, "end_location": { "lat": 43.6618361, "lng": -79.3545312 }, "html_instructions": "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eDundas St W\u003c/b\u003e", "maneuver": "turn-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.6557259, "lng": -79.38373319999999 }, "travel_mode": "DRIVING" }, { "distance": { "text": "0.2 km", "value": 209 }, "duration": { "text": "1 min", "value": 18 }, "end_location": { "lat": 43.6635765, "lng": -79.3554817 }, "html_instructions": "Turn \u003cb\u003eleft\u003c/b\u003e onto the \u003cb\u003eDon Valley Parkway\u003c/b\u003e ramp", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.6618361, "lng": -79.3545312 }, "travel_mode": "DRIVING" }, { "distance": { "text": "12.9 km", "value": 12906 }, "duration": { "text": "10 mins", "value": 570 }, "end_location": { "lat": 43.76282690000001, "lng": -79.33669359999999 }, "html_instructions": "Merge onto \u003cb\u003eDon Valley Pkwy N\u003c/b\u003e", "maneuver": "merge", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.6635765, "lng": -79.3554817 }, "travel_mode": "DRIVING" }, { "distance": { "text": "1.1 km", "value": 1070 }, "duration": { "text": "1 min", "value": 53 }, "end_location": { "lat": 43.768037, "lng": -79.32926310000001 }, "html_instructions": "Take the \u003cb\u003eON-401 E\u003c/b\u003e exit", "maneuver": "ramp-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.76282690000001, "lng": -79.33669359999999 }, "travel_mode": "DRIVING" }, { "distance": { "text": "23.0 km", "value": 22985 }, "duration": { "text": "13 mins", "value": 757 }, "end_location": { "lat": 43.83811679999999, "lng": -79.07197540000001 }, "html_instructions": "Merge onto \u003cb\u003eOntario 401 Express\u003c/b\u003e", "maneuver": "merge", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.768037, "lng": -79.32926310000001 }, "travel_mode": "DRIVING" }, { "distance": { "text": "428 km", "value": 427628 }, "duration": { "text": "4 hours 0 mins", "value": 14372 }, "end_location": { "lat": 45.2083667, "lng": -74.3482841 }, "html_instructions": "Merge onto \u003cb\u003eON-401 E\u003c/b\u003e", "maneuver": "merge", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.83811679999999, "lng": -79.07197540000001 }, "travel_mode": "DRIVING" }, { "distance": { "text": "67.3 km", "value": 67286 }, "duration": { "text": "45 mins", "value": 2677 }, "end_location": { "lat": 45.4623177, "lng": -73.6095157 }, "html_instructions": "Continue onto \u003cb\u003eAutoroute du Souvenir/Autoroute 20\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eEntering Quebec\u003c/div\u003e", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.2083667, "lng": -74.3482841 }, "travel_mode": "DRIVING" }, { "distance": { "text": "3.4 km", "value": 3359 }, "duration": { "text": "3 mins", "value": 158 }, "end_location": { "lat": 45.4851306, "lng": -73.58310320000001 }, "html_instructions": "Keep \u003cb\u003eleft\u003c/b\u003e to continue on \u003cb\u003eAutoroute 720 E\u003c/b\u003e", "maneuver": "keep-left", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.4623177, "lng": -73.6095157 }, "travel_mode": "DRIVING" }, { "distance": { "text": "0.9 km", "value": 882 }, "duration": { "text": "1 min", "value": 63 }, "end_location": { "lat": 45.4911521, "lng": -73.57746179999999 }, "html_instructions": "Take exit \u003cb\u003e3\u003c/b\u003e for \u003cb\u003eRue Guy\u003c/b\u003e toward \u003cb\u003eMontréal/Centre-Ville\u003c/b\u003e", "maneuver": "ramp-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.4851306, "lng": -73.58310320000001 }, "travel_mode": "DRIVING" }, { "distance": { "text": "1.4 km", "value": 1424 }, "duration": { "text": "3 mins", "value": 161 }, "end_location": { "lat": 45.5018118, "lng": -73.56734449999999 }, "html_instructions": "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eRené-Lévesque Blvd W\u003c/b\u003e (signs for \u003cb\u003eRue city/Montréal/Centre Ville\u003c/b\u003e)", "maneuver": "turn-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.4911521, "lng": -73.57746179999999 }, "travel_mode": "DRIVING" }, { "distance": { "text": "15 m", "value": 15 }, "duration": { "text": "1 min", "value": 8 }, "end_location": { "lat": 45.5017123, "lng": -73.5672184 }, "html_instructions": "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eBoulevard Robert-Bourassa\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e", "maneuver": "turn-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.5018118, "lng": -73.56734449999999 }, "travel_mode": "DRIVING" } ], "via_waypoint": [] } ], "overview_polyline": { "points": "POINTS" }, "summary": "ON-401 E", "warnings": [], "waypoint_order": [] } ], "status": "OK" }, { 'content-type': 'application/json; charset=UTF-8', 'transfer-encoding': 'chunked' }); var input = helper.getNode("input"); input.should.have.property('id', 'input'); var directions = helper.getNode("directions"); directions.should.have.property('id', 'directions'); var output = helper.getNode("output"); output.should.have.property('id', 'output'); output.on("input", function (msg) { msg.should.have.property('payload'); msg.payload.should.have.property('status', 'OK'); msg.payload.should.have.property('routes'); msg.payload.routes.length.should.equal(1); msg.payload.routes[0].should.have.property('copyrights'); msg.payload.routes[0].should.have.property('summary'); msg.payload.routes[0].should.have.property('bounds'); msg.payload.routes[0].bounds.should.have.property('northeast'); msg.payload.routes[0].bounds.northeast.should.have.property('lat'); msg.payload.routes[0].bounds.northeast.should.have.property('lon'); msg.payload.routes[0].bounds.should.have.property('southwest'); msg.payload.routes[0].bounds.southwest.should.have.property('lat'); msg.payload.routes[0].bounds.southwest.should.have.property('lon'); msg.payload.routes[0].should.have.property('legs'); msg.payload.routes[0].legs.length.should.equal(1); msg.payload.routes[0].legs[0].should.have.property('distance'); msg.payload.routes[0].legs[0].distance.should.have.property('value'); msg.payload.routes[0].legs[0].distance.should.have.property('text'); msg.payload.routes[0].legs[0].should.have.property('duration'); msg.payload.routes[0].legs[0].duration.should.have.property('value'); msg.payload.routes[0].legs[0].duration.should.have.property('text'); msg.payload.routes[0].legs[0].should.have.property('start_location'); msg.payload.routes[0].legs[0].start_location.should.have.property('address'); msg.payload.routes[0].legs[0].start_location.should.have.property('lat'); msg.payload.routes[0].legs[0].start_location.should.have.property('lon'); msg.payload.routes[0].legs[0].should.have.property('end_location'); msg.payload.routes[0].legs[0].end_location.should.have.property('address'); msg.payload.routes[0].legs[0].end_location.should.have.property('lat'); msg.payload.routes[0].legs[0].end_location.should.have.property('lon'); msg.payload.routes[0].legs[0].should.have.property('steps'); msg.payload.routes[0].legs[0].steps.length.should.be.above(0); msg.payload.routes[0].legs[0].steps[0].should.have.property('distance'); msg.payload.routes[0].legs[0].steps[0].distance.should.have.property('value'); msg.payload.routes[0].legs[0].steps[0].distance.should.have.property('text'); msg.payload.routes[0].legs[0].steps[0].should.have.property('duration'); msg.payload.routes[0].legs[0].steps[0].duration.should.have.property('value'); msg.payload.routes[0].legs[0].steps[0].duration.should.have.property('text'); msg.payload.routes[0].legs[0].steps[0].should.have.property('start_location'); msg.payload.routes[0].legs[0].steps[0].start_location.should.have.property('lat'); msg.payload.routes[0].legs[0].steps[0].start_location.should.have.property('lon'); msg.payload.routes[0].legs[0].steps[0].should.have.property('end_location'); msg.payload.routes[0].legs[0].steps[0].end_location.should.have.property('lat'); msg.payload.routes[0].legs[0].steps[0].end_location.should.have.property('lon'); msg.payload.routes[0].legs[0].steps[0].should.have.property('html_instructions'); msg.payload.routes[0].legs[0].steps[0].should.have.property('travel_mode'); done(); }); input.send({ origin: 'Toronto', destination: 'Montreal', key: 'KEY' }); }); }); it("should succeed without a key", function (done) { helper.load([googleNode, directionsNode], [{ id: "input", type: "helper", wires: [["directions"]] }, { id: "directions", type: "google directions", wires: [["output"]], }, { id: "output", type: "helper" } ], function () { nock('https://maps.googleapis.com:443') .get('/maps/api/directions/json?origin=Toronto&destination=Montreal') .reply(200, { "routes": [ { "bounds": { "northeast": { "lat": 45.5017123, "lng": -73.5672184 }, "southwest": { "lat": 43.6533103, "lng": -79.3827675 } }, "copyrights": "Map data ©2015 Google", "legs": [ { "distance": { "text": "541 km", "value": 540536 }, "duration": { "text": "5 hours 20 mins", "value": 19191 }, "end_address": "Montreal, QC, Canada", "end_location": { "lat": 45.5017123, "lng": -73.5672184 }, "start_address": "Toronto, ON, Canada", "start_location": { "lat": 43.6533103, "lng": -79.3827675 }, "steps": [ { "distance": { "text": "0.3 km", "value": 280 }, "duration": { "text": "1 min", "value": 34 }, "end_location": { "lat": 43.6557259, "lng": -79.38373319999999 }, "html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eBay St\u003c/b\u003e toward \u003cb\u003eHagerman St\u003c/b\u003e", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.6533103, "lng": -79.3827675 }, "travel_mode": "DRIVING" }, { "distance": { "text": "2.5 km", "value": 2492 }, "duration": { "text": "5 mins", "value": 320 }, "end_location": { "lat": 43.6618361, "lng": -79.3545312 }, "html_instructions": "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eDundas St W\u003c/b\u003e", "maneuver": "turn-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.6557259, "lng": -79.38373319999999 }, "travel_mode": "DRIVING" }, { "distance": { "text": "0.2 km", "value": 209 }, "duration": { "text": "1 min", "value": 18 }, "end_location": { "lat": 43.6635765, "lng": -79.3554817 }, "html_instructions": "Turn \u003cb\u003eleft\u003c/b\u003e onto the \u003cb\u003eDon Valley Parkway\u003c/b\u003e ramp", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.6618361, "lng": -79.3545312 }, "travel_mode": "DRIVING" }, { "distance": { "text": "12.9 km", "value": 12906 }, "duration": { "text": "10 mins", "value": 570 }, "end_location": { "lat": 43.76282690000001, "lng": -79.33669359999999 }, "html_instructions": "Merge onto \u003cb\u003eDon Valley Pkwy N\u003c/b\u003e", "maneuver": "merge", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.6635765, "lng": -79.3554817 }, "travel_mode": "DRIVING" }, { "distance": { "text": "1.1 km", "value": 1070 }, "duration": { "text": "1 min", "value": 53 }, "end_location": { "lat": 43.768037, "lng": -79.32926310000001 }, "html_instructions": "Take the \u003cb\u003eON-401 E\u003c/b\u003e exit", "maneuver": "ramp-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.76282690000001, "lng": -79.33669359999999 }, "travel_mode": "DRIVING" }, { "distance": { "text": "23.0 km", "value": 22985 }, "duration": { "text": "13 mins", "value": 757 }, "end_location": { "lat": 43.83811679999999, "lng": -79.07197540000001 }, "html_instructions": "Merge onto \u003cb\u003eOntario 401 Express\u003c/b\u003e", "maneuver": "merge", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.768037, "lng": -79.32926310000001 }, "travel_mode": "DRIVING" }, { "distance": { "text": "428 km", "value": 427628 }, "duration": { "text": "4 hours 0 mins", "value": 14372 }, "end_location": { "lat": 45.2083667, "lng": -74.3482841 }, "html_instructions": "Merge onto \u003cb\u003eON-401 E\u003c/b\u003e", "maneuver": "merge", "polyline": { "points": "POINTS" }, "start_location": { "lat": 43.83811679999999, "lng": -79.07197540000001 }, "travel_mode": "DRIVING" }, { "distance": { "text": "67.3 km", "value": 67286 }, "duration": { "text": "45 mins", "value": 2677 }, "end_location": { "lat": 45.4623177, "lng": -73.6095157 }, "html_instructions": "Continue onto \u003cb\u003eAutoroute du Souvenir/Autoroute 20\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eEntering Quebec\u003c/div\u003e", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.2083667, "lng": -74.3482841 }, "travel_mode": "DRIVING" }, { "distance": { "text": "3.4 km", "value": 3359 }, "duration": { "text": "3 mins", "value": 158 }, "end_location": { "lat": 45.4851306, "lng": -73.58310320000001 }, "html_instructions": "Keep \u003cb\u003eleft\u003c/b\u003e to continue on \u003cb\u003eAutoroute 720 E\u003c/b\u003e", "maneuver": "keep-left", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.4623177, "lng": -73.6095157 }, "travel_mode": "DRIVING" }, { "distance": { "text": "0.9 km", "value": 882 }, "duration": { "text": "1 min", "value": 63 }, "end_location": { "lat": 45.4911521, "lng": -73.57746179999999 }, "html_instructions": "Take exit \u003cb\u003e3\u003c/b\u003e for \u003cb\u003eRue Guy\u003c/b\u003e toward \u003cb\u003eMontréal/Centre-Ville\u003c/b\u003e", "maneuver": "ramp-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.4851306, "lng": -73.58310320000001 }, "travel_mode": "DRIVING" }, { "distance": { "text": "1.4 km", "value": 1424 }, "duration": { "text": "3 mins", "value": 161 }, "end_location": { "lat": 45.5018118, "lng": -73.56734449999999 }, "html_instructions": "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eRené-Lévesque Blvd W\u003c/b\u003e (signs for \u003cb\u003eRue city/Montréal/Centre Ville\u003c/b\u003e)", "maneuver": "turn-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.4911521, "lng": -73.57746179999999 }, "travel_mode": "DRIVING" }, { "distance": { "text": "15 m", "value": 15 }, "duration": { "text": "1 min", "value": 8 }, "end_location": { "lat": 45.5017123, "lng": -73.5672184 }, "html_instructions": "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eBoulevard Robert-Bourassa\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e", "maneuver": "turn-right", "polyline": { "points": "POINTS" }, "start_location": { "lat": 45.5018118, "lng": -73.56734449999999 }, "travel_mode": "DRIVING" } ], "via_waypoint": [] } ], "overview_polyline": { "points": "POINTS" }, "summary": "ON-401 E", "warnings": [], "waypoint_order": [] } ], "status": "OK" }, { 'content-type': 'application/json; charset=UTF-8', 'transfer-encoding': 'chunked' }); var input = helper.getNode("input"); input.should.have.property('id', 'input'); var directions = helper.getNode("directions"); directions.should.have.property('id', 'directions'); var output = helper.getNode("output"); output.should.have.property('id', 'output'); output.on("input", function (msg) { msg.should.have.property('payload'); msg.payload.should.have.property('status', 'OK'); msg.payload.should.have.property('routes'); msg.payload.routes.length.should.equal(1); done(); }); input.send({ origin: 'Toronto', destination: 'Montreal' }); }); }); it("should filter route by transit mode", function (done) { helper.load([googleNode, directionsNode], [{ id: "input", type: "helper", wires: [["directions"]] }, { id: "directions", type: "google directions", wires: [["output"]], }, { id: "output", type: "helper" } ], function () { nock('https://maps.googleapis.com:443') .get('/maps/api/directions/json?key=KEY&origin=Washington%2C%20DC&destination=Alexandria%2C%20VA&mode=bicycling') .reply(200, { "routes": [ { "bounds": { "northeast": { "lat": 38.9079272, "lng": -77.03271699999999 }, "southwest": { "lat": 38.8033974, "lng": -77.0489374 } }, "copyrights": "Map data ©2015 Google", "legs": [ { "distance": { "text": "8.9 mi", "value": 14273 }, "duration": { "text": "49 mins", "value": 2932 }, "end_address": "Alexandria, VA, USA", "end_location": { "lat": 38.804842, "lng": -77.0469806 }, "start_address": "Washington, DC, USA", "start_location":