mtcute/e2e/node/ts/tests/01.auth.ts

85 lines
2.3 KiB
TypeScript
Raw Normal View History

import { tl, User } from '@mtcute/core'
import { BaseTelegramClient, TelegramClient } from '@mtcute/core/client.js'
import { expect } from 'chai'
import { describe, it } from 'mocha'
import { getApiParams } from '../utils.js'
function getAccountId() {
return Math.floor(Math.random() * 10000)
.toString()
.padStart(4, '0')
}
2024-06-09 19:04:09 +03:00
async function authorizeInDc(dc: number, base: BaseTelegramClient) {
const tg = new TelegramClient({ client: base })
2024-06-09 19:04:09 +03:00
while (true) {
2024-02-03 13:26:21 +03:00
await base.mt.storage.load()
await base.storage.clear(true)
2024-06-09 19:04:09 +03:00
const phone = `99966${dc}${getAccountId()}`
let user
2024-06-09 19:04:09 +03:00
try {
2024-06-26 00:09:40 +03:00
const sentCode = await tg.sendCode({ phone })
2024-06-09 19:04:09 +03:00
let auth = await tg.call({
_: 'auth.signIn',
phoneNumber: phone,
phoneCode: `${dc}${dc}${dc}${dc}${dc}`,
phoneCodeHash: sentCode.phoneCodeHash,
})
if (auth._ === 'auth.authorizationSignUpRequired') {
auth = await tg.call({
_: 'auth.signUp',
phoneNumber: phone,
phoneCodeHash: sentCode.phoneCodeHash,
firstName: 'mtcute e2e',
lastName: '',
})
2024-06-09 19:04:09 +03:00
if (auth._ !== 'auth.authorization') {
throw new Error('Unexpected response')
2024-04-23 23:03:00 +03:00
}
}
2024-06-09 19:04:09 +03:00
await tg.notifyLoggedIn(auth)
user = new User(auth.user)
} catch (e) {
if (tl.RpcError.is(e, 'SESSION_PASSWORD_NEEDED') || tl.RpcError.is(e, 'PHONE_NUMBER_FLOOD')) {
2024-06-09 19:04:09 +03:00
// retry with another number
await tg.close()
continue
}
2024-06-09 19:04:09 +03:00
throw e
}
2024-06-09 19:04:09 +03:00
await tg.close()
expect(user.isSelf).to.eq(true)
2024-06-09 19:04:09 +03:00
expect(user.phoneNumber).to.equal(phone)
break
}
}
2024-06-09 19:04:09 +03:00
describe('1. authorization', function () {
this.timeout(300_000)
2024-06-09 19:04:09 +03:00
it('should authorize in default dc', async () => {
const base = new BaseTelegramClient(getApiParams('dc2.session'))
2024-06-09 19:04:09 +03:00
await authorizeInDc(2, base)
})
2024-06-09 19:04:09 +03:00
it('should authorize in dc 1', async () => {
const base = new BaseTelegramClient(getApiParams('dc1.session'))
await authorizeInDc(1, base)
})
})