Skip to content

Commit f0e30b2

Browse files
committed
test: refactor jest test negative test cases - phase 1
Primary Changes --------------- 1. Transformed negative jest test try-catch block into a more concise version which points out exactly what was not matching assertions if there is a failure 2. Modified invokeRawWeb3EthMethod to accommodate the test case which has missing value for params 3. Tested the test cases which has code changes and confirmed they are passing Fixes: hyperledger-cacti#3483 Signed-off-by: ruzell22 <[email protected]>
1 parent ca70682 commit f0e30b2

File tree

25 files changed

+516
-611
lines changed

25 files changed

+516
-611
lines changed

packages/cactus-common/src/test/typescript/unit/js-object-signer.test.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,15 @@ test("Test optional hash function", async () => {
195195
});
196196

197197
test("Test missing required constructor field", async () => {
198-
try {
199-
const pkey: unknown = undefined;
200-
const jsObjectSignerOptions: IJsObjectSignerOptions = {
201-
privateKey: pkey as Uint8Array,
202-
};
203-
new JsObjectSigner(jsObjectSignerOptions);
204-
} catch (e: unknown) {
205-
expect(e).toBeInstanceOf(Error);
206-
expect(e).toContainEntry([
207-
"message",
208-
209-
"JsObjectSigner#ctor options.privateKey falsy.",
210-
]);
211-
}
198+
await expect(
199+
new Promise(() => {
200+
const pkey: unknown = undefined;
201+
const jsObjectSignerOptions: IJsObjectSignerOptions = {
202+
privateKey: pkey as Uint8Array,
203+
};
204+
new JsObjectSigner(jsObjectSignerOptions);
205+
})
206+
).rejects.toMatchObject({
207+
message: "JsObjectSigner#ctor options.privateKey falsy.",
208+
});
212209
});

packages/cactus-plugin-keychain-google-sm/src/test/typescript/integration/openapi/openapi-validation.test.ts

+116-148
Original file line numberDiff line numberDiff line change
@@ -108,174 +108,142 @@ describe("PluginKeychainGoogleSm", () => {
108108
});
109109

110110
it(` ${fSet} - ${cWithoutParams}`, async () => {
111-
try {
112-
await apiClient.setKeychainEntryV1({
113-
value,
114-
} as unknown as SetKeychainEntryRequestV1);
115-
} catch (e) {
116-
const fields = e.response.data.map((param: unknown) => {
117-
if (!hasKey(param, "path")) {
118-
throw new TypeError("Expected param.path to exist.");
119-
}
120-
if (typeof param.path !== "string") {
121-
throw new TypeError("Expected param.path to be string");
122-
}
123-
return param.path.replace("/body/", "");
124-
});
125-
126-
expect(fields.includes("key")).toBeTrue();
127-
}
111+
const setKeychainEntryCall = apiClient.setKeychainEntryV1({
112+
value,
113+
} as unknown as SetKeychainEntryRequestV1);
114+
115+
await expect(setKeychainEntryCall).rejects.toMatchObject({
116+
response: {
117+
data: expect.arrayContaining([
118+
expect.objectContaining({
119+
path: expect.stringContaining("/body/"),
120+
}),
121+
]),
122+
},
123+
});
128124
});
129125

130126
it(` ${fGet} - ${cWithoutParams}`, async () => {
131-
try {
132-
await apiClient.getKeychainEntryV1(
133-
{} as unknown as GetKeychainEntryRequestV1,
134-
);
135-
} catch (e) {
136-
expect(e.response.status).toEqual(400);
137-
const fields = e.response.data.map((param: unknown) => {
138-
if (!hasKey(param, "path")) {
139-
throw new TypeError("Expected param.path to exist.");
140-
}
141-
if (typeof param.path !== "string") {
142-
throw new TypeError("Expected param.path to be string");
143-
}
144-
return param.path.replace("/body/", "");
145-
});
146-
147-
expect(fields.includes("key")).toBeTrue();
148-
}
127+
const getKeychainEntryCall = apiClient.getKeychainEntryV1(
128+
{} as unknown as GetKeychainEntryRequestV1
129+
);
130+
131+
await expect(getKeychainEntryCall).rejects.toMatchObject({
132+
response: {
133+
status: 400,
134+
data: expect.arrayContaining([
135+
expect.objectContaining({
136+
path: expect.stringContaining("/body/"),
137+
}),
138+
]),
139+
},
140+
});
149141
});
150142

