|
1 | 1 | import _ from 'underscore' |
2 | 2 | import { Meteor } from 'meteor/meteor' |
| 3 | +import { PassThrough } from 'stream' |
3 | 4 | import { SupressLogMessages } from '../../../../__mocks__/suppressLogging' |
4 | 5 | import { callKoaRoute } from '../../../../__mocks__/koa-util' |
5 | 6 | import { blueprintsRouter } from '../http' |
@@ -357,4 +358,247 @@ describe('Test blueprint http api', () => { |
357 | 358 | } |
358 | 359 | }) |
359 | 360 | }) |
| 361 | + |
| 362 | + describe('router upload assets', () => { |
| 363 | + describe('POST /assets', () => { |
| 364 | + async function callRoute(body: any) { |
| 365 | + const ctx = await callKoaRoute(blueprintsRouter, { |
| 366 | + method: 'POST', |
| 367 | + url: '/assets', |
| 368 | + |
| 369 | + requestBody: body, |
| 370 | + }) |
| 371 | + |
| 372 | + expect(ctx.response.type).toBe('text/plain') |
| 373 | + return ctx |
| 374 | + } |
| 375 | + |
| 376 | + function resetUploadAssetMock() { |
| 377 | + const uploadBlueprintAsset = api.uploadBlueprintAsset as any as jest.MockInstance<any, any> |
| 378 | + uploadBlueprintAsset.mockClear() |
| 379 | + return uploadBlueprintAsset |
| 380 | + } |
| 381 | + |
| 382 | + beforeEach(() => { |
| 383 | + resetUploadAssetMock() |
| 384 | + }) |
| 385 | + |
| 386 | + test('missing body', async () => { |
| 387 | + SupressLogMessages.suppressLogMessage(/Invalid request body/i) |
| 388 | + const res = await callRoute(undefined) |
| 389 | + expect(res.response.status).toEqual(500) |
| 390 | + |
| 391 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledTimes(0) |
| 392 | + }) |
| 393 | + |
| 394 | + test('empty body', async () => { |
| 395 | + SupressLogMessages.suppressLogMessage(/Missing request body/i) |
| 396 | + const res = await callRoute('') |
| 397 | + expect(res.response.status).toEqual(500) |
| 398 | + |
| 399 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledTimes(0) |
| 400 | + }) |
| 401 | + |
| 402 | + test('non-object body', async () => { |
| 403 | + SupressLogMessages.suppressLogMessage(/Invalid request body/i) |
| 404 | + const res = await callRoute(99) |
| 405 | + expect(res.response.status).toEqual(500) |
| 406 | + |
| 407 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledTimes(0) |
| 408 | + }) |
| 409 | + |
| 410 | + test('empty object body', async () => { |
| 411 | + SupressLogMessages.suppressLogMessage(/Invalid request body/i) |
| 412 | + const res = await callRoute({}) |
| 413 | + expect(res.response.status).toEqual(500) |
| 414 | + |
| 415 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledTimes(0) |
| 416 | + }) |
| 417 | + |
| 418 | + test('with json body', async () => { |
| 419 | + const fileId = 'folder/asset.png' |
| 420 | + const payload = { |
| 421 | + [fileId]: 'Ym9keQ==', |
| 422 | + } |
| 423 | + |
| 424 | + const res = await callRoute(payload) |
| 425 | + expect(res.response.status).toEqual(200) |
| 426 | + expect(res.body).toEqual('') |
| 427 | + |
| 428 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledTimes(1) |
| 429 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledWith(DEFAULT_CONTEXT, fileId, payload[fileId]) |
| 430 | + }) |
| 431 | + |
| 432 | + test('with json body - multiple', async () => { |
| 433 | + const count = 10 |
| 434 | + const payload: Record<string, string> = {} |
| 435 | + for (let i = 0; i < count; i++) { |
| 436 | + payload[`id${i}.png`] = `body${i}` |
| 437 | + } |
| 438 | + |
| 439 | + const res = await callRoute(payload) |
| 440 | + expect(res.response.status).toEqual(200) |
| 441 | + expect(res.body).toEqual('') |
| 442 | + |
| 443 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledTimes(count) |
| 444 | + for (let i = 0; i < count; i++) { |
| 445 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledWith(DEFAULT_CONTEXT, `id${i}.png`, `body${i}`) |
| 446 | + } |
| 447 | + }) |
| 448 | + |
| 449 | + test('with errors', async () => { |
| 450 | + const count = 10 |
| 451 | + const payload: Record<string, string> = {} |
| 452 | + for (let i = 0; i < count; i++) { |
| 453 | + payload[`id${i}.png`] = `body${i}` |
| 454 | + } |
| 455 | + |
| 456 | + const uploadBlueprintAsset = resetUploadAssetMock() |
| 457 | + let called = 0 |
| 458 | + uploadBlueprintAsset.mockImplementation(() => { |
| 459 | + called++ |
| 460 | + if (called === 3 || called === 7) { |
| 461 | + throw new Meteor.Error(505, 'Some thrown error') |
| 462 | + } |
| 463 | + }) |
| 464 | + |
| 465 | + try { |
| 466 | + SupressLogMessages.suppressLogMessage(/Some thrown error/i) |
| 467 | + SupressLogMessages.suppressLogMessage(/Some thrown error/i) |
| 468 | + const res = await callRoute(payload) |
| 469 | + expect(res.response.status).toEqual(500) |
| 470 | + expect(res.body).toEqual( |
| 471 | + 'Errors were encountered: \n[505] Some thrown error\n[505] Some thrown error\n' |
| 472 | + ) |
| 473 | + |
| 474 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledTimes(count) |
| 475 | + for (let i = 0; i < count; i++) { |
| 476 | + expect(api.uploadBlueprintAsset).toHaveBeenCalledWith(DEFAULT_CONTEXT, `id${i}.png`, `body${i}`) |
| 477 | + } |
| 478 | + } finally { |
| 479 | + uploadBlueprintAsset.mockRestore() |
| 480 | + } |
| 481 | + }) |
| 482 | + }) |
| 483 | + |
| 484 | + describe('GET /assets/*fileId', () => { |
| 485 | + function createDataStream() { |
| 486 | + const stream = new PassThrough() |
| 487 | + stream.end('asset') |
| 488 | + return stream |
| 489 | + } |
| 490 | + |
| 491 | + async function callRoute(fileId: string) { |
| 492 | + const ctx = await callKoaRoute(blueprintsRouter, { |
| 493 | + method: 'GET', |
| 494 | + url: `/assets/${fileId}`, |
| 495 | + }) |
| 496 | + |
| 497 | + return ctx |
| 498 | + } |
| 499 | + |
| 500 | + function resetRetrieveAssetMock() { |
| 501 | + const retrieveBlueprintAsset = api.retrieveBlueprintAsset as any as jest.MockInstance<any, any> |
| 502 | + retrieveBlueprintAsset.mockClear() |
| 503 | + return retrieveBlueprintAsset |
| 504 | + } |
| 505 | + |
| 506 | + beforeEach(() => { |
| 507 | + resetRetrieveAssetMock() |
| 508 | + }) |
| 509 | + |
| 510 | + test('png asset', async () => { |
| 511 | + const fileId = 'folder/file.png' |
| 512 | + const dataStream = createDataStream() |
| 513 | + |
| 514 | + const retrieveBlueprintAsset = resetRetrieveAssetMock() |
| 515 | + retrieveBlueprintAsset.mockReturnValue(dataStream) |
| 516 | + |
| 517 | + const res = await callRoute(fileId) |
| 518 | + |
| 519 | + expect(res.statusCode).toEqual(200) |
| 520 | + expect(res.response.type).toEqual('image/png') |
| 521 | + expect(res.body).toBe(dataStream) |
| 522 | + expect(res.response.get('Cache-Control')).toEqual('public, max-age=1296000, immutable') |
| 523 | + |
| 524 | + expect(api.retrieveBlueprintAsset).toHaveBeenCalledTimes(1) |
| 525 | + expect(api.retrieveBlueprintAsset).toHaveBeenCalledWith(DEFAULT_CONTEXT, fileId) |
| 526 | + }) |
| 527 | + |
| 528 | + test('svg asset', async () => { |
| 529 | + const fileId = 'folder/file.svg' |
| 530 | + const dataStream = createDataStream() |
| 531 | + |
| 532 | + const retrieveBlueprintAsset = resetRetrieveAssetMock() |
| 533 | + retrieveBlueprintAsset.mockReturnValue(dataStream) |
| 534 | + |
| 535 | + const res = await callRoute(fileId) |
| 536 | + |
| 537 | + expect(res.statusCode).toEqual(200) |
| 538 | + expect(res.response.type).toEqual('image/svg+xml') |
| 539 | + expect(res.body).toBe(dataStream) |
| 540 | + |
| 541 | + expect(api.retrieveBlueprintAsset).toHaveBeenCalledTimes(1) |
| 542 | + expect(api.retrieveBlueprintAsset).toHaveBeenCalledWith(DEFAULT_CONTEXT, fileId) |
| 543 | + }) |
| 544 | + |
| 545 | + test('gif asset', async () => { |
| 546 | + const fileId = 'folder/file.gif' |
| 547 | + const dataStream = createDataStream() |
| 548 | + |
| 549 | + const retrieveBlueprintAsset = resetRetrieveAssetMock() |
| 550 | + retrieveBlueprintAsset.mockReturnValue(dataStream) |
| 551 | + |
| 552 | + const res = await callRoute(fileId) |
| 553 | + |
| 554 | + expect(res.statusCode).toEqual(200) |
| 555 | + expect(res.response.type).toEqual('image/gif') |
| 556 | + expect(res.body).toBe(dataStream) |
| 557 | + |
| 558 | + expect(api.retrieveBlueprintAsset).toHaveBeenCalledTimes(1) |
| 559 | + expect(api.retrieveBlueprintAsset).toHaveBeenCalledWith(DEFAULT_CONTEXT, fileId) |
| 560 | + }) |
| 561 | + |
| 562 | + test('not found', async () => { |
| 563 | + const fileId = 'folder/missing.png' |
| 564 | + |
| 565 | + const retrieveBlueprintAsset = resetRetrieveAssetMock() |
| 566 | + retrieveBlueprintAsset.mockImplementation(() => { |
| 567 | + const err = new Error('No such file') as Error & { code?: string } |
| 568 | + err.code = 'ENOENT' |
| 569 | + throw err |
| 570 | + }) |
| 571 | + |
| 572 | + SupressLogMessages.suppressLogMessage(/Blueprint asset not found/i) |
| 573 | + const res = await callRoute(fileId) |
| 574 | + expect(res.statusCode).toEqual(404) |
| 575 | + }) |
| 576 | + |
| 577 | + test('path traversal attempt', async () => { |
| 578 | + const fileId = 'folder/../escape.png' |
| 579 | + |
| 580 | + const retrieveBlueprintAsset = resetRetrieveAssetMock() |
| 581 | + retrieveBlueprintAsset.mockImplementation(() => { |
| 582 | + throw new Error('Requested asset outside of asset storage path') |
| 583 | + }) |
| 584 | + |
| 585 | + SupressLogMessages.suppressLogMessage(/Blueprint asset path traversal attempt/i) |
| 586 | + const res = await callRoute(fileId) |
| 587 | + expect(res.statusCode).toEqual(400) |
| 588 | + }) |
| 589 | + |
| 590 | + test('internal error', async () => { |
| 591 | + const fileId = 'folder/file.png' |
| 592 | + |
| 593 | + const retrieveBlueprintAsset = resetRetrieveAssetMock() |
| 594 | + retrieveBlueprintAsset.mockImplementation(() => { |
| 595 | + throw new Error('Some thrown error') |
| 596 | + }) |
| 597 | + |
| 598 | + SupressLogMessages.suppressLogMessage(/Blueprint asset retrieval failed/i) |
| 599 | + const res = await callRoute(fileId) |
| 600 | + expect(res.statusCode).toEqual(500) |
| 601 | + }) |
| 602 | + }) |
| 603 | + }) |
360 | 604 | }) |
0 commit comments