From 6eebee7f49196db240e8dabb75341f9b4c227ff6 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Date: Sat, 3 Feb 2018 23:28:27 +0000 Subject: [PATCH 01/19] aircraft endpoint initial commit --- endpoints/aircraft/documentation.md | 13 +++ endpoints/aircraft/index.js | 86 ++++++++++++++++++++ endpoints/aircraft/tests/integration_test.js | 34 ++++++++ 3 files changed, 133 insertions(+) create mode 100644 endpoints/aircraft/documentation.md create mode 100644 endpoints/aircraft/index.js create mode 100644 endpoints/aircraft/tests/integration_test.js diff --git a/endpoints/aircraft/documentation.md b/endpoints/aircraft/documentation.md new file mode 100644 index 00000000..6c6a330a --- /dev/null +++ b/endpoints/aircraft/documentation.md @@ -0,0 +1,13 @@ +# Icelandic Aircraft Registry + +Source: [The Icelandic Transport Authority](https://www.samgongustofa.is/flug/loftfor/loftfaraskra/) + +- GET [/aircraft](https://apis.is/aircraft) + +Search the Icelandic aircraft registry + +| Parameters | Description | Example | +|-------------------|-----------------------------------------------------------------------|-----------------| +| Search (required) | Aircraft identification, registration number, type, owner or operator | [TF-AAC](https://apis.is/aircraft?search=TF-AAC), [1073](https://apis.is/ship?search=1073) | + +--- diff --git a/endpoints/aircraft/index.js b/endpoints/aircraft/index.js new file mode 100644 index 00000000..d31c585a --- /dev/null +++ b/endpoints/aircraft/index.js @@ -0,0 +1,86 @@ +/* eslint-disable no-undef */ +/* eslint-disable no-restricted-globals */ +/* eslint-disable prefer-promise-reject-errors */ +const request = require('request') +const $ = require('cheerio') +const h = require('apis-helpers') +const app = require('../../server') + +const lookupAircraft = searchStr => new Promise((resolve, reject) => { + // Encode searchString so that Icelandic characters will work + const searchString = encodeURIComponent(searchStr) + const url = `https://www.samgongustofa.is/flug/loftfor/loftfaraskra?aq=${searchString}` + request.get({ + headers: { 'User-Agent': h.browser() }, + url, + }, (error, response, body) => { + if (error || response.statusCode !== 200) { + reject('www.samgongustofa.is refuses to respond or give back data') + } + + const data = $(body) + const fieldList = [] + data.find('.vehicleinfo ul').each((index, element) => { + const fields = [] + $(element).find('li').each((i, el) => { + let val; + if (i < 7) { + val = $(el).find('span').text() + + } else { + // i === 7 contains info about aircraft owner + // i === 8 contains info about aircraft operator + // We'll parse these fields separately + + const text = $(el).find('span').text(); + const info = text.split(/\s{3,}/g); + + val = { + name: info[1], + address: info[2], + locality: info[3], + country: info[4], + } + } + + fields.push(val) + }) + + if (fields.length > 0) { + fieldList.push(fields) + } + }) + + if (fieldList.length > 0 && fieldList[0].length > 0) { + resolve(fieldList.map((fields) => { + return { + id: fields[0], + registrationNumber: parseInt(fields[1]), + type: fields[2], + buildYear: parseInt(fields[3]), + serialNumber: parseInt(fields[4]), + maxWeight: parseInt(fields[5]), + passengers: parseInt(fields[6]), + owner: fields[7], + operator: fields[8], + } + })) + } else { + reject(`No aircraft found with the query ${searchStr}`) + } + }) +}) + +app.get('/aircraft', (req, res) => { + const search = req.query.search || '' + + if (!search) { + return res.status(431).json({ error: 'Please provide a valid search string to lookup' }) + } + + lookupAircraft(search) + .then(aircraft => res.cache().json({ results: aircraft })) + .catch(error => res.status(500).json({ error })) +}) + +module.exports = lookupAircraft diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js new file mode 100644 index 00000000..7298de1a --- /dev/null +++ b/endpoints/aircraft/tests/integration_test.js @@ -0,0 +1,34 @@ +/* eslint-disable import/extensions */ +const assert = require('assert') +const request = require('request') +const helpers = require('../../../lib/test_helpers.js') + +describe('aircraft', () => { + it('should return an array of objects containing correct fields', (done) => { + const fieldsToCheckFor = [ + 'id', + 'registrationNumber', + 'type', + 'buildYear', + 'serialNumber', + 'maxWeight', + 'passengers', + 'owner', + 'operator', + ] + const params = helpers.testRequestParams('/aircraft', { search: '100' }) + const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor) + request.get(params, resultHandler) + }) + it('should return a 404 when an aircraft is not found', (done) => { + const params = helpers.testRequestParams('/aircraft', { search: 'loftur' }) + request.get(params, (error, response, body) => { + if (error) { + return done(error) + } + const json = JSON.parse(body) + assert.equal(json.error, 'No aircraft found with the query \'loftur\'') + done() + }) + }) +}) From 3cc779657f21a350efc4b9adcddaf751256ad877 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Date: Wed, 7 Feb 2018 22:37:24 +0000 Subject: [PATCH 02/19] lint errors fixed --- endpoints/aircraft/index.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/endpoints/aircraft/index.js b/endpoints/aircraft/index.js index d31c585a..5acafc1f 100644 --- a/endpoints/aircraft/index.js +++ b/endpoints/aircraft/index.js @@ -23,17 +23,16 @@ const lookupAircraft = searchStr => new Promise((resolve, reject) => { data.find('.vehicleinfo ul').each((index, element) => { const fields = [] $(element).find('li').each((i, el) => { - let val; + let val if (i < 7) { val = $(el).find('span').text() - - } else { + } else { // i === 7 contains info about aircraft owner // i === 8 contains info about aircraft operator // We'll parse these fields separately - const text = $(el).find('span').text(); - const info = text.split(/\s{3,}/g); + const text = $(el).find('span').text() + const info = text.split(/\s{3,}/g) val = { name: info[1], @@ -45,7 +44,7 @@ const lookupAircraft = searchStr => new Promise((resolve, reject) => { fields.push(val) }) - + if (fields.length > 0) { fieldList.push(fields) } @@ -55,12 +54,12 @@ const lookupAircraft = searchStr => new Promise((resolve, reject) => { resolve(fieldList.map((fields) => { return { id: fields[0], - registrationNumber: parseInt(fields[1]), + registrationNumber: parseInt(fields[1], 10), type: fields[2], - buildYear: parseInt(fields[3]), - serialNumber: parseInt(fields[4]), - maxWeight: parseInt(fields[5]), - passengers: parseInt(fields[6]), + buildYear: parseInt(fields[3], 10), + serialNumber: parseInt(fields[4], 10), + maxWeight: parseInt(fields[5], 10), + passengers: parseInt(fields[6], 10), owner: fields[7], operator: fields[8], } From a481422dc95c33852c1aa718ab652b7050902fff Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Sun, 3 Feb 2019 20:26:23 +0000 Subject: [PATCH 03/19] Fix integration test for aircraft endpoint --- endpoints/aircraft/tests/integration_test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index 7298de1a..e1efc537 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -16,18 +16,18 @@ describe('aircraft', () => { 'owner', 'operator', ] - const params = helpers.testRequestParams('/aircraft', { search: '100' }) + const params = helpers.testRequestParams('/aircraft', { aq: '100' }) const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor) request.get(params, resultHandler) }) it('should return a 404 when an aircraft is not found', (done) => { - const params = helpers.testRequestParams('/aircraft', { search: 'loftur' }) + const params = helpers.testRequestParams('/aircraft', { aq: 'loftur' }) request.get(params, (error, response, body) => { if (error) { return done(error) } const json = JSON.parse(body) - assert.equal(json.error, 'No aircraft found with the query \'loftur\'') + assert.equal(json.error, 'No aircraft found with the query loftur') done() }) }) From aa3472d8b33c4e3135f1a2d5e3d4dba4a89a343c Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Sun, 3 Feb 2019 20:44:40 +0000 Subject: [PATCH 04/19] Pull from apis --- .env | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 00000000..c51ccb7f --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +RECORD_MOCK_DATA=true +SENTRY_URL= \ No newline at end of file From 301194d6886c464acda7f626e7c7b1c86486fa78 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Sun, 3 Feb 2019 20:46:49 +0000 Subject: [PATCH 05/19] f --- .env | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index c51ccb7f..00000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -RECORD_MOCK_DATA=true -SENTRY_URL= \ No newline at end of file From f06651d1ba5be9e9879c3e73f6a4cf1e3e285124 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Sun, 3 Feb 2019 20:50:39 +0000 Subject: [PATCH 06/19] Small fixes --- endpoints/aircraft/documentation.md | 2 +- endpoints/aircraft/tests/integration_test.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/endpoints/aircraft/documentation.md b/endpoints/aircraft/documentation.md index 6c6a330a..53ca9eec 100644 --- a/endpoints/aircraft/documentation.md +++ b/endpoints/aircraft/documentation.md @@ -8,6 +8,6 @@ Search the Icelandic aircraft registry | Parameters | Description | Example | |-------------------|-----------------------------------------------------------------------|-----------------| -| Search (required) | Aircraft identification, registration number, type, owner or operator | [TF-AAC](https://apis.is/aircraft?search=TF-AAC), [1073](https://apis.is/ship?search=1073) | +| Search (required) | Aircraft identification, registration number, type, owner or operator | [TF-AAC](https://apis.is/aircraft?search=TF-AAC), [1073](https://apis.is/aircraft?search=1073) | --- diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index e1efc537..0712fa5e 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -16,12 +16,12 @@ describe('aircraft', () => { 'owner', 'operator', ] - const params = helpers.testRequestParams('/aircraft', { aq: '100' }) + const params = helpers.testRequestParams('/aircraft', { search: '100' }) const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor) request.get(params, resultHandler) }) it('should return a 404 when an aircraft is not found', (done) => { - const params = helpers.testRequestParams('/aircraft', { aq: 'loftur' }) + const params = helpers.testRequestParams('/aircraft', { search: 'loftur' }) request.get(params, (error, response, body) => { if (error) { return done(error) From f6edf6edcab319c6376be68a55fdf89fe01f44a4 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Sun, 3 Feb 2019 21:47:38 +0000 Subject: [PATCH 07/19] Finalize aircraft endpoint code --- endpoints/aircraft/documentation.md | 2 +- endpoints/aircraft/index.js | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/endpoints/aircraft/documentation.md b/endpoints/aircraft/documentation.md index 53ca9eec..a1a2a099 100644 --- a/endpoints/aircraft/documentation.md +++ b/endpoints/aircraft/documentation.md @@ -8,6 +8,6 @@ Search the Icelandic aircraft registry | Parameters | Description | Example | |-------------------|-----------------------------------------------------------------------|-----------------| -| Search (required) | Aircraft identification, registration number, type, owner or operator | [TF-AAC](https://apis.is/aircraft?search=TF-AAC), [1073](https://apis.is/aircraft?search=1073) | +| Search (required) | Aircraft identification, registration number, type, owner or operator | [TF-AAC](https://apis.is/aircraft?search=TF-AAC), [1073](https://apis.is/aircraft?search=1073), [Flugfélag Íslands](https://apis.is/aircraft?search=Flugfélag+Íslands)| --- diff --git a/endpoints/aircraft/index.js b/endpoints/aircraft/index.js index 5acafc1f..3442999f 100644 --- a/endpoints/aircraft/index.js +++ b/endpoints/aircraft/index.js @@ -7,9 +7,7 @@ const h = require('apis-helpers') const app = require('../../server') const lookupAircraft = searchStr => new Promise((resolve, reject) => { - // Encode searchString so that Icelandic characters will work - const searchString = encodeURIComponent(searchStr) - const url = `https://www.samgongustofa.is/flug/loftfor/loftfaraskra?aq=${searchString}` + const url = `https://www.samgongustofa.is/flug/loftfor/loftfaraskra?aq=${searchStr}` request.get({ headers: { 'User-Agent': h.browser() }, url, @@ -51,35 +49,38 @@ const lookupAircraft = searchStr => new Promise((resolve, reject) => { }) if (fieldList.length > 0 && fieldList[0].length > 0) { - resolve(fieldList.map((fields) => { - return { + const aircraft = fieldList.map((fields) => + ({ id: fields[0], registrationNumber: parseInt(fields[1], 10), type: fields[2], - buildYear: parseInt(fields[3], 10), + productionYear: parseInt(fields[3], 10), serialNumber: parseInt(fields[4], 10), maxWeight: parseInt(fields[5], 10), passengers: parseInt(fields[6], 10), owner: fields[7], operator: fields[8], - } - })) + }) + ) + + resolve(aircraft) } else { reject(`No aircraft found with the query ${searchStr}`) } }) }) -app.get('/aircraft', (req, res) => { +app.get('/aircraft', async (req, res) => { const search = req.query.search || '' if (!search) { return res.status(431).json({ error: 'Please provide a valid search string to lookup' }) } - lookupAircraft(search) - .then(aircraft => res.cache().json({ results: aircraft })) - .catch(error => res.status(500).json({ error })) + try { + const results = await lookupAircraft(search, res) + res.cache().json({ results }) + } catch (error) { + res.status(500).json({ error }) + } }) - -module.exports = lookupAircraft From e03eab99b595a162325a78f6bcb012c7ba487416 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Sun, 3 Feb 2019 21:54:38 +0000 Subject: [PATCH 08/19] Test fix --- endpoints/aircraft/tests/integration_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index 0712fa5e..067e33a7 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -9,7 +9,7 @@ describe('aircraft', () => { 'id', 'registrationNumber', 'type', - 'buildYear', + 'constructionYear', 'serialNumber', 'maxWeight', 'passengers', From 6d7a801fe9fa7111c2e7da055d8c584260aa72a0 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Sun, 3 Feb 2019 21:58:51 +0000 Subject: [PATCH 09/19] Test fix (2) --- endpoints/aircraft/tests/integration_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index 067e33a7..b4e7c706 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -9,7 +9,7 @@ describe('aircraft', () => { 'id', 'registrationNumber', 'type', - 'constructionYear', + 'productionYear', 'serialNumber', 'maxWeight', 'passengers', From d52f74c51cc5af3c4223aa469bdc2e3fd00a3005 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Sun, 3 Feb 2019 22:16:43 +0000 Subject: [PATCH 10/19] Test villa (3) --- endpoints/aircraft/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/endpoints/aircraft/index.js b/endpoints/aircraft/index.js index 3442999f..3302c0c8 100644 --- a/endpoints/aircraft/index.js +++ b/endpoints/aircraft/index.js @@ -62,6 +62,7 @@ const lookupAircraft = searchStr => new Promise((resolve, reject) => { operator: fields[8], }) ) + console.info(aircraft) resolve(aircraft) } else { @@ -70,15 +71,16 @@ const lookupAircraft = searchStr => new Promise((resolve, reject) => { }) }) -app.get('/aircraft', async (req, res) => { - const search = req.query.search || '' +app.get('/aircraft/:search?', async (req, res) => { + const search = (req.query.search || req.params.search || '').replace(' ', '+') - if (!search) { + if (search === '') { return res.status(431).json({ error: 'Please provide a valid search string to lookup' }) } try { - const results = await lookupAircraft(search, res) + const results = await lookupAircraft(search) + console.info({ results }) res.cache().json({ results }) } catch (error) { res.status(500).json({ error }) From e69408288e9b1c53d1c0b719f1d47adfe7f1652c Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Mon, 4 Feb 2019 11:47:50 +0000 Subject: [PATCH 11/19] Test failure fix attempt --- endpoints/aircraft/index.js | 10 +++++----- endpoints/aircraft/tests/integration_test.js | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/endpoints/aircraft/index.js b/endpoints/aircraft/index.js index 3302c0c8..6e0db54b 100644 --- a/endpoints/aircraft/index.js +++ b/endpoints/aircraft/index.js @@ -53,8 +53,8 @@ const lookupAircraft = searchStr => new Promise((resolve, reject) => { ({ id: fields[0], registrationNumber: parseInt(fields[1], 10), - type: fields[2], - productionYear: parseInt(fields[3], 10), + type: fields[2].replace('\t', ''), + buildYear: parseInt(fields[3], 10), serialNumber: parseInt(fields[4], 10), maxWeight: parseInt(fields[5], 10), passengers: parseInt(fields[6], 10), @@ -62,7 +62,6 @@ const lookupAircraft = searchStr => new Promise((resolve, reject) => { operator: fields[8], }) ) - console.info(aircraft) resolve(aircraft) } else { @@ -75,14 +74,15 @@ app.get('/aircraft/:search?', async (req, res) => { const search = (req.query.search || req.params.search || '').replace(' ', '+') if (search === '') { - return res.status(431).json({ error: 'Please provide a valid search string to lookup' }) + return res.status(400).json({ error: 'Please provide a valid search string to lookup' }) } try { const results = await lookupAircraft(search) - console.info({ results }) res.cache().json({ results }) } catch (error) { res.status(500).json({ error }) } }) + +module.exports = lookupAircraft diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index b4e7c706..9148e7e2 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -9,7 +9,7 @@ describe('aircraft', () => { 'id', 'registrationNumber', 'type', - 'productionYear', + 'buildYear', 'serialNumber', 'maxWeight', 'passengers', @@ -26,8 +26,21 @@ describe('aircraft', () => { if (error) { return done(error) } - const json = JSON.parse(body) - assert.equal(json.error, 'No aircraft found with the query loftur') + //const json = JSON.parse(body) + //assert.equal(json.error, 'No aircraft found with the query loftur') + assert.strictEqual(response.statusCode, 404, `Invalid status code. Actual: ${response.statusCode}. Expected: 404`) + done() + }) + }) + it('should return a 400 when a search parameter is not provided', (done) => { + const params = helpers.testRequestParams('/aircraft') + request.get(params, (error, response, body) => { + if (error) { + return done(error) + } + //const json = JSON.parse(body) + //assert.equal(json.error, 'Please provide a valid search string to lookup') + assert.equal(response.statusCode, 400, `Invalid status code. Actual: ${response.statusCode}. Expected: 400`) done() }) }) From 5f0ec1632ccd9c1f57001c8f87af81e29423bbd7 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Mon, 4 Feb 2019 12:06:23 +0000 Subject: [PATCH 12/19] Fix lint errors --- endpoints/aircraft/tests/integration_test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index 9148e7e2..8ba319f3 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -22,24 +22,24 @@ describe('aircraft', () => { }) it('should return a 404 when an aircraft is not found', (done) => { const params = helpers.testRequestParams('/aircraft', { search: 'loftur' }) - request.get(params, (error, response, body) => { + request.get(params, (error, response) => { if (error) { return done(error) } - //const json = JSON.parse(body) - //assert.equal(json.error, 'No aircraft found with the query loftur') + // const json = JSON.parse(body) + // assert.equal(json.error, 'No aircraft found with the query loftur') assert.strictEqual(response.statusCode, 404, `Invalid status code. Actual: ${response.statusCode}. Expected: 404`) done() }) }) it('should return a 400 when a search parameter is not provided', (done) => { const params = helpers.testRequestParams('/aircraft') - request.get(params, (error, response, body) => { + request.get(params, (error, response) => { if (error) { return done(error) } - //const json = JSON.parse(body) - //assert.equal(json.error, 'Please provide a valid search string to lookup') + // const json = JSON.parse(body) + // assert.equal(json.error, 'Please provide a valid search string to lookup') assert.equal(response.statusCode, 400, `Invalid status code. Actual: ${response.statusCode}. Expected: 400`) done() }) From cdfe03d7dba60dcbf6433be116a0896a9a507af8 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Mon, 4 Feb 2019 12:13:30 +0000 Subject: [PATCH 13/19] Debug CI test --- endpoints/aircraft/index.js | 1 + endpoints/aircraft/tests/integration_test.js | 16 ++++++++-------- lib/test_helpers.js | 3 ++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/endpoints/aircraft/index.js b/endpoints/aircraft/index.js index 6e0db54b..f48517a7 100644 --- a/endpoints/aircraft/index.js +++ b/endpoints/aircraft/index.js @@ -79,6 +79,7 @@ app.get('/aircraft/:search?', async (req, res) => { try { const results = await lookupAircraft(search) + console.info({ results }) res.cache().json({ results }) } catch (error) { res.status(500).json({ error }) diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index 8ba319f3..441443f4 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -17,30 +17,30 @@ describe('aircraft', () => { 'operator', ] const params = helpers.testRequestParams('/aircraft', { search: '100' }) + console.info(params) const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor) request.get(params, resultHandler) }) it('should return a 404 when an aircraft is not found', (done) => { const params = helpers.testRequestParams('/aircraft', { search: 'loftur' }) - request.get(params, (error, response) => { + request.get(params, (error, response, body) => { if (error) { return done(error) } - // const json = JSON.parse(body) - // assert.equal(json.error, 'No aircraft found with the query loftur') - assert.strictEqual(response.statusCode, 404, `Invalid status code. Actual: ${response.statusCode}. Expected: 404`) + console.info(json) + const json = JSON.parse(body) + assert.strictEqual(json.error, 'No aircraft found with the query loftur') done() }) }) it('should return a 400 when a search parameter is not provided', (done) => { const params = helpers.testRequestParams('/aircraft') - request.get(params, (error, response) => { + request.get(params, (error, response, body) => { if (error) { return done(error) } - // const json = JSON.parse(body) - // assert.equal(json.error, 'Please provide a valid search string to lookup') - assert.equal(response.statusCode, 400, `Invalid status code. Actual: ${response.statusCode}. Expected: 400`) + const json = JSON.parse(body) + assert.strictEqual(json.error, 'Please provide a valid search string to lookup') done() }) }) diff --git a/lib/test_helpers.js b/lib/test_helpers.js index dd567432..588722af 100644 --- a/lib/test_helpers.js +++ b/lib/test_helpers.js @@ -53,7 +53,8 @@ exports.testRequestHandlerForFields = (done, fieldsToCheckFor, customCallback, c } catch (e) { throw e } - + console.info(body) + console.info(json) // Check for the presence of the results property assertResults(json, canBeEmpty) From a63566a768fb624b5da8a84346453b858768da1b Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Mon, 4 Feb 2019 12:24:28 +0000 Subject: [PATCH 14/19] Fix lint error --- endpoints/aircraft/tests/integration_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index 441443f4..4292bf00 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -27,8 +27,8 @@ describe('aircraft', () => { if (error) { return done(error) } - console.info(json) const json = JSON.parse(body) + console.info(json) assert.strictEqual(json.error, 'No aircraft found with the query loftur') done() }) From dd0b09e29de00fb77e6d2c74042d7d14605af1d9 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Mon, 4 Feb 2019 12:55:54 +0000 Subject: [PATCH 15/19] Add nock to integration test --- endpoints/aircraft/index.js | 2 +- endpoints/aircraft/tests/integration_test.js | 88 +-- endpoints/aircraft/tests/loftfaraskra.fixture | 514 ++++++++++++++++++ 3 files changed, 568 insertions(+), 36 deletions(-) create mode 100644 endpoints/aircraft/tests/loftfaraskra.fixture diff --git a/endpoints/aircraft/index.js b/endpoints/aircraft/index.js index f48517a7..1433b415 100644 --- a/endpoints/aircraft/index.js +++ b/endpoints/aircraft/index.js @@ -7,7 +7,7 @@ const h = require('apis-helpers') const app = require('../../server') const lookupAircraft = searchStr => new Promise((resolve, reject) => { - const url = `https://www.samgongustofa.is/flug/loftfor/loftfaraskra?aq=${searchStr}` + const url = `http://www.samgongustofa.is/flug/loftfor/loftfaraskra?aq=${searchStr}` request.get({ headers: { 'User-Agent': h.browser() }, url, diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index 4292bf00..fe9f6cea 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -1,47 +1,65 @@ /* eslint-disable import/extensions */ +const fs = require('fs') const assert = require('assert') +const nock = require('nock') const request = require('request') const helpers = require('../../../lib/test_helpers.js') describe('aircraft', () => { - it('should return an array of objects containing correct fields', (done) => { - const fieldsToCheckFor = [ - 'id', - 'registrationNumber', - 'type', - 'buildYear', - 'serialNumber', - 'maxWeight', - 'passengers', - 'owner', - 'operator', - ] - const params = helpers.testRequestParams('/aircraft', { search: '100' }) - console.info(params) - const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor) - request.get(params, resultHandler) + before(() => { + nock('http://www.samgongustofa.is/flug/loftfor/loftfaraskra') + .get('/') + .query({ aq: '100' }) + .reply(200, fs.readFileSync(`${__dirname}/loftfaraskra.fixture`)) }) - it('should return a 404 when an aircraft is not found', (done) => { - const params = helpers.testRequestParams('/aircraft', { search: 'loftur' }) - request.get(params, (error, response, body) => { - if (error) { - return done(error) - } - const json = JSON.parse(body) - console.info(json) - assert.strictEqual(json.error, 'No aircraft found with the query loftur') - done() + + describe('correct-fields', () => { + this.timeout(20000) + it('should return an array of objects containing correct fields', (done) => { + const fieldsToCheckFor = [ + 'id', + 'registrationNumber', + 'type', + 'buildYear', + 'serialNumber', + 'maxWeight', + 'passengers', + 'owner', + 'operator', + ] + const params = helpers.testRequestParams('/aircraft', { search: '100' }) + console.info(params) + const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor) + request.get(params, resultHandler) }) }) - it('should return a 400 when a search parameter is not provided', (done) => { - const params = helpers.testRequestParams('/aircraft') - request.get(params, (error, response, body) => { - if (error) { - return done(error) - } - const json = JSON.parse(body) - assert.strictEqual(json.error, 'Please provide a valid search string to lookup') - done() + + describe('aircraft-not-found', () => { + this.timeout(20000) + it('should return a 404 when an aircraft is not found', (done) => { + const params = helpers.testRequestParams('/aircraft', { search: 'loftur' }) + request.get(params, (error, response, body) => { + if (error) { + return done(error) + } + const json = JSON.parse(body) + console.info(json) + assert.strictEqual(json.error, 'No aircraft found with the query loftur') + done() + }) + }) + + this.timeout(20000) + it('should return a 400 when a search parameter is not provided', (done) => { + const params = helpers.testRequestParams('/aircraft') + request.get(params, (error, response, body) => { + if (error) { + return done(error) + } + const json = JSON.parse(body) + assert.strictEqual(json.error, 'Please provide a valid search string to lookup') + done() + }) }) }) }) diff --git a/endpoints/aircraft/tests/loftfaraskra.fixture b/endpoints/aircraft/tests/loftfaraskra.fixture new file mode 100644 index 00000000..5a280659 --- /dev/null +++ b/endpoints/aircraft/tests/loftfaraskra.fixture @@ -0,0 +1,514 @@ + + + + + + + + + + + + + Uppfletting í loftfaraskrá | Loftfaraskrá | Samgöngustofa + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + +
+
+ + + + + +
+
+ + + + + +
+

