From 50fde379c65d881c865b91e57ec858e4da5c7723 Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Sat, 15 Sep 2018 21:53:16 +0200 Subject: [PATCH] Refactored web app for clarity --- lib/shortcode.json | 2 +- services/nodevoto-web/app.js | 27 ++++++++++++++++----------- test/nodevoto-web/app.test.js | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/shortcode.json b/lib/shortcode.json index 322fb4d..9c8a331 100644 --- a/lib/shortcode.json +++ b/lib/shortcode.json @@ -98,5 +98,5 @@ "VoteRelaxed": ":relaxed:", "VoteSunglasses": ":sunglasses:", "VoteJoy": ":joy:", - "VotePoop": "ERROR" + "VotePoop": ":poop:" } \ No newline at end of file diff --git a/services/nodevoto-web/app.js b/services/nodevoto-web/app.js index bc23400..b50d9ed 100644 --- a/services/nodevoto-web/app.js +++ b/services/nodevoto-web/app.js @@ -36,15 +36,20 @@ class App { routes.get('/api/leaderboard', this.handleLeaderboard.bind(this)); } - async handleLeaderboard(req, res) { - const findByShortcode = wrapOp(this.emojiClient.FindByShortcode.bind(this.emojiClient)); - const getResults = wrapOp(this.votingClient.Results.bind(this.votingClient)); + _FindByShortcode(arg) { + return wrapOp(this.emojiClient.FindByShortcode.bind(this.emojiClient))(arg); + } + + _Results() { + return wrapOp(this.votingClient.Results.bind(this.votingClient))(); + } + async handleLeaderboard(req, res) { try { - let response = await getResults(); + let response = await this._Results(); let list = response.results.map(async (item) => { - return findByShortcode({ Shortcode: item.Shortcode }).then(r => { + return this._FindByShortcode({ Shortcode: item.Shortcode }).then(r => { return { 'shortcode': r.Emoji.shortcode, 'unicode': r.Emoji.unicode, 'votes': item.Votes }; @@ -60,17 +65,17 @@ class App { async handleVoteEmoji(req, res) { let emojiShortcode = req.query['choice']; + let response; + let vote; + if (emojiShortcode === undefined || emojiShortcode === '') { logger.error(`Emoji choice [${emojiShortcode}] is mandatory`); return res.status(400).end(); } - let vote; - const findByShortcode = wrapOp(this.emojiClient.FindByShortcode.bind(this.emojiClient)); - try { - response = await findByShortcode({ Shortcode: emojiShortcode }); + response = await this._FindByShortcode({ Shortcode: emojiShortcode }); } catch (err) { logger.error(err); return res.status(500).json(err.message); @@ -89,8 +94,8 @@ class App { if (op !== null && this.votingClient[op] !== undefined) { vote = wrapOp(this.votingClient[op].bind(this.votingClient)); - } else if (emojiShortcode === ':poop:') { - vote = wrapOp(this.votingClient.VotePoop.bind(this.votingClient)); + } else { + logger.error(`Emoji lacks implementation of rpc operation [${op}]`); } try { diff --git a/test/nodevoto-web/app.test.js b/test/nodevoto-web/app.test.js index 7574434..5d2ca97 100644 --- a/test/nodevoto-web/app.test.js +++ b/test/nodevoto-web/app.test.js @@ -20,6 +20,9 @@ const testMap = [ },{ unicode: '🐷', shortcode: ':pig:' + },{ + unicode: '💩', + shortcode: ':poop:' } ]; @@ -70,6 +73,10 @@ class VotingMock { this.inc(':ghost:'); return callback(null); } + + VotePoop (args, callback) { + return callback('Unkown error', null); + } } describe('app', () => { @@ -105,6 +112,16 @@ describe('app', () => { expect(voting.get(':ghost:')).equals(1); }); + it('should reject vote for :poop: emoji', async() => { + try { + let response = await superget(`http://127.0.0.1:${WEB_PORT}/api/vote?choice=:poop:`); + expect(response).to.equal(null); + } catch(err) { + expect(err.status).equals(500); + expect(err.message).equals('Internal Server Error'); + } + }); + it('should reject vote without choice parameter', async() => { try { let response = await superget(`http://127.0.0.1:${WEB_PORT}/api/vote`);