@api-client/core 0.18.34 → 0.18.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/browser.d.ts +1 -0
- package/build/src/browser.d.ts.map +1 -1
- package/build/src/browser.js +1 -0
- package/build/src/browser.js.map +1 -1
- package/build/src/data/XmlReader.js +1 -1
- package/build/src/data/XmlReader.js.map +1 -1
- package/build/src/sdk/SdkMock.d.ts +301 -0
- package/build/src/sdk/SdkMock.d.ts.map +1 -0
- package/build/src/sdk/SdkMock.js +700 -0
- package/build/src/sdk/SdkMock.js.map +1 -0
- package/build/src/sdk/TrashSdk.d.ts +1 -1
- package/build/src/sdk/TrashSdk.d.ts.map +1 -1
- package/build/src/sdk/TrashSdk.js +1 -1
- package/build/src/sdk/TrashSdk.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/data/XmlReader.ts +1 -1
- package/src/sdk/README-MOCK.md +40 -0
- package/src/sdk/SdkMock.ts +792 -0
- package/src/sdk/TrashSdk.ts +1 -1
- package/tests/unit/amf/schema_gen_xml.spec.ts +7 -8
|
@@ -0,0 +1,792 @@
|
|
|
1
|
+
import { nanoid } from '../nanoid.js'
|
|
2
|
+
import type { IOrganization } from '../models/store/Organization.js'
|
|
3
|
+
import type { GroupSchema } from '../models/store/Group.js'
|
|
4
|
+
import type { IUser } from '../models/store/User.js'
|
|
5
|
+
import { OrganizationKind } from '../models/kinds.js'
|
|
6
|
+
import { GroupKind } from '../models/kinds.js'
|
|
7
|
+
import { Kind as UserKind } from '../models/store/User.js'
|
|
8
|
+
import { File, type IFile, type FileBreadcrumb } from '../models/store/File.js'
|
|
9
|
+
import { CertificateFileKind, DomainFileKind, FolderKind, ProjectKind } from '../models/kinds.js'
|
|
10
|
+
import type { ContextListResult, IBulkOperationResult } from '../events/BaseEvents.js'
|
|
11
|
+
import type { MediaPatchRevision } from '../patch/types.js'
|
|
12
|
+
import type { IFolder } from '../models/Folder.js'
|
|
13
|
+
import type { TrashEntry } from '../models/TrashEntry.js'
|
|
14
|
+
import type { StoreSdk } from './StoreSdkWeb.js'
|
|
15
|
+
import * as sinon from 'sinon'
|
|
16
|
+
import { Exception } from '../exceptions/exception.js'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Options for customizing mock responses.
|
|
20
|
+
*/
|
|
21
|
+
export interface MockResponseOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Custom response data to return instead of generated random data.
|
|
24
|
+
*/
|
|
25
|
+
data?: unknown
|
|
26
|
+
/**
|
|
27
|
+
* HTTP status code to return. Defaults to 200.
|
|
28
|
+
*/
|
|
29
|
+
status?: number
|
|
30
|
+
/**
|
|
31
|
+
* Custom headers to include in the response.
|
|
32
|
+
*/
|
|
33
|
+
headers?: Record<string, string>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A stub reference that can be used to restore the original behavior.
|
|
38
|
+
*/
|
|
39
|
+
export interface StubReference {
|
|
40
|
+
/**
|
|
41
|
+
* Restores the original behavior.
|
|
42
|
+
*/
|
|
43
|
+
restore: () => void
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* SDK mocking utility for testing. Provides simple API to mock SDK calls
|
|
48
|
+
* with random or custom responses.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { SdkMock } from '@api-client/core/sdk/SdkMock.js';
|
|
53
|
+
* import { StoreSdk } from '@api-client/core/sdk/StoreSdkWeb.js';
|
|
54
|
+
*
|
|
55
|
+
* const sdk = new StoreSdk('http://localhost:8080');
|
|
56
|
+
* const mocker = new SdkMock(sdk);
|
|
57
|
+
*
|
|
58
|
+
* // Simple usage - returns random valid organization
|
|
59
|
+
* const stub1 = mocker.organizations.list();
|
|
60
|
+
*
|
|
61
|
+
* // Custom response
|
|
62
|
+
* const stub2 = mocker.organizations.create({
|
|
63
|
+
* data: { key: 'org-1', name: 'Test Org', ... }
|
|
64
|
+
* });
|
|
65
|
+
*
|
|
66
|
+
* // Custom status and headers
|
|
67
|
+
* const stub3 = mocker.users.me({
|
|
68
|
+
* status: 404,
|
|
69
|
+
* headers: { 'X-Custom': 'value' }
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* // Restore all stubs
|
|
73
|
+
* mocker.restore();
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export class SdkMock {
|
|
77
|
+
private stubs: StubReference[] = []
|
|
78
|
+
|
|
79
|
+
constructor(private sdk: StoreSdk) {}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Organization API mocks.
|
|
83
|
+
*/
|
|
84
|
+
organizations = {
|
|
85
|
+
/**
|
|
86
|
+
* Mocks the `organizations.list()` method.
|
|
87
|
+
* @param options Optional response customization.
|
|
88
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
89
|
+
*/
|
|
90
|
+
list: (options?: MockResponseOptions): StubReference => {
|
|
91
|
+
return this.createStub('organizations', 'list', () => {
|
|
92
|
+
const defaultData = {
|
|
93
|
+
items: [this.generateOrganization(), this.generateOrganization()],
|
|
94
|
+
nextPageToken: undefined,
|
|
95
|
+
}
|
|
96
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
97
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
98
|
+
}
|
|
99
|
+
return (options?.data ?? defaultData) as unknown
|
|
100
|
+
})
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Mocks the `organizations.create()` method.
|
|
105
|
+
* @param options Optional response customization.
|
|
106
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
107
|
+
*/
|
|
108
|
+
create: (options?: MockResponseOptions): StubReference => {
|
|
109
|
+
return this.createStub('organizations', 'create', () => {
|
|
110
|
+
const defaultData = this.generateOrganization()
|
|
111
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
112
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
113
|
+
}
|
|
114
|
+
return (options?.data ?? defaultData) as unknown
|
|
115
|
+
})
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Mocks the `organizations.read()` method.
|
|
120
|
+
* @param options Optional response customization.
|
|
121
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
122
|
+
*/
|
|
123
|
+
read: (options?: MockResponseOptions): StubReference => {
|
|
124
|
+
return this.createStub('organizations', 'read', () => {
|
|
125
|
+
const defaultData = this.generateOrganization()
|
|
126
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
127
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
128
|
+
}
|
|
129
|
+
return (options?.data ?? defaultData) as unknown
|
|
130
|
+
})
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Mocks the `organizations.delete()` method.
|
|
135
|
+
* @param options Optional response customization.
|
|
136
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
137
|
+
*/
|
|
138
|
+
delete: (options?: MockResponseOptions): StubReference => {
|
|
139
|
+
return this.createStub('organizations', 'delete', () => {
|
|
140
|
+
const status = options?.status ?? 204
|
|
141
|
+
if (status !== 204) {
|
|
142
|
+
throw new Exception('Mocked error', { status })
|
|
143
|
+
}
|
|
144
|
+
return undefined
|
|
145
|
+
})
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Group API mocks.
|
|
151
|
+
*/
|
|
152
|
+
groups = {
|
|
153
|
+
/**
|
|
154
|
+
* Mocks the `groups.list()` method.
|
|
155
|
+
* @param options Optional response customization.
|
|
156
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
157
|
+
*/
|
|
158
|
+
list: (options?: MockResponseOptions): StubReference => {
|
|
159
|
+
return this.createStub('groups', 'list', () => {
|
|
160
|
+
const defaultData = {
|
|
161
|
+
items: [this.generateGroup(), this.generateGroup()],
|
|
162
|
+
nextPageToken: undefined,
|
|
163
|
+
}
|
|
164
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
165
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
166
|
+
}
|
|
167
|
+
return (options?.data ?? defaultData) as unknown
|
|
168
|
+
})
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Mocks the `groups.create()` method.
|
|
173
|
+
* @param options Optional response customization.
|
|
174
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
175
|
+
*/
|
|
176
|
+
create: (options?: MockResponseOptions): StubReference => {
|
|
177
|
+
return this.createStub('groups', 'create', () => {
|
|
178
|
+
const defaultData = this.generateGroup()
|
|
179
|
+
const status = options?.status ?? 201
|
|
180
|
+
if (status !== 201) {
|
|
181
|
+
throw new Exception('Mocked error', { status })
|
|
182
|
+
}
|
|
183
|
+
return (options?.data ?? defaultData) as unknown
|
|
184
|
+
})
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Mocks the `groups.read()` method.
|
|
189
|
+
* @param options Optional response customization.
|
|
190
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
191
|
+
*/
|
|
192
|
+
read: (options?: MockResponseOptions): StubReference => {
|
|
193
|
+
return this.createStub('groups', 'read', () => {
|
|
194
|
+
const defaultData = this.generateGroup()
|
|
195
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
196
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
197
|
+
}
|
|
198
|
+
return (options?.data ?? defaultData) as unknown
|
|
199
|
+
})
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Mocks the `groups.update()` method.
|
|
204
|
+
* @param options Optional response customization.
|
|
205
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
206
|
+
*/
|
|
207
|
+
update: (options?: MockResponseOptions): StubReference => {
|
|
208
|
+
return this.createStub('groups', 'update', () => {
|
|
209
|
+
const defaultData = this.generateGroup()
|
|
210
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
211
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
212
|
+
}
|
|
213
|
+
return (options?.data ?? defaultData) as unknown
|
|
214
|
+
})
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Mocks the `groups.delete()` method.
|
|
219
|
+
* @param options Optional response customization.
|
|
220
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
221
|
+
*/
|
|
222
|
+
delete: (options?: MockResponseOptions): StubReference => {
|
|
223
|
+
return this.createStub('groups', 'delete', () => {
|
|
224
|
+
const status = options?.status ?? 204
|
|
225
|
+
if (status !== 204) {
|
|
226
|
+
throw new Exception('Mocked error', { status })
|
|
227
|
+
}
|
|
228
|
+
return undefined
|
|
229
|
+
})
|
|
230
|
+
},
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* User API mocks.
|
|
235
|
+
*/
|
|
236
|
+
users = {
|
|
237
|
+
/**
|
|
238
|
+
* Mocks the `user.me()` method.
|
|
239
|
+
* @param options Optional response customization.
|
|
240
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
241
|
+
*/
|
|
242
|
+
me: (options?: MockResponseOptions): StubReference => {
|
|
243
|
+
return this.createStub('user', 'me', () => {
|
|
244
|
+
const defaultData = this.generateUser()
|
|
245
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
246
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
247
|
+
}
|
|
248
|
+
return (options?.data ?? defaultData) as unknown
|
|
249
|
+
})
|
|
250
|
+
},
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Auth API mocks.
|
|
255
|
+
*/
|
|
256
|
+
auth = {
|
|
257
|
+
/**
|
|
258
|
+
* Mocks the `auth.oauthRedirect()` method.
|
|
259
|
+
* This method returns `null` by default as it performs window navigation.
|
|
260
|
+
* @param options Optional response customization.
|
|
261
|
+
* @returns A stub reference that can be used to restore the original behavior.
|
|
262
|
+
*/
|
|
263
|
+
oauthRedirect: (options?: MockResponseOptions): StubReference => {
|
|
264
|
+
return this.createStub(
|
|
265
|
+
'auth',
|
|
266
|
+
'oauthRedirect',
|
|
267
|
+
() => {
|
|
268
|
+
const defaultData = options?.data ?? null
|
|
269
|
+
return defaultData
|
|
270
|
+
},
|
|
271
|
+
true
|
|
272
|
+
)
|
|
273
|
+
},
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Files API mocks.
|
|
278
|
+
*/
|
|
279
|
+
file = {
|
|
280
|
+
/**
|
|
281
|
+
* Mocks the `file.list()` method.
|
|
282
|
+
*/
|
|
283
|
+
list: (options?: MockResponseOptions): StubReference => {
|
|
284
|
+
return this.createStub('file', 'list', () => {
|
|
285
|
+
const defaultData: ContextListResult<IFile> = {
|
|
286
|
+
items: [this.generateFile(), this.generateFile()],
|
|
287
|
+
}
|
|
288
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
289
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
290
|
+
}
|
|
291
|
+
return (options?.data ?? defaultData) as unknown
|
|
292
|
+
})
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Mocks the `file.createMeta()` method.
|
|
297
|
+
*/
|
|
298
|
+
createMeta: (options?: MockResponseOptions): StubReference => {
|
|
299
|
+
return this.createStub('file', 'createMeta', () => {
|
|
300
|
+
const defaultData: IFile = this.generateFile()
|
|
301
|
+
const status = options?.status ?? 201
|
|
302
|
+
if (status !== 201) {
|
|
303
|
+
throw new Exception('Mocked error', { status })
|
|
304
|
+
}
|
|
305
|
+
return (options?.data ?? defaultData) as unknown
|
|
306
|
+
})
|
|
307
|
+
},
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Mocks the `file.createMedia()` method.
|
|
311
|
+
*/
|
|
312
|
+
createMedia: (options?: MockResponseOptions): StubReference => {
|
|
313
|
+
return this.createStub('file', 'createMedia', () => {
|
|
314
|
+
const status = options?.status ?? 200
|
|
315
|
+
if (status !== 200) {
|
|
316
|
+
throw new Exception('Mocked error', { status })
|
|
317
|
+
}
|
|
318
|
+
return undefined
|
|
319
|
+
})
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Mocks the `file.create()` method.
|
|
324
|
+
*/
|
|
325
|
+
create: (options?: MockResponseOptions): StubReference => {
|
|
326
|
+
return this.createStub('file', 'create', () => {
|
|
327
|
+
const status = options?.status ?? 201
|
|
328
|
+
if (status !== 201) {
|
|
329
|
+
throw new Exception('Mocked error', { status })
|
|
330
|
+
}
|
|
331
|
+
const defaultData: IFile = this.generateFile()
|
|
332
|
+
return (options?.data ?? defaultData) as unknown
|
|
333
|
+
})
|
|
334
|
+
},
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Mocks the `file.createFolder()` method.
|
|
338
|
+
*/
|
|
339
|
+
createFolder: (options?: MockResponseOptions): StubReference => {
|
|
340
|
+
return this.createStub('file', 'createFolder', () => {
|
|
341
|
+
const status = options?.status ?? 201
|
|
342
|
+
if (status !== 201) {
|
|
343
|
+
throw new Exception('Mocked error', { status })
|
|
344
|
+
}
|
|
345
|
+
const defaultData: IFolder = this.generateFolder()
|
|
346
|
+
return (options?.data ?? defaultData) as unknown
|
|
347
|
+
})
|
|
348
|
+
},
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Mocks the `file.read()` method.
|
|
352
|
+
*/
|
|
353
|
+
read: (options?: MockResponseOptions): StubReference => {
|
|
354
|
+
return this.createStub('file', 'read', () => {
|
|
355
|
+
const defaultData: IFile = this.generateFile()
|
|
356
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
357
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
358
|
+
}
|
|
359
|
+
return (options?.data ?? defaultData) as unknown
|
|
360
|
+
})
|
|
361
|
+
},
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Mocks the `file.readMedia()` method.
|
|
365
|
+
*/
|
|
366
|
+
readMedia: (options?: MockResponseOptions): StubReference => {
|
|
367
|
+
return this.createStub('file', 'readMedia', () => {
|
|
368
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
369
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
370
|
+
}
|
|
371
|
+
const defaultData = { media: { ok: true }, version: 1 }
|
|
372
|
+
return (options?.data ?? defaultData) as unknown
|
|
373
|
+
})
|
|
374
|
+
},
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Mocks the `file.readBulk()` method.
|
|
378
|
+
*/
|
|
379
|
+
readBulk: (options?: MockResponseOptions): StubReference => {
|
|
380
|
+
return this.createStub('file', 'readBulk', () => {
|
|
381
|
+
const defaultData: IBulkOperationResult<IFile> = {
|
|
382
|
+
items: [this.generateFile(), undefined],
|
|
383
|
+
}
|
|
384
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
385
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
386
|
+
}
|
|
387
|
+
return (options?.data ?? defaultData) as unknown
|
|
388
|
+
})
|
|
389
|
+
},
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Mocks the `file.patch()` method.
|
|
393
|
+
*/
|
|
394
|
+
patch: (options?: MockResponseOptions): StubReference => {
|
|
395
|
+
return this.createStub('file', 'patch', () => {
|
|
396
|
+
const defaultData: IFile = this.generateFile()
|
|
397
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
398
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
399
|
+
}
|
|
400
|
+
return (options?.data ?? defaultData) as unknown
|
|
401
|
+
})
|
|
402
|
+
},
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Mocks the `file.patchMedia()` method.
|
|
406
|
+
*/
|
|
407
|
+
patchMedia: (options?: MockResponseOptions): StubReference => {
|
|
408
|
+
return this.createStub('file', 'patchMedia', () => {
|
|
409
|
+
const defaultData: MediaPatchRevision = this.generateMediaPatchRevision()
|
|
410
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
411
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
412
|
+
}
|
|
413
|
+
return (options?.data ?? defaultData) as unknown
|
|
414
|
+
})
|
|
415
|
+
},
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Mocks the `file.delete()` method.
|
|
419
|
+
*/
|
|
420
|
+
delete: (options?: MockResponseOptions): StubReference => {
|
|
421
|
+
return this.createStub('file', 'delete', () => {
|
|
422
|
+
const status = options?.status ?? 204
|
|
423
|
+
if (status !== 204) {
|
|
424
|
+
throw new Exception('Mocked error', { status })
|
|
425
|
+
}
|
|
426
|
+
return undefined
|
|
427
|
+
})
|
|
428
|
+
},
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Mocks the `file.deleteBulk()` method.
|
|
432
|
+
*/
|
|
433
|
+
deleteBulk: (options?: MockResponseOptions): StubReference => {
|
|
434
|
+
return this.createStub('file', 'deleteBulk', () => {
|
|
435
|
+
const status = options?.status ?? 204
|
|
436
|
+
if (status !== 204) {
|
|
437
|
+
throw new Exception('Mocked error', { status })
|
|
438
|
+
}
|
|
439
|
+
return undefined
|
|
440
|
+
})
|
|
441
|
+
},
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Mocks the `file.patchUsers()` method.
|
|
445
|
+
*/
|
|
446
|
+
patchUsers: (options?: MockResponseOptions): StubReference => {
|
|
447
|
+
return this.createStub('file', 'patchUsers', () => {
|
|
448
|
+
const defaultData: IFile = this.generateFile()
|
|
449
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
450
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
451
|
+
}
|
|
452
|
+
return (options?.data ?? defaultData) as unknown
|
|
453
|
+
})
|
|
454
|
+
},
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Mocks the `file.addUser()` method.
|
|
458
|
+
*/
|
|
459
|
+
addUser: (options?: MockResponseOptions): StubReference => {
|
|
460
|
+
return this.createStub('file', 'addUser', () => {
|
|
461
|
+
const defaultData: IFile = this.generateFile()
|
|
462
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
463
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
464
|
+
}
|
|
465
|
+
return (options?.data ?? defaultData) as unknown
|
|
466
|
+
})
|
|
467
|
+
},
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Mocks the `file.removeUser()` method.
|
|
471
|
+
*/
|
|
472
|
+
removeUser: (options?: MockResponseOptions): StubReference => {
|
|
473
|
+
return this.createStub('file', 'removeUser', () => {
|
|
474
|
+
const defaultData: IFile = this.generateFile()
|
|
475
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
476
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
477
|
+
}
|
|
478
|
+
return (options?.data ?? defaultData) as unknown
|
|
479
|
+
})
|
|
480
|
+
},
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Mocks the `file.listUsers()` method.
|
|
484
|
+
*/
|
|
485
|
+
listUsers: (options?: MockResponseOptions): StubReference => {
|
|
486
|
+
return this.createStub('file', 'listUsers', () => {
|
|
487
|
+
const defaultData: ContextListResult<IUser> = {
|
|
488
|
+
items: [this.generateUser(), this.generateUser()],
|
|
489
|
+
}
|
|
490
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
491
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
492
|
+
}
|
|
493
|
+
return (options?.data ?? defaultData) as unknown
|
|
494
|
+
})
|
|
495
|
+
},
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Mocks the `file.breadcrumbs()` method.
|
|
499
|
+
*/
|
|
500
|
+
breadcrumbs: (options?: MockResponseOptions): StubReference => {
|
|
501
|
+
return this.createStub('file', 'breadcrumbs', () => {
|
|
502
|
+
const defaultData: ContextListResult<FileBreadcrumb> = {
|
|
503
|
+
items: this.generateBreadcrumbs(),
|
|
504
|
+
}
|
|
505
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
506
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
507
|
+
}
|
|
508
|
+
return (options?.data ?? defaultData) as unknown
|
|
509
|
+
})
|
|
510
|
+
},
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Shared API mocks.
|
|
515
|
+
*/
|
|
516
|
+
shared = {
|
|
517
|
+
list: (options?: MockResponseOptions): StubReference => {
|
|
518
|
+
return this.createStub('shared', 'list', () => {
|
|
519
|
+
const defaultData: ContextListResult<IFile> = {
|
|
520
|
+
items: [this.generateFile(), this.generateFile()],
|
|
521
|
+
}
|
|
522
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
523
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
524
|
+
}
|
|
525
|
+
return (options?.data ?? defaultData) as unknown
|
|
526
|
+
})
|
|
527
|
+
},
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Trash API mocks.
|
|
532
|
+
*/
|
|
533
|
+
trash = {
|
|
534
|
+
list: (options?: MockResponseOptions): StubReference => {
|
|
535
|
+
return this.createStub('trash', 'list', () => {
|
|
536
|
+
const defaultData: ContextListResult<TrashEntry> = {
|
|
537
|
+
items: [this.generateTrashEntry(), this.generateTrashEntry()],
|
|
538
|
+
}
|
|
539
|
+
if (options?.status !== undefined && options.status !== 200) {
|
|
540
|
+
throw new Exception('Mocked error', { status: options.status })
|
|
541
|
+
}
|
|
542
|
+
return (options?.data ?? defaultData) as unknown
|
|
543
|
+
})
|
|
544
|
+
},
|
|
545
|
+
delete: (options?: MockResponseOptions): StubReference => {
|
|
546
|
+
return this.createStub('trash', 'delete', () => {
|
|
547
|
+
const status = options?.status ?? 204
|
|
548
|
+
if (status !== 204) {
|
|
549
|
+
throw new Exception('Mocked error', { status })
|
|
550
|
+
}
|
|
551
|
+
return undefined
|
|
552
|
+
})
|
|
553
|
+
},
|
|
554
|
+
restore: (options?: MockResponseOptions): StubReference => {
|
|
555
|
+
return this.createStub('trash', 'restore', () => {
|
|
556
|
+
const status = options?.status ?? 204
|
|
557
|
+
if (status !== 204) {
|
|
558
|
+
throw new Exception('Mocked error', { status })
|
|
559
|
+
}
|
|
560
|
+
return undefined
|
|
561
|
+
})
|
|
562
|
+
},
|
|
563
|
+
empty: (options?: MockResponseOptions): StubReference => {
|
|
564
|
+
return this.createStub('trash', 'empty', () => {
|
|
565
|
+
const status = options?.status ?? 204
|
|
566
|
+
if (status !== 204) {
|
|
567
|
+
throw new Exception('Mocked error', { status })
|
|
568
|
+
}
|
|
569
|
+
return undefined
|
|
570
|
+
})
|
|
571
|
+
},
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Restores all stubs created by this mocker.
|
|
576
|
+
*/
|
|
577
|
+
restore(): void {
|
|
578
|
+
this.stubs.forEach((stub) => stub.restore())
|
|
579
|
+
this.stubs = []
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Creates a stub for a specific SDK method.
|
|
584
|
+
* @param api The API name (e.g., 'organizations', 'groups', 'user', 'auth').
|
|
585
|
+
* @param method The method name to stub.
|
|
586
|
+
* @param implementation The stub implementation that returns the mocked data.
|
|
587
|
+
* @returns A stub reference.
|
|
588
|
+
*/
|
|
589
|
+
private createStub(
|
|
590
|
+
api: 'organizations' | 'groups' | 'user' | 'auth' | 'file' | 'shared' | 'trash',
|
|
591
|
+
method: string,
|
|
592
|
+
implementation: () => unknown,
|
|
593
|
+
sync?: boolean
|
|
594
|
+
): StubReference {
|
|
595
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
596
|
+
const target = (this.sdk as any)[api]
|
|
597
|
+
if (!target) {
|
|
598
|
+
throw new Error(`API '${api}' not found in SDK`)
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
const original = target[method]
|
|
602
|
+
if (typeof original !== 'function') {
|
|
603
|
+
throw new Error(`Method '${method}' not found in ${api} API`)
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// Store original and create stub using sinon
|
|
607
|
+
const stub = sync
|
|
608
|
+
? sinon.stub(target, method).callsFake(() => implementation())
|
|
609
|
+
: sinon.stub(target, method).callsFake(async () => implementation())
|
|
610
|
+
|
|
611
|
+
const stubRef: StubReference = {
|
|
612
|
+
restore: () => {
|
|
613
|
+
if (stub) {
|
|
614
|
+
stub.restore()
|
|
615
|
+
}
|
|
616
|
+
const index = this.stubs.indexOf(stubRef)
|
|
617
|
+
if (index > -1) {
|
|
618
|
+
this.stubs.splice(index, 1)
|
|
619
|
+
}
|
|
620
|
+
},
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
this.stubs.push(stubRef)
|
|
624
|
+
return stubRef
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Creates a response object from data and options.
|
|
629
|
+
* @param defaultData Default data to return if not overridden.
|
|
630
|
+
* @param options Response options.
|
|
631
|
+
* @returns The final data to return (for non-HTTP responses) or IStoreResponse (for HTTP responses).
|
|
632
|
+
*/
|
|
633
|
+
private createResponse(defaultData: unknown, options?: MockResponseOptions): unknown {
|
|
634
|
+
// For SDK-level stubs we return plain data. Status and headers are
|
|
635
|
+
// accepted but not propagated, since high-level SDK methods return data.
|
|
636
|
+
const data = options?.data !== undefined ? options.data : defaultData
|
|
637
|
+
return data
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* Generates a random organization object.
|
|
642
|
+
*/
|
|
643
|
+
private generateOrganization(): IOrganization {
|
|
644
|
+
return {
|
|
645
|
+
kind: OrganizationKind,
|
|
646
|
+
key: nanoid(),
|
|
647
|
+
name: `Organization ${this.randomString()}`,
|
|
648
|
+
createdBy: nanoid(),
|
|
649
|
+
createdDate: Date.now() - Math.random() * 1000 * 60 * 60 * 24 * 30,
|
|
650
|
+
grantType: this.randomChoice(['owner', 'manager', 'editor', 'viewer'] as const),
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* Generates a random group object.
|
|
656
|
+
*/
|
|
657
|
+
private generateGroup(): GroupSchema {
|
|
658
|
+
return {
|
|
659
|
+
kind: GroupKind,
|
|
660
|
+
key: nanoid(),
|
|
661
|
+
name: `Group ${this.randomString()}`,
|
|
662
|
+
description: `Description for ${this.randomString()}`,
|
|
663
|
+
owner: nanoid(),
|
|
664
|
+
oid: nanoid(),
|
|
665
|
+
users: [],
|
|
666
|
+
createdAt: Date.now() - Math.random() * 1000 * 60 * 60 * 24 * 30,
|
|
667
|
+
updatedAt: Date.now() - Math.random() * 1000 * 60 * 60 * 24 * 7,
|
|
668
|
+
icon: `https://example.com/icon-${nanoid()}.png`,
|
|
669
|
+
color: this.randomColor(),
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Generates a random user object.
|
|
675
|
+
*/
|
|
676
|
+
private generateUser(): IUser {
|
|
677
|
+
const firstName = this.randomString()
|
|
678
|
+
const lastName = this.randomString()
|
|
679
|
+
return {
|
|
680
|
+
kind: UserKind,
|
|
681
|
+
key: nanoid(),
|
|
682
|
+
name: `${firstName} ${lastName}`,
|
|
683
|
+
email: [
|
|
684
|
+
{
|
|
685
|
+
email: `${firstName.toLowerCase()}.${lastName.toLowerCase()}@example.com`,
|
|
686
|
+
verified: true,
|
|
687
|
+
},
|
|
688
|
+
],
|
|
689
|
+
status: 'active',
|
|
690
|
+
created: Date.now() - Math.random() * 1000 * 60 * 60 * 24 * 365,
|
|
691
|
+
updated: Date.now() - Math.random() * 1000 * 60 * 60 * 24 * 7,
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* Generates a random file meta object.
|
|
697
|
+
*/
|
|
698
|
+
private generateFile(): IFile {
|
|
699
|
+
const kind = this.randomChoice([ProjectKind, DomainFileKind, CertificateFileKind, FolderKind] as const)
|
|
700
|
+
const name = `File ${this.randomString()}`
|
|
701
|
+
return File.createSchema({ kind, info: { name } })
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* Generates a random folder meta object.
|
|
706
|
+
*/
|
|
707
|
+
private generateFolder(): IFolder {
|
|
708
|
+
const file = File.createSchema({ kind: FolderKind, info: { name: `Folder ${this.randomString()}` } }) as IFolder
|
|
709
|
+
return file
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Generates a random media patch revision object.
|
|
714
|
+
*/
|
|
715
|
+
private generateMediaPatchRevision(): MediaPatchRevision {
|
|
716
|
+
const version = Math.floor(Math.random() * 10) + 1
|
|
717
|
+
return {
|
|
718
|
+
id: nanoid(),
|
|
719
|
+
timestamp: Date.now(),
|
|
720
|
+
patch: [],
|
|
721
|
+
version,
|
|
722
|
+
revert: [],
|
|
723
|
+
newVersion: version + 1,
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Generates a random breadcrumbs list.
|
|
729
|
+
*/
|
|
730
|
+
private generateBreadcrumbs(): FileBreadcrumb[] {
|
|
731
|
+
const depth = 2 + Math.floor(Math.random() * 2) // 2-3
|
|
732
|
+
const items: FileBreadcrumb[] = []
|
|
733
|
+
for (let i = 0; i < depth; i += 1) {
|
|
734
|
+
items.push({ key: nanoid(), kind: i === depth - 1 ? ProjectKind : FolderKind, name: this.randomString() })
|
|
735
|
+
}
|
|
736
|
+
return items
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* Generates a random trash entry.
|
|
741
|
+
*/
|
|
742
|
+
private generateTrashEntry(): TrashEntry {
|
|
743
|
+
return {
|
|
744
|
+
key: nanoid(),
|
|
745
|
+
refKey: nanoid(),
|
|
746
|
+
kind: this.randomChoice([ProjectKind, DomainFileKind, CertificateFileKind, FolderKind] as const),
|
|
747
|
+
name: `Deleted ${this.randomString()}`,
|
|
748
|
+
info: { byMe: false, time: Date.now() - Math.floor(Math.random() * 1000000), user: nanoid(), name: 'User' },
|
|
749
|
+
capabilities: { canDelete: true, canRestore: true },
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Generates a random string.
|
|
755
|
+
*/
|
|
756
|
+
private randomString(): string {
|
|
757
|
+
const words = [
|
|
758
|
+
'Alpha',
|
|
759
|
+
'Beta',
|
|
760
|
+
'Gamma',
|
|
761
|
+
'Delta',
|
|
762
|
+
'Epsilon',
|
|
763
|
+
'Zeta',
|
|
764
|
+
'Theta',
|
|
765
|
+
'Lambda',
|
|
766
|
+
'Sigma',
|
|
767
|
+
'Omega',
|
|
768
|
+
'Phoenix',
|
|
769
|
+
'Dragon',
|
|
770
|
+
'Tiger',
|
|
771
|
+
'Eagle',
|
|
772
|
+
'Falcon',
|
|
773
|
+
]
|
|
774
|
+
return words[Math.floor(Math.random() * words.length)]
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Returns a random choice from an array.
|
|
779
|
+
*/
|
|
780
|
+
private randomChoice<T>(choices: readonly T[]): T {
|
|
781
|
+
return choices[Math.floor(Math.random() * choices.length)]
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Generates a random hex color.
|
|
786
|
+
*/
|
|
787
|
+
private randomColor(): string {
|
|
788
|
+
return `#${Math.floor(Math.random() * 16777215)
|
|
789
|
+
.toString(16)
|
|
790
|
+
.padStart(6, '0')}`
|
|
791
|
+
}
|
|
792
|
+
}
|