|
| 1 | +const { expect } = require('chai'); |
| 2 | +const { MockRuntime } = require('@axway/api-builder-sdk'); |
| 3 | + |
| 4 | +const getPlugin = require('../src'); |
| 5 | +const actions = require('../src/reverseGeocode'); |
| 6 | + |
| 7 | +const validPluginConfig = require('./config/google-maps.testconfig').pluginConfig['@axway-api-builder-ext/api-builder-plugin-fn-google-maps']; |
| 8 | +const invalidPluginConfig = require('./config/google-maps.incomplete').pluginConfig['@axway-api-builder-ext/api-builder-plugin-fn-google-maps']; |
| 9 | + |
| 10 | +describe('Google-Maps Reverse-Geocode-API Tests', () => { |
| 11 | + let runtime; |
| 12 | + before(async () => invalidRuntime = new MockRuntime(await getPlugin(invalidPluginConfig))); |
| 13 | + before(async () => runtime = new MockRuntime(await getPlugin(validPluginConfig))); |
| 14 | + |
| 15 | + describe('#constructor', () => { |
| 16 | + it('should define flow-nodes', () => { |
| 17 | + expect(actions).to.be.an('object'); |
| 18 | + expect(actions.reverseGeocode).to.be.a('function'); |
| 19 | + expect(runtime).to.exist; |
| 20 | + const flownode = runtime.getFlowNode('googleMaps'); |
| 21 | + expect(flownode).to.be.a('object'); |
| 22 | + expect(flownode.name).to.equal('Google Maps'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should define valid flow-nodes', () => { |
| 26 | + expect(runtime.validate()).to.not.throw; |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('#reverseGeocode', () => { |
| 31 | + it('should error when neither latLng or place_id is given', async () => { |
| 32 | + const flowNode = runtime.getFlowNode('googleMaps'); |
| 33 | + |
| 34 | + const result = await flowNode.reverseGeocode({ |
| 35 | + place_id: null, |
| 36 | + latlng: null |
| 37 | + }); |
| 38 | + |
| 39 | + expect(result.callCount).to.equal(1); |
| 40 | + expect(result.output).to.equal('error'); |
| 41 | + expect(result.args[0]).to.equal(null); |
| 42 | + expect(result.context).to.be.an('Object'); |
| 43 | + expect(result.context.error).to.have.property('message', 'You must at least provide latlng or place_id'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should error if not configured - API-Key is missing', async () => { |
| 47 | + const flowNode = invalidRuntime.getFlowNode('googleMaps'); |
| 48 | + |
| 49 | + const result = await flowNode.reverseGeocode({ |
| 50 | + place_id: 'ChIJQxkLC8ZQqEcRdfnVRF5vXVs' |
| 51 | + }); |
| 52 | + |
| 53 | + expect(result.callCount).to.equal(1); |
| 54 | + expect(result.output).to.equal('error'); |
| 55 | + expect(result.args[0]).to.equal(null); |
| 56 | + expect(result.context).to.be.an('Object'); |
| 57 | + expect(result.context.error).to.have.property('message', 'Google API-Key is missing. Please complete your configuration in conf/google-maps.default.js'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('Invalid request with no params given at all', async () => { |
| 61 | + const flowNode = runtime.getFlowNode('googleMaps'); |
| 62 | + |
| 63 | + const result = await flowNode.reverseGeocode({}); |
| 64 | + |
| 65 | + expect(result.callCount).to.equal(1); |
| 66 | + expect(result.output).to.equal('error'); |
| 67 | + expect(result.args[0]).to.equal(null); |
| 68 | + expect(result.context).to.be.an('Object'); |
| 69 | + expect(result.context.error).to.have.property('message', 'You must at least provide latlng or place_id'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('Valid request with a given place_id', async () => { |
| 73 | + const flowNode = runtime.getFlowNode('googleMaps'); |
| 74 | + |
| 75 | + const result = await flowNode.reverseGeocode({ |
| 76 | + place_id: 'ChIJQxkLC8ZQqEcRdfnVRF5vXVs' |
| 77 | + }); |
| 78 | + |
| 79 | + expect(result.callCount).to.equal(1); |
| 80 | + expect(result.output).to.equal('next'); |
| 81 | + expect(result.args[0]).to.equal(null); |
| 82 | + expect(result.context).to.be.an('Object'); |
| 83 | + expect(result.context.result).to.deep.include({ status: 'OK' }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('Request with an invalid place_id', async () => { |
| 87 | + const flowNode = runtime.getFlowNode('googleMaps'); |
| 88 | + |
| 89 | + const result = await flowNode.reverseGeocode({ |
| 90 | + place_id: 'ChIJQxkLC8ZQqEcRdabVRF5vXVs' |
| 91 | + }); |
| 92 | + |
| 93 | + expect(result.callCount).to.equal(1); |
| 94 | + expect(result.output).to.equal('notFound'); |
| 95 | + expect(result.args[0]).to.equal(null); |
| 96 | + expect(result.context).to.be.an('Object'); |
| 97 | + expect(result.context.notFound).to.deep.include({ status: 'ZERO_RESULTS' }); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments