This repository was archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathintegration-tests.js
More file actions
263 lines (199 loc) · 7.74 KB
/
integration-tests.js
File metadata and controls
263 lines (199 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Integration tests.
*
* @see https://jestjs.io/docs/en/expect
* @see https://github.com/jest-community/jest-extended#api
* @see https://jestjs.io/docs/en/asynchronous.html
* @see https://jestjs.io/docs/en/mock-functions#mocking-modules
* @author Tim Malone <tdmalone@gmail.com>
*/
/* global jest */
'use strict';
/****************************************************************
* Environment Configuration.
****************************************************************/
const app = require( '../src/app' ),
points = require( '../src/points' );
const pathToListener = '../';
const pg = require( 'pg' ),
http = require( 'http' );
const config = require( './_config' );
const originalProcessEnv = process.env,
postgres = new pg.Pool( config.postgresPoolConfig );
/****************************************************************
* Jest Setup.
****************************************************************/
// Catch all console output during tests.
console.error = jest.fn();
console.info = jest.fn();
console.log = jest.fn();
console.warn = jest.fn();
// Drop the scores table + case insensitive extension before we start, as our tests rely on that.
beforeAll( async() => {
const dbClient = await postgres.connect();
await dbClient.query( 'DROP TABLE IF EXISTS ' + config.scoresTableName );
await dbClient.query( 'DROP EXTENSION IF EXISTS CITEXT' );
await dbClient.release();
});
// Clear module cache + reset environment variables before each test.
beforeEach( () => {
jest.resetModules();
process.env = { ...originalProcessEnv };
});
/****************************************************************
* Express Server tests.
****************************************************************/
describe( 'The Express server', () => {
it( 'returns HTTP 200 for GET operations', ( done ) => {
expect.hasAssertions();
const listener = require( pathToListener )();
listener.on( 'listening', () => {
http.get( 'http://localhost:' + config.PORT, ( response ) => {
listener.close();
expect( response.statusCode ).toBe( 200 );
done();
});
});
});
it( 'correctly returns the Slack event challenge value', ( done ) => {
expect.assertions( 2 );
const listener = require( pathToListener )();
const requestBody = { challenge: Math.random().toString() };
listener.on( 'listening', () => {
const request = http.request( config.defaultRequestOptions, ( response ) => {
let data = '';
response.on( 'data', ( chunk ) => {
data += chunk;
}).on( 'end', () => {
listener.close();
expect( response.statusCode ).toBe( 200 );
expect( data ).toBe( requestBody.challenge );
done();
});
});
request.write( JSON.stringify( requestBody ) );
request.end();
});
});
it( 'returns HTTP 500 when no verification token is set', ( done ) => {
expect.hasAssertions();
delete process.env.SLACK_VERIFICATION_TOKEN;
const listener = require( pathToListener )();
listener.on( 'listening', () => {
http.request( config.defaultRequestOptions, ( response ) => {
listener.close();
expect( response.statusCode ).toBe( 500 );
done();
}).end();
});
});
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( pathToListener )();
listener.on( 'listening', () => {
http.request( config.defaultRequestOptions, ( response ) => {
listener.close();
expect( response.statusCode ).toBe( 500 );
done();
}).end();
});
});
it( 'returns HTTP 403 when verification token is incorrect', ( done ) => {
expect.hasAssertions();
const listener = require( pathToListener )();
const body = { token: 'something_is_not_right' };
listener.on( 'listening', () => {
const request = http.request( config.defaultRequestOptions, ( response ) => {
listener.close();
expect( response.statusCode ).toBe( 403 );
done();
});
request.write( JSON.stringify( body ) );
request.end();
});
});
it( 'responds to Slack on retries and then drops the event', () => {
expect.assertions( 4 );
const mockExpress = require( './mocks/express' );
// The first time, we expect an error because we haven't put a valid event together yet.
mockExpress.response.send = jest.fn();
expect( () => {
app.handlePost( mockExpress.request, mockExpress.response );
}).toThrow();
expect( mockExpress.response.send ).toHaveBeenCalledTimes( 1 );
// The second time, we clear the mocks, and then pretend Slack is retrying a request.
// This one should simply return false.
mockExpress.response.send.mockClear();
mockExpress.request.headers['x-slack-retry-num'] = 1;
const result = app.handlePost( mockExpress.request, mockExpress.response );
expect( result ).toBeFalse();
expect( mockExpress.response.send ).toHaveBeenCalledTimes( 1 );
});
}); // Express Server tests.
/****************************************************************
* Postgres tests.
****************************************************************/
describe( 'The database', () => {
const defaultUser = 'U00000000',
defaultItem = 'something';
const tableExistsQuery = 'SELECT EXISTS ( ' +
'SELECT 1 FROM information_schema.tables ' +
'WHERE table_name = \'' + config.scoresTableName + '\'' +
')';
const extensionExistsQuery = 'SELECT * FROM pg_extension WHERE extname = \'citext\'';
it( 'does not yet have the ' + config.scoresTableName + ' table', async() => {
expect.hasAssertions();
const dbClient = await postgres.connect();
const query = await dbClient.query( tableExistsQuery );
await dbClient.release();
expect( query.rows[0].exists ).toBeFalse();
});
it( 'does not yet have the case-insensitive extension', async() => {
expect.hasAssertions();
const dbClient = await postgres.connect();
const query = await dbClient.query( extensionExistsQuery );
await dbClient.release();
expect( query.rowCount ).toBe( 0 );
});
it( 'creates the ' + config.scoresTableName + ' table on the first request', async() => {
expect.hasAssertions();
const newScore = await points.updateScore( defaultItem, '+' );
const dbClient = await postgres.connect();
const query = await dbClient.query( tableExistsQuery );
await dbClient.release();
expect( query.rows[0].exists ).toBeTrue();
expect( newScore ).toBe( 1 );
});
it( 'also creates the case-insensitive extension on the first request', async() => {
expect.hasAssertions();
const dbClient = await postgres.connect();
const query = await dbClient.query( extensionExistsQuery );
await dbClient.release();
expect( query.rowCount ).toBe( 1 );
});
it( 'does not cause any errors on a second request when everything already exists', async() => {
expect.hasAssertions();
const newScore = await points.updateScore( defaultItem, '+' );
expect( newScore ).toBe( 2 );
});
it( 'returns a list of top scores in the correct order', async() => {
expect.hasAssertions();
const expectedScores = [
{
item: defaultUser,
score: 3
},
{
item: defaultItem,
score: 2
}
];
// Give us a few additional scores so we can check the order works.
await points.updateScore( defaultUser, '+' );
await points.updateScore( defaultUser, '+' );
await points.updateScore( defaultUser, '+' );
const topScores = await points.retrieveTopScores();
expect( topScores ).toEqual( expectedScores );
});
}); // Postgres tests.