From 3c630703e7f0629e08d6b9177fa6d7f5e52ad6ee Mon Sep 17 00:00:00 2001 From: susaha135 Date: Mon, 21 Jul 2025 21:11:48 +0530 Subject: [PATCH] Adding tw visitor cookie --- .../src/utils/pages/view/twilio-console.ts | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/packages/flex-plugin-e2e-tests/src/utils/pages/view/twilio-console.ts b/packages/flex-plugin-e2e-tests/src/utils/pages/view/twilio-console.ts index e8adf8e1f..9e274d5eb 100644 --- a/packages/flex-plugin-e2e-tests/src/utils/pages/view/twilio-console.ts +++ b/packages/flex-plugin-e2e-tests/src/utils/pages/view/twilio-console.ts @@ -48,18 +48,44 @@ export class TwilioConsole extends Base { credentials: 'include', }); const data = await response.json(); - return JSON.parse(data.body).csrf; + // Get tw-visitor cookie from response headers + let twVisitor = ''; + try { + // @ts-ignore + const rawHeaders = response.headers.get('set-cookie') || document.cookie; + const match = rawHeaders.match(/tw-visitor=([^;]+);?/); + if (match) { + twVisitor = match[1]; + } + } catch (e) { + // fallback: try document.cookie + const match = document.cookie.match(/tw-visitor=([^;]+);?/); + if (match) { + twVisitor = match[1]; + } + } + return { + csrfToken: JSON.parse(data.body).csrf, + twVisitorCookie: twVisitor, + }; }); - if (csrfToken) { + if (csrfToken.csrfToken && csrfToken.twVisitorCookie) { const loginURL = `${this._baseUrl}/userauth/submitLoginPassword`; await this.page.evaluate( // eslint-disable-next-line @typescript-eslint/promise-function-async - (data: Record) => { + (data: { + url: string; + email: string; + password: string; + csrfToken: { csrfToken: any; twVisitorCookie: string }; + twVisitorCookie: string; + }) => { return fetch(data.url, { headers: { - 'x-twilio-csrf': csrfToken, + 'x-twilio-csrf': data.csrfToken.csrfToken, 'Content-Type': 'application/json', + cookie: `tw-visitor=${data.twVisitorCookie}`, }, body: JSON.stringify({ email: data.email, @@ -74,14 +100,14 @@ export class TwilioConsole extends Base { email: testParams.secrets.console.email, password: testParams.secrets.console.password, csrfToken, + twVisitorCookie: csrfToken.twVisitorCookie, }, ); - // Log in Flex via service login logger.info('Logging in Flex via service login on first load'); await this.goto({ baseUrl: this._baseUrl, path }); } else { - throw new Error('Unable to fetch CSRF token to login to Twilio Console'); + throw new Error('Unable to fetch CSRF token or tw-visitor cookie to login to Twilio Console'); } }