diff --git a/src/lib/core/use-case/list-dids/pipeline-element-get-did.ts b/src/lib/core/use-case/list-dids/pipeline-element-get-did.ts index 5829e1aee..376e1df0b 100644 --- a/src/lib/core/use-case/list-dids/pipeline-element-get-did.ts +++ b/src/lib/core/use-case/list-dids/pipeline-element-get-did.ts @@ -77,7 +77,14 @@ export default class GetDIDsPipelineElement extends BaseStreamingPostProcessingP } - transformResponseModel(responseModel: ListDIDsResponse, dto: DIDExtendedDTO): ListDIDsResponse { + transformResponseModel(responseModel: ListDIDsResponse, dto: DIDExtendedDTO): ListDIDsResponse | ListDIDsError { + if (dto.status === 'error') return { + status: 'error', + name: '', + message: '', + error: '', + code: 500, + }; responseModel.bytes = dto.bytes; responseModel.length = dto.length; responseModel.did_type = dto.did_type; diff --git a/test/api/did/list-dids.test.ts b/test/api/did/list-dids.test.ts index 3a39472b3..3202fbe8a 100644 --- a/test/api/did/list-dids.test.ts +++ b/test/api/did/list-dids.test.ts @@ -1,16 +1,24 @@ -import { BaseController } from '@/lib/sdk/controller' -import { ListDIDsRequest } from '@/lib/core/usecase-models/list-dids-usecase-models' +import {BaseController} from '@/lib/sdk/controller' +import {ListDIDsRequest} from '@/lib/core/usecase-models/list-dids-usecase-models' import appContainer from '@/lib/infrastructure/ioc/container-config' import CONTROLLERS from '@/lib/infrastructure/ioc/ioc-symbols-controllers' -import { ListDIDsControllerParameters } from '@/lib/infrastructure/controller/list-dids-controller' -import { NextApiResponse } from 'next' -import { Readable } from 'stream' -import { MockHttpStreamableResponseFactory } from 'test/fixtures/http-fixtures' -import MockRucioServerFactory, { MockEndpoint } from 'test/fixtures/rucio-server' +import {ListDIDsControllerParameters} from '@/lib/infrastructure/controller/list-dids-controller' +import {NextApiResponse} from 'next' +import {Readable} from 'stream' +import {MockHttpStreamableResponseFactory} from 'test/fixtures/http-fixtures' +import MockRucioServerFactory, {MockEndpoint} from 'test/fixtures/rucio-server' describe('DID API Tests', () => { + beforeEach(() => { fetchMock.doMock() + }) + afterEach(() => { + fetchMock.dontMock() + }) + + + it('Should successfully stream DIDs', async () => { const listDIDsEndpoint: MockEndpoint = { url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/dids/search`, method: 'GET', @@ -102,12 +110,7 @@ describe('DID API Tests', () => { dataset2StatusEndpoint, dataset3StatusEndpoint, ]) - }) - afterEach(() => { - fetchMock.dontMock() - }) - it('Should successfully stream DIDs', async () => { const res = MockHttpStreamableResponseFactory.getMockResponse() const listDIDsController = appContainer.get>( CONTROLLERS.LIST_DIDS, @@ -138,7 +141,7 @@ describe('DID API Tests', () => { reject(err) }) }) - + await done @@ -172,4 +175,131 @@ describe('DID API Tests', () => { } ]) }) + + it('Should output an error for DIDs which status cannot be retrieved', async () => { + fetchMock.doMock() + + const listDIDsEndpoint: MockEndpoint = { + url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/dids/search`, + method: 'GET', + includes: 'test/dids/search', + response: { + status: 200, + headers: { + 'Content-Type': 'application/x-json-stream', + }, + body: Readable.from( + [ + '"dataset1"\n', + '"dataset2"\n', + '"dataset3"\n', + ].join('\n'), + ), + }, + } + + const dataset1StatusEndpoint: MockEndpoint = { + url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/dataset1/status?dynamic_depth=FILE`, + method: 'GET', + response: { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + scope: 'test', + name: 'dataset1', + type: 'DATASET', + account: 'root', + open: true, + monotonic: false, + expired_at: null, + length: 0, + bytes: 0, + }), + }, + } + + const dataset3StatusEndpoint: MockEndpoint = { + url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/dataset3/status?dynamic_depth=FILE`, + method: 'GET', + response: { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + scope: 'test', + name: 'dataset3', + type: 'DATASET', + account: 'root', + open: true, + monotonic: false, + expired_at: null, + bytes: 456, + length: 789, + }), + }, + } + + MockRucioServerFactory.createMockRucioServer(true, [ + listDIDsEndpoint, + dataset1StatusEndpoint, + dataset3StatusEndpoint, + ]) + + const res = MockHttpStreamableResponseFactory.getMockResponse() + const listDIDsController = appContainer.get>( + CONTROLLERS.LIST_DIDS, + ) + const controllerParams: ListDIDsControllerParameters = { + response: res as unknown as NextApiResponse, + rucioAuthToken: MockRucioServerFactory.VALID_RUCIO_TOKEN, + query: "test:dataset1", + type: "dataset" + } + await listDIDsController.execute( + controllerParams, + ) + + const receivedData: any[] = [] + const onData = (data: any) => { + receivedData.push(JSON.parse(data)) + } + + const done = new Promise((resolve, reject) => { + res.on('data', onData) + res.on('end', () => { + res.off('data', onData) + resolve() + }) + res.on('error', err => { + res.off('data', onData) + reject(err) + }) + }) + + await done + + expect(receivedData.length).toEqual(3) + expect(receivedData[0]).toEqual({ + "status": "success", + "name": "dataset1", + "scope": "test", + "did_type": "Dataset", + "bytes": 0, + "length": 0, + "open": true, + }) + expect(receivedData[1].status).toEqual("error") + expect(receivedData[2]).toEqual({ + "status": "success", + "name": "dataset3", + "scope": "test", + "did_type": "Dataset", + "bytes": 456, + "length": 789, + "open": true, + }) + }) })