Skip to content

Commit

Permalink
Make the list DIDs pipeline element discard results with errors and c…
Browse files Browse the repository at this point in the history
…over this by a test
  • Loading branch information
MytsV committed Aug 29, 2024
1 parent 4de5d83 commit 4357a1d
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 14 deletions.
9 changes: 8 additions & 1 deletion src/lib/core/use-case/list-dids/pipeline-element-get-did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
156 changes: 143 additions & 13 deletions test/api/did/list-dids.test.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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<BaseController<ListDIDsControllerParameters, ListDIDsRequest>>(
CONTROLLERS.LIST_DIDS,
Expand Down Expand Up @@ -138,7 +141,7 @@ describe('DID API Tests', () => {
reject(err)
})
})

await done


Expand Down Expand Up @@ -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<BaseController<ListDIDsControllerParameters, ListDIDsRequest>>(
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<void>((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,
})
})
})

0 comments on commit 4357a1d

Please sign in to comment.