This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* dev: Minor: add some additional helpful linting rules
- Loading branch information
Showing
9 changed files
with
54 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
* @author Tim Malone <[email protected]> | ||
*/ | ||
|
||
/* global jest */ | ||
|
||
'use strict'; | ||
|
||
const pg = require( 'pg' ), | ||
|
@@ -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 ) { | ||
|
@@ -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'] | ||
); | ||
|
@@ -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 ) { | ||
|
@@ -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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
* @author Tim Malone <[email protected]> | ||
*/ | ||
|
||
/* global jest */ | ||
|
||
'use strict'; | ||
|
||
const objectAssignDeep = require( 'object-assign-deep' ), | ||
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
* @author Tim Malone <[email protected]> | ||
*/ | ||
|
||
/* global jest */ | ||
|
||
'use strict'; | ||
|
||
const app = require( '../src/app' ), | ||
|
@@ -196,7 +198,7 @@ describe( 'handleEvent', () => { | |
|
||
expect.hasAssertions(); | ||
|
||
return app.handleEvent( event ).then( data => { | ||
return app.handleEvent( event ).then( ( data ) => { | ||
expect( data ).toBe( false ); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
* @author Tim Malone <[email protected]> | ||
*/ | ||
|
||
/* global jest */ | ||
|
||
'use strict'; | ||
|
||
/**************************************************************** | ||
|
@@ -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(); | ||
|
@@ -137,7 +139,7 @@ describe( 'The database', () => { | |
const user = 'U00000100', | ||
options = { | ||
itemToCheck: user, | ||
extraBody: { event: { user: user } } | ||
extraBody: { event: { user } } | ||
}; | ||
|
||
runner( '<@' + user + '>++', options, ( result ) => { | ||
|
@@ -152,7 +154,7 @@ describe( 'The database', () => { | |
const user = 'U00000300', | ||
options = { | ||
itemToCheck: user, | ||
extraBody: { event: { user: user } } | ||
extraBody: { event: { user } } | ||
}; | ||
|
||
runner( '<@' + user + '>++', options, ( result ) => { | ||
|
@@ -167,7 +169,7 @@ describe( 'The database', () => { | |
const user = 'U00000200', | ||
options = { | ||
itemToCheck: user, | ||
extraBody: { event: { user: user } } | ||
extraBody: { event: { user } } | ||
}; | ||
|
||
runner( '<@' + user + '>--', options, ( result ) => { | ||
|
@@ -182,7 +184,7 @@ describe( 'The database', () => { | |
const user = 'U00000400', | ||
options = { | ||
itemToCheck: user, | ||
extraBody: { event: { user: user } } | ||
extraBody: { event: { user } } | ||
}; | ||
|
||
runner( '<@' + user + '>--', options, ( result ) => { | ||
|
@@ -234,7 +236,7 @@ describe( 'Slack messaging', () => { | |
expect.hasAssertions(); | ||
const user = 'U00000100', | ||
options = { | ||
extraBody: { event: { user: user } } | ||
extraBody: { event: { user } } | ||
}; | ||
|
||
slackClientMock.chat.postMessage.mockClear(); | ||
|
@@ -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(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
* @author Tim Malone <[email protected]> | ||
*/ | ||
|
||
/* global jest */ | ||
|
||
'use strict'; | ||
|
||
/**************************************************************** | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
* @author Tim Malone <[email protected]> | ||
*/ | ||
|
||
/* global jest */ | ||
|
||
'use strict'; | ||
|
||
const messages = require( '../src/messages' ); | ||
|