2023-12-23 22:00:20 +03:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import { describe, it } from 'mocha'
|
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
import { tl, User } from '@mtcute/core'
|
2024-03-04 01:39:18 +03:00
|
|
|
import { BaseTelegramClient, TelegramClient } from '@mtcute/core/client.js'
|
2023-12-23 22:00:20 +03:00
|
|
|
|
|
|
|
import { getApiParams } from '../utils.js'
|
|
|
|
|
|
|
|
const getAccountId = () =>
|
|
|
|
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 })
|
2023-12-23 22:00:20 +03:00
|
|
|
|
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)
|
2023-12-23 22:00:20 +03:00
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
const phone = `99966${dc}${getAccountId()}`
|
|
|
|
|
|
|
|
const sentCode = await tg.sendCode({ phone })
|
|
|
|
|
|
|
|
let user
|
2023-12-23 22:00:20 +03:00
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
try {
|
|
|
|
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: '',
|
2023-12-23 22:00:20 +03:00
|
|
|
})
|
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
|
|
|
}
|
2023-12-23 22:00:20 +03:00
|
|
|
}
|
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
await tg.notifyLoggedIn(auth)
|
|
|
|
|
|
|
|
user = new User(auth.user)
|
|
|
|
} catch (e) {
|
2024-06-13 14:04:51 +03:00
|
|
|
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
|
|
|
|
}
|
2023-12-23 22:00:20 +03:00
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
throw e
|
2023-12-23 22:00:20 +03:00
|
|
|
}
|
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
await tg.close()
|
2023-12-23 22:00:20 +03:00
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
expect(user.isSelf).to.be.true
|
|
|
|
expect(user.phoneNumber).to.equal(phone)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-12-23 22:00:20 +03:00
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
describe('1. authorization', function () {
|
|
|
|
this.timeout(300_000)
|
2023-12-23 22:00:20 +03:00
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
it('should authorize in default dc', async () => {
|
|
|
|
const base = new BaseTelegramClient(getApiParams('dc2.session'))
|
2023-12-23 22:00:20 +03:00
|
|
|
|
2024-06-09 19:04:09 +03:00
|
|
|
await authorizeInDc(2, base)
|
|
|
|
})
|
2023-12-23 22:00:20 +03:00
|
|
|
|
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)
|
2023-12-23 22:00:20 +03:00
|
|
|
})
|
|
|
|
})
|