Skip to content

Commit

Permalink
refactor: bulk refactor ipex flow
Browse files Browse the repository at this point in the history
  • Loading branch information
bao-sotatek committed May 9, 2024
1 parent cea2a1f commit 17fbfdb
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 149 deletions.
1 change: 1 addition & 0 deletions src/core/agent/records/credentialMetadataRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CredentialMetadataRecord extends BaseRecord {
isArchived: this.isArchived,
isDeleted: this.isDeleted,
connectionId: this.connectionId,
id: this.id,
};
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/core/agent/records/credentialStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const credentialMetadataRecordB = new CredentialMetadataRecord({
id: id2,
});

describe("Connection service of agent", () => {
describe("Credential storage test", () => {
beforeEach(() => {
jest.resetAllMocks();
});
Expand Down Expand Up @@ -88,4 +88,22 @@ describe("Connection service of agent", () => {
const record = await credentialStorage.getCredentialMetadata("id");
expect(record).toBe(null);
});

test("Should get credential by ids", async () => {
const ids = [credentialMetadataRecordA.id, credentialMetadataRecordB.id];
storageService.findAllByQuery.mockResolvedValue([
credentialMetadataRecordA,
credentialMetadataRecordB,
]);
expect(await credentialStorage.getCrentialMetadataByIds(ids)).toEqual([
credentialMetadataRecordA,
credentialMetadataRecordB,
]);
expect(storageService.findAllByQuery).toBeCalledWith(
{
$or: ids.map((id) => ({ id })),
},
CredentialMetadataRecord
);
});
});
9 changes: 9 additions & 0 deletions src/core/agent/records/credentialStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ class CredentialStorage {
await this.storageService.update(record);
}
}

async getCrentialMetadataByIds(ids: string[]) {
return this.storageService.findAllByQuery(
{
$or: ids.map((id) => ({ id })),
},
CredentialMetadataRecord
);
}
}

export { CredentialStorage };
2 changes: 1 addition & 1 deletion src/core/agent/records/identifierStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const identifierMetadataRecord2 = new IdentifierMetadataRecord({
id: "id2",
});

