pythonpioneer / login

This repo only contain the login code.
0 stars 0 forks source link

Token Expiration Test #6

Open pythonpioneer opened 1 month ago

pythonpioneer commented 1 month ago

Issue

Issue Type: [Test]

Issue Category: [Backend]

Issue Desc:

You can find this test inside src/v1/__tests__/logoutUser.test.ts

Code Example

describe("when refresh token is expired", () => {

  it("Should return status code 401", async () => {

    /** FLOW OF THE TEST
           * Register the user
       * Login the user
           * store the refresh token to use it for logout
       * fast-forward the time to expire the refresh token, (REFRESH_TOKEN_AGE VARIABLE IS AVAILABLE, YOU CAN IMPORT IT)
       * Logout the user
       * assert the test, based on the standard status codes
           */

         // register the user
        await request(app).post(registerRoute).send(userData);

    // login the user
    const response = await request(app).post(loginRoute).send({
        email: userData.email,
        password: userData.password
    });

    expect(response.status).toBe(200);
        expect(response.body.data.accessToken).toBeDefined();
    expect(response.body.data.refreshToken).toBeDefined();

    // saving the refresh token
    const refreshToken = response.body.data.refreshToken;

    // fast-forward the time to expire the refresh token
    jest.useFakeTimers();
    jest.advanceTimersByTime(REFRESH_TOKEN_AGE + 1);

    // now, make the request to the logout route
    const logoutResponse = await request(app).post(logoutRoute)
        .set('Cookie', `refreshToken=${refreshToken}`)
        .send();

    expect(logoutResponse.status).toBe(401);

    // shift to real timer
    jest.useRealTimers();
  });
});

Problem Snapshot

Screenshot 2024-10-10 at 11 26 28 PM

Setup to Contribute

Contributing to this project is straightforward. Follow these steps to get started:

If you encounter any issues while setting up the project, please don't hesitate to leave a message in the comments section. You can also refer to the contributing guidelines for more detailed instructions and best practices.

pythonpioneer commented 1 week ago

We can create a utility method to generate expired tokens or tokens that will expire after some time.