Skip to content

Commit e83ddc8

Browse files
elenaizaguirrebrioux
authored andcommitted
fix: openapi validation test for consortium-manual plugin
Includes tests for endpoints getConsortiumJwsV1 and getNodeJwsV1, each one of them with test cases: - Right request - Sending an invalid parameter closes hyperledger-cacti#1297 relationed with hyperledger-cacti#847 Signed-off-by: Elena Izaguirre <[email protected]>
1 parent 044df00 commit e83ddc8

File tree

1 file changed

+326
-0
lines changed
  • packages/cactus-test-plugin-consortium-manual/src/test/typescript/integration/plugin-consortium-manual/openapi

1 file changed

+326
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
import { createServer } from "http";
2+
import { AddressInfo } from "net";
3+
4+
import test, { Test } from "tape";
5+
import { JWK } from "jose";
6+
import { v4 as uuidV4 } from "uuid";
7+
8+
import {
9+
ApiServer,
10+
AuthorizationProtocol,
11+
ConfigService,
12+
} from "@hyperledger/cactus-cmd-api-server";
13+
import {
14+
IPluginConsortiumManualOptions,
15+
PluginConsortiumManual,
16+
DefaultApi,
17+
Configuration,
18+
} from "@hyperledger/cactus-plugin-consortium-manual";
19+
import {
20+
CactusNode,
21+
Consortium,
22+
ConsortiumDatabase,
23+
ConsortiumMember,
24+
} from "@hyperledger/cactus-core-api";
25+
import { PluginRegistry } from "@hyperledger/cactus-core";
26+
27+
const testCase = "cactus-plugin-consortium-manual API";
28+
29+
test(testCase, async (t: Test) => {
30+
const fGetNodeJwt = "getNodeJws";
31+
const fGetConsortiumJws = "getConsortiumJws";
32+
const cOk = "without bad request error";
33+
const cInvalidParams = "sending invalid parameters";
34+
35+
let node1Host: string, node2Host: string, node3Host: string;
36+
37+
test(`${testCase} - Create environment`, async (t2: Test) => {
38+
const consortiumId = uuidV4();
39+
const consortiumName = "Example Corp. & Friends Crypto Consortium";
40+
41+
const memberId1 = uuidV4();
42+
const memberId2 = uuidV4();
43+
const memberId3 = uuidV4();
44+
45+
const httpServer1 = createServer();
46+
await new Promise((resolve, reject) => {
47+
httpServer1.once("error", reject);
48+
httpServer1.once("listening", resolve);
49+
httpServer1.listen(0, "127.0.0.1");
50+
});
51+
const addressInfo1 = httpServer1.address() as AddressInfo;
52+
node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`;
53+
54+
const keyPair1 = await JWK.generate("EC", "secp256k1");
55+
const pubKeyPem1 = keyPair1.toPEM(false);
56+
57+
const httpServer2 = createServer();
58+
await new Promise((resolve, reject) => {
59+
httpServer2.once("error", reject);
60+
httpServer2.once("listening", resolve);
61+
httpServer2.listen(0, "127.0.0.1");
62+
});
63+
const addressInfo2 = httpServer2.address() as AddressInfo;
64+
node2Host = `http://${addressInfo2.address}:${addressInfo2.port}`;
65+
66+
const keyPair2 = await JWK.generate("EC", "secp256k1");
67+
const pubKeyPem2 = keyPair2.toPEM(false);
68+
69+
const httpServer3 = createServer();
70+
await new Promise((resolve, reject) => {
71+
httpServer3.once("error", reject);
72+
httpServer3.once("listening", resolve);
73+
httpServer3.listen(0, "127.0.0.1");
74+
});
75+
const addressInfo3 = httpServer3.address() as AddressInfo;
76+
node3Host = `http://${addressInfo3.address}:${addressInfo3.port}`;
77+
78+
const keyPair3 = await JWK.generate("EC", "secp256k1");
79+
const pubKeyPem3 = keyPair3.toPEM(false);
80+
81+
const node1: CactusNode = {
82+
consortiumId,
83+
memberId: memberId1,
84+
id: "Example_Cactus_Node_1",
85+
nodeApiHost: node1Host,
86+
publicKeyPem: pubKeyPem1,
87+
ledgerIds: [],
88+
pluginInstanceIds: [],
89+
};
90+
91+
const member1: ConsortiumMember = {
92+
id: memberId1,
93+
name: "Example Corp 1",
94+
nodeIds: [node1.id],
95+
};
96+
97+
const node2: CactusNode = {
98+
consortiumId,
99+
memberId: memberId2,
100+
id: "Example_Cactus_Node_2",
101+
nodeApiHost: node2Host,
102+
publicKeyPem: pubKeyPem2,
103+
ledgerIds: [],
104+
pluginInstanceIds: [],
105+
};
106+
107+
const member2: ConsortiumMember = {
108+
id: memberId2,
109+
name: "Example Corp 2",
110+
nodeIds: [node2.id],
111+
};
112+
113+
const node3: CactusNode = {
114+
consortiumId,
115+
memberId: memberId3,
116+
id: "Example_Cactus_Node_3",
117+
nodeApiHost: node3Host,
118+
publicKeyPem: pubKeyPem3,
119+
ledgerIds: [],
120+
pluginInstanceIds: [],
121+
};
122+
123+
const member3: ConsortiumMember = {
124+
id: memberId3,
125+
name: "Example Corp 3",
126+
nodeIds: [node3.id],
127+
};
128+
129+
const consortium: Consortium = {
130+
id: consortiumId,
131+
mainApiHost: node1Host,
132+
name: consortiumName,
133+
memberIds: [memberId1, memberId2, memberId3],
134+
};
135+
136+
const consortiumDatabase: ConsortiumDatabase = {
137+
cactusNode: [node1, node2, node3],
138+
consortium: [consortium],
139+
consortiumMember: [member1, member2, member3],
140+
ledger: [],
141+
pluginInstance: [],
142+
};
143+
144+
t2.comment(`Setting up first node...`);
145+
{
146+
const pluginRegistry = new PluginRegistry({ plugins: [] });
147+
const options: IPluginConsortiumManualOptions = {
148+
instanceId: uuidV4(),
149+
pluginRegistry,
150+
keyPairPem: keyPair1.toPEM(true),
151+
consortiumDatabase,
152+
logLevel: "trace",
153+
};
154+
const pluginConsortiumManual = new PluginConsortiumManual(options);
155+
const configService = new ConfigService();
156+
const apiServerOptions = configService.newExampleConfig();
157+
apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE;
158+
apiServerOptions.configFile = "";
159+
apiServerOptions.apiCorsDomainCsv = "*";
160+
apiServerOptions.apiPort = addressInfo1.port;
161+
apiServerOptions.grpcPort = 0;
162+
apiServerOptions.cockpitPort = 0;
163+
apiServerOptions.apiTlsEnabled = false;
164+
const config = configService.newExampleConfigConvict(apiServerOptions);
165+
166+
pluginRegistry.add(pluginConsortiumManual);
167+
168+
const apiServer = new ApiServer({
169+
httpServerApi: httpServer1,
170+
config: config.getProperties(),
171+
pluginRegistry,
172+
});
173+
test.onFinish(() => apiServer.shutdown());
174+
await apiServer.start();
175+
}
176+
t2.comment(`Set up first node OK`);
177+
178+
t2.comment(`Setting up second node...`);
179+
{
180+
const pluginRegistry = new PluginRegistry({ plugins: [] });
181+
182+
const options: IPluginConsortiumManualOptions = {
183+
instanceId: uuidV4(),
184+
pluginRegistry,
185+
keyPairPem: keyPair2.toPEM(true),
186+
consortiumDatabase,
187+
logLevel: "trace",
188+
};
189+
const pluginConsortiumManual = new PluginConsortiumManual(options);
190+
const configService = new ConfigService();
191+
const apiServerOptions = configService.newExampleConfig();
192+
apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE;
193+
apiServerOptions.configFile = "";
194+
apiServerOptions.apiCorsDomainCsv = "*";
195+
apiServerOptions.apiPort = addressInfo2.port;
196+
apiServerOptions.cockpitPort = 0;
197+
apiServerOptions.grpcPort = 0;
198+
apiServerOptions.apiTlsEnabled = false;
199+
const config = configService.newExampleConfigConvict(apiServerOptions);
200+
201+
pluginRegistry.add(pluginConsortiumManual);
202+
203+
const apiServer = new ApiServer({
204+
httpServerApi: httpServer2,
205+
config: config.getProperties(),
206+
pluginRegistry,
207+
});
208+
test.onFinish(() => apiServer.shutdown());
209+
await apiServer.start();
210+
}
211+
t2.comment(`Set up second node OK`);
212+
213+
t2.comment(`Setting up third node...`);
214+
{
215+
const pluginRegistry = new PluginRegistry({ plugins: [] });
216+
const options: IPluginConsortiumManualOptions = {
217+
instanceId: uuidV4(),
218+
pluginRegistry,
219+
keyPairPem: keyPair3.toPEM(true),
220+
consortiumDatabase,
221+
logLevel: "trace",
222+
};
223+
const pluginConsortiumManual = new PluginConsortiumManual(options);
224+
225+
const configService = new ConfigService();
226+
const apiServerOptions = configService.newExampleConfig();
227+
apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE;
228+
apiServerOptions.configFile = "";
229+
apiServerOptions.apiCorsDomainCsv = "*";
230+
apiServerOptions.apiPort = addressInfo3.port;
231+
apiServerOptions.cockpitPort = 0;
232+
apiServerOptions.grpcPort = 0;
233+
apiServerOptions.apiTlsEnabled = false;
234+
const config = configService.newExampleConfigConvict(apiServerOptions);
235+
236+
pluginRegistry.add(pluginConsortiumManual);
237+
238+
const apiServer = new ApiServer({
239+
httpServerApi: httpServer3,
240+
config: config.getProperties(),
241+
pluginRegistry,
242+
});
243+
test.onFinish(() => apiServer.shutdown());
244+
await apiServer.start();
245+
}
246+
t2.comment(`Set up third node OK`);
247+
248+
t2.end();
249+
});
250+
251+
test(`${testCase} - ${fGetNodeJwt} - ${cOk}`, async (t2: Test) => {
252+
const configuration = new Configuration({ basePath: node1Host });
253+
const api = new DefaultApi(configuration);
254+
const res = await api.getNodeJwsV1();
255+
t2.equal(
256+
res.status,
257+
200,
258+
`Endpoint ${fGetNodeJwt}: response.status === 200 OK`,
259+
);
260+
t2.ok(res.data, "Node JWS data OK");
261+
262+
t2.end();
263+
});
264+
265+
test(`${testCase} - ${fGetConsortiumJws} - ${cOk}`, async (t2: Test) => {
266+
const configuration = new Configuration({ basePath: node1Host });
267+
const api = new DefaultApi(configuration);
268+
const res = await api.getConsortiumJwsV1();
269+
t2.equal(
270+
res.status,
271+
200,
272+
`Endpoint ${fGetConsortiumJws}: response.status === 200 OK`,
273+
);
274+
t2.ok(res.data, "Consortium JWS data OK");
275+
276+
t2.end();
277+
});
278+
279+
test(`${testCase} - ${fGetNodeJwt} - ${cInvalidParams}`, async (t2: Test) => {
280+
const configuration = new Configuration({ basePath: node1Host });
281+
const api = new DefaultApi(configuration);
282+
try {
283+
await api.getNodeJwsV1({ fake: 4 });
284+
} catch (e) {
285+
t2.equal(
286+
e.response.status,
287+
400,
288+
`Endpoint ${fGetNodeJwt} with fake=4: response.status === 400 OK`,
289+
);
290+
const fields = e.response.data.map((param: any) =>
291+
param.path.replace(".body.", ""),
292+
);
293+
t2.ok(
294+
fields.includes("fake"),
295+
"Rejected because fake is not a valid parameter",
296+
);
297+
}
298+
299+
t2.end();
300+
});
301+
302+
test(`${testCase} - ${fGetConsortiumJws} - ${cInvalidParams}`, async (t2: Test) => {
303+
const configuration = new Configuration({ basePath: node1Host });
304+
const api = new DefaultApi(configuration);
305+
try {
306+
await api.getConsortiumJwsV1({ fake: 4 });
307+
} catch (e) {
308+
t2.equal(
309+
e.response.status,
310+
400,
311+
`Endpoint ${fGetConsortiumJws} with fake=4: response.status === 400 OK`,
312+
);
313+
const fields = e.response.data.map((param: any) =>
314+
param.path.replace(".body.", ""),
315+
);
316+
t2.ok(
317+
fields.includes("fake"),
318+
"Rejected because fake is not a valid parameter",
319+
);
320+
}
321+
322+
t2.end();
323+
});
324+
325+
t.end();
326+
});

0 commit comments

Comments
 (0)