describe("Connection service of agent", () => {
describe("Identifier storage test", () => {
beforeEach(() => {
jest.resetAllMocks();
});
Expand Down
180 changes: 63 additions & 117 deletions src/core/agent/services/ipexCommunicationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const credentialStorage = jest.mocked({
getCredentialMetadataByConnectionId: jest.fn(),
saveCredentialMetadataRecord: jest.fn(),
updateCredentialMetadata: jest.fn(),
getCrentialMetadataByIds: jest.fn(),
});

let credentialListMock = jest.fn();
Expand Down Expand Up @@ -70,6 +71,7 @@ let getExchangeMock = jest.fn().mockImplementation((id: string) => {
});
const ipexOfferMock = jest.fn();
const ipexGrantMock = jest.fn();
const schemaGetMock = jest.fn();
const signifyClient = jest.mocked({
connect: jest.fn(),
boot: jest.fn(),
Expand Down Expand Up @@ -141,6 +143,9 @@ const signifyClient = jest.mocked({
query: jest.fn(),
get: jest.fn(),
}),
schemas: () => ({
get: schemaGetMock,
}),
});

jest.mock("signify-ts", () => ({
Expand Down Expand Up @@ -267,18 +272,14 @@ describe("Ipex communication service of agent", () => {
test("can offer Keri Acdc when received the ipex apply", async () => {
const id = "uuid";
const date = new Date();
notificationStorage.findById = jest.fn().mockImplementation((id) => {
if (id == "uuid") {
return {
id,
createdAt: date,
a: {
d: "keri",
},
};
}
return;
});
const noti = {
id,
createdAt: date,
a: {
d: "keri",
},
};

getExchangeMock = jest.fn().mockReturnValue({
exn: {
a: {
Expand All @@ -293,7 +294,7 @@ describe("Ipex communication service of agent", () => {
signifyName: "abc123",
});
ipexOfferMock.mockResolvedValue(["offer", "sigs", "gend"]);
await ipexCommunicationService.offerAcdc(id, "credId");
await ipexCommunicationService.offerAcdcFromApply(noti, {});
expect(ipexOfferMock).toBeCalledWith({
senderName: "abc123",
recipient: "i",
Expand All @@ -306,18 +307,13 @@ describe("Ipex communication service of agent", () => {
test("can not offer Keri Acdc if aid is not existed", async () => {
const id = "uuid";
const date = new Date();
notificationStorage.findById = jest.fn().mockImplementation((id) => {
if (id == "uuid") {
return {
id,
createdAt: date,
a: {
d: "keri",
},
};
}
return;
});
const noti = {
id,
createdAt: date,
a: {
d: "keri",
},
};
getExchangeMock = jest.fn().mockReturnValue({
exn: {
a: {
Expand All @@ -334,7 +330,7 @@ describe("Ipex communication service of agent", () => {
new Error(IdentifierStorage.IDENTIFIER_METADATA_RECORD_MISSING)
);
await expect(
ipexCommunicationService.offerAcdc(id, "credId")
ipexCommunicationService.offerAcdcFromApply(noti, {})
).rejects.toThrowError(
IdentifierStorage.IDENTIFIER_METADATA_RECORD_MISSING
);
Expand All @@ -343,18 +339,13 @@ describe("Ipex communication service of agent", () => {
test("can grant Keri Acdc when received the ipex agree", async () => {
const id = "uuid";
const date = new Date();
notificationStorage.findById = jest.fn().mockImplementation((id) => {
if (id == "uuid") {
return {
id,
createdAt: date,
a: {
d: "keri",
},
};
}
return;
});
const noti = {
id,
createdAt: date,
a: {
d: "keri",
},
};
getExchangeMock = jest.fn().mockReturnValue({
exn: {
a: {
Expand All @@ -371,7 +362,7 @@ describe("Ipex communication service of agent", () => {
signifyName: "abc123",
});
ipexGrantMock.mockResolvedValue(["offer", "sigs", "gend"]);
await ipexCommunicationService.grantApplyAcdc(id);
await ipexCommunicationService.grantAcdcFromOffer(noti);
expect(ipexGrantMock).toBeCalledWith({
acdc: {},
acdcAttachment: undefined,
Expand All @@ -388,18 +379,13 @@ describe("Ipex communication service of agent", () => {
test("can not grant Keri Acdc if aid is not existed", async () => {
const id = "uuid";
const date = new Date();
notificationStorage.findById = jest.fn().mockImplementation((id) => {
if (id == "uuid") {
return {
id,
createdAt: date,
a: {
d: "keri",
},
};
}
return;
});
const noti = {
id,
createdAt: date,
a: {
d: "keri",
},
};
getExchangeMock = jest.fn().mockReturnValue({
exn: {
a: {
Expand All @@ -419,7 +405,7 @@ describe("Ipex communication service of agent", () => {
new Error(IdentifierStorage.IDENTIFIER_METADATA_RECORD_MISSING)
);
await expect(
ipexCommunicationService.grantApplyAcdc(id)
ipexCommunicationService.grantAcdcFromOffer(noti)
).rejects.toThrowError(
IdentifierStorage.IDENTIFIER_METADATA_RECORD_MISSING
);
Expand All @@ -428,18 +414,13 @@ describe("Ipex communication service of agent", () => {
test("can not grant Keri Acdc if acdc is not existed", async () => {
const id = "uuid";
const date = new Date();
notificationStorage.findById = jest.fn().mockImplementation((id) => {
if (id == "uuid") {
return {
id,
createdAt: date,
a: {
d: "keri",
},
};
}
return;
});
const noti = {
id,
createdAt: date,
a: {
d: "keri",
},
};
getExchangeMock = jest.fn().mockReturnValue({
exn: {
a: {
Expand All @@ -453,7 +434,7 @@ describe("Ipex communication service of agent", () => {
});
credentialGetMock = jest.fn().mockReturnValue(null);
await expect(
ipexCommunicationService.grantApplyAcdc(id)
ipexCommunicationService.grantAcdcFromOffer(noti)
).rejects.toThrowError(IpexCommunicationService.CREDENTIAL_NOT_FOUND);
});

Expand All @@ -470,73 +451,38 @@ describe("Ipex communication service of agent", () => {
e: {},
},
});
notificationStorage.findById = jest.fn().mockResolvedValue({
const noti = {
id: notiId,
createdAt: "2024-04-29T11:01:04.903Z",
createdAt: new Date("2024-04-29T11:01:04.903Z"),
a: {
d: "saidForUuid",
},
};
schemaGetMock.mockResolvedValue({
title: "Qualified vLEI Issuer Credential",
description: "Qualified vLEI Issuer Credential",
});
credentialStorage.getCredentialMetadata.mockResolvedValue({
id: "id",
status: "confirmed",
});
credentialListMock.mockResolvedValue([
credentialStorage.getCrentialMetadataByIds.mockResolvedValue([
{
sad: {
d: "d",
},
},
]);
expect(
await ipexCommunicationService.getMatchingCredsForApply(notiId)
).toEqual([
{
acdc: {
sad: {
d: "d",
},
},
credentialType: undefined,
id: "id",
issuanceDate: undefined,
id: "metadata:d",
status: "confirmed",
},
]);
});

test("cannot get matching credential for apply if cannot find the credential metdadata", async () => {
const notiId = "notiId";
getExchangeMock = jest.fn().mockResolvedValue({
exn: {
a: {
i: "uuid",
a: {},
s: "schemaSaid",
},
i: "i",
e: {},
},
});
notificationStorage.findById = jest.fn().mockResolvedValue({
id: notiId,
createdAt: "2024-04-29T11:01:04.903Z",
a: {
d: "saidForUuid",
},
});
credentialStorage.getCredentialMetadata.mockResolvedValue(null);
credentialListMock.mockResolvedValue([
{
sad: {
d: "d",
},
},
]);
await expect(
ipexCommunicationService.getMatchingCredsForApply(notiId)
).rejects.toThrowError(
IpexCommunicationService.CREDENTIAL_MISSING_METADATA_ERROR_MSG
);
expect(
await ipexCommunicationService.getMatchingCredsForApply(noti)
).toEqual({
credentials: [{ acdc: { d: "d" }, connectionId: undefined }],
schema: {
description: "Qualified vLEI Issuer Credential",
name: "Qualified vLEI Issuer Credential",
},
});
});
});

0 comments on commit 17fbfdb

Please sign in to comment.