Skip to content

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

improve test coverage

Thiago Araujo da Silva8745e976de53
diff --git a/backend/e2e-test/routes/v1/auth-token.spec.ts b/backend/e2e-test/routes/v1/auth-token.spec.ts
index 7a0b7c9fa8..93cb52de13 100644
--- a/backend/e2e-test/routes/v1/auth-token.spec.ts
+++ b/backend/e2e-test/routes/v1/auth-token.spec.ts
@@ -69,7 +69,7 @@ describe("Auth Token V1", () => {
expect(res.statusCode).toBeGreaterThanOrEqual(400);
});
- test("Logout invalidates session, subsequent refresh and access fail", async () => {
+ test("Logout invalidates session, subsequent access and refresh fail", async () => {
// Login to get tokens
const loginRes = await testServer.inject({
method: "POST",
@@ -98,6 +98,17 @@ describe("Auth Token V1", () => {
});
expect(logoutRes.statusCode).toBe(200);
+ // Before the refresh below: replaying the old refresh token deletes the session, which would turn this into the
+ // missing-session case instead of the stale-access-version one.
+ 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);
+
// Subsequent refresh should fail
const refreshRes = await testServer.inject({
method: "POST",
@@ -108,15 +119,6 @@ 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 () => {
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 74b139513d..db7e237b66 100644
--- a/backend/src/services/auth-token/auth-token-service.test.ts
+++ b/backend/src/services/auth-token/auth-token-service.test.ts
@@ -254,7 +254,12 @@ describe("tokenServiceFactory — org scope of user tokens", () => {
};
const build = ({
- user = { id: USER_ID, isAccepted: true, isLocked: false, temporaryLockDateEnd: null as Date | null },
+ user = { id: USER_ID, isAccepted: true, isLocked: false, temporaryLockDateEnd: null } as {
+ id: string;
+ isAccepted: boolean;
+ isLocked: boolean;
+ temporaryLockDateEnd: Date | null;
+ } | null,
memberships = { [ROOT]: true, [SUB]: true } as Record<
string,
boolean | { isActive: boolean; status: string } | undefined
@@ -339,7 +344,16 @@ describe("tokenServiceFactory — org scope of user tokens", () => {
});
// 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.
+ // logged-out sessions; a missing or unaccepted user holding a session is hard to reach there.
+ 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.message).toBe("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 }