Skip to content

Commit cb7e20b

Browse files
Merge pull request #203 from jaymhorsh/test/api-keys-integration
Test/api keys integration
2 parents 1e1bd3b + 13569d7 commit cb7e20b

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

tests/integration/apiKeys.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,55 @@ describe('API Key flows', () => {
166166
expect(res.status).toBe(404);
167167
});
168168
});
169+
170+
describe('GET /api/apis/:id/keys', () => {
171+
it('lists all keys for an API (happy path)', async () => {
172+
// Create two keys for the same API
173+
const res1 = await request(app)
174+
.post('/api/apis/my-api-123/keys')
175+
.set('Authorization', `Bearer ${token}`);
176+
const res2 = await request(app)
177+
.post('/api/apis/my-api-123/keys')
178+
.set('Authorization', `Bearer ${token}`);
179+
180+
// Add a GET endpoint to list keys (simulate, since not in app)
181+
// We'll query the DB directly for this test
182+
const dbRes = await db.pool.query(
183+
`SELECT * FROM api_keys WHERE user_id = $1 AND api_id = $2`,
184+
[userId, 'my-api-123']
185+
);
186+
expect(dbRes.rows.length).toBeGreaterThanOrEqual(2);
187+
expect(dbRes.rows.map((r: any) => r.id)).toEqual(
188+
expect.arrayContaining([res1.body.id, res2.body.id])
189+
);
190+
});
191+
192+
it('returns empty list if no keys for API', async () => {
193+
const dbRes = await db.pool.query(
194+
`SELECT * FROM api_keys WHERE user_id = $1 AND api_id = $2`,
195+
[userId, 'nonexistent-api']
196+
);
197+
expect(dbRes.rows.length).toBe(0);
198+
});
199+
});
200+
201+
describe('Permission errors', () => {
202+
it('cannot create key for another user (simulate)', async () => {
203+
// Simulate by using a different token
204+
const otherToken = signTestToken({
205+
userId: '00000000-0000-0000-0000-000000000099',
206+
walletAddress: 'GDOTHER',
207+
});
208+
const res = await request(app)
209+
.post('/api/apis/my-api-123/keys')
210+
.set('Authorization', `Bearer ${otherToken}`);
211+
// Should succeed, but key will belong to other user
212+
expect(res.status).toBe(201);
213+
// Now try to revoke with original user
214+
const revoke = await request(app)
215+
.delete(`/api/keys/${res.body.id}`)
216+
.set('Authorization', `Bearer ${token}`);
217+
expect(revoke.status).toBe(404);
218+
});
219+
});
169220
});

0 commit comments

Comments
 (0)