From dc3a15261b5477e61c610032f62fb87993e456a5 Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Sun, 9 Jun 2024 19:04:09 +0300 Subject: [PATCH] test(e2e): improved auth flow --- e2e/deno/tests/01.auth.ts | 117 ++++++++++++++----------------- e2e/node/ts/tests/01.auth.ts | 131 ++++++++++++++++------------------- 2 files changed, 111 insertions(+), 137 deletions(-) diff --git a/e2e/deno/tests/01.auth.ts b/e2e/deno/tests/01.auth.ts index da739c1d..a4e75f5f 100644 --- a/e2e/deno/tests/01.auth.ts +++ b/e2e/deno/tests/01.auth.ts @@ -1,6 +1,6 @@ import { assertEquals } from 'https://deno.land/std@0.223.0/assert/mod.ts' -import { MtcuteError, tl } from '@mtcute/core' +import { tl, User } from '@mtcute/core' import { BaseTelegramClient, TelegramClient } from '@mtcute/core/client.js' import { getApiParams } from '../utils.ts' @@ -10,85 +10,72 @@ const getAccountId = () => .toString() .padStart(4, '0') -Deno.test('1. authorization', { sanitizeResources: false }, async (t) => { - await t.step('should authorize in default dc', async () => { - const base = new BaseTelegramClient(getApiParams('dc2.session')) - const tg = new TelegramClient({ client: base }) +async function authorizeInDc(dc: number, base: BaseTelegramClient) { + const tg = new TelegramClient({ client: base }) - // reset storage just in case + while (true) { await base.mt.storage.load() await base.storage.clear(true) - while (true) { - const phone = `999662${getAccountId()}` - let user + const phone = `99966${dc}${getAccountId()}` - try { - user = await tg.start({ - phone, - code: () => '22222', + const sentCode = await tg.sendCode({ phone }) + + let user + + 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: '', }) - } catch (e) { - if ( - (e instanceof MtcuteError && e.message.match(/Signup is no longer supported|2FA is enabled/)) || - tl.RpcError.is(e, 'SESSION_PASSWORD_NEEDED') || - tl.RpcError.is(e, 'PHONE_NUMBER_FLOOD') - ) { - // retry with another number - await tg.logOut().catch((err) => { - console.error('Failed to log out:', err) - }) - continue - } else { - await tg.close() - throw e + + if (auth._ !== 'auth.authorization') { + throw new Error('Unexpected response') } } - await tg.close() + await tg.notifyLoggedIn(auth) - assertEquals(user.isSelf, true) - assertEquals(user.phoneNumber, phone) - break + user = new User(auth.user) + } catch (e) { + if (tl.RpcError.is(e, 'SESSION_PASSWORD_NEEDED') || tl.RpcError.is('PHONE_NUMBER_FLOOD')) { + // retry with another number + await tg.close() + continue + } + + throw e } + + await tg.close() + + assertEquals(user.isSelf, true) + assertEquals(user.phoneNumber, phone) + break + } +} + +Deno.test('1. authorization', { sanitizeResources: false }, async (t) => { + await t.step('should authorize in default dc', async () => { + const base = new BaseTelegramClient(getApiParams('dc2.session')) + + await authorizeInDc(2, base) }) await t.step('should authorize in dc 1', async () => { const base = new BaseTelegramClient(getApiParams('dc1.session')) - const tg = new TelegramClient({ client: base }) - // reset storage just in case - await base.mt.storage.load() - await base.mt.storage.clear(true) - - while (true) { - const phone = `999661${getAccountId()}` - let user - - try { - user = await tg.start({ - phone, - code: () => '11111', - }) - } catch (e) { - if ( - (e instanceof MtcuteError && e.message.match(/Signup is no longer supported|2FA is enabled/)) || - tl.RpcError.is(e, 'SESSION_PASSWORD_NEEDED') || - tl.RpcError.is(e, 'PHONE_NUMBER_FLOOD') - ) { - // retry with another number - continue - } else { - await tg.close() - throw e - } - } - - await tg.close() - - assertEquals(user.isSelf, true) - assertEquals(user.phoneNumber, phone) - break - } + await authorizeInDc(1, base) }) }) diff --git a/e2e/node/ts/tests/01.auth.ts b/e2e/node/ts/tests/01.auth.ts index 5fbfd704..88602da1 100644 --- a/e2e/node/ts/tests/01.auth.ts +++ b/e2e/node/ts/tests/01.auth.ts @@ -1,7 +1,7 @@ import { expect } from 'chai' import { describe, it } from 'mocha' -import { MtcuteError, tl } from '@mtcute/core' +import { tl, User } from '@mtcute/core' import { BaseTelegramClient, TelegramClient } from '@mtcute/core/client.js' import { getApiParams } from '../utils.js' @@ -11,87 +11,74 @@ const getAccountId = () => .toString() .padStart(4, '0') +async function authorizeInDc(dc: number, base: BaseTelegramClient) { + const tg = new TelegramClient({ client: base }) + + while (true) { + await base.mt.storage.load() + await base.storage.clear(true) + + const phone = `99966${dc}${getAccountId()}` + + const sentCode = await tg.sendCode({ phone }) + + let user + + 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: '', + }) + + if (auth._ !== 'auth.authorization') { + throw new Error('Unexpected response') + } + } + + await tg.notifyLoggedIn(auth) + + user = new User(auth.user) + } catch (e) { + if (tl.RpcError.is(e, 'SESSION_PASSWORD_NEEDED') || tl.RpcError.is('PHONE_NUMBER_FLOOD')) { + // retry with another number + await tg.close() + continue + } + + throw e + } + + await tg.close() + + expect(user.isSelf).to.be.true + expect(user.phoneNumber).to.equal(phone) + break + } +} + describe('1. authorization', function () { this.timeout(300_000) it('should authorize in default dc', async () => { const base = new BaseTelegramClient(getApiParams('dc2.session')) - const tg = new TelegramClient({ client: base }) - // reset storage just in case - await base.mt.storage.load() - await base.storage.clear(true) - - while (true) { - const phone = `999662${getAccountId()}` - let user - - try { - user = await tg.start({ - phone, - code: () => '22222', - }) - } catch (e) { - if ( - (e instanceof MtcuteError && e.message.match(/Signup is no longer supported|2FA is enabled/)) || - tl.RpcError.is(e, 'SESSION_PASSWORD_NEEDED') || - tl.RpcError.is(e, 'PHONE_NUMBER_FLOOD') - ) { - // retry with another number - await tg.logOut().catch((err) => { - console.error('Failed to log out:', err) - }) - continue - } else { - await tg.close() - throw e - } - } - - await tg.close() - - expect(user.isSelf).to.be.true - expect(user.phoneNumber).to.equal(phone) - break - } + await authorizeInDc(2, base) }) it('should authorize in dc 1', async () => { const base = new BaseTelegramClient(getApiParams('dc1.session')) - const tg = new TelegramClient({ client: base }) - // reset storage just in case - await base.mt.storage.load() - await base.mt.storage.clear(true) - - while (true) { - const phone = `999661${getAccountId()}` - let user - - try { - user = await tg.start({ - phone, - code: () => '11111', - }) - } catch (e) { - if ( - (e instanceof MtcuteError && e.message.match(/Signup is no longer supported|2FA is enabled/)) || - tl.RpcError.is(e, 'SESSION_PASSWORD_NEEDED') || - tl.RpcError.is(e, 'PHONE_NUMBER_FLOOD') - ) { - // retry with another number - continue - } else { - await tg.close() - throw e - } - } - - await tg.close() - - expect(user.isSelf).to.be.true - expect(user.phoneNumber).to.equal(phone) - break - } + await authorizeInDc(1, base) }) })