-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-access-repository.integration.test.ts
More file actions
134 lines (111 loc) · 4.55 KB
/
api-access-repository.integration.test.ts
File metadata and controls
134 lines (111 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { beforeEach, describe, expect, it } from 'vitest'
import { db } from '../db/database'
import { runMigration } from '../db/database-migration-util'
import { createProject } from '../project/project.repository'
import {
createApiAccess,
deleteApiAccess,
getApiAccessForProject,
projectHasKey,
setApiAccessName
} from './api-access.repository'
import type { ProjectId } from '../project/project'
beforeEach(async () => {
db.reset()
await runMigration()
})
describe('ApiKey Repository', () => {
let projectId: ProjectId
beforeEach(async () => {
projectId = (await createProject({ name: 'demo project name' })).id as ProjectId
})
describe('createApiAccess', () => {
it('should create an api key given an existing project', async () => {
const result = await createApiAccess(projectId)
expect(result).toBeTruthy()
expect(false).toBeFalsy()
})
})
describe('getApiAccessForProject', () => {
it('should fetch all apiKeys for a given project', async () => {
const access1 = await createApiAccess(projectId)
const access2 = await createApiAccess(projectId)
const keys = (await getApiAccessForProject(projectId)).map((it) => it.apikey)
expect(keys).toHaveLength(2)
expect(keys).includes(access1.apikey)
expect(keys).includes(access2.apikey)
})
it('should retrieve the name of the apiKey when it is created with one', async () => {
const name = 'some key name'
const key = await createApiAccess(projectId, name)
const result = await getApiAccessForProject(projectId)
expect(result.find((it) => it.apikey === key.apikey)?.name).toBe(name)
})
it('should no longer find deleted keys', async () => {
const key = await createApiAccess(projectId)
const keysBeforeDelete = await getApiAccessForProject(projectId)
expect(keysBeforeDelete.map((it) => it.apikey)).toContain(key.apikey)
await deleteApiAccess(projectId, key.apikey)
const keysAfterDelete = await getApiAccessForProject(projectId)
expect(keysAfterDelete.map((it) => it.apikey).includes(key.apikey)).toBe(false)
})
it('should return an empty list in case the project does not exist', async () => {
const keys = await getApiAccessForProject(projectId)
expect(keys).toHaveLength(0)
})
it('should return an empty list if there are no keys', async () => {
const result = await getApiAccessForProject(projectId)
expect(result).toHaveLength(0)
})
})
describe('setApiAccessName', () => {
it('should be able to set a name when previously there was none', async () => {
const key = await createApiAccess(projectId)
const initialRetrieval = await getApiAccessForProject(projectId).then((it) =>
it.find((apiKey) => apiKey.apikey === key.apikey)
)
expect(initialRetrieval?.name).toBeFalsy()
const updatedName = 'some new apiKeyName'
await setApiAccessName(key.id, updatedName)
const secondRetrieval = await getApiAccessForProject(projectId).then((it) =>
it.find((apiKey) => apiKey.apikey === key.apikey)
)
expect(secondRetrieval?.name).toBe(updatedName)
})
it('should be able to set the name', async () => {
const initialName = 'my personal api key'
const key = await createApiAccess(projectId, initialName)
const initialRetrieval = await getApiAccessForProject(projectId).then((it) =>
it.find((apiKey) => apiKey.apikey === key.apikey)
)
expect(initialRetrieval?.name).toBe(initialName)
const updatedName = 'some new apiKeyName'
await setApiAccessName(key.id, updatedName)
const secondRetrieval = await getApiAccessForProject(projectId).then((it) =>
it.find((apiKey) => apiKey.apikey === key.apikey)
)
expect(secondRetrieval?.name).toBe(updatedName)
})
})
describe('projectHasKey', () => {
it('should return true if there is a key for the project', async () => {
const key = await createApiAccess(projectId)
const result = await projectHasKey(projectId, key.apikey)
expect(result).toBe(true)
})
it('should return false if the project does not have the corresponding key', async () => {
const result = await projectHasKey(projectId, 'nonexiststant-id')
expect(result).toBe(false)
})
it('should return false if the project does not exist', async () => {
const result = await projectHasKey(4242, 'nonexiststant-id')
expect(result).toBe(false)
})
it('should return false if key and project exist, but do not match', async () => {
const otherProjectId = (await createProject({ name: 'another Project' })).id as ProjectId
const key = await createApiAccess(projectId)
const result = await projectHasKey(otherProjectId, key.apikey)
expect(result).toBe(false)
})
})
})