File size: 4,504 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
const { createWriteStream } = require( 'fs' );
const { mkdir } = require( 'fs/promises' );
const path = require( 'path' );
const { _electron: electron } = require( 'playwright' );
const config = require( '../../../app/lib/config' );

let APP_PATH;

switch ( process.platform ) {
	case 'linux':
		APP_PATH = path.join( __dirname, '../../../release/linux-unpacked/wpcom' );
		break;
	case 'darwin':
		// On Apple Silicon, the output file is under a separate directory.
		if ( process.arch.includes( 'arm' ) ) {
			APP_PATH = path.join(
				__dirname,
				'../../../release/mac-arm64/WordPress.com.app/Contents/MacOS/WordPress.com'
			);
			break;
		}
		// Codepath for Intel architecture.
		APP_PATH = path.join(
			__dirname,
			'../../../release/mac/WordPress.com.app/Contents/MacOS/WordPress.com'
		);
		break;
	default:
		throw new Error( 'unsupported platform' );
}

const CONSOLE_PATH = path.join( __dirname, '../results/console.log' );
const SCREENSHOT_PATH = path.join( __dirname, '../results/screenshot.png' );
const HAR_PATH = path.join( __dirname, '../results/network.har' );
const WP_DEBUG_LOG = path.resolve( __dirname, '../results/app.log' );

const BASE_URL = process.env.WP_DESKTOP_BASE_URL?.replace( /\/$/, '' ) ?? 'https://wordpress.com';

const skipIfOAuthLogin = config.oauthLoginEnabled ? it.skip : it;
const runIfOAuthLogin = config.oauthLoginEnabled ? it : it.skip;

describe( 'User Can log in', () => {
	jest.setTimeout( 60000 );

	let mainWindow;
	let electronApp;
	let consoleStream;

	beforeAll( async () => {
		await mkdir( path.dirname( CONSOLE_PATH ), { recursive: true } );
		consoleStream = await createWriteStream( CONSOLE_PATH );

		electronApp = await electron.launch( {
			executablePath: APP_PATH,
			args: [ '--disable-http-cache', '--start-maximized' ],
			timeout: 0,
			recordHar: {
				path: HAR_PATH,
			},
			env: {
				...process.env,
				WP_DESKTOP_BASE_URL: BASE_URL,
				WP_DEBUG_LOG, // This will override logging path from the Electron main process.
				// Ensure other CI-specific overrides (such as disabling the auto-updater)
				DEBUG: true,
				CI: true,
			},
		} );

		// Find main window. Playwright has problems identifying the main window when using `firstWindow`, so we
		// iterate over all windows and find it by URL.
		for ( const window of await electronApp.windows() ) {
			const windowUrl = await window.url();
			if ( windowUrl.startsWith( BASE_URL ) ) {
				mainWindow = window;
				break;
			}
		}
		if ( ! mainWindow ) {
			mainWindow = await electronApp.firstWindow();
		}

		// Capture console
		mainWindow.on( 'console', ( data ) =>
			consoleStream.write( `${ new Date().toUTCString() } [${ data.type() }] ${ data.text() }\n` )
		);

		// Wait for everythingm to be loaded before starting
		await mainWindow.waitForLoadState();
		for ( const [ , frame ] of mainWindow.frames().entries() ) {
			await frame.waitForLoadState();
		}
	} );

	runIfOAuthLogin( 'Start the OAuth login flow', async function () {
		const loginButton = await mainWindow.waitForSelector(
			'a:has-text("Log in with WordPress.com")'
		);
		const href = await loginButton.getAttribute( 'href' );
		// eslint-disable-next-line jest/no-standalone-expect
		expect( href ).toBe( '/desktop-start-login' );
	} );

	skipIfOAuthLogin( 'Log in with username and password', async function () {
		await mainWindow.fill( '#usernameOrEmail', process.env.E2EGUTENBERGUSER );
		await mainWindow.keyboard.press( 'Enter' );
		await mainWindow.fill( '#password', process.env.E2EPASSWORD );

		// Wait for response from the Login endpoint.
		const [ response ] = await Promise.all( [
			mainWindow.waitForResponse( '**/wp-login.php?action=login-endpoint' ),
			mainWindow.click( 'button:has-text("Log In")' ),
		] );

		// If the account credentials are rejected, throw an error containing the text of
		// the validation error.
		// Credentaials can be rejected for any number of reasons:
		// 	- closed account
		//	- wrong password
		if ( response.status() === 400 ) {
			throw new Error(
				await mainWindow
					.waitForSelector( 'div.is-error' )
					.then( ( element ) => element.innerText() )
			);
		}

		// eslint-disable-next-line jest/no-standalone-expect
		expect( response.status() ).toBe( 200 );
	} );

	afterAll( async () => {
		if ( consoleStream ) {
			consoleStream.end();
		}

		if ( mainWindow ) {
			await mainWindow.screenshot( { path: SCREENSHOT_PATH } );
		}

		if ( electronApp ) {
			try {
				await electronApp.close();
			} catch {}
		}
	} );
} );