BearND has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/336884 )
Change subject: On this day: throw 501 for unsupported language ...................................................................... On this day: throw 501 for unsupported language Also refactored some tests a bit to make it easier to cover all event types. Bug: T157647 Change-Id: I1121f940a79ce3e3224ee2a8cbc40cac5ff6b887 --- M routes/on-this-day.js M test/features/onthisday/on-this-day.js 2 files changed, 54 insertions(+), 50 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps refs/changes/84/336884/1 diff --git a/routes/on-this-day.js b/routes/on-this-day.js index eef55ab..f3f31e8 100644 --- a/routes/on-this-day.js +++ b/routes/on-this-day.js @@ -7,6 +7,7 @@ const mUtil = require('../lib/mobile-util'); const parsoid = require('../lib/parsoid-access'); const BBPromise = require('bluebird'); +const HTTPError = require('../lib/util').HTTPError; const languages = require('../lib/on-this-day.languages').languages; let app; @@ -428,6 +429,16 @@ .then((doc) => [doc, revision]); } +let assertLanguage = function (lang) { + if (!languages[lang]) { + throw new HTTPError({ + status: 501, + type: 'unsupported_language', + title: 'Language not supported', + detail: 'The language you have requested is not yet supported.' + }); + } +}; /** * Fetches document for URI, extracts sought elements, responds * @param {!Object} req Request @@ -440,6 +451,8 @@ */ function fetchAndRespond(req, res, titleFunction, extractionFunction) { const lang = req.params.domain.split('.')[0]; + assertLanguage(lang); + return fetchDocAndRevision(req, titleFunction) .then((docAndRevision) => { const doc = docAndRevision[0]; @@ -500,6 +513,8 @@ */ router.get('/all/:mm/:dd', (req, res) => { const lang = req.params.domain.split('.')[0]; + assertLanguage(lang); + return BBPromise.all([ fetchDocAndRevision(req, dayTitleForRequest), fetchDocAndRevision(req, selectedTitleForRequest) diff --git a/test/features/onthisday/on-this-day.js b/test/features/onthisday/on-this-day.js index 68d60b1..9d77988 100644 --- a/test/features/onthisday/on-this-day.js +++ b/test/features/onthisday/on-this-day.js @@ -11,6 +11,14 @@ const onThisDayLangs = require('../../../lib/on-this-day.languages'); const languages = onThisDayLangs.languages; +const eventTypes = [ + 'all', + 'selected', + 'births', + 'deaths', + 'events', + 'holidays' +]; // UTILITY @@ -82,25 +90,6 @@ this.timeout(20000); // eslint-disable-line no-invalid-this before(() => { return server.start(); }); - - it('"births" should respond to GET req w/expected headers, incl CORS and CSP headers', () => { - headers.checkHeaders(`${server.config.uri}en.wikipedia.org/v1/onthisday/births/01/01`); - }); - it('"deaths" should respond to GET req w/expected headers, incl CORS and CSP headers', () => { - headers.checkHeaders(`${server.config.uri}en.wikipedia.org/v1/onthisday/deaths/01/01`); - }); - it('"events" should respond to GET req w/expected headers, incl CORS and CSP headers', () => { - headers.checkHeaders(`${server.config.uri}en.wikipedia.org/v1/onthisday/events/01/01`); - }); - it('"holidays" should respond to GET req w/expected headers, incl CORS and CSP headers', () => { - headers.checkHeaders(`${server.config.uri}en.wikipedia.org/v1/onthisday/holidays/01/01`); - }); - it('"selected" should respond to GET req w/expected headers, incl CORS and CSP headers', () => { - headers.checkHeaders(`${server.config.uri}en.wikipedia.org/v1/onthisday/selected/01/01`); - }); - it('"all" should respond to GET req w/expected headers, incl CORS and CSP headers', () => { - headers.checkHeaders(`${server.config.uri}en.wikipedia.org/v1/onthisday/all/01/01`); - }); // TEST PAGE TITLE GENERATION @@ -293,50 +282,51 @@ // DO NOT TEST FOR EXACT RESULT COUNT - THESE CHANGE AS PAGES ARE EDITED. // INSTEAD TEST THAT AT LEAST SOME RESULTS ARE RETURNED. - function january30uriForEndpointName(endpointName) { - return `${server.config.uri}en.wikipedia.org/v1/onthisday/${endpointName}/01/30/`; + function january30uriForEndpointName(endpointName, lang = 'en') { + return `${server.config.uri}${lang}.wikipedia.org/v1/onthisday/${endpointName}/01/30/`; } - function getJanuary30ResponseForEndpointName(endpointName) { - return preq.get(january30uriForEndpointName(endpointName)); + function getJanuary30ResponseForEndpointName(endpointName, lang) { + return preq.get(january30uriForEndpointName(endpointName, lang)); } function verifyNonZeroEndpointResults(response, endpointName) { + assert.equal(response.status, 200); assert.ok(response.body.length > 0, `${endpointName} should have fetched some results`); } - function fetchAndVerifyNonZeroResultsForEndpointName(endpointName) { - return getJanuary30ResponseForEndpointName(endpointName) + function fetchAndVerifyNonZeroResultsForEndpointName(endpointName, lang) { + return getJanuary30ResponseForEndpointName(endpointName, lang) .then((response) => { verifyNonZeroEndpointResults(response, endpointName); }); } - it('BIRTHS fetches some results', () => { - return fetchAndVerifyNonZeroResultsForEndpointName('births'); - }); - it('DEATHS fetches some results', () => { - return fetchAndVerifyNonZeroResultsForEndpointName('deaths'); - }); + for (const type of eventTypes) { + it(`${type}: should respond to GET req w/expected headers`, () => { + headers.checkHeaders(january30uriForEndpointName(type)); + }); - it('EVENTS fetches some results', () => { - return fetchAndVerifyNonZeroResultsForEndpointName('events'); - }); + it(`${type}: unsupported language throws 501`, () => { + return getJanuary30ResponseForEndpointName(type, 'nl') + .catch((error) => { + assert.equal(error.status, 501); + }); + }); - it('HOLIDAYS fetches some results', () => { - return fetchAndVerifyNonZeroResultsForEndpointName('holidays'); - }); + if (type !== 'all') { + it(`${type}: fetches some results`, () => { + return fetchAndVerifyNonZeroResultsForEndpointName(type); + }); + } + } - it('SELECTED fetches some results', () => { - return fetchAndVerifyNonZeroResultsForEndpointName('selected'); - }); - - it('ALL fetches some results for births, deaths, events, holidays and selected', () => { + it('"all" fetches some results for births, deaths, events, holidays and selected', () => { return getJanuary30ResponseForEndpointName('all') - .then((response) => { - assert.ok(response.body.births.length > 0, 'ALL should return some births'); - assert.ok(response.body.deaths.length > 0, 'ALL should return some deaths'); - assert.ok(response.body.events.length > 0, 'ALL should return some events'); - assert.ok(response.body.holidays.length > 0, 'ALL should return some holidays'); - assert.ok(response.body.selected.length > 0, 'ALL should return some selected'); - }); + .then((response) => { + assert.ok(response.body.births.length > 0, 'ALL should return some births'); + assert.ok(response.body.deaths.length > 0, 'ALL should return some deaths'); + assert.ok(response.body.events.length > 0, 'ALL should return some events'); + assert.ok(response.body.holidays.length > 0, 'ALL should return some holidays'); + assert.ok(response.body.selected.length > 0, 'ALL should return some selected'); + }); }); it('eventsForYearListElements returns a WMFEvent for only year list elements', () => { @@ -441,5 +431,4 @@ const listElements = onThisDay.testing.listElementsByHeadingID(document, 'Births'); assert.ok(listElements.length === 208); }); - }); -- To view, visit https://gerrit.wikimedia.org/r/336884 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1121f940a79ce3e3224ee2a8cbc40cac5ff6b887 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/mobileapps Gerrit-Branch: master Gerrit-Owner: BearND <bsitzm...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits