From 3729ec4cdfe628aec68ec462e8cea229f444b37b Mon Sep 17 00:00:00 2001 From: Maxwell <145994855+ping-maxwell@users.noreply.github.com> Date: Thu, 4 Dec 2025 07:45:39 +1000 Subject: [PATCH] chore: more unit tests for api-key (#6499) --- .../src/plugins/api-key/api-key.test.ts | 111 ++++++++++++++---- 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/packages/better-auth/src/plugins/api-key/api-key.test.ts b/packages/better-auth/src/plugins/api-key/api-key.test.ts index 1b1a3928e7..09541d5a8f 100644 --- a/packages/better-auth/src/plugins/api-key/api-key.test.ts +++ b/packages/better-auth/src/plugins/api-key/api-key.test.ts @@ -1681,37 +1681,98 @@ describe("api-key", async () => { // Sessions from API keys // ========================================================================= - it("should get session from an API key", async () => { - const { client, auth, signInWithTestUser } = await getTestInstance( - { - plugins: [ - apiKey({ - enableSessionForAPIKeys: true, - }), - ], - }, - { - clientOptions: { - plugins: [apiKeyClient()], + describe("enableSessionForAPIKeys", () => { + it("should get session from an API key", async () => { + const { client, auth, signInWithTestUser } = await getTestInstance( + { + plugins: [ + apiKey({ + enableSessionForAPIKeys: true, + }), + ], }, - }, - ); + { + clientOptions: { + plugins: [apiKeyClient()], + }, + }, + ); - const { headers: userHeaders } = await signInWithTestUser(); + const { headers: userHeaders } = await signInWithTestUser(); - const { data: apiKey2 } = await client.apiKey.create( - {}, - { headers: userHeaders }, - ); - if (!apiKey2) return; - const headers = new Headers(); - headers.set("x-api-key", apiKey2.key); + const { data: apiKey2 } = await client.apiKey.create( + {}, + { headers: userHeaders }, + ); + if (!apiKey2) return; + const headers = new Headers(); + headers.set("x-api-key", apiKey2.key); - const session = await auth.api.getSession({ - headers: headers, + const session = await auth.api.getSession({ + headers: headers, + }); + + expect(session?.session).toBeDefined(); }); - expect(session?.session).toBeDefined(); + it("should not get session from an API key if enableSessionForAPIKeys is false", async () => { + const { client, auth, signInWithTestUser } = await getTestInstance( + { + plugins: [ + apiKey({ + enableSessionForAPIKeys: false, + }), + ], + }, + { + clientOptions: { + plugins: [apiKeyClient()], + }, + }, + ); + const { headers: userHeaders } = await signInWithTestUser(); + const { data: apiKey2 } = await client.apiKey.create( + {}, + { headers: userHeaders }, + ); + if (!apiKey2) return; + const headers = new Headers(); + headers.set("x-api-key", apiKey2.key); + const session = await auth.api.getSession({ + headers: headers, + }); + expect(session).toBeNull(); + }); + + it("should get the Response object when asResponse is true", async () => { + const { client, auth, signInWithTestUser } = await getTestInstance( + { + plugins: [ + apiKey({ + enableSessionForAPIKeys: true, + }), + ], + }, + { + clientOptions: { + plugins: [apiKeyClient()], + }, + }, + ); + const { headers: userHeaders } = await signInWithTestUser(); + const { data: apiKey2 } = await client.apiKey.create( + {}, + { headers: userHeaders }, + ); + if (!apiKey2) return; + const headers = new Headers(); + headers.set("x-api-key", apiKey2.key); + const res = await auth.api.getSession({ + headers: headers, + asResponse: true, + }); + expect(res).toBeInstanceOf(Response); + }); }); // =========================================================================