11import { HmacSHA1 } from "crypto-js" ;
22import { getRequestFn , LogService , MatrixClient , MemoryStorageProvider , PantalaimonClient } from "matrix-bot-sdk" ;
3- import config from '../../src/config' ;
43
54const REGISTRATION_ATTEMPTS = 10 ;
65const REGISTRATION_RETRY_BASE_DELAY_MS = 100 ;
@@ -16,8 +15,8 @@ const REGISTRATION_RETRY_BASE_DELAY_MS = 100;
1615 * @param admin True to make the user an admin, false otherwise.
1716 * @returns The response from synapse.
1817 */
19- export async function registerUser ( username : string , displayname : string , password : string , admin : boolean ) : Promise < void > {
20- let registerUrl = `${ config . homeserverUrl } /_synapse/admin/v1/register`
18+ export async function registerUser ( homeserver : string , username : string , displayname : string , password : string , admin : boolean ) : Promise < void > {
19+ let registerUrl = `${ homeserver } /_synapse/admin/v1/register`
2120 const data : { nonce : string } = await new Promise ( ( resolve , reject ) => {
2221 getRequestFn ( ) ( { uri : registerUrl , method : "GET" , timeout : 60000 } , ( error : any , response : any , resBody : any ) => {
2322 error ? reject ( error ) : resolve ( JSON . parse ( resBody ) )
@@ -81,7 +80,7 @@ export type RegistrationOptions = {
8180 *
8281 * @returns A string that is both the username and password of a new user.
8382 */
84- async function registerNewTestUser ( options : RegistrationOptions ) {
83+ async function registerNewTestUser ( homeserver : string , options : RegistrationOptions ) {
8584 do {
8685 let username ;
8786 if ( "exact" in options . name ) {
@@ -90,7 +89,7 @@ async function registerNewTestUser(options: RegistrationOptions) {
9089 username = `mjolnir-test-user-${ options . name . contains } ${ Math . floor ( Math . random ( ) * 100000 ) } `
9190 }
9291 try {
93- await registerUser ( username , username , username , Boolean ( options . isAdmin ) ) ;
92+ await registerUser ( homeserver , username , username , username , Boolean ( options . isAdmin ) ) ;
9493 return username ;
9594 } catch ( e ) {
9695 if ( e ?. body ?. errcode === 'M_USER_IN_USE' ) {
@@ -113,13 +112,13 @@ async function registerNewTestUser(options: RegistrationOptions) {
113112 *
114113 * @returns A new `MatrixClient` session for a unique test user.
115114 */
116- export async function newTestUser ( options : RegistrationOptions ) : Promise < MatrixClient > {
117- const username = await registerNewTestUser ( options ) ;
118- const pantalaimon = new PantalaimonClient ( config . homeserverUrl , new MemoryStorageProvider ( ) ) ;
115+ export async function newTestUser ( homeserver : string , options : RegistrationOptions ) : Promise < MatrixClient > {
116+ const username = await registerNewTestUser ( homeserver , options ) ;
117+ const pantalaimon = new PantalaimonClient ( homeserver , new MemoryStorageProvider ( ) ) ;
119118 const client = await pantalaimon . createClientWithCredentials ( username , username ) ;
120119 if ( ! options . isThrottled ) {
121120 let userId = await client . getUserId ( ) ;
122- await overrideRatelimitForUser ( userId ) ;
121+ await overrideRatelimitForUser ( homeserver , userId ) ;
123122 }
124123 return client ;
125124}
@@ -130,20 +129,20 @@ let _globalAdminUser: MatrixClient;
130129 * Get a client that can perform synapse admin API actions.
131130 * @returns A client logged in with an admin user.
132131 */
133- async function getGlobalAdminUser ( ) : Promise < MatrixClient > {
132+ async function getGlobalAdminUser ( homeserver : string ) : Promise < MatrixClient > {
134133 // Initialize global admin user if needed.
135134 if ( ! _globalAdminUser ) {
136135 const USERNAME = "mjolnir-test-internal-admin-user" ;
137136 try {
138- await registerUser ( USERNAME , USERNAME , USERNAME , true ) ;
137+ await registerUser ( homeserver , USERNAME , USERNAME , USERNAME , true ) ;
139138 } catch ( e ) {
140139 if ( e . isAxiosError && e ?. response ?. data ?. errcode === 'M_USER_IN_USE' ) {
141140 // Then we've already registered the user in a previous run and that is ok.
142141 } else {
143142 throw e ;
144143 }
145144 }
146- _globalAdminUser = await new PantalaimonClient ( config . homeserverUrl , new MemoryStorageProvider ( ) ) . createClientWithCredentials ( USERNAME , USERNAME ) ;
145+ _globalAdminUser = await new PantalaimonClient ( homeserver , new MemoryStorageProvider ( ) ) . createClientWithCredentials ( USERNAME , USERNAME ) ;
147146 }
148147 return _globalAdminUser ;
149148}
@@ -152,8 +151,8 @@ async function getGlobalAdminUser(): Promise<MatrixClient> {
152151 * Disable ratelimiting for this user in Synapse.
153152 * @param userId The user to disable ratelimiting for, has to include both the server part and local part.
154153 */
155- export async function overrideRatelimitForUser ( userId : string ) {
156- await ( await getGlobalAdminUser ( ) ) . doRequest ( "POST" , `/_synapse/admin/v1/users/${ userId } /override_ratelimit` , null , {
154+ export async function overrideRatelimitForUser ( homeserver : string , userId : string ) {
155+ await ( await getGlobalAdminUser ( homeserver ) ) . doRequest ( "POST" , `/_synapse/admin/v1/users/${ userId } /override_ratelimit` , null , {
157156 "messages_per_second" : 0 ,
158157 "burst_count" : 0
159158 } ) ;
@@ -163,8 +162,8 @@ export async function overrideRatelimitForUser(userId: string) {
163162 * Put back the default ratelimiting for this user in Synapse.
164163 * @param userId The user to use default ratelimiting for, has to include both the server part and local part.
165164 */
166- export async function resetRatelimitForUser ( userId : string ) {
167- await ( await getGlobalAdminUser ( ) ) . doRequest ( "DELETE" , `/_synapse/admin/v1/users/${ userId } /override_ratelimit` , null ) ;
165+ export async function resetRatelimitForUser ( homeserver : string , userId : string ) {
166+ await ( await getGlobalAdminUser ( homeserver ) ) . doRequest ( "DELETE" , `/_synapse/admin/v1/users/${ userId } /override_ratelimit` , null ) ;
168167}
169168
170169
0 commit comments