Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
* dev:
  Minor: add some additional helpful linting rules
  • Loading branch information
tdmalone committed Aug 11, 2018
2 parents 0918ed3 + d37ec14 commit 646ad49
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module.exports = {

rules: {

'arrow-parens': [ 'error', 'always' ],
'arrow-spacing': [ 'error' ],
'array-bracket-newline': [ 'error', 'consistent' ],
'array-element-newline': [ 'error', 'consistent' ],

Expand Down Expand Up @@ -62,6 +64,7 @@ module.exports = {

'no-multi-str': [ 'off' ],
'no-var': [ 'error' ],
'object-shorthand': [ 'warn' ],
'prefer-const': [ 'error' ]

} // Rules.
Expand Down
28 changes: 15 additions & 13 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @author Tim Malone <[email protected]>
*/

/* global jest */

'use strict';

const pg = require( 'pg' ),
Expand Down Expand Up @@ -94,7 +96,7 @@ const isValidEvent = ( event ) => {
* 'operation' being done on it - expressed as a valid mathematical operation
* (i.e. + or -).
*/
const extractEventData = ( text => {
const extractEventData = ( ( text ) => {
const data = text.match( /@([A-Za-z0-9]+?)>?\s*([-+]{2}|{1})/ );

if ( ! data ) {
Expand Down Expand Up @@ -232,7 +234,7 @@ const handleEvent = async( event ) => {
* @param {express.req} request An Express request. See https://expressjs.com/en/4x/api.html#req.
* @return {void}
*/
const logRequest = request => {
const logRequest = ( request ) => {
console.log(
request.ip + ' ' + request.method + ' ' + request.path + ' ' + request.headers['user-agent']
);
Expand All @@ -252,7 +254,7 @@ const logRequest = request => {
* @return {object|bool} If invalid, an error object containing an 'error' with HTTP status code
* and a 'message' to return to the user; otherwise, if valid, returns true.
*/
const validateToken = ( suppliedToken, serverToken ) =>{
const validateToken = ( suppliedToken, serverToken ) => {

// Sanity check for bad values on the server side - either empty, or still set to the default.
if ( ! serverToken.trim() || 'xxxxxxxxxxxxxxxxxxxxxxxx' === serverToken ) {
Expand Down Expand Up @@ -335,14 +337,14 @@ const handlePost = ( request, response ) => {
}; // HandlePost.

module.exports = {
setSlackClient: setSlackClient,
isValidEvent: isValidEvent,
extractEventData: extractEventData,
respondToUser: respondToUser,
updateScore: updateScore,
handleEvent: handleEvent,
logRequest: logRequest,
validateToken: validateToken,
handleGet: handleGet,
handlePost: handlePost
setSlackClient,
isValidEvent,
extractEventData,
respondToUser,
updateScore,
handleEvent,
logRequest,
validateToken,
handleGet,
handlePost
};
4 changes: 2 additions & 2 deletions src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,6 @@ const getRandomMessage = ( operation, item, score ) => {
}; // GetRandomMessage.

module.exports = {
getRandomMessage: getRandomMessage,
messages: messages
getRandomMessage,
messages
};
2 changes: 1 addition & 1 deletion tests/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const PORT = process.env.PORT || 80,

module.exports = {

PORT: PORT,
PORT,

scoresTableName: 'scores',

Expand Down
8 changes: 5 additions & 3 deletions tests/_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @author Tim Malone <[email protected]>
*/

/* global jest */

'use strict';

const objectAssignDeep = require( 'object-assign-deep' ),
Expand Down Expand Up @@ -53,22 +55,22 @@ const runner = async( text, options, callback ) => {
token: SLACK_VERIFICATION_TOKEN,
event: {
type: 'message',
text: text
text
}
};

if ( 'undefined' !== typeof options.extraBody ) {
body = objectAssignDeep( body, options.extraBody );
}

const request = http.request( config.defaultRequestOptions, response => {
const request = http.request( config.defaultRequestOptions, ( response ) => {

response
.on( 'data', () => {})
.on( 'end', async() => {

// Wait for the operations after the HTTP requests returns to be completed before testing.
await new Promise( resolve => setTimeout( resolve, HTTP_RETURN_DELAY ) );
await new Promise( ( resolve ) => setTimeout( resolve, HTTP_RETURN_DELAY ) );

// If we weren't provided with an itemToCheck, return to the callback now, passing it an
// instance of a dbClient if it accepts at least one argument.
Expand Down
4 changes: 3 additions & 1 deletion tests/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @author Tim Malone <[email protected]>
*/

/* global jest */

'use strict';

const app = require( '../src/app' ),
Expand Down Expand Up @@ -196,7 +198,7 @@ describe( 'handleEvent', () => {

expect.hasAssertions();

return app.handleEvent( event ).then( data => {
return app.handleEvent( event ).then( ( data ) => {
expect( data ).toBe( false );
});
});
Expand Down
18 changes: 10 additions & 8 deletions tests/e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @author Tim Malone <[email protected]>
*/

/* global jest */

'use strict';

/****************************************************************
Expand Down Expand Up @@ -40,7 +42,7 @@ beforeAll( async() => {
await dbClient.query( 'DROP TABLE IF EXISTS ' + config.scoresTableName );
await dbClient.release();

return new Promise( resolve => {
return new Promise( ( resolve ) => {
listener = require( '../' )({ slack: slackClientMock });
listener.on( 'listening', () => {
resolve();
Expand Down Expand Up @@ -137,7 +139,7 @@ describe( 'The database', () => {
const user = 'U00000100',
options = {
itemToCheck: user,
extraBody: { event: { user: user } }
extraBody: { event: { user } }
};

runner( '<@' + user + '>++', options, ( result ) => {
Expand All @@ -152,7 +154,7 @@ describe( 'The database', () => {
const user = 'U00000300',
options = {
itemToCheck: user,
extraBody: { event: { user: user } }
extraBody: { event: { user } }
};

runner( '<@' + user + '>++', options, ( result ) => {
Expand All @@ -167,7 +169,7 @@ describe( 'The database', () => {
const user = 'U00000200',
options = {
itemToCheck: user,
extraBody: { event: { user: user } }
extraBody: { event: { user } }
};

runner( '<@' + user + '>--', options, ( result ) => {
Expand All @@ -182,7 +184,7 @@ describe( 'The database', () => {
const user = 'U00000400',
options = {
itemToCheck: user,
extraBody: { event: { user: user } }
extraBody: { event: { user } }
};

runner( '<@' + user + '>--', options, ( result ) => {
Expand Down Expand Up @@ -234,7 +236,7 @@ describe( 'Slack messaging', () => {
expect.hasAssertions();
const user = 'U00000100',
options = {
extraBody: { event: { user: user } }
extraBody: { event: { user } }
};

slackClientMock.chat.postMessage.mockClear();
Expand Down Expand Up @@ -447,14 +449,14 @@ describe( 'Slack messaging', () => {
expect.hasAssertions();

const channel = 'C00000000',
options = { extraBody: { event: { channel: channel } } };
options = { extraBody: { event: { channel } } };

slackClientMock.chat.postMessage.mockClear();
runner( '@SomeRandom++', options, () => {

expect( slackClientMock.chat.postMessage )
.toHaveBeenCalledTimes( 1 )
.toHaveBeenCalledWith( expect.objectContaining({ channel: channel }) );
.toHaveBeenCalledWith( expect.objectContaining({ channel }) );

done();

Expand Down
24 changes: 13 additions & 11 deletions tests/integration-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @author Tim Malone <[email protected]>
*/

/* global jest */

'use strict';

/****************************************************************
Expand Down Expand Up @@ -53,13 +55,13 @@ beforeEach( () => {

describe( 'The Express server', () => {

it( 'returns HTTP 200 for GET operations', done => {
it( 'returns HTTP 200 for GET operations', ( done ) => {
expect.hasAssertions();

const listener = require( '../' )();

listener.on( 'listening', () => {
http.get( 'http://localhost:' + config.PORT, response => {
http.get( 'http://localhost:' + config.PORT, ( response ) => {
listener.close();
expect( response.statusCode ).toBe( 200 );
done();
Expand All @@ -68,17 +70,17 @@ describe( 'The Express server', () => {

});

it( 'correctly returns the Slack event challenge value', done => {
it( 'correctly returns the Slack event challenge value', ( done ) => {
expect.assertions( 2 );

const listener = require( '../' )();
const requestBody = { challenge: Math.random().toString() };

listener.on( 'listening', () => {
const request = http.request( config.defaultRequestOptions, response => {
const request = http.request( config.defaultRequestOptions, ( response ) => {
let data = '';

response.on( 'data', chunk => {
response.on( 'data', ( chunk ) => {
data += chunk;
}).on( 'end', () => {
listener.close();
Expand All @@ -94,14 +96,14 @@ describe( 'The Express server', () => {
});
});

it( 'returns HTTP 500 when no verification token is set', done => {
it( 'returns HTTP 500 when no verification token is set', ( done ) => {
expect.hasAssertions();

delete process.env.SLACK_VERIFICATION_TOKEN;
const listener = require( '../' )();

listener.on( 'listening', () => {
http.request( config.defaultRequestOptions, response => {
http.request( config.defaultRequestOptions, ( response ) => {
listener.close();
expect( response.statusCode ).toBe( 500 );
done();
Expand All @@ -110,14 +112,14 @@ describe( 'The Express server', () => {

});

it( 'returns HTTP 500 when verification token is still set to the default', done => {
it( 'returns HTTP 500 when verification token is still set to the default', ( done ) => {
expect.hasAssertions();

process.env.SLACK_VERIFICATION_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxx';
const listener = require( '../' )();

listener.on( 'listening', () => {
http.request( config.defaultRequestOptions, response => {
http.request( config.defaultRequestOptions, ( response ) => {
listener.close();
expect( response.statusCode ).toBe( 500 );
done();
Expand All @@ -126,15 +128,15 @@ describe( 'The Express server', () => {

});

it( 'returns HTTP 403 when verification token is incorrect', done => {
it( 'returns HTTP 403 when verification token is incorrect', ( done ) => {
expect.hasAssertions();

const listener = require( '../' )();
const body = { token: 'something_is_not_right' };

listener.on( 'listening', () => {

const request = http.request( config.defaultRequestOptions, response => {
const request = http.request( config.defaultRequestOptions, ( response ) => {
listener.close();
expect( response.statusCode ).toBe( 403 );
done();
Expand Down
2 changes: 2 additions & 0 deletions tests/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @author Tim Malone <[email protected]>
*/

/* global jest */

'use strict';

const messages = require( '../src/messages' );
Expand Down

0 comments on commit 646ad49

Please sign in to comment.