Skip to content

Rate Testing#1605

Open
Zach-O-Bates wants to merge 6 commits into
OpenEnergyDashboard:developmentfrom
Zach-O-Bates:rate_testing
Open

Rate Testing#1605
Zach-O-Bates wants to merge 6 commits into
OpenEnergyDashboard:developmentfrom
Zach-O-Bates:rate_testing

Conversation

@Zach-O-Bates
Copy link
Copy Markdown

Description

This pull request resolves issues with login rate limiting and test reliability. The login rate limiter was updated to behave differently based on environment variables, allowing normal tests to run without interference while enforcing strict limits during dedicated rate limit testing. A separate ratetest environment was introduced to properly validate 429 responses without impacting other test suites.

Fixes #1564

Type of change

(Check the ones that apply by placing an "x" instead of the space in the [ ] so it becomes [x])

  • Note merging this changes the database configuration.
  • This change requires a documentation update

Checklist

  • I have followed the OED pull request ideas
  • I have removed text in ( ) from the issue request
  • You acknowledge that every person contributing to this work has signed the OED Contributing License Agreement and each author is listed in the Description section.

Limitations

N/A

@Zach-O-Bates Zach-O-Bates changed the title Rate testing Rate Testing Apr 13, 2026
@huss
Copy link
Copy Markdown
Member

huss commented Apr 16, 2026

This is to note that PR #1593 may change the route location of the login and this could impact this PR.

OED really should check all route names to verify they are correct compared to usage here and the upcoming test code (this part is outside the scope of the PR).

Copy link
Copy Markdown
Member

@huss huss left a comment

Choose a reason for hiding this comment

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

@Zach-O-Bates Thanks for working on this issue. Overall it is okay but I've made some comments to consider. Please let me know if anything is not clear or you have questions/thoughts.

Comment thread src/server/app.js Outdated
Comment thread src/server/tempRatetest/rateTest.js Outdated
Comment thread src/server/tempRatetest/rateTest.js Outdated
Comment thread src/server/tempRatetest/rateTest.js Outdated
Comment thread src/server/app.js Outdated
Comment thread src/server/app.js Outdated
Comment thread src/server/tempRatetest/rateTest.js Outdated
});
expect(first).to.have.status(401);

const second = await chai.request(app)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This implicitly assumes the rate is 1 request per a unit of time and that the test runs fast enough to not cross that amount of time between the first and second attempt. That should be okay but a comment should make it explicit. Note if the time frame of the limit is changed as suggested in another comment (to per hour) then the odds of this happening goes to zero but the rationale should still be commented.

Comment thread src/server/tempRatetest/rateTest.js Outdated
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This file tests the login route rate limiting functionality.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The name of this file does not seem to scope it as only for login (rateTest.js). I think the name should stay the same but the comment state it currently only does the login route. There should also be a TODO to check every rate limit in app.js.

I think an issue should be opened to say this needs to be done (unless you do it now which I'm assuming you are not). Would you like to do that or should I?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I can open it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not seeing the issue yet.

Comment thread src/server/app.js Outdated
Comment thread src/server/tempRatetest/rateTest.js Outdated
@@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The location of this file seems off. package.json has src/server/rateTest/**/*.js which seems reasonable. The test will not run at this time with the package script. I'm putting off testing until this and other comments are resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for fixing the location of the file. I know I said the proposed location was okay but I've reconsidered. How would you feel about changing the directory name from rateTest/ to testRate/. That would mean it would be listed next to the test/ directory and that seems useful to me.

Copy link
Copy Markdown
Member

@huss huss left a comment

Choose a reason for hiding this comment

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

@Zach-O-Bates Thank you for addressing my previous comments. I have made a couple of new ones and also commented on a couple of older ones. I think this is getting much closer. Please let me know if anything is not clear or you have thoughts/questions.

Comment thread src/server/app.js
const loginLimiter = rateLimit({
// Window of 1 hour
windowMs: 60 * 60 * 1000,
/* Rationale: The login route requires a more specifc and strict rate limit that must be tested seperately.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Misspelled: specifc, seperately & enviroment

It ensures that repeated login attempts are blocked after
exceeding the configured rate limit. */

// TODO: Create additonal rate limits tests for other OED Routes
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

spelling: additonal

Comment thread src/server/tempRatetest/rateTest.js Outdated
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This file tests the login route rate limiting functionality.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not seeing the issue yet.

Comment thread src/server/tempRatetest/rateTest.js Outdated
@@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for fixing the location of the file. I know I said the proposed location was okay but I've reconsidered. How would you feel about changing the directory name from rateTest/ to testRate/. That would mean it would be listed next to the test/ directory and that seems useful to me.

});

//First request that makes it through to authentication
expect(first).to.have.status(HTTP_CODE.UNAUTHORIZED);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test is failing on my machine. It is giving a 404/NOT_FOUND instead of 401/UNAUTHORIZED. I have not looked into why. Do you see that when you do npm run rateTest?

I'm going to put off testing all the new package script choices until this is resolved.

//First request that makes it through to authentication
expect(first).to.have.status(HTTP_CODE.UNAUTHORIZED);

const second = await chai.request(app)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Redoing comment since code moved. I had:

This implicitly assumes the rate is 1 request per a unit of time and that the test runs fast enough to not cross that amount of time between the first and second attempt. That should be okay but a comment should make it explicit. Note if the time frame of the limit is changed as suggested in another comment (to per hour) then the odds of this happening goes to zero but the rationale should still be commented.

I think discussing here that the limit should be 1 for an hour so this fails with a comment has value.

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.

limit login rate

2 participants