From 0486cd2e6351fe18efdc25ede0a55025b2e9f3cb Mon Sep 17 00:00:00 2001 From: benedettadavico Date: Tue, 24 Oct 2023 11:16:06 +0200 Subject: [PATCH] refactoring the utils --- common/api-test-utils/config/config.yaml | 8 +- common/api-test-utils/config/configHandler.ts | 9 +- envs/qa.env | 31 ++- .../mixnode/contract-cache-mixnode.test.ts | 2 +- .../status/status-gateway.test.ts | 10 +- .../status/status-mixnode.test.ts | 20 +- nym-api/tests/src/config/config.yaml | 18 -- nym-api/tests/src/config/configHandler.ts | 64 ----- .../src/endpoints/abstracts/APIClient.ts | 10 +- nym-api/tests/src/restClient/RestClient.ts | 243 ------------------ .../functional_test/gateways/gateway.test.ts | 2 +- 11 files changed, 51 insertions(+), 366 deletions(-) delete mode 100644 nym-api/tests/src/config/config.yaml delete mode 100644 nym-api/tests/src/config/configHandler.ts delete mode 100644 nym-api/tests/src/restClient/RestClient.ts diff --git a/common/api-test-utils/config/config.yaml b/common/api-test-utils/config/config.yaml index 246a674d41..5607678e1c 100644 --- a/common/api-test-utils/config/config.yaml +++ b/common/api-test-utils/config/config.yaml @@ -4,9 +4,15 @@ common: Content-Type: application/json sandbox: api_base_url: https://sandbox-nym-api1.nymtech.net/api/v1 + mix_id: 7 + identity_key: 2wEzyW6Pihk19xbCK9kB81vYuDFUvTzTAr77arvxdfSg + gateway_identity: HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua log_level: error time_zone: utc prod: api_base_url: https://validator.nymtech.net/api/v1 + mix_id: 730 + mixnode_identity: 3pMCJswCyA19MGYWGDWT5fBk2M8ybSZGXttyAoNY5gBB + gateway_identity: 2BuMSfMW3zpeAjKXyKLhmY4QW1DXurrtSPEJ6CjX3SEh log_level: error - time_zone: utc + time_zone: utc \ No newline at end of file diff --git a/common/api-test-utils/config/configHandler.ts b/common/api-test-utils/config/configHandler.ts index c90f5d4cda..69164f940a 100644 --- a/common/api-test-utils/config/configHandler.ts +++ b/common/api-test-utils/config/configHandler.ts @@ -40,9 +40,8 @@ class ConfigHandler { private setCommonConfig(): void { try { const baseWorkingDirectory = __dirname; - console.log(`Base Working Directory: ${baseWorkingDirectory}`); this.commonConfig = YAML.parse( - readFileSync("/Users/benedetta/nym/workspace/nym/common/api-test-utils/config/config.yaml", "utf8"), + readFileSync(baseWorkingDirectory + "/config.yaml", "utf8"), ).common; } catch (error) { throw Error(`Error reading common config: (${error})`); @@ -52,8 +51,9 @@ class ConfigHandler { private setEnvironmentConfig(environment: string): void { this.ensureEnvironmentIsValid(environment); try { + const baseWorkingDirectory = __dirname; this.environmentConfig = YAML.parse( - readFileSync("/Users/benedetta/nym/workspace/nym/common/api-test-utils/config/config.yaml", "utf8"), + readFileSync(baseWorkingDirectory + "/config.yaml", "utf8"), )[environment]; } catch (error) { console.log("fadsfasdfasdfsdfsa") @@ -62,9 +62,10 @@ class ConfigHandler { } public getEnvironmentConfig(environment: string): any { + const baseWorkingDirectory = __dirname; return ( this.environmentConfig || - YAML.parse(readFileSync("api-test-utils/config/config.yaml", "utf8"))[environment] + YAML.parse(readFileSync(baseWorkingDirectory + "/config.yaml", "utf8"))[environment] ); } diff --git a/envs/qa.env b/envs/qa.env index 82bb8b9d3c..65b00aa281 100644 --- a/envs/qa.env +++ b/envs/qa.env @@ -1,26 +1,29 @@ CONFIGURED=true -NETWORK_NAME=qa - RUST_LOG=info RUST_BACKTRACE=1 - +NETWORK_NAME=qa BECH32_PREFIX=n MIX_DENOM=unym MIX_DENOM_DISPLAY=nym STAKE_DENOM=unyx STAKE_DENOM_DISPLAY=nyx DENOMS_EXPONENT=6 -MIXNET_CONTRACT_ADDRESS=n1rjzps6qrmdqmf0xz4cn4x4rcmqeqzq6hnzqg4wcvd0r2lyasdq5sepn5s8 -VESTING_CONTRACT_ADDRESS=n1xr3rq8yvd7qplsw5yx90ftsr2zdhg4e9z60h5duusgxpv72hud3sjkxkav -COCONUT_BANDWIDTH_CONTRACT_ADDRESS=n1ghd753shjuwexxywmgs4xz7x2q732vcn7ty4yw -GROUP_CONTRACT_ADDRESS=n1rw8fw2mpcpzzq3jpa4e52ufawnmj5a4u68p35umvgskewuw0nlzsaa5w4m -MULTISIG_CONTRACT_ADDRESS=n17p9rzwnnfxcjp32un9ug7yhhzgtkhvl988qccs -COCONUT_DKG_CONTRACT_ADDRESS=n17p9rzwnnfxcjp32un9ug7yhhzgtkhvl988qccs -EPHEMERA_CONTRACT_ADDRESS=n1fc7nakzuyfn2qzkclafcsc54asamnclg064962lwne40w2lq558qftzjza -REWARDING_VALIDATOR_ADDRESS=n1tfzd4qz3a45u8p4mr5zmzv66457uwjgcl05jdq -STATISTICS_SERVICE_DOMAIN_ADDRESS="http://0.0.0.0" -NYXD="https://qa-validator.nymtech.net" -NYM_API="https://qa-validator-api.nymtech.net/api/" + +MIXNET_CONTRACT_ADDRESS=n14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sjyvg3g +VESTING_CONTRACT_ADDRESS=n1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrq73f2nw +BANDWIDTH_CLAIM_CONTRACT_ADDRESS=n19lc9u84cz0yz3fww5283nucc9yvr8gsjmgeul0 +COCONUT_BANDWIDTH_CONTRACT_ADDRESS=n1w798gp0zqv3s9hjl3jlnwxtwhykga6rn93p46q2crsdqhaj3y4gs68f74j +GROUP_CONTRACT_ADDRESS=n1sthrn5ep8ls5vzz8f9gp89khhmedahhdqd244dh9uqzk3hx2pzrsvf7zgk +MULTISIG_CONTRACT_ADDRESS=n1sr06m8yqg0wzqqyqvzvp5t07dj4nevx9u8qc7j4qa72qu8e3ct8qledthy +COCONUT_DKG_CONTRACT_ADDRESS=n1udfs22xpxle475m2nz7u47jfa3vngncdegmczwwdx00cmetypa3s7uyuqn +REWARDING_VALIDATOR_ADDRESS=n1rfvpsynktze6wvn6ldskj8xgwfzzk5v6pnff39 +NAME_SERVICE_CONTRACT_ADDRESS=n1qum2tr7hh4y7ruzew68c64myjec0dq2s2njf6waja5t0w879lutqadamme +SERVICE_PROVIDER_DIRECTORY_CONTRACT_ADDRESS=n13ehuhysn5mqjeaheeuew2gjs785f6k7jm8vfsqg3jhtpkwppcmzq6m2hmz + +STATISTICS_SERVICE_DOMAIN_ADDRESS="https://mainnet-stats.nymte.ch:8090" +EXPLORER_API=https://qa-network-explorer.qa.nymte.ch/api +NYXD="https://qa-validator.qa.nymte.ch/" +NYM_API="https://qa-nym-api.qa.nymte.ch/api" DKG_TIME_CONFIGURATION="600,300,300,60,60,1209600" diff --git a/nym-api/tests/functional_test/contract_cache/mixnode/contract-cache-mixnode.test.ts b/nym-api/tests/functional_test/contract_cache/mixnode/contract-cache-mixnode.test.ts index 6182f8a63c..b486aa862f 100644 --- a/nym-api/tests/functional_test/contract_cache/mixnode/contract-cache-mixnode.test.ts +++ b/nym-api/tests/functional_test/contract_cache/mixnode/contract-cache-mixnode.test.ts @@ -1,5 +1,5 @@ import ContractCache from "../../../src/endpoints/ContractCache"; -import ConfigHandler from "../../../src/config/configHandler"; +import ConfigHandler from "../../../../../common/api-test-utils/config/configHandler" let contract: ContractCache; let config: ConfigHandler; diff --git a/nym-api/tests/functional_test/status/status-gateway.test.ts b/nym-api/tests/functional_test/status/status-gateway.test.ts index 71f2becc6d..af1c74b923 100644 --- a/nym-api/tests/functional_test/status/status-gateway.test.ts +++ b/nym-api/tests/functional_test/status/status-gateway.test.ts @@ -1,5 +1,5 @@ import Status from "../../src/endpoints/Status"; -import ConfigHandler from "../../src/config/configHandler"; +import ConfigHandler from "../../../../common/api-test-utils/config/configHandler" let status: Status; let config: ConfigHandler; @@ -29,7 +29,7 @@ describe("Get gateway data", (): void => { }); it("Get a gateway history", async (): Promise => { - const identity_key = config.environmnetConfig.gateway_identity; + const identity_key = config.environmentConfig.gateway_identity; const response = await status.getGatewayHistory(identity_key); if ("identity" in response) { @@ -47,14 +47,14 @@ describe("Get gateway data", (): void => { }); it("Get gateway core status count", async (): Promise => { - const identity_key = config.environmnetConfig.gateway_identity; + const identity_key = config.environmentConfig.gateway_identity; const response = await status.getGatewayCoreCount(identity_key); expect(identity_key).toStrictEqual(response.identity); expect(typeof response.count).toBe("number"); }); it("Get gateway average uptime", async (): Promise => { - const identity_key = config.environmnetConfig.gateway_identity; + const identity_key = config.environmentConfig.gateway_identity; const response = await status.getGatewayAverageUptime(identity_key); if ("mix_id" in response) { expect(identity_key).toStrictEqual(response.identity); @@ -67,7 +67,7 @@ describe("Get gateway data", (): void => { }); it("Get a gateway status report", async (): Promise => { - const identity_key = config.environmnetConfig.gateway_identity; + const identity_key = config.environmentConfig.gateway_identity; const response = await status.getGatewayStatusReport(identity_key); if ("mix_id" in response) { expect(identity_key).toStrictEqual(response.identity); diff --git a/nym-api/tests/functional_test/status/status-mixnode.test.ts b/nym-api/tests/functional_test/status/status-mixnode.test.ts index 6590843f02..e5046510fb 100644 --- a/nym-api/tests/functional_test/status/status-mixnode.test.ts +++ b/nym-api/tests/functional_test/status/status-mixnode.test.ts @@ -1,5 +1,5 @@ import Status from "../../src/endpoints/Status"; -import ConfigHandler from "../../src/config/configHandler"; +import ConfigHandler from "../../../../common/api-test-utils/config/configHandler" let status: Status; let config: ConfigHandler; @@ -11,7 +11,7 @@ describe("Get mixnode data", (): void => { }); it("Get a mixnode report", async (): Promise => { - const identity_key = config.environmnetConfig.mix_id; + const identity_key = config.environmentConfig.mix_id; const response = await status.getMixnodeStatusReport(identity_key); if ("mix_id" in response) { expect(typeof response.last_day).toBe("number"); @@ -22,7 +22,7 @@ describe("Get mixnode data", (): void => { }); it("Get a mixnode stake saturation", async (): Promise => { - const identity_key = config.environmnetConfig.mix_id; + const identity_key = config.environmentConfig.mix_id; const response = await status.getMixnodeStakeSaturation(identity_key); if ("saturation" in response) { expect(typeof response.as_at).toBe("number"); @@ -34,7 +34,7 @@ describe("Get mixnode data", (): void => { }); it("Get a mixnode average uptime", async (): Promise => { - const identity_key = config.environmnetConfig.mix_id; + const identity_key = config.environmentConfig.mix_id; const response = await status.getMixnodeAverageUptime(identity_key); if ("mix_id" in response) { expect(identity_key).toStrictEqual(response.mix_id); @@ -45,7 +45,7 @@ describe("Get mixnode data", (): void => { }); it("Get a mixnode history", async (): Promise => { - const identity_key = config.environmnetConfig.mix_id; + const identity_key = config.environmentConfig.mix_id; const response = await status.getMixnodeHistory(identity_key); if ("mix_id" in response) { response.history.forEach((x) => { @@ -62,14 +62,14 @@ describe("Get mixnode data", (): void => { }); it("Get a mixnode core count", async (): Promise => { - const identity_key = config.environmnetConfig.mix_id; + const identity_key = config.environmentConfig.mix_id; const response = await status.getMixnodeCoreCount(identity_key); expect(identity_key).toStrictEqual(response.mix_id); expect(typeof response.count).toBe("number"); }); it("Get a mixnode reward estimation", async (): Promise => { - const identity_key = config.environmnetConfig.mix_id; + const identity_key = config.environmentConfig.mix_id; const response = await status.getMixnodeRewardComputation(identity_key); if ("estimation" in response) { expect(response.reward_params.interval.sybil_resistance).toStrictEqual( @@ -83,7 +83,7 @@ describe("Get mixnode data", (): void => { }); it("Get a mixnode inclusion probability", async (): Promise => { - const identity_key = config.environmnetConfig.mix_id; + const identity_key = config.environmentConfig.mix_id; const response = await status.getMixnodeInclusionProbability(identity_key); if ("mix_id" in response) { expect(typeof response.in_active).toBe("string"); @@ -119,7 +119,7 @@ describe("Get mixnode data", (): void => { }); it("Get a mixnode status", async (): Promise => { - const identity_key = config.environmnetConfig.mix_id; + const identity_key = config.environmentConfig.mix_id; const response = await status.getMixnodeStatus(identity_key); const unfiltered_mixnodes_response = await status.getUnfilteredMixnodes(); const mixnode = unfiltered_mixnodes_response.find( @@ -155,7 +155,7 @@ describe("Get mixnode data", (): void => { }); it("with correct data", async (): Promise => { - const mix_id = config.environmnetConfig.mix_id; + const mix_id = config.environmentConfig.mix_id; const response = await status.sendMixnodeRewardEstimatedComputation( mix_id ); diff --git a/nym-api/tests/src/config/config.yaml b/nym-api/tests/src/config/config.yaml deleted file mode 100644 index 85603a7efa..0000000000 --- a/nym-api/tests/src/config/config.yaml +++ /dev/null @@ -1,18 +0,0 @@ -common: - request_headers: - Accept: application/json - Content-Type: application/json -sandbox: - api_base_url: https://sandbox-nym-api1.nymtech.net/api/v1 - mix_id: 7 - identity_key: 2wEzyW6Pihk19xbCK9kB81vYuDFUvTzTAr77arvxdfSg - gateway_identity: HjNEDJuotWV8VD4ufeA1jeheTnfNJ7Jorevp57hgaZua - log_level: error - time_zone: utc -prod: - api_base_url: https://validator.nymtech.net/api/v1 - mix_id: 730 - mixnode_identity: 3pMCJswCyA19MGYWGDWT5fBk2M8ybSZGXttyAoNY5gBB - gateway_identity: 2BuMSfMW3zpeAjKXyKLhmY4QW1DXurrtSPEJ6CjX3SEh - log_level: error - time_zone: utc diff --git a/nym-api/tests/src/config/configHandler.ts b/nym-api/tests/src/config/configHandler.ts deleted file mode 100644 index 1d29402012..0000000000 --- a/nym-api/tests/src/config/configHandler.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { readFileSync } from "fs"; -import { TLogLevelName } from "tslog"; - -import YAML from "yaml"; - -class ConfigHandler { - private static instance: ConfigHandler; - - private validEnvironments = ["sandbox", "prod"]; - - public commonConfig: { request_headers: object }; - - public environment: string; - - public environmnetConfig: { - log_level: TLogLevelName; - time_zone: string; - api_base_url: string; - mix_id: number; - identity_key: string; - gateway_identity: string; - }; - - private constructor() { - this.setCommonConfig(); - this.setEnvironmentConfig(process.env.TEST_ENV || "sandbox" || "prod"); - } - - public static getInstance(): ConfigHandler { - if (!ConfigHandler.instance) { - ConfigHandler.instance = new ConfigHandler(); - } - return ConfigHandler.instance; - } - - private setCommonConfig(): void { - try { - this.commonConfig = YAML.parse( - readFileSync("src/config/config.yaml", "utf8") - ).common; - } catch (error) { - throw Error(`Error reading common config: (${error})`); - } - } - - private setEnvironmentConfig(environment: string): void { - this.ensureEnvironmentIsValid(environment); - try { - this.environmnetConfig = YAML.parse( - readFileSync("src/config/config.yaml", "utf8") - )[environment]; - } catch (error) { - throw Error(`Error reading environment config: (${error})`); - } - } - - private ensureEnvironmentIsValid(environment: string): void { - if (this.validEnvironments.indexOf(environment) === -1) { - throw Error(`Config environment is not valid: "${environment}"`); - } - } -} - -export default ConfigHandler; diff --git a/nym-api/tests/src/endpoints/abstracts/APIClient.ts b/nym-api/tests/src/endpoints/abstracts/APIClient.ts index 93249de2ff..d440fd5695 100644 --- a/nym-api/tests/src/endpoints/abstracts/APIClient.ts +++ b/nym-api/tests/src/endpoints/abstracts/APIClient.ts @@ -1,10 +1,10 @@ import { Logger } from "tslog"; -import ConfigHandler from "../../config/configHandler"; -import { RestClient } from "../../restClient/RestClient"; +import ConfigHandler from "../../../../../common/api-test-utils/config/configHandler"; +import { RestClient } from "../../../../../common/api-test-utils/restClient/RestClient"; export abstract class APIClient { protected constructor(serviceUrl: string) { - const baseUrl: string = this.config.environmnetConfig.api_base_url; + const baseUrl: string = this.config.environmentConfig.api_base_url; this.url = baseUrl + serviceUrl; this.restClient = new RestClient(this.url); this.serviceName = this.constructor.toString().match(/\w+/g)[1]; @@ -16,9 +16,9 @@ export abstract class APIClient { protected config = ConfigHandler.getInstance(); protected log: Logger = new Logger({ - minLevel: this.config.environmnetConfig.log_level, + minLevel: this.config.environmentConfig.log_level, dateTimeTimezone: - this.config.environmnetConfig.time_zone || + this.config.environmentConfig.time_zone || Intl.DateTimeFormat().resolvedOptions().timeZone, }); diff --git a/nym-api/tests/src/restClient/RestClient.ts b/nym-api/tests/src/restClient/RestClient.ts deleted file mode 100644 index 7992d7afa7..0000000000 --- a/nym-api/tests/src/restClient/RestClient.ts +++ /dev/null @@ -1,243 +0,0 @@ -import axios, { - AxiosInstance, - AxiosRequestConfig, - AxiosResponse, - Method, -} from "axios"; -import { Logger } from "tslog"; -import { stringify } from "yaml"; - -import https from "https"; - -import ConfigHandler from "../config/configHandler"; - -const config = ConfigHandler.getInstance(); -const log = new Logger({ - minLevel: config.environmnetConfig.log_level, - dateTimeTimezone: - config.environmnetConfig.time_zone || - Intl.DateTimeFormat().resolvedOptions().timeZone, -}); - -function isSet(property): boolean { - return property !== undefined && property !== null; -} - -export class RestClient { - private static authToken: string; - - private axiosInstance: AxiosInstance; - - constructor(baseUrl: string) { - this.axiosInstance = axios.create({ baseURL: baseUrl }); - } - - private httpsAgent = new https.Agent({ - rejectUnauthorized: false, - }); - - // Not returning an actual auth token for this example project. - // Just showing how it can be done! - static async getToken(requestHeaders: object) { - requestHeaders["Authorization"] = `asdf`; - } - - public async callEndpoint({ - route, - method, - authToken, - headers, - data, - additionalConfigs, - params, - }: IAxiosCallEndpointArgs): Promise { - let response; - let responseLog = "Response: "; - let requestHeaders = headers; - - // if headers are not passed in, use the default headers - if (requestHeaders == undefined) { - requestHeaders = { ...config.commonConfig.request_headers }; - } - - // if authToken is passed in, add it to the request headers - if (authToken !== undefined) { - requestHeaders = { - ...requestHeaders, - ...{ - Authorization: `Bearer ${authToken}`, - }, - }; - } - - // if we have not set the auth headers yet, set them - else if (!requestHeaders.Authorization) { - await RestClient.getToken(requestHeaders); - } - - log.debug( - RestClient.prepareLogRecord({ - route, - method, - headers: requestHeaders, - data, - additionalConfigs, - params, - }) - ); - - await this.axiosInstance - .request({ - url: route, - method, - data, - headers: requestHeaders, - httpsAgent: this.httpsAgent, - params, - ...additionalConfigs, - }) - .then((res) => { - response = res; - responseLog = ` Status = ${res.status} ${res.statusText}`; - }) - .catch((error) => { - response = error.response; - if (response === undefined) - responseLog = ` Something wrong happened, did not get proper error from server! (${error.message})`; - else - responseLog = ` Status = ${response.status} ${response.statusText}, ${error.message}`; - }); - log.debug(responseLog); - return response; - } - - public async sendPost({ - route, - authToken, - data, - params, - headers, - additionalConfigs, - }: IAxiosHttpRequestArgs): Promise { - return this.callEndpoint({ - route, - method: "POST", - authToken, - data, - params, - headers, - additionalConfigs, - }); - } - - public async sendGet({ - route, - authToken, - params, - headers, - additionalConfigs, - }: IAxiosHttpRequestArgs): Promise { - return this.callEndpoint({ - route, - method: "GET", - authToken, - params, - headers, - additionalConfigs, - }); - } - - public async sendDelete({ - route, - authToken, - params, - headers, - additionalConfigs, - }: IAxiosHttpRequestArgs): Promise { - return this.callEndpoint({ - route, - method: "DELETE", - authToken, - params, - headers, - additionalConfigs, - }); - } - - public async sendPatch({ - route, - authToken, - data, - headers, - additionalConfigs, - }: IAxiosHttpRequestArgs): Promise { - return this.callEndpoint({ - route, - method: "PATCH", - authToken, - data, - headers, - additionalConfigs, - }); - } - - public async sendPut({ - route, - authToken, - data, - headers, - additionalConfigs, - }: IAxiosHttpRequestArgs): Promise { - return this.callEndpoint({ - route, - method: "PUT", - authToken, - data, - headers, - additionalConfigs, - }); - } - - private static prepareLogRecord({ - route, - method, - headers, - data, - additionalConfigs, - params, - }: IAxiosCallEndpointArgs): string { - let logRecord = `Request: ${method} ${route}`; - if (isSet(headers)) - logRecord = `${logRecord}\nHeaders: ${stringify(headers)}`; - - if (isSet(params)) logRecord = `${logRecord}\nParams: ${stringify(params)}`; - - if (isSet(additionalConfigs)) { - logRecord = `${logRecord}\nAdditional Configuration: ${stringify( - additionalConfigs - )}`; - } - - if (isSet(data)) { - const jsonData = stringify(data); - // We don't want to log anything that isn't json data - logRecord = `${logRecord}\nData: ${ - jsonData === undefined ? "Some data, not JSON!" : jsonData - }`; - } - return logRecord; - } -} - -export interface IAxiosHttpRequestArgs { - route: string; - authToken?: string; - data?: object; - params?: object; - headers?: any; - additionalConfigs?: AxiosRequestConfig; -} - -export interface IAxiosCallEndpointArgs extends IAxiosHttpRequestArgs { - method: Method; -} diff --git a/nym-node/tests/functional_test/gateways/gateway.test.ts b/nym-node/tests/functional_test/gateways/gateway.test.ts index 8a94d4b975..73e2837111 100644 --- a/nym-node/tests/functional_test/gateways/gateway.test.ts +++ b/nym-node/tests/functional_test/gateways/gateway.test.ts @@ -7,7 +7,7 @@ describe("Get gateway related info", (): void => { beforeAll(async (): Promise => { try { gatewayHosts = await getGatewayIPAddresses(); - // console.log(gatewayHosts); + console.log(gatewayHosts); } catch (error) { throw new Error(`Error fetching gateway IP addresses: ${error.message}`); }