@@ -5,6 +5,32 @@ import { PrismaClient } from "../generated/client";
55import { generateApiKey , serializeApiKeyScopes } from "../auth/apiKeys" ;
66import { getTestPrisma , setupTestDb } from "./testUtils" ;
77
8+ type ApiKeyFixture = {
9+ id : string ;
10+ token : string ;
11+ } ;
12+
13+ async function createApiKeyFixture (
14+ prisma : PrismaClient ,
15+ userId : string ,
16+ name : string ,
17+ ) : Promise < ApiKeyFixture > {
18+ const generated = generateApiKey ( ) ;
19+ const apiKey = await prisma . apiKey . create ( {
20+ data : {
21+ userId,
22+ name,
23+ keyId : generated . keyId ,
24+ tokenHash : generated . tokenHash ,
25+ prefix : generated . prefix ,
26+ scopes : serializeApiKeyScopes ( ) ,
27+ } ,
28+ select : { id : true } ,
29+ } ) ;
30+
31+ return { id : apiKey . id , token : generated . token } ;
32+ }
33+
834describe ( "API key authentication" , ( ) => {
935 let prisma : PrismaClient ;
1036 let app : any ;
@@ -37,20 +63,9 @@ describe("API key authentication", () => {
3763 } ) ;
3864 userId = user . id ;
3965
40- const generated = generateApiKey ( ) ;
41- apiKeyToken = generated . token ;
42- const apiKey = await prisma . apiKey . create ( {
43- data : {
44- userId,
45- name : "Obsidian automation" ,
46- keyId : generated . keyId ,
47- tokenHash : generated . tokenHash ,
48- prefix : generated . prefix ,
49- scopes : serializeApiKeyScopes ( ) ,
50- } ,
51- select : { id : true } ,
52- } ) ;
53- apiKeyId = apiKey . id ;
66+ const apiKeyFixture = await createApiKeyFixture ( prisma , userId , "Obsidian automation" ) ;
67+ apiKeyToken = apiKeyFixture . token ;
68+ apiKeyId = apiKeyFixture . id ;
5469
5570 const adminUser = await prisma . user . create ( {
5671 data : {
@@ -62,18 +77,8 @@ describe("API key authentication", () => {
6277 } ,
6378 select : { id : true } ,
6479 } ) ;
65- const adminGenerated = generateApiKey ( ) ;
66- adminApiKeyToken = adminGenerated . token ;
67- await prisma . apiKey . create ( {
68- data : {
69- userId : adminUser . id ,
70- name : "Admin automation" ,
71- keyId : adminGenerated . keyId ,
72- tokenHash : adminGenerated . tokenHash ,
73- prefix : adminGenerated . prefix ,
74- scopes : serializeApiKeyScopes ( ) ,
75- } ,
76- } ) ;
80+ const adminApiKeyFixture = await createApiKeyFixture ( prisma , adminUser . id , "Admin automation" ) ;
81+ adminApiKeyToken = adminApiKeyFixture . token ;
7782 } ) ;
7883
7984 afterAll ( async ( ) => {
0 commit comments