Uppfletting í loftfaraskrá

+
+
+ + +
+
+
+
+ + + +
+

Loftfaraskrá: 100

+
+
    +
  • + Einkennisstafir: + TF-JMH +
  • +
  • + Skráningarnr: + 100 +
  • +
  • + Tegund: + Piper Aircraft, Inc. PA-23 +
  • +
  • + Framleiðsluár: + 1957 +
  • +
  • + Raðnúmer: + 23-1027 +
  • +
  • + Hármarksþungi: + 1588 kg +
  • +
  • + Farþegafjöldi: + 4 +
  • + +
  • + Eigandi: + + Þórarinn Ágústsson
    + + Grundargata 1
    + 600 Akureyri
    + Ísland +
    +
    +
  • + +
  • + Umráðandi: + + Þórarinn Ágústsson
    + + Grundargata 1
    + 600 Akureyri
    + Ísland +
    +
    +
  • +
+
+
+ + + + + + + + + + + + + + + + +

+ Var efnið hjálplegt? + + Nei +

+ + + +
+
+ + + + + + + + + + + + +
Þetta vefsvæði byggir á Eplica
+ + + + + + + + + + + + + + + + + + + + From 019bd3643dd9c6d57ec9ccd3bdc7e13e55507d66 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Mon, 4 Feb 2019 12:59:53 +0000 Subject: [PATCH 16/19] Remove timeout calls --- endpoints/aircraft/tests/integration_test.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index fe9f6cea..3bc7949d 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -14,7 +14,6 @@ describe('aircraft', () => { }) describe('correct-fields', () => { - this.timeout(20000) it('should return an array of objects containing correct fields', (done) => { const fieldsToCheckFor = [ 'id', @@ -35,7 +34,6 @@ describe('aircraft', () => { }) describe('aircraft-not-found', () => { - this.timeout(20000) it('should return a 404 when an aircraft is not found', (done) => { const params = helpers.testRequestParams('/aircraft', { search: 'loftur' }) request.get(params, (error, response, body) => { @@ -49,7 +47,6 @@ describe('aircraft', () => { }) }) - this.timeout(20000) it('should return a 400 when a search parameter is not provided', (done) => { const params = helpers.testRequestParams('/aircraft') request.get(params, (error, response, body) => { From d00941748f1ba5a5e85bd540833b0011b729ff66 Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Mon, 4 Feb 2019 13:03:22 +0000 Subject: [PATCH 17/19] Test failure debug attempt --- lib/test_helpers.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/test_helpers.js b/lib/test_helpers.js index 588722af..be650830 100644 --- a/lib/test_helpers.js +++ b/lib/test_helpers.js @@ -5,6 +5,7 @@ const assert = require('assert') function assertResults(json, canBeEmpty) { assert(json.results, 'Does not contain a \'results\' field') + console.info('assertResultsJSON: ', json) if (!canBeEmpty) { assert(json.results.length > 0, 'Results are empty') } @@ -53,8 +54,8 @@ exports.testRequestHandlerForFields = (done, fieldsToCheckFor, customCallback, c } catch (e) { throw e } - console.info(body) - console.info(json) + console.info('BODY: ', body) + console.info('JSON: ', json) // Check for the presence of the results property assertResults(json, canBeEmpty) From 0e1526b68bd4dd3670d0b856078f2a08e5d0bc4d Mon Sep 17 00:00:00 2001 From: Valentin Oliver Loftsson Date: Mon, 4 Feb 2019 17:57:18 +0000 Subject: [PATCH 18/19] Add url to nock mock url collection --- endpoints/aircraft/index.js | 1 - lib/test_helpers.js | 2 -- test.js | 1 + 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/endpoints/aircraft/index.js b/endpoints/aircraft/index.js index 1433b415..ae0844e6 100644 --- a/endpoints/aircraft/index.js +++ b/endpoints/aircraft/index.js @@ -79,7 +79,6 @@ app.get('/aircraft/:search?', async (req, res) => { try { const results = await lookupAircraft(search) - console.info({ results }) res.cache().json({ results }) } catch (error) { res.status(500).json({ error }) diff --git a/lib/test_helpers.js b/lib/test_helpers.js index be650830..0171872b 100644 --- a/lib/test_helpers.js +++ b/lib/test_helpers.js @@ -5,7 +5,6 @@ const assert = require('assert') function assertResults(json, canBeEmpty) { assert(json.results, 'Does not contain a \'results\' field') - console.info('assertResultsJSON: ', json) if (!canBeEmpty) { assert(json.results.length > 0, 'Results are empty') } @@ -54,7 +53,6 @@ exports.testRequestHandlerForFields = (done, fieldsToCheckFor, customCallback, c } catch (e) { throw e } - console.info('BODY: ', body) console.info('JSON: ', json) // Check for the presence of the results property assertResults(json, canBeEmpty) diff --git a/test.js b/test.js index f9b7d09f..9564dcd8 100644 --- a/test.js +++ b/test.js @@ -27,6 +27,7 @@ after(() => { 'http://fotbolti.net:80', 'http://skoli.landsbjorg.is:80', 'http://www.worldfengur.com:80', + 'http://samgongustofa.is:80', ].includes(o.scope) }) fs.writeFileSync(mockDataFilename, JSON.stringify(noLocalhost, null, 2)) From 662ca1b5d9825ee5807e2bdc47ac82f1a17958b1 Mon Sep 17 00:00:00 2001 From: Kristjan Broder Lund Date: Tue, 5 Feb 2019 20:23:49 +0000 Subject: [PATCH 19/19] chore(aircraft): fix tests --- endpoints/aircraft/index.js | 2 +- endpoints/aircraft/tests/integration_test.js | 11 +- ...askra.fixture => loftfaraskra.100.fixture} | 0 .../tests/loftfaraskra.loftur.fixture | 454 ++++++++++++++++++ lib/test_helpers.js | 2 +- test.js | 1 - 6 files changed, 462 insertions(+), 8 deletions(-) rename endpoints/aircraft/tests/{loftfaraskra.fixture => loftfaraskra.100.fixture} (100%) create mode 100644 endpoints/aircraft/tests/loftfaraskra.loftur.fixture diff --git a/endpoints/aircraft/index.js b/endpoints/aircraft/index.js index ae0844e6..6e0db54b 100644 --- a/endpoints/aircraft/index.js +++ b/endpoints/aircraft/index.js @@ -7,7 +7,7 @@ const h = require('apis-helpers') const app = require('../../server') const lookupAircraft = searchStr => new Promise((resolve, reject) => { - const url = `http://www.samgongustofa.is/flug/loftfor/loftfaraskra?aq=${searchStr}` + const url = `https://www.samgongustofa.is/flug/loftfor/loftfaraskra?aq=${searchStr}` request.get({ headers: { 'User-Agent': h.browser() }, url, diff --git a/endpoints/aircraft/tests/integration_test.js b/endpoints/aircraft/tests/integration_test.js index 3bc7949d..5a2995c1 100644 --- a/endpoints/aircraft/tests/integration_test.js +++ b/endpoints/aircraft/tests/integration_test.js @@ -7,10 +7,13 @@ const helpers = require('../../../lib/test_helpers.js') describe('aircraft', () => { before(() => { - nock('http://www.samgongustofa.is/flug/loftfor/loftfaraskra') - .get('/') + nock('https://www.samgongustofa.is') + .get('/flug/loftfor/loftfaraskra') .query({ aq: '100' }) - .reply(200, fs.readFileSync(`${__dirname}/loftfaraskra.fixture`)) + .reply(200, fs.readFileSync(`${__dirname}/loftfaraskra.100.fixture`)) + .get('/flug/loftfor/loftfaraskra') + .query({ aq: 'loftur' }) + .reply(200, fs.readFileSync(`${__dirname}/loftfaraskra.loftur.fixture`)) }) describe('correct-fields', () => { @@ -27,7 +30,6 @@ describe('aircraft', () => { 'operator', ] const params = helpers.testRequestParams('/aircraft', { search: '100' }) - console.info(params) const resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor) request.get(params, resultHandler) }) @@ -41,7 +43,6 @@ describe('aircraft', () => { return done(error) } const json = JSON.parse(body) - console.info(json) assert.strictEqual(json.error, 'No aircraft found with the query loftur') done() }) diff --git a/endpoints/aircraft/tests/loftfaraskra.fixture b/endpoints/aircraft/tests/loftfaraskra.100.fixture similarity index 100% rename from endpoints/aircraft/tests/loftfaraskra.fixture rename to endpoints/aircraft/tests/loftfaraskra.100.fixture diff --git a/endpoints/aircraft/tests/loftfaraskra.loftur.fixture b/endpoints/aircraft/tests/loftfaraskra.loftur.fixture new file mode 100644 index 00000000..754ac270 --- /dev/null +++ b/endpoints/aircraft/tests/loftfaraskra.loftur.fixture @@ -0,0 +1,454 @@ + + + + + + + + + + + + + Uppfletting í loftfaraskrá | Loftfaraskrá | Samgöngustofa + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + +
+
+ + + + + +
+
+ + + + + +
+

Uppfletting í loftfaraskrá

+
+
+ + +
+
+
+
+ + + Ekkert loftfar fannst + + + + + + + + + + + + + + + +

+ Var efnið hjálplegt? + + Nei +

+ + + +
+
+ + + + + + + + + + + + +
Þetta vefsvæði byggir á Eplica
+ + + + + + + + + + + + + + + + + + + + diff --git a/lib/test_helpers.js b/lib/test_helpers.js index 0171872b..dd567432 100644 --- a/lib/test_helpers.js +++ b/lib/test_helpers.js @@ -53,7 +53,7 @@ exports.testRequestHandlerForFields = (done, fieldsToCheckFor, customCallback, c } catch (e) { throw e } - console.info('JSON: ', json) + // Check for the presence of the results property assertResults(json, canBeEmpty) diff --git a/test.js b/test.js index 9564dcd8..f9b7d09f 100644 --- a/test.js +++ b/test.js @@ -27,7 +27,6 @@ after(() => { 'http://fotbolti.net:80', 'http://skoli.landsbjorg.is:80', 'http://www.worldfengur.com:80', - 'http://samgongustofa.is:80', ].includes(o.scope) }) fs.writeFileSync(mockDataFilename, JSON.stringify(noLocalhost, null, 2))