|
| 1 | +import http from "http"; |
| 2 | +import { AddressInfo } from "net"; |
| 3 | +import test, { Test } from "tape-promise/tape"; |
| 4 | +import { v4 as uuidv4 } from "uuid"; |
| 5 | +import { v4 as internalIpV4 } from "internal-ip"; |
| 6 | +import bodyParser from "body-parser"; |
| 7 | +import express from "express"; |
| 8 | + |
| 9 | +import { |
| 10 | + Containers, |
| 11 | + pruneDockerAllIfGithubAction, |
| 12 | + PostgresTestContainer, |
| 13 | + IrohaTestLedger, |
| 14 | +} from "@hyperledger/cactus-test-tooling"; |
| 15 | +import { PluginRegistry } from "@hyperledger/cactus-core"; |
| 16 | +import { PluginImportType } from "@hyperledger/cactus-core-api"; |
| 17 | + |
| 18 | +import { |
| 19 | + IListenOptions, |
| 20 | + LogLevelDesc, |
| 21 | + Servers, |
| 22 | +} from "@hyperledger/cactus-common"; |
| 23 | +import { RuntimeError } from "run-time-error"; |
| 24 | +import { |
| 25 | + PluginLedgerConnectorIroha, |
| 26 | + DefaultApi as IrohaApi, |
| 27 | + PluginFactoryLedgerConnector, |
| 28 | +} from "../../../../main/typescript/public-api"; |
| 29 | + |
| 30 | +import { Configuration } from "@hyperledger/cactus-core-api"; |
| 31 | + |
| 32 | +import { |
| 33 | + IrohaCommand, |
| 34 | + KeyPair, |
| 35 | + RunTransactionRequestV1, |
| 36 | +} from "../../../../main/typescript/generated/openapi/typescript-axios"; |
| 37 | +import cryptoHelper from "iroha-helpers-ts/lib/cryptoHelper"; |
| 38 | + |
| 39 | +import OAS from "../../../../main/json/openapi.json"; |
| 40 | +import { installOpenapiValidationMiddleware } from "@hyperledger/cactus-core"; |
| 41 | + |
| 42 | +const testCase = "Iroha plugin openapi validation"; |
| 43 | +const logLevel: LogLevelDesc = "INFO"; |
| 44 | + |
| 45 | +test.onFailure(async () => { |
| 46 | + await Containers.logDiagnostics({ logLevel }); |
| 47 | +}); |
| 48 | + |
| 49 | +test("BEFORE " + testCase, async (t: Test) => { |
| 50 | + const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
| 51 | + await t.doesNotReject(pruning, "Pruning didn't throw OK"); |
| 52 | + t.end(); |
| 53 | +}); |
| 54 | + |
| 55 | +test(testCase, async (t: Test) => { |
| 56 | + const postgres = new PostgresTestContainer({ logLevel }); |
| 57 | + |
| 58 | + test.onFinish(async () => { |
| 59 | + await postgres.stop(); |
| 60 | + }); |
| 61 | + |
| 62 | + await postgres.start(); |
| 63 | + const postgresHost = await internalIpV4(); |
| 64 | + const postgresPort = await postgres.getPostgresPort(); |
| 65 | + const irohaHost = await internalIpV4(); |
| 66 | + if (!postgresHost || !irohaHost) { |
| 67 | + throw new RuntimeError("Could not determine the internal IPV4 address."); |
| 68 | + } |
| 69 | + |
| 70 | + const keyPair1: KeyPair = cryptoHelper.generateKeyPair(); |
| 71 | + const adminPriv = keyPair1.privateKey; |
| 72 | + const adminPubA = keyPair1.publicKey; |
| 73 | + const keyPair2: KeyPair = cryptoHelper.generateKeyPair(); |
| 74 | + const nodePrivA = keyPair2.privateKey; |
| 75 | + const nodePubA = keyPair2.publicKey; |
| 76 | + const keyPair3: KeyPair = cryptoHelper.generateKeyPair(); |
| 77 | + const userPub = keyPair3.publicKey; |
| 78 | + const iroha = new IrohaTestLedger({ |
| 79 | + adminPriv: adminPriv, |
| 80 | + adminPub: adminPubA, |
| 81 | + nodePriv: nodePrivA, |
| 82 | + nodePub: nodePubA, |
| 83 | + postgresHost: postgresHost, |
| 84 | + postgresPort: postgresPort, |
| 85 | + logLevel: logLevel, |
| 86 | + }); |
| 87 | + |
| 88 | + test.onFinish(async () => { |
| 89 | + await iroha.stop(); |
| 90 | + }); |
| 91 | + await iroha.start(); |
| 92 | + const irohaPort = await iroha.getRpcToriiPort(); |
| 93 | + const rpcToriiPortHost = await iroha.getRpcToriiPortHost(); |
| 94 | + const factory = new PluginFactoryLedgerConnector({ |
| 95 | + pluginImportType: PluginImportType.Local, |
| 96 | + }); |
| 97 | + |
| 98 | + const connector: PluginLedgerConnectorIroha = await factory.create({ |
| 99 | + rpcToriiPortHost, |
| 100 | + instanceId: uuidv4(), |
| 101 | + pluginRegistry: new PluginRegistry(), |
| 102 | + }); |
| 103 | + |
| 104 | + const expressApp = express(); |
| 105 | + expressApp.use(bodyParser.json({ limit: "250mb" })); |
| 106 | + const server = http.createServer(expressApp); |
| 107 | + const listenOptions: IListenOptions = { |
| 108 | + hostname: "0.0.0.0", |
| 109 | + port: 0, |
| 110 | + server, |
| 111 | + }; |
| 112 | + const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; |
| 113 | + test.onFinish(async () => await Servers.shutdown(server)); |
| 114 | + const { address, port } = addressInfo; |
| 115 | + const apiHost = `http://${address}:${port}`; |
| 116 | + const apiConfig = new Configuration({ basePath: apiHost }); |
| 117 | + const apiClient = new IrohaApi(apiConfig); |
| 118 | + |
| 119 | + await installOpenapiValidationMiddleware({ |
| 120 | + logLevel, |
| 121 | + app: expressApp, |
| 122 | + apiSpec: OAS, |
| 123 | + }); |
| 124 | + |
| 125 | + await connector.getOrCreateWebServices(); |
| 126 | + await connector.registerWebServices(expressApp); |
| 127 | + |
| 128 | + const admin = iroha.getDefaultAdminAccount(); |
| 129 | + const domain = iroha.getDefaultDomain(); |
| 130 | + const adminID = `${admin}@${domain}`; |
| 131 | + const user = uuidv4().substring(0, 5); |
| 132 | + |
| 133 | + const fRun = "runTransactionV1"; |
| 134 | + const cOk = "without bad request error"; |
| 135 | + const cWithoutParams = "not sending all required parameters"; |
| 136 | + const cInvalidParams = "sending invalid parameters"; |
| 137 | + |
| 138 | + test(`${testCase} - ${fRun} - ${cOk}`, async (t2: Test) => { |
| 139 | + const parameters = { |
| 140 | + commandName: IrohaCommand.CreateAccount, |
| 141 | + baseConfig: { |
| 142 | + irohaHost: irohaHost, |
| 143 | + irohaPort: irohaPort, |
| 144 | + creatorAccountId: adminID, |
| 145 | + privKey: [adminPriv], |
| 146 | + quorum: 1, |
| 147 | + timeoutLimit: 5000, |
| 148 | + tls: false, |
| 149 | + }, |
| 150 | + params: [user, domain, userPub], |
| 151 | + }; |
| 152 | + const res = await apiClient.runTransactionV1(parameters); |
| 153 | + t2.ok(res); |
| 154 | + t2.equal(res.status, 200); |
| 155 | + |
| 156 | + t2.end(); |
| 157 | + }); |
| 158 | + |
| 159 | + test(`${testCase} - ${fRun} - ${cWithoutParams}`, async (t2: Test) => { |
| 160 | + try { |
| 161 | + const parameters = { |
| 162 | + commandName: IrohaCommand.CreateAccount, |
| 163 | + baseConfig: { |
| 164 | + irohaHost: irohaHost, |
| 165 | + irohaPort: irohaPort, |
| 166 | + creatorAccountId: adminID, |
| 167 | + privKey: [adminPriv], |
| 168 | + quorum: 1, |
| 169 | + timeoutLimit: 5000, |
| 170 | + tls: false, |
| 171 | + }, |
| 172 | + // params: [user, domain, userPub], |
| 173 | + }; |
| 174 | + await apiClient.runTransactionV1( |
| 175 | + (parameters as any) as RunTransactionRequestV1, |
| 176 | + ); |
| 177 | + } catch (e) { |
| 178 | + t2.equal( |
| 179 | + e.response.status, |
| 180 | + 400, |
| 181 | + `Endpoint ${fRun} without required params: response.status === 400 OK`, |
| 182 | + ); |
| 183 | + const fields = e.response.data.map((param: any) => |
| 184 | + param.path.replace(".body.", ""), |
| 185 | + ); |
| 186 | + t2.ok(fields.includes("params"), "Rejected because params is required"); |
| 187 | + } |
| 188 | + t2.end(); |
| 189 | + }); |
| 190 | + |
| 191 | + test(`${testCase} - ${fRun} - ${cInvalidParams}`, async (t2: Test) => { |
| 192 | + try { |
| 193 | + const parameters = { |
| 194 | + commandName: IrohaCommand.CreateAccount, |
| 195 | + baseConfig: { |
| 196 | + irohaHost: irohaHost, |
| 197 | + irohaPort: irohaPort, |
| 198 | + creatorAccountId: adminID, |
| 199 | + privKey: [adminPriv], |
| 200 | + quorum: 1, |
| 201 | + timeoutLimit: 5000, |
| 202 | + tls: false, |
| 203 | + }, |
| 204 | + params: [user, domain, userPub], |
| 205 | + fake: 4, |
| 206 | + }; |
| 207 | + await apiClient.runTransactionV1( |
| 208 | + (parameters as any) as RunTransactionRequestV1, |
| 209 | + ); |
| 210 | + } catch (e) { |
| 211 | + t2.equal( |
| 212 | + e.response.status, |
| 213 | + 400, |
| 214 | + `Endpoint ${fRun} with fake=4: response.status === 400 OK`, |
| 215 | + ); |
| 216 | + const fields = e.response.data.map((param: any) => |
| 217 | + param.path.replace(".body.", ""), |
| 218 | + ); |
| 219 | + t2.ok( |
| 220 | + fields.includes("fake"), |
| 221 | + "Rejected because fake is not a valid parameter", |
| 222 | + ); |
| 223 | + } |
| 224 | + t2.end(); |
| 225 | + }); |
| 226 | + |
| 227 | + t.end(); |
| 228 | +}); |
| 229 | + |
| 230 | +test("AFTER " + testCase, async (t: Test) => { |
| 231 | + const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
| 232 | + await t.doesNotReject(pruning, "Pruning didn't throw OK"); |
| 233 | + t.end(); |
| 234 | +}); |
0 commit comments