mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-28 09:56:25 -05:00
chore: more unit tests for api-key (#6499)
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
// =========================================================================
|
||||
|
||||
Reference in New Issue
Block a user