File size: 7,947 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
 * @group authentication
 *
 * Google blocks Chrome-based browsers that are controlled via automation
 * from performing login to their services.
 * The workaround is to use a non-Google browser, such as Firefox.
 * See: https://stackoverflow.com/questions/66209119/automation-google-login-with-python-and-selenium-shows-this-browser-or-app-may
 * @browser firefox
 */

import { LoginPage, SecretsManager, GoogleLoginPage, TOTPClient } from '@automattic/calypso-e2e';
import { Page, Browser } from 'playwright';

declare const browser: Browser;

describe( 'Authentication: Google', function () {
	const credentials = SecretsManager.secrets.testAccounts.googleLoginUser;

	let page: Page;
	let googlePopupPage: Page;
	let loginPage: LoginPage;
	let googleLoginPage: GoogleLoginPage;

	const totpClient = new TOTPClient( credentials.totpKey as string );
	let code: string;

	describe( 'WordPress.com', function () {
		beforeAll( async () => {
			page = await browser.newPage();
		} );

		it( 'Navigate to /login', async function () {
			loginPage = new LoginPage( page );
			await loginPage.visit();

			await page.waitForURL( /log-in/ );

			// Differently from the "WooCommerce" scenario below (see the comment below),
			// this doesn't seem to affect the click of the button, but I added it here for
			// consistency purposes.
			await page.waitForLoadState( 'networkidle' );
		} );

		it( 'Click on "Continue with Google" button', async function () {
			googlePopupPage = await loginPage.clickLoginWithGoogle();
			await googlePopupPage.waitForURL( /accounts\.google\.com/ );
		} );

		it( 'Enter Google username', async function () {
			await googlePopupPage.waitForURL( /identifier/ );

			googleLoginPage = new GoogleLoginPage( googlePopupPage );
			await googleLoginPage.enterUsername( credentials.username );
			await googleLoginPage.clickButton( 'Next' );
		} );

		it( 'Enter Google password', async function () {
			await googlePopupPage.waitForURL( /challenge/ );

			await googleLoginPage.enterPassword( credentials.password );
			await googleLoginPage.clickButton( 'Next' );
		} );

		it( 'Enter 2FA challenge if required - Challenge 1', async function () {
			code = totpClient.getToken();

			await googleLoginPage.enter2FACode( code );
			await googleLoginPage.clickButton( 'Next' );
		} );

		// In a game of cat and mouse, Google now appears to require
		// two 2FA challege for our `googleLoginUser` account.
		// It is not known why, but the most likely explanation is that
		// this spec has tripped something in the Google backend.
		// The result being the user is required to enter two sets of
		// TOTP codes, and because the TOTP codes regenerate every 30s
		// this means the spec has to wait until the TOTP code updates.
		// This behavior was confirmed manually by @worldomonation as well.
		it( 'Enter 2FA challenge if required - Challenge 2', async function () {
			await page.waitForLoadState( 'networkidle' );
			// Check if the unique text indicating the second 2FA challenge is present
			const isSecondChallengePresent = await googleLoginPage.isVisible( 'text="Verify it’s you"' );

			if ( ! isSecondChallengePresent ) {
				return;
			}

			// Wait until the TOTP code generated by the client
			// changes, meaning the 30s window has rolled over.
			while ( totpClient.getToken() === code ) {
				console.log(
					`Google Authentication: second 2FA challenge encountered, waiting for TOTP code to change from ${ code }`
				);
				await new Promise( ( resolve ) => setTimeout( resolve, 1000 ) );
			}

			code = totpClient.getToken();

			await googleLoginPage.enter2FACode( code );
			await googleLoginPage.clickButton( 'Next' );
		} );

		it( 'Click the "Continue" button in the login confirmation screen', async function () {
			const googlePopupPageClosePromise = googlePopupPage.waitForEvent( 'close' );
			// The next screen has a prompt that asks the user to confirm the login by clicking 'Continue'.
			await googleLoginPage.clickButton( 'Continue' );
			// The popup should close after clicking the button,
			// if it does not, something has gone wrong.
			await googlePopupPageClosePromise;
		} );

		it( 'Redirected to /home upon successful login', async function () {
			await page.waitForURL( /.*\/home\/.*/ );
		} );
	} );

	describe( 'WooCommerce', function () {
		beforeAll( async () => {
			page = await browser.newPage();
		} );

		it( 'Navigate to /login', async function () {
			loginPage = new LoginPage( page );
			await loginPage.visit( {
				path: SecretsManager.secrets.wooLoginPath,
			} );

			await page.waitForURL( /log-in/ );

			// This is needed or PW won't click the "Continue with Google" 99% of the time.
			await page.waitForLoadState( 'networkidle' );
		} );

		it( 'Click on "Continue with Google" button', async function () {
			googlePopupPage = await loginPage.clickLoginWithGoogle();
			await googlePopupPage.waitForURL( /accounts\.google\.com/ );
		} );

		it( 'Enter Google username', async function () {
			await googlePopupPage.waitForURL( /identifier/ );

			googleLoginPage = new GoogleLoginPage( googlePopupPage );
			await googleLoginPage.enterUsername( credentials.username );
			await googleLoginPage.clickButton( 'Next' );
		} );

		it( 'Enter Google password', async function () {
			await googlePopupPage.waitForURL( /challenge/ );

			await googleLoginPage.enterPassword( credentials.password );
			await googleLoginPage.clickButton( 'Next' );
		} );

		it( 'Enter 2FA challenge if required - Challenge 1', async function () {
			// Wait until the TOTP code generated by the client
			// changes, meaning the 30s window has rolled over.
			while ( totpClient.getToken() === code ) {
				console.log(
					`Google Authentication: second 2FA challenge encountered, waiting for TOTP code to change from ${ code }`
				);
				await new Promise( ( resolve ) => setTimeout( resolve, 1000 ) );
			}

			code = totpClient.getToken();

			await googleLoginPage.enter2FACode( code );
			await googleLoginPage.clickButton( 'Next' );
		} );

		// In a game of cat and mouse, Google now appears to require
		// two 2FA challege for our `googleLoginUser` account.
		// It is not known why, but the most likely explanation is that
		// this spec has tripped something in the Google backend.
		// The result being the user is required to enter two sets of
		// TOTP codes, and because the TOTP codes regenerate every 30s
		// this means the spec has to wait until the TOTP code updates.
		// This behavior was confirmed manually by @worldomonation as well.
		it( 'Enter 2FA challenge if required - Challenge 2', async function () {
			// Check if the unique text indicating the second 2FA challenge is present
			const isSecondChallengePresent = await googleLoginPage.isVisible( 'text="Verify it’s you"' );

			if ( ! isSecondChallengePresent ) {
				return;
			}

			// Wait until the TOTP code generated by the client
			// changes, meaning the 30s window has rolled over.
			while ( totpClient.getToken() === code ) {
				console.log(
					`Google Authentication: second 2FA challenge encountered, waiting for TOTP code to change from ${ code }`
				);
				await new Promise( ( resolve ) => setTimeout( resolve, 1000 ) );
			}

			await googleLoginPage.enter2FACode( totpClient.getToken() );
			await googleLoginPage.clickButton( 'Next' );
		} );

		it( 'Click the "Continue" button in the login confirmation screen', async function () {
			const googlePopupPageClosePromise = googlePopupPage.waitForEvent( 'close' );
			// The next screen has a prompt that asks the user to confirm the login by clicking 'Continue'.
			await googleLoginPage.clickButton( 'Continue' );
			// The popup should close after clicking the button,
			// if it does not, something has gone wrong.
			await googlePopupPageClosePromise;
		} );

		it( 'Redirected to woo.com upon successful login', async function () {
			await page.waitForURL( /.*woo\.com*/ );
		} );
	} );
} );