Skip to content

Commit

Permalink
tests: list-did-contents api tests #323
Browse files Browse the repository at this point in the history
  • Loading branch information
maany committed Sep 18, 2023
1 parent 613afd8 commit dc92db4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/lib/infrastructure/ioc/container-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import ReplicaGatewayOutputPort from "@/lib/core/port/secondary/replica-gateway-
import ReplicaGateway from "../gateway/replica-gateway/replica-gateway";
import ListFileReplicasFeature from "./features/list-file-replicas-feature";
import ListDatasetReplicasFeature from "./features/list-dataset-replicas-feature";
import ListDIDContentsFeature from "./features/list-did-contents-feature";


/**
Expand All @@ -80,11 +81,12 @@ loadFeaturesSync(appContainer, [
new DIDKeyValuePairsDataFeature(appContainer),
new GetSubscriptionFeature(appContainer),
new ListDidsFeature(appContainer),
new ListDIDRulesFeature(appContainer),
new ListSubscriptionsFeature(appContainer),
new ListDIDContentsFeature(appContainer),
new ListDIDParentsFeature(appContainer),
new ListFileReplicasFeature(appContainer),
new ListDIDRulesFeature(appContainer),
new ListDatasetReplicasFeature(appContainer),
new ListFileReplicasFeature(appContainer),
new ListSubscriptionsFeature(appContainer),
])

appContainer.bind<UserPassLoginInputPort>(INPUT_PORT.USERPASS_LOGIN).to(UserPassLoginUseCase).inRequestScope();
Expand Down
86 changes: 86 additions & 0 deletions test/api/did/list-did-contents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import appContainer from "@/lib/infrastructure/ioc/container-config";
import CONTROLLERS from "@/lib/infrastructure/ioc/ioc-symbols-controllers";
import { BaseController } from "@/lib/sdk/controller";
import { NextApiResponse } from "next";
import { MockHttpStreamableResponseFactory } from "test/fixtures/http-fixtures";
import MockRucioServerFactory, { MockEndpoint } from "test/fixtures/rucio-server";
import { Readable } from "stream";
import { DIDType } from "@/lib/core/entity/rucio";
import { ListDIDContentsControllerParameters } from "@/lib/infrastructure/controller/list-did-contents-controller";
import { ListDIDContentsRequest } from "@/lib/core/usecase-models/list-did-contents-usecase-models";

describe("List DID Contents Feature tests", () => {
beforeEach(() => {
fetchMock.doMock();
const didListContentsMockEndpoint: MockEndpoint = {
url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/dataset1/dids`,
method: 'GET',
includes: '/dataset1/dids',
response: {
status: 200,
headers: {
'Content-Type': 'application/x-json-stream',
},
body: Readable.from(
[
JSON.stringify({"scope": "test", "name": "file1", "type": "FILE", "bytes": 10485760, "adler32": "517daa38", "md5": "e4319066a5d3771954652a6905cebe82"}),
JSON.stringify({"scope": "test", "name": "file2", "type": "FILE", "bytes": 10485760, "adler32": "fc6ff847", "md5": "bcc3619205bf64d3cbe984b27c042a01"}),
].join('\n')
)
}
}

MockRucioServerFactory.createMockRucioServer(true, [didListContentsMockEndpoint]);
});

afterEach(() => {
fetchMock.dontMock();
});
it("should list DID contents", async () => {
const res = MockHttpStreamableResponseFactory.getMockResponse();

const listDIDContentsController = appContainer.get<BaseController<ListDIDContentsControllerParameters, ListDIDContentsRequest>>(CONTROLLERS.LIST_DID_CONTENTS);
const listDIDContentsControllerParams: ListDIDContentsControllerParameters = {
response: res as unknown as NextApiResponse,
rucioAuthToken: MockRucioServerFactory.VALID_RUCIO_TOKEN,
name: 'dataset1',
scope: 'test'
}

await listDIDContentsController.execute(listDIDContentsControllerParams);

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
console.log(receivedData)
expect(receivedData.length).toBe(2)
expect(receivedData[0]).toEqual({
"status": "success",
"scope": "test",
"name": "file1",
"did_type": DIDType.FILE
})
expect(receivedData[1]).toEqual({
"status": "success",
"scope": "test",
"name": "file2",
"did_type": DIDType.FILE
})

});
});
16 changes: 8 additions & 8 deletions test/api/did/list-did-parents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { ListDIDParentsControllerParameters } from "@/lib/infrastructure/control
import { ListDIDParentsRequest } from "@/lib/core/usecase-models/list-did-parents-usecase-models";
import { DIDType } from "@/lib/core/entity/rucio";

describe("List DID Rules Feature tests", () => {
describe("List DID Parents Feature tests", () => {
beforeEach(() => {
fetchMock.doMock();
const didListRulesMockEndpoint: MockEndpoint = {
const didListParentsMockEndpoint: MockEndpoint = {
url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/file1/parents`,
method: "GET",
includes: "test/dataset1/rules",
includes: "test/file1/parents",
response: {
status: 200,
headers: {
Expand All @@ -36,24 +36,24 @@ describe("List DID Rules Feature tests", () => {
},
};

MockRucioServerFactory.createMockRucioServer(true, [didListRulesMockEndpoint]);
MockRucioServerFactory.createMockRucioServer(true, [didListParentsMockEndpoint]);
});

afterEach(() => {
fetchMock.dontMock();
});
it("should list DID rules", async () => {
it("should list DID parents", async () => {
const res = MockHttpStreamableResponseFactory.getMockResponse();

const listDIDRulesController = appContainer.get<BaseController<ListDIDParentsControllerParameters, ListDIDParentsRequest>>(CONTROLLERS.LIST_DID_PARENTS);
const listDIDRulesControllerParams: ListDIDParentsControllerParameters = {
const listDIDParentsController = appContainer.get<BaseController<ListDIDParentsControllerParameters, ListDIDParentsRequest>>(CONTROLLERS.LIST_DID_PARENTS);
const listDIDParentsControllerParams: ListDIDParentsControllerParameters = {
response: res as unknown as NextApiResponse,
rucioAuthToken: MockRucioServerFactory.VALID_RUCIO_TOKEN,
name: 'file1',
scope: 'test'
}

await listDIDRulesController.execute(listDIDRulesControllerParams);
await listDIDParentsController.execute(listDIDParentsControllerParams);

const receivedData: any[] = []
const onData = (data: any) => {
Expand Down

0 comments on commit dc92db4

Please sign in to comment.