Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix tests

Thiago Araujo da Silva4db2d1e261eb
diff --git a/backend/e2e-test/routes/v1/auth-token.spec.ts b/backend/e2e-test/routes/v1/auth-token.spec.ts
index d0d48222e4..7a0b7c9fa8 100644
--- a/backend/e2e-test/routes/v1/auth-token.spec.ts
+++ b/backend/e2e-test/routes/v1/auth-token.spec.ts
@@ -2,6 +2,13 @@ import { seedData1 } from "@app/db/seed-data";
import { extractCookie } from "../../testUtils/cookies";
+const sessionNoLongerValidResponse = {
+ reqId: expect.any(String),
+ statusCode: 401,
+ error: "InvalidToken",
+ message: "Your session is no longer valid, please re-authenticate"
+};
+
describe("Auth Token V1", () => {
test("checkAuth with valid JWT returns 200", async () => {
const res = await testServer.inject({
@@ -62,7 +69,7 @@ describe("Auth Token V1", () => {
expect(res.statusCode).toBeGreaterThanOrEqual(400);
});
- test("Logout invalidates session, subsequent refresh fails", async () => {
+ test("Logout invalidates session, subsequent refresh and access fail", async () => {
// Login to get tokens
const loginRes = await testServer.inject({
method: "POST",
@@ -101,6 +108,15 @@ describe("Auth Token V1", () => {
});
expect(refreshRes.statusCode).toBeGreaterThanOrEqual(400);
+
+ const checkAuthRes = await testServer.inject({
+ method: "POST",
+ url: "/api/v1/auth/checkAuth",
+ headers: { authorization: `Bearer ${accessToken}` }
+ });
+
+ expect(checkAuthRes.statusCode).toBe(401);
+ expect(checkAuthRes.json()).toEqual(sessionNoLongerValidResponse);
});
test("checkAuth with a JWT whose session was revoked returns 401", async () => {
@@ -136,11 +152,6 @@ describe("Auth Token V1", () => {
});
expect(res.statusCode).toBe(401);
- expect(res.json()).toEqual({
- reqId: expect.any(String),
- statusCode: 401,
- error: "InvalidToken",
- message: "Your session is no longer valid, please re-authenticate"
- });
+ expect(res.json()).toEqual(sessionNoLongerValidResponse);
});
});
diff --git a/backend/src/services/auth-token/auth-token-service.test.ts b/backend/src/services/auth-token/auth-token-service.test.ts
index 2ab22880a0..74b139513d 100644
--- a/backend/src/services/auth-token/auth-token-service.test.ts
+++ b/backend/src/services/auth-token/auth-token-service.test.ts
@@ -254,13 +254,7 @@ describe("tokenServiceFactory — org scope of user tokens", () => {
};
const build = ({
- session = SESSION as typeof SESSION | null,
- user = { id: USER_ID, isAccepted: true, isLocked: false, temporaryLockDateEnd: null as Date | null } as {
- id: string;
- isAccepted: boolean;
- isLocked: boolean;
- temporaryLockDateEnd: Date | null;
- } | null,
+ user = { id: USER_ID, isAccepted: true, isLocked: false, temporaryLockDateEnd: null as Date | null },
memberships = { [ROOT]: true, [SUB]: true } as Record<
string,
boolean | { isActive: boolean; status: string } | undefined
@@ -280,7 +274,7 @@ describe("tokenServiceFactory — org scope of user tokens", () => {
)
};
const service = tokenServiceFactory({
- tokenDAL: { findOneTokenSession: vi.fn().mockResolvedValue(session) } as never,
+ tokenDAL: { findOneTokenSession: vi.fn().mockResolvedValue(SESSION) } as never,
userDAL: { findById: vi.fn().mockResolvedValue(user) } as never,
orgDAL: orgDAL as never,
membershipUserDAL: {} as never,
@@ -344,48 +338,17 @@ describe("tokenServiceFactory — org scope of user tokens", () => {
);
});
- // UnauthorizedError, not NotFoundError: clients only treat a 401 as "log in again", and a 404 reads as a missing resource.
- test("a token whose session no longer exists is refused with UnauthorizedError", async () => {
- const { service } = build({ session: null });
- const err = await expectRejected(
- service.fnValidateJwtIdentity(accessToken({ organizationId: ROOT })),
- UnauthorizedError
- );
- expect(err).toMatchObject({
- name: "InvalidToken",
- message: "Your session is no longer valid, please re-authenticate"
- });
- });
-
- test("a token issued before its session was invalidated is refused with UnauthorizedError", async () => {
- const { service } = build({ session: { ...SESSION, accessVersion: 2 } });
- const err = await expectRejected(
- service.fnValidateJwtIdentity(accessToken({ organizationId: ROOT })),
- UnauthorizedError
- );
- expect(err).toMatchObject({
- name: "InvalidToken",
- message: "Your session is no longer valid, please re-authenticate"
+ // UnauthorizedError, not NotFoundError: clients only treat a 401 as "log in again". The e2e suite covers revoked and
+ // logged-out sessions; an unaccepted user holding a session is hard to reach there.
+ test("a token whose user is not accepted is refused with UnauthorizedError", async () => {
+ const { service } = build({
+ user: { id: USER_ID, isAccepted: false, isLocked: false, temporaryLockDateEnd: null }
});
- });
-
- test("a token whose user no longer exists is refused with UnauthorizedError", async () => {
- const { service } = build({ user: null });
const err = await expectRejected(
service.fnValidateJwtIdentity(accessToken({ organizationId: ROOT })),
UnauthorizedError
);
- expect(err).toMatchObject({
- name: "InvalidToken",
- message: "Your session is no longer valid, please re-authenticate"
- });
- });
-
- test("a token whose user is not accepted is refused with UnauthorizedError", async () => {
- const { service } = build({
- user: { id: USER_ID, isAccepted: false, isLocked: false, temporaryLockDateEnd: null }
- });
- await expectRejected(service.fnValidateJwtIdentity(accessToken({ organizationId: ROOT })), UnauthorizedError);
+ expect(err.message).toBe("Your session is no longer valid, please re-authenticate");
});
test("subOrganizationId without organizationId is refused", async () => {