|
1 | 1 | import { Request, Response, NextFunction, RequestHandler } from 'express'; |
2 | 2 | import { GraphQLSchema, buildSchema } from 'graphql'; |
3 | | -import redis from 'redis-mock'; |
| 3 | +import redis from 'redis'; |
| 4 | +import redisMock from 'redis-mock'; |
4 | 5 | import { RedisClientType } from 'redis'; |
5 | 6 | import expressRateLimitMiddleware from '../../src/middleware/index'; |
6 | | -import { Socket } from 'net'; |
7 | 7 |
|
8 | 8 | let middleware: RequestHandler; |
9 | 9 | let mockRequest: Partial<Request>; |
@@ -70,20 +70,24 @@ describe('Express Middleware tests', () => { |
70 | 70 | describe('...successfully connects to redis using standard connection options', () => { |
71 | 71 | beforeEach(() => { |
72 | 72 | // TODO: Setup mock redis store. |
| 73 | + redisMock; |
73 | 74 | }); |
74 | 75 |
|
75 | | - test('...via connection string', () => { |
76 | | - // TODO: use event listener to listen for connections to a mock redis store |
| 76 | + test('...via url', () => { |
| 77 | + // TODO: Connect to redis instance and add 'connect' event listener |
| 78 | + // assert that event listener is called once |
77 | 79 | expect(true).toBeFalsy(); |
78 | 80 | }); |
79 | 81 |
|
80 | | - test('via indiividual parameters', () => { |
81 | | - // TODO: |
| 82 | + test('via socket', () => { |
| 83 | + // TODO: Connect to redis instance and add 'connect' event listener |
| 84 | + // assert that event listener is called once |
82 | 85 | expect(true).toBeFalsy(); |
83 | 86 | }); |
84 | 87 |
|
85 | 88 | test('defaults to localhost', () => { |
86 | | - // TODO: |
| 89 | + // TODO: Connect to redis instance and add 'connect' event listener |
| 90 | + // assert that event listener is called once |
87 | 91 | expect(true).toBeFalsy(); |
88 | 92 | }); |
89 | 93 | }); |
@@ -153,6 +157,17 @@ describe('Express Middleware tests', () => { |
153 | 157 | expressRateLimitMiddleware('TOKEN_BUCKET', {}, invalidSchema, { url: '' }) |
154 | 158 | ).toThrowError('ValidationError'); |
155 | 159 | }); |
| 160 | + |
| 161 | + test('Throw an error in unable to connect to redis', () => { |
| 162 | + expect( |
| 163 | + expressRateLimitMiddleware( |
| 164 | + 'TOKEN_BUCKET', |
| 165 | + { bucketSize: 10, refillRate: 1 }, |
| 166 | + schema, |
| 167 | + { socket: { host: 'localhost', port: 1 } } |
| 168 | + ) |
| 169 | + ).toThrow('ECONNREFUSED'); |
| 170 | + }); |
156 | 171 | }); |
157 | 172 |
|
158 | 173 | describe('Middleware is Functional', () => { |
@@ -321,13 +336,22 @@ describe('Express Middleware tests', () => { |
321 | 336 | }); |
322 | 337 |
|
323 | 338 | test('Uses User IP Address in Redis', async () => { |
| 339 | + // FIXME: In order to test this accurately the middleware would need to connect |
| 340 | + // to a mock instance or the tests would need to connect to an actual redis instance |
| 341 | + // We could use NODE_ENV varibale in the implementation to determine the connection type. |
| 342 | + |
| 343 | + // connecting to the actual redis client here |
324 | 344 | const client: RedisClientType = redis.createClient(); |
| 345 | + await client.connect(); |
325 | 346 | // Check for change in the redis store for the IP key |
326 | | - const initialValue: string | null = await client.get(mockRequest?.ip); |
| 347 | + |
| 348 | + // @ts-ignore mockRequest will always have an ip address. |
| 349 | + const initialValue: string | null = await client.get(mockRequest.ip); |
327 | 350 |
|
328 | 351 | middleware(mockRequest as Request, mockResponse as Response, nextFunction); |
329 | 352 |
|
330 | | - const finalValue: string | null = await client.get(mockRequest?.ip); |
| 353 | + // @ts-ignore |
| 354 | + const finalValue: string | null = await client.get(mockRequest.ip); |
331 | 355 |
|
332 | 356 | expect(finalValue).not.toBeNull(); |
333 | 357 | expect(finalValue).not.toBe(initialValue); |
|
0 commit comments