|
1 |
| -import test, { Test } from "tape-promise/tape"; |
| 1 | +import "jest-extended"; |
2 | 2 |
|
3 | 3 | import { v4 as uuidv4 } from "uuid";
|
4 | 4 | import { createServer } from "http";
|
@@ -33,117 +33,119 @@ import { PluginRegistry } from "@hyperledger/cactus-core";
|
33 | 33 |
|
34 | 34 | import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
|
35 | 35 |
|
36 |
| -const testCase = "Test sign transaction endpoint"; |
37 |
| -const logLevel: LogLevelDesc = "TRACE"; |
| 36 | +const testCase = "Test get block endpoint"; |
| 37 | +const logLevel: LogLevelDesc = "INFO"; |
38 | 38 |
|
39 |
| -test("BEFORE " + testCase, async (t: Test) => { |
40 |
| - const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
41 |
| - await t.doesNotReject(pruning, "Pruning didn't throw OK"); |
42 |
| - t.end(); |
43 |
| -}); |
44 |
| - |
45 |
| -test(testCase, async (t: Test) => { |
46 |
| - const keyEncoder: KeyEncoder = new KeyEncoder("secp256k1"); |
47 |
| - const keychainId = uuidv4(); |
48 |
| - const keychainRef = uuidv4(); |
49 |
| - |
50 |
| - const { privateKey } = Secp256k1Keys.generateKeyPairsBuffer(); |
51 |
| - const keyHex = privateKey.toString("hex"); |
52 |
| - const pem = keyEncoder.encodePrivate(keyHex, KeyFormat.Raw, KeyFormat.PEM); |
53 |
| - |
54 |
| - const keychain = new PluginKeychainMemory({ |
55 |
| - backend: new Map([[keychainRef, pem]]), |
56 |
| - keychainId, |
57 |
| - logLevel, |
58 |
| - instanceId: uuidv4(), |
59 |
| - }); |
| 39 | +describe(testCase, () => { |
| 40 | + const besuTestLedger = new BesuTestLedger(); |
| 41 | + let apiServer: ApiServer; |
60 | 42 |
|
61 |
| - const httpServer1 = createServer(); |
62 |
| - await new Promise((resolve, reject) => { |
63 |
| - httpServer1.once("error", reject); |
64 |
| - httpServer1.once("listening", resolve); |
65 |
| - httpServer1.listen(0, "127.0.0.1"); |
| 43 | + beforeAll(async () => { |
| 44 | + const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
| 45 | + await expect(pruning).resolves.toBeTruthy(); |
| 46 | + await besuTestLedger.start(); |
66 | 47 | });
|
67 |
| - const addressInfo1 = httpServer1.address() as AddressInfo; |
68 |
| - t.comment(`HttpServer1 AddressInfo: ${JSON.stringify(addressInfo1)}`); |
69 |
| - const node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`; |
70 |
| - t.comment(`Cactus Node 1 Host: ${node1Host}`); |
71 |
| - |
72 |
| - const besuTestLedger = new BesuTestLedger(); |
73 |
| - await besuTestLedger.start(); |
74 | 48 |
|
75 |
| - const tearDown = async () => { |
| 49 | + afterAll(async () => { |
76 | 50 | await besuTestLedger.stop();
|
77 | 51 | await besuTestLedger.destroy();
|
78 |
| - }; |
79 |
| - |
80 |
| - test.onFinish(tearDown); |
81 |
| - |
82 |
| - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); |
83 |
| - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); |
84 |
| - |
85 |
| - // 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin |
86 |
| - const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); |
87 |
| - |
88 |
| - // 3. Instantiate the web service consortium plugin |
89 |
| - const options: IPluginLedgerConnectorBesuOptions = { |
90 |
| - instanceId: uuidv4(), |
91 |
| - rpcApiHttpHost, |
92 |
| - rpcApiWsHost, |
93 |
| - pluginRegistry, |
94 |
| - logLevel, |
95 |
| - }; |
96 |
| - const pluginValidatorBesu = new PluginLedgerConnectorBesu(options); |
97 |
| - |
98 |
| - // 4. Create the API Server object that we embed in this test |
99 |
| - const configService = new ConfigService(); |
100 |
| - const apiServerOptions = await configService.newExampleConfig(); |
101 |
| - apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE; |
102 |
| - apiServerOptions.configFile = ""; |
103 |
| - apiServerOptions.apiCorsDomainCsv = "*"; |
104 |
| - apiServerOptions.apiPort = addressInfo1.port; |
105 |
| - apiServerOptions.cockpitPort = 0; |
106 |
| - apiServerOptions.grpcPort = 0; |
107 |
| - apiServerOptions.crpcPort = 0; |
108 |
| - apiServerOptions.apiTlsEnabled = false; |
109 |
| - const config = await configService.newExampleConfigConvict(apiServerOptions); |
110 |
| - |
111 |
| - pluginRegistry.add(pluginValidatorBesu); |
112 |
| - |
113 |
| - const apiServer = new ApiServer({ |
114 |
| - httpServerApi: httpServer1, |
115 |
| - config: config.getProperties(), |
116 |
| - pluginRegistry, |
| 52 | + await apiServer.shutdown(); |
117 | 53 | });
|
118 | 54 |
|
119 |
| - // 5. make sure the API server is shut down when the testing if finished. |
120 |
| - test.onFinish(() => apiServer.shutdown()); |
121 |
| - |
122 |
| - // 6. Start the API server which is now listening on port A and it's healthcheck works through the main SDK |
123 |
| - await apiServer.start(); |
124 |
| - |
125 |
| - // 7. Instantiate the main SDK dynamically with whatever port the API server ended up bound to (port 0) |
126 |
| - t.comment(`AddressInfo: ${JSON.stringify(addressInfo1)}`); |
127 |
| - |
128 |
| - const request: GetBlockV1Request = { |
129 |
| - blockHashOrBlockNumber: 0, |
130 |
| - }; |
131 |
| - |
132 |
| - const configuration = new BesuApiClientOptions({ basePath: node1Host }); |
133 |
| - const api = new BesuApiClient(configuration); |
134 |
| - |
135 |
| - // Test for 200 valid response test case |
136 |
| - const res = await api.getBlockV1(request); |
137 |
| - |
138 |
| - const { status, data } = res; |
139 |
| - t.true(status >= 200, "status GTE 200 OK"); |
140 |
| - t.true(status < 300, "status LT 300 OK"); |
141 |
| - t.ok(data, "GetBlockResponse Truthy OK"); |
142 |
| - t.true(typeof data.block === "object", "Response data is OK"); |
143 |
| -}); |
| 55 | + afterAll(async () => { |
| 56 | + const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
| 57 | + await expect(pruning).resolves.toBeTruthy(); |
| 58 | + }); |
144 | 59 |
|
145 |
| -test("AFTER " + testCase, async (t: Test) => { |
146 |
| - const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
147 |
| - await t.doesNotReject(pruning, "Pruning didn't throw OK"); |
148 |
| - t.end(); |
| 60 | + test(testCase, async () => { |
| 61 | + const keyEncoder: KeyEncoder = new KeyEncoder("secp256k1"); |
| 62 | + const keychainId = uuidv4(); |
| 63 | + const keychainRef = uuidv4(); |
| 64 | + |
| 65 | + const { privateKey } = Secp256k1Keys.generateKeyPairsBuffer(); |
| 66 | + const keyHex = privateKey.toString("hex"); |
| 67 | + const pem = keyEncoder.encodePrivate(keyHex, KeyFormat.Raw, KeyFormat.PEM); |
| 68 | + |
| 69 | + const keychain = new PluginKeychainMemory({ |
| 70 | + backend: new Map([[keychainRef, pem]]), |
| 71 | + keychainId, |
| 72 | + logLevel, |
| 73 | + instanceId: uuidv4(), |
| 74 | + }); |
| 75 | + |
| 76 | + const httpServer1 = createServer(); |
| 77 | + await new Promise((resolve, reject) => { |
| 78 | + httpServer1.once("error", reject); |
| 79 | + httpServer1.once("listening", resolve); |
| 80 | + httpServer1.listen(0, "127.0.0.1"); |
| 81 | + }); |
| 82 | + const addressInfo1 = httpServer1.address() as AddressInfo; |
| 83 | + console.log(`HttpServer1 AddressInfo: ${JSON.stringify(addressInfo1)}`); |
| 84 | + |
| 85 | + const node1Host = `http://${addressInfo1.address}:${addressInfo1.port}`; |
| 86 | + console.log(`Cactus Node 1 Host: ${node1Host}`); |
| 87 | + |
| 88 | + const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); |
| 89 | + const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); |
| 90 | + |
| 91 | + // 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin |
| 92 | + const pluginRegistry = new PluginRegistry({ plugins: [keychain] }); |
| 93 | + |
| 94 | + // 3. Instantiate the web service consortium plugin |
| 95 | + const options: IPluginLedgerConnectorBesuOptions = { |
| 96 | + instanceId: uuidv4(), |
| 97 | + rpcApiHttpHost, |
| 98 | + rpcApiWsHost, |
| 99 | + pluginRegistry, |
| 100 | + logLevel, |
| 101 | + }; |
| 102 | + const pluginValidatorBesu = new PluginLedgerConnectorBesu(options); |
| 103 | + |
| 104 | + // 4. Create the API Server object that we embed in this test |
| 105 | + const configService = new ConfigService(); |
| 106 | + const apiServerOptions = await configService.newExampleConfig(); |
| 107 | + apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE; |
| 108 | + apiServerOptions.configFile = ""; |
| 109 | + apiServerOptions.apiCorsDomainCsv = "*"; |
| 110 | + apiServerOptions.apiPort = addressInfo1.port; |
| 111 | + apiServerOptions.cockpitPort = 0; |
| 112 | + apiServerOptions.grpcPort = 0; |
| 113 | + apiServerOptions.crpcPort = 0; |
| 114 | + apiServerOptions.apiTlsEnabled = false; |
| 115 | + const config = |
| 116 | + await configService.newExampleConfigConvict(apiServerOptions); |
| 117 | + |
| 118 | + pluginRegistry.add(pluginValidatorBesu); |
| 119 | + |
| 120 | + apiServer = new ApiServer({ |
| 121 | + httpServerApi: httpServer1, |
| 122 | + config: config.getProperties(), |
| 123 | + pluginRegistry, |
| 124 | + }); |
| 125 | + |
| 126 | + // 5. make sure the API server is shut down when the testing if finished. |
| 127 | + |
| 128 | + // 6. Start the API server which is now listening on port A and it's healthcheck works through the main SDK |
| 129 | + await apiServer.start(); |
| 130 | + |
| 131 | + // 7. Instantiate the main SDK dynamically with whatever port the API server ended up bound to (port 0) |
| 132 | + console.log(`AddressInfo: ${JSON.stringify(addressInfo1)}`); |
| 133 | + |
| 134 | + const request: GetBlockV1Request = { |
| 135 | + blockHashOrBlockNumber: 0, |
| 136 | + }; |
| 137 | + |
| 138 | + const configuration = new BesuApiClientOptions({ basePath: node1Host }); |
| 139 | + const api = new BesuApiClient(configuration); |
| 140 | + |
| 141 | + // Test for 200 valid response test case |
| 142 | + const res = await api.getBlockV1(request); |
| 143 | + |
| 144 | + const { status, data } = res; |
| 145 | + |
| 146 | + expect(status).toBeGreaterThanOrEqual(200); |
| 147 | + expect(status).toBeLessThan(300); |
| 148 | + expect(data).toBeTruthy(); |
| 149 | + expect(data.block).toBeObject(); |
| 150 | + }); |
149 | 151 | });
|
0 commit comments