Repository navigation
Expand file tree
/
Copy pathadmin-api.spec.ts
More file actions
306 lines (302 loc) · 13.3 KB
/
Copy pathadmin-api.spec.ts
File metadata and controls
306 lines (302 loc) · 13.3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright 2026 BoxLite AI
* SPDX-License-Identifier: AGPL-3.0
*/
jest.mock('uuid', () => ({ v4: jest.fn(() => 'mock-uuid'), validate: jest.fn(() => true) }))
import { HttpException, ValidationPipe, type INestApplication } from '@nestjs/common'
import { Test } from '@nestjs/testing'
import { getRedisConnectionToken } from '@nestjs-modules/ioredis'
import Ajv2020 from 'ajv/dist/2020'
import addFormats from 'ajv-formats'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import type { AddressInfo } from 'node:net'
import { parse } from 'yaml'
import { CombinedAuthGuard } from '../auth/combined-auth.guard'
import { AuthenticatedRateLimitGuard } from '../common/guards/authenticated-rate-limit.guard'
import { AllExceptionsFilter } from '../filters/all-exceptions.filter'
import { OrganizationUserDto } from '../organization/dto/organization-user.dto'
import { OrganizationUser } from '../organization/entities/organization-user.entity'
import { OrganizationMemberRole } from '../organization/enums/organization-member-role.enum'
import { OrganizationActionGuard } from '../organization/guards/organization-action.guard'
import { OrganizationInvitationService } from '../organization/services/organization-invitation.service'
import { OrganizationService } from '../organization/services/organization.service'
import { OrganizationUserService } from '../organization/services/organization-user.service'
import { User } from '../user/user.entity'
import { BoxliteInvitationController } from './boxlite-invitation.controller'
import { BoxliteMemberController } from './boxlite-member.controller'
const specRoot = resolve(__dirname, '../../../../openapi')
const specFile = 'admin.openapi.yaml'
const spec = parse(readFileSync(`${specRoot}/${specFile}`, 'utf8'))
const specId = 'https://contract.test/admin.openapi.yaml'
const ajv = addFormats(new Ajv2020({ strict: false, allErrors: true }))
ajv.addSchema({ components: spec.components, paths: spec.paths }, specId)
ajv.addSchema(
{ components: parse(readFileSync(`${specRoot}/box.openapi.yaml`, 'utf8')).components },
'https://contract.test/box.openapi.yaml',
)
const inviteId = '6f1c2a4e-0b7d-4c3e-9a5f-2d8e1b7c4a90'
const member = {
userId: 'owner',
email: 'owner@example.com',
name: 'Owner',
role: 'owner',
createdAt: new Date('2026-09-01Z'),
}
const invitation = {
id: inviteId,
email: 'new@example.com',
role: 'owner',
status: 'pending',
invitedBy: 'owner@example.com',
createdAt: new Date('2026-09-02Z'),
expiresAt: new Date('2026-09-09Z'),
}
const operations = [
{ method: 'GET', path: '/organizations/users', status: 200, scope: 'read:members' },
{ method: 'GET', path: '/organizations/invites', status: 200, scope: 'read:members' },
{ method: 'POST', path: '/organizations/invites', status: 201, scope: 'write:members' },
{
method: 'DELETE',
path: '/organizations/users/member',
schema: '/organizations/users/{user_id}',
status: 204,
scope: 'write:members',
},
{
method: 'DELETE',
path: `/organizations/invites/${inviteId}`,
schema: '/organizations/invites/{invite_id}',
status: 204,
scope: 'write:members',
},
]
describe('Admin API HTTP contract', () => {
let app: INestApplication
let principal: any
const members = {
findOne: jest.fn(async (org: string, user: string) =>
org === 'org-1' && ['owner', 'member'].includes(user) ? { role: user, organizationId: org, userId: user } : null,
),
findPage: jest.fn(async () => [member]),
findAll: jest.fn(async () => [member]),
delete: jest.fn(),
}
const invites = {
create: jest.fn(async () => invitation),
findPage: jest.fn(async () => [invitation]),
findPending: jest.fn(async () => [invitation]),
cancel: jest.fn(),
}
beforeAll(async () => {
const module = await Test.createTestingModule({
controllers: [BoxliteMemberController, BoxliteInvitationController],
providers: [
OrganizationActionGuard,
{ provide: OrganizationUserService, useValue: members },
{ provide: OrganizationInvitationService, useValue: invites },
{ provide: OrganizationService, useValue: { findOne: async (id: string) => ({ id }) } },
{ provide: getRedisConnectionToken(), useValue: { get: async () => null, set: async () => 'OK' } },
],
})
.overrideGuard(CombinedAuthGuard)
.useValue({
canActivate: (ctx: any) => {
ctx.switchToHttp().getRequest().user = { ...principal }
return true
},
})
.overrideGuard(AuthenticatedRateLimitGuard)
.useValue({ canActivate: () => true })
.compile()
app = module.createNestApplication()
app.setGlobalPrefix('api')
app.useGlobalPipes(new ValidationPipe({ transform: true }))
app.useGlobalFilters(new AllExceptionsFilter({ incrementFailedAuth: jest.fn() } as never))
await app.listen(0)
})
afterAll(async () => {
await app?.close()
})
beforeEach(() => {
jest.clearAllMocks()
principal = {
userId: 'owner',
email: 'owner@example.com',
role: 'user',
organizationId: 'org-1',
apiKey: { organizationId: 'org-1', permissions: ['read:members', 'write:members'] },
}
})
async function call(method: string, path: string, body?: unknown, headers: Record<string, string> = {}) {
const address = app.getHttpServer().address() as AddressInfo
return fetch(`http://127.0.0.1:${address.port}/api/v1${path}`, {
method,
headers: { 'content-type': 'application/json', ...headers },
body: body === undefined ? undefined : JSON.stringify(body),
})
}
const payload = { email: 'new@example.com', role: 'owner' }
async function success(response: Response, path: string, method: string, status: number) {
expect(response.status).toBe(status)
const contract = spec.paths[path]?.[method.toLowerCase()]?.responses[status]
expect(contract).toBeDefined()
if (status === 204) {
expect(contract.content).toBeUndefined()
expect(await response.text()).toBe('')
return
}
const pointer = path.replace(/~/g, '~0').replace(/\//g, '~1')
const validate = ajv.getSchema(
`${specId}#/paths/${pointer}/${method.toLowerCase()}/responses/${status}/content/application~1json/schema`,
)!
const body = await response.json()
expect(validate(body) ? [] : validate.errors).toEqual([])
return body
}
it.each(operations)('serves $method $path with its declared scope and success schema', async (op) => {
principal.apiKey.permissions = [op.scope]
await success(
await call(op.method, op.path, op.method === 'POST' ? payload : undefined),
op.schema ?? op.path,
op.method,
op.status,
)
})
it.each(operations)('keeps read and write independent on $method $path', async (op) => {
principal.apiKey.permissions = [op.scope === 'read:members' ? 'write:members' : 'read:members']
expect((await call(op.method, op.path, op.method === 'POST' ? payload : undefined)).status).toBe(403)
expect(invites.create).not.toHaveBeenCalled()
expect(members.delete).not.toHaveBeenCalled()
})
it.each([[], ['write:boxes'], ['read:tenant', 'write:tenant']])(
'rejects keys with obsolete or unrelated scopes %j',
async (...scopes) => {
principal.apiKey.permissions = scopes
expect((await call('GET', '/organizations/users')).status).toBe(403)
},
)
it.each(operations)('retains owner JWT access on $method $path', async (op) => {
delete principal.apiKey
expect((await call(op.method, op.path, op.method === 'POST' ? payload : undefined)).status).toBe(op.status)
})
it.each(operations)('enforces membership role on $method $path', async (op) => {
principal.userId = 'member'
expect((await call(op.method, op.path, op.method === 'POST' ? payload : undefined)).status).toBe(
op.method === 'GET' ? 200 : 403,
)
})
it('cannot use a header to move a key into another organization', async () => {
const response = await call('GET', '/organizations/users', undefined, { 'X-BoxLite-Organization-ID': 'org-2' })
expect(response.status).toBe(200)
expect(members.findPage).toHaveBeenCalledWith('org-1', 100, undefined)
})
it.each(['runner', 'proxy', 'health-check'])('refuses %s machine credentials', async (role) => {
principal = { role }
expect((await call('GET', '/organizations/users')).status).toBe(403)
})
it('bounds platform-admin keys by their delegated scopes', async () => {
principal.role = 'admin'
principal.apiKey.permissions = []
expect((await call('GET', '/organizations/users')).status).toBe(403)
})
it.each(['/members', '/default/members', '/org-1/members', '/invitations', '/default/invitations'])(
'removes obsolete route %s',
async (path) => {
delete principal.apiKey
expect((await call('GET', path)).status).toBe(404)
},
)
it.each(['missing email', 'missing user'])('normalizes %s in user responses', async (missing) => {
const membership = Object.assign(new OrganizationUser(), { ...member, userId: 'owner', assignedRoles: [] })
const user = missing === 'missing user' ? undefined : Object.assign(new User(), { name: 'Owner', email: '' })
members.findPage.mockResolvedValueOnce([OrganizationUserDto.fromEntities(membership, user)])
const body = await success(await call('GET', '/organizations/users'), '/organizations/users', 'GET', 200)
expect(body.users[0]).toMatchObject({
type: 'user',
id: 'owner',
email: null,
added_at: member.createdAt.toISOString(),
})
expect(body.users[0]).not.toHaveProperty('user_id')
})
it('returns expired invites with role and stable resource fields', async () => {
const body = await success(await call('GET', '/organizations/invites'), '/organizations/invites', 'GET', 200)
expect(body.invites[0]).toEqual({
type: 'invite',
id: inviteId,
email: invitation.email,
role: 'owner',
status: 'expired',
added_at: invitation.createdAt.toISOString(),
expires_at: invitation.expiresAt.toISOString(),
})
})
it('returns the original creation status and forwards the idempotency key', async () => {
const response = await call('POST', '/organizations/invites', payload, { 'Idempotency-Key': inviteId })
const body = await success(response, '/organizations/invites', 'POST', 201)
expect(body.status).toBe('pending')
expect(invites.create).toHaveBeenCalledWith(
'org-1',
{ email: payload.email, role: 'owner', assignedRoleIds: [], expiresAt: undefined },
'owner@example.com',
{ userId: 'owner', idempotencyKey: inviteId },
)
})
it.each([
{ email: 'a@example.com' },
{ ...payload, role: 'member' },
{ ...payload, extra: true },
{ ...payload, email: 'bad' },
{ ...payload, expires_at: null },
{ ...payload, expires_at: 'not-a-date' },
])('rejects invalid create request %j', async (body) => {
expect((await call('POST', '/organizations/invites', body)).status).toBe(400)
expect(invites.create).not.toHaveBeenCalled()
})
it('rejects invalid idempotency keys before invoking the service', async () => {
expect((await call('POST', '/organizations/invites', payload, { 'Idempotency-Key': 'invalid' })).status).toBe(400)
expect(invites.create).not.toHaveBeenCalled()
})
it.each(['0', '1001', '1.5', 'bad'])('rejects pageSize %s', async (size) => {
expect((await call('GET', `/organizations/users?pageSize=${size}`)).status).toBe(400)
expect(members.findPage).not.toHaveBeenCalled()
})
it('uses the next-page cursor and binds it to its organization and resource', async () => {
members.findPage.mockResolvedValueOnce([member, { ...member, userId: 'user-2' }])
const first = await success(
await call('GET', '/organizations/users?pageSize=1'),
'/organizations/users',
'GET',
200,
)
expect(first.users).toHaveLength(1)
expect(first.next_page_token).toEqual(expect.any(String))
const second = await success(
await call('GET', `/organizations/users?pageSize=1&pageToken=${encodeURIComponent(first.next_page_token)}`),
'/organizations/users',
'GET',
200,
)
expect(members.findPage).toHaveBeenLastCalledWith('org-1', 1, 'owner')
expect(second).not.toHaveProperty('next_page_token')
expect((await call('GET', `/organizations/invites?pageToken=${first.next_page_token}`)).status).toBe(400)
principal.organizationId = 'org-2'
principal.apiKey.organizationId = 'org-2'
principal.role = 'admin'
expect((await call('GET', `/organizations/users?pageToken=${first.next_page_token}`)).status).toBe(400)
})
it.each(['%%%', '', 'e30'])('rejects malformed pageToken %s', async (token) => {
expect((await call('GET', `/organizations/invites?pageToken=${encodeURIComponent(token)}`)).status).toBe(400)
expect(invites.findPage).not.toHaveBeenCalled()
})
it.each([400, 401, 403, 404, 409, 422, 429, 500, 503])('uses the shared Box error schema for %s', async (status) => {
members.findPage.mockRejectedValueOnce(new HttpException('Request refused', status))
const response = await call('GET', '/organizations/users')
expect(response.status).toBe(status)
const body = await response.json()
const validate = ajv.getSchema('https://contract.test/box.openapi.yaml#/components/schemas/ErrorResponse')!
expect(validate(body) ? [] : validate.errors).toEqual([])
expect(body.error.message).toBe('Request refused')
})
})