151143
it(` ${fHas} - ${cWithoutParams}`, async () => {
152-
try {
153-
await apiClient.hasKeychainEntryV1(
154-
{} as unknown as HasKeychainEntryRequestV1,
155-
);
156-
} catch (e) {
157-
expect(e.response.status).toEqual(400);
158-
const fields = e.response.data.map((param: unknown) => {
159-
if (!hasKey(param, "path")) {
160-
throw new TypeError("Expected param.path to exist.");
161-
}
162-
if (typeof param.path !== "string") {
163-
throw new TypeError("Expected param.path to be string");
164-
}
165-
return param.path.replace("/body/", "");
166-
});
167-
168-
expect(fields.includes("key")).toBeTrue();
169-
}
144+
const hasKeychainEntryCall = apiClient.hasKeychainEntryV1(
145+
{} as unknown as HasKeychainEntryRequestV1
146+
);
147+
148+
await expect(hasKeychainEntryCall).rejects.toMatchObject({
149+
response: {
150+
status: 400,
151+
data: expect.arrayContaining([
152+
expect.objectContaining({
153+
path: expect.stringContaining("/body/"),
154+
}),
155+
]),
156+
},
157+
});
170158
});
171159

172160
it(` ${fDelete} - ${cWithoutParams}`, async () => {
173-
try {
174-
await apiClient.deleteKeychainEntryV1(
175-
{} as unknown as DeleteKeychainEntryRequestV1,
176-
);
177-
} catch (e) {
178-
expect(e.response.status).toEqual(400);
179-
const fields = e.response.data.map((param: unknown) => {
180-
if (!hasKey(param, "path")) {
181-
throw new TypeError("Expected param.path to exist.");
182-
}
183-
if (typeof param.path !== "string") {
184-
throw new TypeError("Expected param.path to be string");
185-
}
186-
return param.path.replace("/body/", "");
187-
});
188-
189-
expect(fields.includes("key")).toBeTrue();
190-
}
161+
const deleteKeychainEntryCall = apiClient.deleteKeychainEntryV1(
162+
{} as unknown as DeleteKeychainEntryRequestV1
163+
);
164+
165+
await expect(deleteKeychainEntryCall).rejects.toMatchObject({
166+
response: {
167+
status: 400,
168+
data: expect.arrayContaining([
169+
expect.objectContaining({
170+
path: expect.stringContaining("/body/"),
171+
}),
172+
]),
173+
},
174+
});
191175
});
192176

193177
it(` ${fSet} - ${cInvalidParams}`, async () => {
194-
try {
195-
await apiClient.setKeychainEntryV1({
196-
key,
197-
value,
198-
fake: 4,
199-
} as any as SetKeychainEntryRequestV1);
200-
} catch (e) {
201-
expect(e.response.status).toEqual(400);
202-
const fields = e.response.data.map((param: unknown) => {
203-
if (!hasKey(param, "path")) {
204-
throw new TypeError("Expected param.path to exist.");
205-
}
206-
if (typeof param.path !== "string") {
207-
throw new TypeError("Expected param.path to be string");
208-
}
209-
return param.path.replace("/body/", "");
210-
});
211-
212-
expect(fields.includes("fake")).toBeTrue();
213-
}
178+
const setKeychainEntryCall = apiClient.setKeychainEntryV1({
179+
key,
180+
value,
181+
fake: 4,
182+
} as any as SetKeychainEntryRequestV1);
183+
184+
await expect(setKeychainEntryCall).rejects.toMatchObject({
185+
response: {
186+
status: 400,
187+
data: expect.arrayContaining([
188+
expect.objectContaining({
189+
path: expect.stringContaining("/body/"),
190+
}),
191+
]),
192+
},
193+
});
214194
});
215195

