diff --git a/package.json b/package.json index 0fe933b..1f5ee70 100644 --- a/package.json +++ b/package.json @@ -16,12 +16,10 @@ "test:manual": "NODE_ENV=harness ts-node test/integration/manualLaunchScript.ts" }, "devDependencies": { - "@types/axios": "^0.14.0", "@types/crypto-js": "^4.0.2", "@types/jsdom": "^16.2.11", "@types/mocha": "^9.0.0", "@types/node": "^16.7.10", - "axios": "^0.21.4", "crypto-js": "^4.1.1", "eslint": "^7.32", "expect": "^27.0.6", diff --git a/test/integration/clientHelper.ts b/test/integration/clientHelper.ts index 502397d..cbe032c 100644 --- a/test/integration/clientHelper.ts +++ b/test/integration/clientHelper.ts @@ -1,6 +1,5 @@ -import axios from "axios"; import { HmacSHA1 } from "crypto-js"; -import { LogService, MatrixClient, MemoryStorageProvider, PantalaimonClient } from "matrix-bot-sdk"; +import { getRequestFn, LogService, MatrixClient, MemoryStorageProvider, PantalaimonClient } from "matrix-bot-sdk"; import config from "../../src/config"; const REGISTRATION_ATTEMPTS = 10; @@ -17,24 +16,38 @@ const REGISTRATION_RETRY_BASE_DELAY_MS = 100; * @param admin True to make the user an admin, false otherwise. * @returns The response from synapse. */ -export async function registerUser(username: string, displayname: string, password: string, admin: boolean) { - const registerUrl = `${config.homeserverUrl}/_synapse/admin/v1/register`; +export async function registerUser(username: string, displayname: string, password: string, admin: boolean): Promise { + let registerUrl = `${config.homeserverUrl}/_synapse/admin/v1/register` + const data: {nonce: string} = await new Promise((resolve, reject) => { + getRequestFn()({uri: registerUrl, method: "GET", timeout: 60000}, (error, response, resBody) => { + error ? reject(error) : resolve(JSON.parse(resBody)) + }); + }); + const nonce = data.nonce!; + let mac = HmacSHA1(`${nonce}\0${username}\0${password}\0${admin ? 'admin' : 'notadmin'}`, 'REGISTRATION_SHARED_SECRET'); for (let i = 1; i <= REGISTRATION_ATTEMPTS; ++i) { try { - const { data: { nonce } } = await axios.get(registerUrl); - const mac = HmacSHA1(`${nonce}\0${username}\0${password}\0${admin ? 'admin' : 'notadmin'}`, 'REGISTRATION_SHARED_SECRET'); - return await axios.post(registerUrl, { - nonce, - username, - displayname, - password, - admin, - mac: mac.toString() + const params = { + uri: registerUrl, + method: "POST", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify({ + nonce, + username, + displayname, + password, + admin, + mac: mac.toString() + }), + timeout: 60000 + } + return await new Promise((resolve, reject) => { + getRequestFn()(params, error => error ? reject(error) : resolve()); }); } catch (ex) { // In case of timeout or throttling, backoff and retry. if (ex?.code === 'ESOCKETTIMEDOUT' || ex?.code === 'ETIMEDOUT' - || ex?.response?.data?.errcode === 'M_LIMIT_EXCEEDED') { + || ex?.body?.errcode === 'M_LIMIT_EXCEEDED') { await new Promise(resolve => setTimeout(resolve, REGISTRATION_RETRY_BASE_DELAY_MS * i * i)); continue; } @@ -80,7 +93,7 @@ async function registerNewTestUser(options: RegistrationOptions) { await registerUser(username, username, username, options.isAdmin); return username; } catch (e) { - if (e.isAxiosError && e?.response?.data?.errcode === 'M_USER_IN_USE') { + if (e?.body?.errcode === 'M_USER_IN_USE') { if ("exact" in options.name) { LogService.debug("test/clientHelper", `${username} already registered, reusing`); return username; diff --git a/test/integration/mjolnirSetupUtils.ts b/test/integration/mjolnirSetupUtils.ts index f6d7849..bf7c924 100644 --- a/test/integration/mjolnirSetupUtils.ts +++ b/test/integration/mjolnirSetupUtils.ts @@ -52,12 +52,9 @@ async function configureMjolnir() { try { await registerUser(config.pantalaimon.username, config.pantalaimon.username, config.pantalaimon.password, true) } catch (e) { - if (e.isAxiosError) { - console.log('Received error while registering', e.response.data || e.response); - if (e.response.data && e.response.data.errcode === 'M_USER_IN_USE') { - console.log(`${config.pantalaimon.username} already registered, skipping`); - return; - } + if (e?.body?.errcode === 'M_USER_IN_USE') { + console.log(`${config.pantalaimon.username} already registered, skipping`); + return; } throw e; }; diff --git a/test/integration/policyConsumptionTest.ts b/test/integration/policyConsumptionTest.ts index 1b6a405..de6d796 100644 --- a/test/integration/policyConsumptionTest.ts +++ b/test/integration/policyConsumptionTest.ts @@ -1,17 +1,24 @@ import { strict as assert } from "assert"; import { newTestUser } from "./clientHelper"; -import { getMessagesByUserIn } from "../../src/utils"; import config from "../../src/config"; -import axios from "axios"; -import { LogService } from "matrix-bot-sdk"; +import { getRequestFn, LogService, MatrixClient } from "matrix-bot-sdk"; import { createBanList, getFirstReaction } from "./commands/commandUtils"; /** * Get a copy of the rules from the ruleserver. */ -async function currentRules() { - return await (await axios.get(`http://${config.web.address}:${config.web.port}/api/1/ruleserver/updates/`)).data +async function currentRules(): Promise<{ start: object, stop: object, since: string }> { + return await new Promise((resolve, reject) => getRequestFn()({ + uri: `http://${config.web.address}:${config.web.port}/api/1/ruleserver/updates/`, + method: "GET" + }, (error, response, body) => { + if (error) { + reject(error) + } else { + resolve(JSON.parse(body)) + } + })); } /** diff --git a/yarn.lock b/yarn.lock index 67f9055..e517c4f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -83,13 +83,6 @@ resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82" integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw== -"@types/axios@^0.14.0": - version "0.14.0" - resolved "https://registry.yarnpkg.com/@types/axios/-/axios-0.14.0.tgz#ec2300fbe7d7dddd7eb9d3abf87999964cafce46" - integrity sha1-7CMA++fX3d1+udOr+HmZlkyvzkY= - dependencies: - axios "*" - "@types/body-parser@*": version "1.19.1" resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.1.tgz#0c0174c42a7d017b818303d4b5d969cb0b75929c" @@ -386,13 +379,6 @@ aws4@^1.8.0: resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59" integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA== -axios@*, axios@^0.21.4: - version "0.21.4" - resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575" - integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg== - dependencies: - follow-redirects "^1.14.0" - balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" @@ -1123,11 +1109,6 @@ flatted@^3.1.0: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.2.tgz#64bfed5cb68fe3ca78b3eb214ad97b63bedce561" integrity sha512-JaTY/wtrcSyvXJl4IMFHPKyFur1sE9AUqc0QnhOaJ0CxHtAoIV8pYDzeEfAaNEtGkOfq4gr3LBFmdXW5mOQFnA== -follow-redirects@^1.14.0: - version "1.14.7" - resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.7.tgz#2004c02eb9436eee9a21446a6477debf17e81685" - integrity sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ== - forever-agent@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"