Skip to content

Commit b0b9d90

Browse files
committed
add name to apiKey
1 parent b339317 commit b0b9d90

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"test:integration": "cross-env DATABASE_LOCATION=:memory: vitest run --project integration",
1818
"test:e2e": "playwright test",
1919
"---- DB ------------------------------------------------------------": "",
20-
"setupdb": "pnpm migrate && pnpm db-types",
20+
"setupdb": "pnpm migrate:latest && pnpm db-types",
2121
"migrate": "tsx ./services/src/kysely/migrator.ts",
2222
"migrate:latest": "pnpm run migrate -- latest",
2323
"migrate:up": "pnpm run migrate -- up",

services/src/auth/api-key-repository.integration.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
createApiKey,
88
deleteApiKey,
99
getApiKeysForProject,
10-
projectHasKey
10+
projectHasKey,
11+
setApiKeyName
1112
} from './api-key.repository'
1213

1314
beforeEach(async () => {
@@ -40,6 +41,14 @@ describe('ApiKey Repository', () => {
4041
expect(result).includes(key2.key)
4142
})
4243

44+
it('should retrieve the name of the apiKey when it is created with one', async () => {
45+
const name = 'some key name'
46+
const key = await createApiKey(projectId, name)
47+
const result = await getApiKeysForProject(projectId)
48+
49+
expect(result.find((it) => it.key === key.key)?.name).toBe(name)
50+
})
51+
4352
it('should no longer find deleted keys', async () => {
4453
const key = await createApiKey(projectId)
4554

@@ -62,6 +71,41 @@ describe('ApiKey Repository', () => {
6271
})
6372
})
6473

74+
describe('setApiKeyName', () => {
75+
it('should be able to set a name when previously there was none', async () => {
76+
const key = await createApiKey(projectId)
77+
const initialRetrieval = await getApiKeysForProject(projectId).then((it) =>
78+
it.find((apiKey) => apiKey.key === key.key)
79+
)
80+
81+
expect(initialRetrieval?.name).toBeFalsy()
82+
const updatedName = 'some new apiKeyName'
83+
await setApiKeyName(key.id, updatedName)
84+
85+
const secondRetrieval = await getApiKeysForProject(projectId).then((it) =>
86+
it.find((apiKey) => apiKey.key === key.key)
87+
)
88+
expect(secondRetrieval?.name).toBe(updatedName)
89+
})
90+
91+
it('should be able to set the name', async () => {
92+
const initialName = 'my personal api key'
93+
const key = await createApiKey(projectId, initialName)
94+
const initialRetrieval = await getApiKeysForProject(projectId).then((it) =>
95+
it.find((apiKey) => apiKey.key === key.key)
96+
)
97+
98+
expect(initialRetrieval?.name).toBe(initialName)
99+
const updatedName = 'some new apiKeyName'
100+
await setApiKeyName(key.id, updatedName)
101+
102+
const secondRetrieval = await getApiKeysForProject(projectId).then((it) =>
103+
it.find((apiKey) => apiKey.key === key.key)
104+
)
105+
expect(secondRetrieval?.name).toBe(updatedName)
106+
})
107+
})
108+
65109
describe('projectHasKey', () => {
66110
it('should return true if there is a key for the project', async () => {
67111
const key = await createApiKey(projectId)

services/src/auth/api-key.repository.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { db } from '../db/database'
22
import type { ApiKeyCreationParams, SelectableApiKey } from './api-key'
33
import { v4 as uuid } from 'uuid'
44

5-
export function createApiKey(projectId: number): Promise<SelectableApiKey> {
5+
export function createApiKey(
6+
projectId: number,
7+
name: string | null = null
8+
): Promise<SelectableApiKey> {
69
const insertKey: ApiKeyCreationParams = {
710
key: uuid(),
11+
name,
812
project_id: projectId
913
}
1014
return db
@@ -18,6 +22,16 @@ export function getApiKeysForProject(projectId: number): Promise<SelectableApiKe
1822
return db.selectFrom('apikeys').selectAll().where('project_id', '==', projectId).execute()
1923
}
2024

25+
export async function setApiKeyName(keyId: number, name: string): Promise<void> {
26+
await db
27+
.updateTable('apikeys')
28+
.set({
29+
name
30+
})
31+
.where('id', '==', keyId)
32+
.execute()
33+
}
34+
2135
export async function projectHasKey(projectId: number, key: string): Promise<boolean> {
2236
const result = await db
2337
.selectFrom('apikeys')

services/src/kysely/migrations/2024-05-16T19:00:00Z_apikey.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createTableMigration } from '../migration.util'
44
export async function up(db: Kysely<unknown>): Promise<void> {
55
await createTableMigration(db, 'apikeys')
66
.addColumn('key', 'text', (col) => col.unique().notNull())
7+
.addColumn('name', 'text')
78
.addColumn('project_id', 'integer', (col) =>
89
col.references('projects.id').onDelete('cascade').notNull()
910
)

0 commit comments

Comments
 (0)