Skip to content

Commit 4fc2900

Browse files
committed
Updated redis tests
1 parent 16ea57a commit 4fc2900

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"description": "A GraphQL rate limiting library using query complexity analysis.",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
78
"test": "jest --passWithNoTests",
89
"lint": "eslint src test",

test/middleware/express.test.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Request, Response, NextFunction, RequestHandler } from 'express';
22
import { GraphQLSchema, buildSchema } from 'graphql';
3-
import redis from 'redis-mock';
3+
import redis from 'redis';
4+
import redisMock from 'redis-mock';
45
import { RedisClientType } from 'redis';
56
import expressRateLimitMiddleware from '../../src/middleware/index';
6-
import { Socket } from 'net';
77

88
let middleware: RequestHandler;
99
let mockRequest: Partial<Request>;
@@ -70,20 +70,24 @@ describe('Express Middleware tests', () => {
7070
describe('...successfully connects to redis using standard connection options', () => {
7171
beforeEach(() => {
7272
// TODO: Setup mock redis store.
73+
redisMock;
7374
});
7475

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
7779
expect(true).toBeFalsy();
7880
});
7981

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
8285
expect(true).toBeFalsy();
8386
});
8487

8588
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
8791
expect(true).toBeFalsy();
8892
});
8993
});
@@ -153,6 +157,17 @@ describe('Express Middleware tests', () => {
153157
expressRateLimitMiddleware('TOKEN_BUCKET', {}, invalidSchema, { url: '' })
154158
).toThrowError('ValidationError');
155159
});
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+
});
156171
});
157172

158173
describe('Middleware is Functional', () => {
@@ -321,13 +336,22 @@ describe('Express Middleware tests', () => {
321336
});
322337

323338
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
324344
const client: RedisClientType = redis.createClient();
345+
await client.connect();
325346
// 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);
327350

328351
middleware(mockRequest as Request, mockResponse as Response, nextFunction);
329352

330-
const finalValue: string | null = await client.get(mockRequest?.ip);
353+
// @ts-ignore
354+
const finalValue: string | null = await client.get(mockRequest.ip);
331355

332356
expect(finalValue).not.toBeNull();
333357
expect(finalValue).not.toBe(initialValue);

0 commit comments

Comments
 (0)