Skip to content

Conversation

@shalarewicz
Copy link
Collaborator

@shalarewicz shalarewicz commented May 26, 2022

Summary

Writes tests for Token Bucket implementation of Express Rate Limiting Middleware.
Sets package type to module
Reconfigures jest using typescript for to support module configuration.

Type of Change

  • Testing addition

Issues

Evidence

  • All tests fail or are skipped as expected:
    image

@evanmcneely evanmcneely marked this pull request as ready for review May 26, 2022 02:30
@evanmcneely evanmcneely marked this pull request as draft May 26, 2022 02:34
@shalarewicz shalarewicz changed the title Initial tests for middleware funcitonality. Initial tests for middleware functionality. May 26, 2022

complexRequest = {
// complexity should be 10 if 'first' is accounted for.
// scalars: 1, droid: 1, reviews (4 * (1 Review, 1 episode))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enums are equivalent to scalarn and have a weight of zero, not 1, so this won't work as expected.

// Send 5 queries of complexity 2. These should all succeed
middleware(mockRequest as Request, mockResponse as Response, nextFunction);

// advance the timers by 1 second for the next request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says 1 second and the code says 20ms. I think the code is right but the intention is muddled.

});

test('Uses User IP Address in Redis', async () => {
const client: RedisClientType = redis.createClient();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this redis client getting into the middleware or accessing the data inside the middleware? I'm don't know enough about redis to understand what is happening here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to flush this out a bit since during middleware initialization the node-redis client would connect to an existing redis instance whereas during testing redis-mock does all operations in memory. This may just need to be dropped from the tests or we can use something like NODE_ENV=TEST to control how the middleware connects to redis during initialization.

@shalarewicz shalarewicz requested review from feiw101 and milos381 May 28, 2022 02:51
@shalarewicz shalarewicz marked this pull request as ready for review May 28, 2022 02:52
@shalarewicz shalarewicz requested a review from evanmcneely May 28, 2022 16:40
Copy link
Contributor

@evanmcneely evanmcneely left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think the complexities of some of the test queries will make the tests not behave as as expected. See comments.

@shalarewicz shalarewicz requested a review from evanmcneely May 28, 2022 17:41
@shalarewicz shalarewicz merged commit d29c6d4 into dev Jun 2, 2022
@shalarewicz shalarewicz deleted the sh/middleware-framework branch June 2, 2022 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Write tests for Express rate limter middleware

4 participants