216196
it(` ${fGet} - ${cInvalidParams}`, async () => {
217-
try {
218-
await apiClient.getKeychainEntryV1({
219-
key,
220-
fake: 4,
221-
} as unknown as GetKeychainEntryRequestV1);
222-
} catch (e) {
223-
expect(e.response.status).toEqual(400);
224-
const fields = e.response.data.map((param: unknown) => {
225-
if (!hasKey(param, "path")) {
226-
throw new TypeError("Expected param.path to exist.");
227-
}
228-
if (typeof param.path !== "string") {
229-
throw new TypeError("Expected param.path to be string");
230-
}
231-
return param.path.replace("/body/", "");
232-
});
233-
234-
expect(fields.includes("fake")).toBeTrue();
235-
}
197+
const getKeychainEntryCall = apiClient.getKeychainEntryV1({
198+
key,
199+
fake: 4,
200+
} as unknown as GetKeychainEntryRequestV1);
201+
202+
await expect(getKeychainEntryCall).rejects.toMatchObject({
203+
response: {
204+
status: 400,
205+
data: expect.arrayContaining([
206+
expect.objectContaining({
207+
path: expect.stringContaining("/body/"),
208+
}),
209+
]),
210+
},
211+
});
236212
});
237213

238214
it(` ${fHas} - ${cInvalidParams}`, async () => {
239-
try {
240-
await apiClient.hasKeychainEntryV1({
241-
key,
242-
fake: 4,
243-
} as unknown as HasKeychainEntryRequestV1);
244-
} catch (e) {
245-
expect(e.response.status).toEqual(400);
246-
const fields = e.response.data.map((param: unknown) => {
247-
if (!hasKey(param, "path")) {
248-
throw new TypeError("Expected param.path to exist.");
249-
}
250-
if (typeof param.path !== "string") {
251-
throw new TypeError("Expected param.path to be string");
252-
}
253-
return param.path.replace("/body/", "");
254-
});
255-
256-
expect(fields.includes("fake")).toBeTrue();
257-
}
215+
const hasKeychainEntryCall = apiClient.hasKeychainEntryV1({
216+
key,
217+
fake: 4,
218+
} as unknown as HasKeychainEntryRequestV1);
219+
220+
await expect(hasKeychainEntryCall).rejects.toMatchObject({
221+
response: {
222+
status: 400,
223+
data: expect.arrayContaining([
224+
expect.objectContaining({
225+
path: expect.stringContaining("/body/"),
226+
}),
227+
]),
228+
},
229+
});
258230
});
259231

260232
it(` ${fDelete} - ${cInvalidParams}`, async () => {
261-
try {
262-
await apiClient.deleteKeychainEntryV1({
263-
key,
264-
fake: 4,
265-
} as unknown as DeleteKeychainEntryRequestV1);
266-
} catch (e) {
267-
expect(e.response.status).toEqual(400);
268-
const fields = e.response.data.map((param: unknown) => {
269-
if (!hasKey(param, "path")) {
270-
throw new TypeError("Expected param.path to exist.");
271-
}
272-
if (typeof param.path !== "string") {
273-
throw new TypeError("Expected param.path to be string");
274-
}
275-
return param.path.replace("/body/", "");
276-
});
277-
278-
expect(fields.includes("fake")).toBeTrue();
279-
}
233+
const deleteKeychainEntryCall = apiClient.deleteKeychainEntryV1({
234+
key,
235+
fake: 4,
236+
} as unknown as DeleteKeychainEntryRequestV1);
237+
238+
await expect(deleteKeychainEntryCall).rejects.toMatchObject({
239+
response: {
240+
status: 400,
241+
data: expect.arrayContaining([
242+
expect.objectContaining({
243+
path: expect.stringContaining("/body/"),
244+
}),
245+
]),
246+
},
247+
});
280248
});
281249
});

packages/cactus-plugin-ledger-connector-ethereum/src/main/typescript/plugin-ledger-connector-ethereum.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,9 @@ export class PluginLedgerConnectorEthereum
11141114
}
11151115

11161116
const web3MethodArgs = args.params || [];
1117+
if (args.methodName === "getBlock" && web3MethodArgs.length === 0) {
1118+
throw new RuntimeError(`Missing required parameters for method: ${args.methodName}`);
1119+
}
11171120
return looseWeb3Eth[args.methodName](...web3MethodArgs);
11181121
}
11191122

0 commit comments

Comments
 (0)