Skip to content

Commit

Permalink
refactor(tests): rate limit wait helper
Browse files Browse the repository at this point in the history
POST to /eligibility/confirm needs to respect rate limit
  • Loading branch information
thekaveman committed Apr 14, 2023
1 parent ea98a9b commit 6642af0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions tests/cypress/specs/courtesy-cards.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe("Courtesy Cards", () => {
});

it("Confirms an eligible user", () => {
helpers.rateLimitWait();

cy.get("#sub").type(users.eligible.sub);
cy.get("#name").type(users.eligible.name);
cy.get("#form-eligibility-verification button[type='submit']").click();
Expand All @@ -18,6 +20,8 @@ describe("Courtesy Cards", () => {
});

it("Rejects an ineligible user", () => {
helpers.rateLimitWait();

cy.get("#sub").type(users.ineligible.sub);
cy.get("#name").type(users.ineligible.name);
cy.get("#form-eligibility-verification button[type='submit']").click();
Expand Down
9 changes: 9 additions & 0 deletions tests/cypress/specs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ const agencies = require("../fixtures/transit-agencies");

const agency = agencies[0].fields;

// from nginx.conf
const RATE_LIMIT = 12;
// 12 requests/minute ==> 5 seconds in milliseconds
const WAIT_TIME = (60 / RATE_LIMIT) * 1000;

export const rateLimitWait = () => {
cy.wait(WAIT_TIME);
};

export const selectAgency = () => {
cy.location("pathname").should("eq", "/");

Expand Down
7 changes: 2 additions & 5 deletions tests/cypress/specs/rate-limit.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ const users = require("../fixtures/users.json");
const helpers = require("./helpers");
const { eligibility_url, post_confirm } = require("../plugins/eligibility");

const RATE_LIMIT = 12;
// 5 seconds in milliseconds => allows 12 requests/minute
const WAIT_TIME = 5 * 1000;
const SUCCESS_STATUS = 302;
const FAIL_STATUS = 503;

Expand Down Expand Up @@ -43,7 +40,7 @@ describe("Rate limiting feature spec", () => {

it("Allows requests with enough time in between", () => {
// start with a wait, since the previous test already triggered the limit
cy.wait(WAIT_TIME);
helpers.rateLimitWait();

const sub = users.ineligible.sub;
const name = users.ineligible.name;
Expand All @@ -59,7 +56,7 @@ describe("Rate limiting feature spec", () => {
for (let i = 0; i < RATE_LIMIT; i++) {
post_confirm(csrfmiddlewaretoken, sub, name, false).then((res) => {
expect(res.status).to.eq(SUCCESS_STATUS);
cy.wait(WAIT_TIME);
helpers.rateLimitWait();
});
}
});
Expand Down

0 comments on commit 6642af0

Please sign in to comment.