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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/**
 * @jest-environment jsdom
 */

import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import deepFreeze from 'deep-freeze';
import documentHeadReducer from 'calypso/state/document-head/reducer';
import purchasesReducer from 'calypso/state/purchases/reducer';
import siteConnectionReducer from 'calypso/state/site-connection/reducer';
import uiReducer from 'calypso/state/ui/reducer';
import { renderWithProvider } from '../../../client/test-helpers/testing-library';
import { JetpackAuthorize } from '../authorize';
import { JPC_PATH_PLANS } from '../constants';
import { OFFER_RESET_FLOW_TYPES } from '../flow-types';

const noop = () => {};
const CLIENT_ID = 98765;
const SITE_SLUG = 'an.example.site';
const DEFAULT_PROPS = deepFreeze( {
	authAttempts: 0,
	authorizationData: {
		authorizeError: false,
		authorizeSuccess: false,
		clientId: CLIENT_ID,
		clientNotResponding: true,
		isAuthorizing: false,
		timestamp: 1509368045859,
		userAlreadyConnected: false,
	},
	authorize: noop,
	authQuery: {
		authApproved: false,
		blogname: 'Example Blog',
		clientId: CLIENT_ID,
		closeWindowAfterLogin: false,
		closeWindowAfterAuthorize: false,
		from: 'banner-44-slide-1-dashboard',
		homeUrl: `http://${ SITE_SLUG }`,
		jpVersion: '5.4',
		nonce: 'fooBarNonce',
		redirectAfterAuth: `http://${ SITE_SLUG }/wp-admin/admin.php?page=jetpack`,
		redirectUri: `http://${ SITE_SLUG }/wp-admin/admin.php?page=jetpack&action=authorize&_wpnonce=fooBarNonce&redirect=http%3A%2F%2F${ SITE_SLUG }%2Fwp-admin%2Fadmin.php%3Fpage%3Djetpack`,
		scope: 'administrator:fooBarBaz',
		secret: 'fooBarSecret',
		site: `http://${ SITE_SLUG }`,
		siteIcon: '',
		siteUrl: `http://${ SITE_SLUG }`,
		state: '1',
		userEmail: `email@${ SITE_SLUG }`,
	},
	calypsoStartedConnection: false,
	hasExpiredSecretError: false,
	hasXmlrpcError: false,
	isAlreadyOnSitesList: false,
	isFetchingAuthorizationSite: false,
	isFetchingSites: false,
	recordTracksEvent: noop,
	retryAuth: noop,
	siteSlug: SITE_SLUG,
	translate: ( string ) => string,
	user: {
		display_name: "A User's Name",
	},
	partnerSlug: null,
	selectedPlanSlug: null,
	siteHasJetpackPaidProduct: false,
	userAlreadyConnected: false,
	userHasUnattachedLicenses: false,
} );

const APPROVE_SSO_CLIENT_ID = 99821;

jest.mock( '../persistence-utils', () => ( {
	...jest.requireActual( '../persistence-utils' ),
	isSsoApproved: ( clientId ) => clientId === APPROVE_SSO_CLIENT_ID,
} ) );

function renderWithRedux( ui ) {
	return renderWithProvider( ui, {
		reducers: {
			ui: uiReducer,
			documentHead: documentHeadReducer,
			purchases: purchasesReducer,
			siteConnection: siteConnectionReducer,
		},
	} );
}

let windowOpenSpy;

beforeEach( () => {
	windowOpenSpy = jest.spyOn( global.window, 'open' ).mockImplementation( jest.fn() );
} );

afterEach( () => {
	windowOpenSpy?.mockRestore();
} );

describe( 'JetpackAuthorize', () => {
	test( 'renders as expected', () => {
		const { container } = renderWithRedux( <JetpackAuthorize { ...DEFAULT_PROPS } /> );

		expect( container ).toMatchSnapshot();
	} );

	describe( 'isSso', () => {
		const isSso = new JetpackAuthorize().isSso;
		const queryDataSiteId = 12349876;

		test( 'returns true for valid SSO', () => {
			document.cookie = `jetpack_sso_approved=${ queryDataSiteId };`;
			const props = {
				authQuery: {
					from: 'sso',
					clientId: APPROVE_SSO_CLIENT_ID,
				},
			};
			expect( isSso( props ) ).toBe( true );
		} );

		test( 'returns false with non-sso from', () => {
			document.cookie = `jetpack_sso_approved=${ queryDataSiteId };`;
			const props = {
				authQuery: {
					from: 'elsewhere',
					clientId: queryDataSiteId,
				},
			};
			expect( isSso( props ) ).toBe( false );
		} );

		test( 'returns false without approved cookie', () => {
			document.cookie = 'jetpack_sso_approved=;';
			const props = {
				authQuery: {
					from: 'sso',
					clientId: queryDataSiteId,
				},
			};
			expect( isSso( props ) ).toBe( false );
		} );

		test( 'returns false with no cookie or queryDataSiteId', () => {
			document.cookie = 'jetpack_sso_approved=;';
			const props = {
				authQuery: {
					from: 'sso',
					clientId: null,
				},
			};
			expect( isSso( props ) ).toBe( false );
		} );
	} );

	describe( 'isWooRedirect', () => {
		const isWooRedirect = new JetpackAuthorize().isWooRedirect;

		test( 'should return true for woo services', () => {
			const props = { authQuery: { from: 'woocommerce-services-auto-authorize' } };
			expect( isWooRedirect( props ) ).toBe( true );
		} );

		test( 'should return true for old woo setup wizard', () => {
			const props = { authQuery: { from: 'woocommerce-setup-wizard' } };
			expect( isWooRedirect( props ) ).toBe( true );
		} );

		test( 'should return true for legacy woo onboarding', () => {
			const props = { authQuery: { from: 'woocommerce-onboarding' } };
			expect( isWooRedirect( props ) ).toBe( true );
		} );

		test( 'returns false with non-woo from', () => {
			const props = { authQuery: { from: 'elsewhere' } };
			expect( isWooRedirect( props ) ).toBeFalsy();
		} );
	} );

	describe( 'shouldAutoAuthorize', () => {
		let authorizeMock;

		beforeEach( () => {
			authorizeMock = jest.fn();
		} );

		test( 'should authorize if isSso', () => {
			const authQuery = {
				...DEFAULT_PROPS.authQuery,
				from: 'sso',
				clientId: APPROVE_SSO_CLIENT_ID,
			};

			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authorize={ authorizeMock }
					authQuery={ authQuery }
				/>
			);

			expect( authorizeMock ).toHaveBeenCalled();
		} );

		test( 'should auto-authorize for WOO services', () => {
			const authQuery = {
				...DEFAULT_PROPS.authQuery,
				from: 'woocommerce-services-auto-authorize',
			};

			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authorize={ authorizeMock }
					authQuery={ authQuery }
				/>
			);

			expect( authorizeMock ).toHaveBeenCalled();
		} );

		test( 'should not auto-authorize for WOO onboarding', () => {
			const authQuery = {
				...DEFAULT_PROPS.authQuery,
				from: 'woocommerce-onboarding',
			};

			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authorize={ authorizeMock }
					authQuery={ authQuery }
				/>
			);

			expect( authorizeMock ).not.toHaveBeenCalled();
		} );

		test( 'should auto-authorize for the old WOO setup wizard', () => {
			const authQuery = {
				...DEFAULT_PROPS.authQuery,
				from: 'woocommerce-setup-wizard',
			};

			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authorize={ authorizeMock }
					authQuery={ authQuery }
				/>
			);

			expect( authorizeMock ).toHaveBeenCalled();
		} );
	} );

	describe( 'isJetpackUpgradeFlow', () => {
		const isJetpackUpgradeFlow = new JetpackAuthorize().isJetpackUpgradeFlow;

		test( 'should see plans', () => {
			const props = {
				authQuery: {
					redirectAfterAuth: 'page=jetpack&action=something_else',
				},
			};

			expect( isJetpackUpgradeFlow( props ) ).toBe( false );
		} );

		test( 'should be sent back', () => {
			const props = {
				authQuery: {
					redirectAfterAuth: 'page=jetpack&action=authorize_redirect',
				},
			};

			expect( isJetpackUpgradeFlow( props ) ).toBe( true );
		} );
	} );

	describe( 'isFromJetpackConnectionManager', () => {
		const isFromJetpackConnectionManager = new JetpackAuthorize().isFromJetpackConnectionManager;

		test( 'is from connection manager', () => {
			const props = {
				authQuery: {
					from: 'connection-ui',
				},
			};

			expect( isFromJetpackConnectionManager( props ) ).toBe( true );
		} );

		test( 'is not from connection manager', () => {
			const props = {
				authQuery: {
					from: 'not-connection-ui',
				},
			};

			expect( isFromJetpackConnectionManager( props ) ).toBe( false );
		} );
	} );

	describe( 'isFromJetpackOnboardingFlow', () => {
		const isFromJetpackOnboarding = new JetpackAuthorize().isFromJetpackOnboarding;

		test( 'is from jetpack onboarding', () => {
			const props = {
				authQuery: {
					from: 'jetpack-onboarding',
				},
			};

			expect( isFromJetpackOnboarding( props ) ).toBe( true );
		} );

		test( 'is not from jetpack onboarding', () => {
			const props = {
				authQuery: {
					from: 'not-jetpack-onboarding',
				},
			};

			expect( isFromJetpackOnboarding( props ) ).toBe( false );
		} );
	} );

	describe( 'isFromJetpackBackupPlugin', () => {
		const isFromJetpackBackupPlugin = new JetpackAuthorize().isFromJetpackBackupPlugin;

		test( 'is from backup plugin', () => {
			const props = {
				authQuery: {
					from: 'jetpack-backup',
				},
			};

			expect( isFromJetpackBackupPlugin( props ) ).toBe( true );
		} );

		test( 'is not from backup plugin', () => {
			const props = {
				authQuery: {
					from: 'not-jetpack-backup',
				},
			};

			expect( isFromJetpackBackupPlugin( props ) ).toBe( false );
		} );
	} );

	describe( 'isFromJetpackBoost', () => {
		const isFromJetpackBoost = new JetpackAuthorize().isFromJetpackBoost;

		test( 'is from jetpack boost', () => {
			const props = {
				authQuery: {
					from: 'jetpack-boost-something',
				},
			};

			expect( isFromJetpackBoost( props ) ).toBe( true );
		} );

		test( 'is not from jetpack boost', () => {
			const props = {
				authQuery: {
					from: 'not-jetpack-boost-something',
				},
			};

			expect( isFromJetpackBoost( props ) ).toBe( false );
		} );
	} );

	describe( 'getRedirectionTarget', () => {
		let originalWindowLocation;

		beforeEach( () => {
			originalWindowLocation = global.window.location;
			delete global.window.location;
			global.window.location = {
				href: 'http://wwww.example.com',
				origin: 'http://www.example.com',
			};
		} );

		afterEach( () => {
			global.window.location = originalWindowLocation;
		} );

		test( 'should redirect to /checkout if the selected plan/product is Jetpack plan/product', async () => {
			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authQuery={ {
						...DEFAULT_PROPS.authQuery,
						alreadyAuthorized: true,
					} }
					isAlreadyOnSitesList
					isFetchingSites
					selectedPlanSlug={ OFFER_RESET_FLOW_TYPES[ 0 ] }
				/>
			);

			await userEvent.click( screen.getByText( 'Return to your site' ) );

			expect( windowOpenSpy ).toHaveBeenCalledWith(
				`/checkout/${ SITE_SLUG }/${ OFFER_RESET_FLOW_TYPES[ 0 ] }`,
				expect.any( String )
			);
		} );

		test( 'should redirect to wp-admin when site has a purchased plan/product', async () => {
			delete global.window.location;
			global.window.location = {
				href: 'http://wwww.example.com',
				origin: 'http://www.example.com',
			};

			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authQuery={ {
						...DEFAULT_PROPS.authQuery,
						alreadyAuthorized: true,
					} }
					isAlreadyOnSitesList
					isFetchingSites
					siteHasJetpackPaidProduct
				/>
			);

			await userEvent.click( screen.getByText( 'Return to your site' ) );

			expect( windowOpenSpy ).toHaveBeenCalledWith(
				DEFAULT_PROPS.authQuery.redirectAfterAuth,
				expect.any( String )
			);
		} );

		test( 'should redirect to /jetpack/connect/plans when user has an unattached "user"(not partner) license key', async () => {
			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authQuery={ {
						...DEFAULT_PROPS.authQuery,
						alreadyAuthorized: true,
					} }
					isAlreadyOnSitesList
					isFetchingSites
					userHasUnattachedLicenses
				/>
			);

			await userEvent.click( screen.getByText( 'Return to your site' ) );

			expect( windowOpenSpy ).toHaveBeenCalledWith(
				`${ JPC_PATH_PLANS }/${ SITE_SLUG }?redirect_to=${ encodeURIComponent(
					DEFAULT_PROPS.authQuery.redirectAfterAuth
				) }`,
				expect.any( String )
			);
		} );

		test( 'should redirect to the /jetpack/connect/plans page by default', async () => {
			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authQuery={ {
						...DEFAULT_PROPS.authQuery,
						alreadyAuthorized: true,
					} }
					isAlreadyOnSitesList
					isFetchingSites
				/>
			);

			await userEvent.click( screen.getByText( 'Return to your site' ) );

			expect( windowOpenSpy ).toHaveBeenCalledWith(
				`${ JPC_PATH_PLANS }/${ SITE_SLUG }?redirect_to=${ encodeURIComponent(
					DEFAULT_PROPS.authQuery.redirectAfterAuth
				) }`,
				expect.any( String )
			);
		} );
	} );

	describe( 'handleSignIn', () => {
		let originalWindowLocation;

		beforeEach( () => {
			originalWindowLocation = global.window.location;
			delete global.window.location;
			global.window.location = {
				href: 'http://wwww.example.com',
				origin: 'http://www.example.com',
			};
		} );

		afterEach( () => {
			global.window.location = originalWindowLocation;
		} );

		test( 'should redirect to url that returns from props.logoutUser', async () => {
			const redirectTo = 'http://www.example.com/redirect';
			const logoutUser = jest.fn().mockResolvedValue( {
				redirect_to: redirectTo,
			} );
			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authQuery={ {
						...DEFAULT_PROPS.authQuery,
						alreadyAuthorized: true,
					} }
					isAlreadyOnSitesList
					isFetchingSites
					logoutUser={ logoutUser }
				/>
			);

			await userEvent.click( screen.getByText( 'Sign in as a different user' ) );
			expect( global.window.location.href ).toBe( redirectTo );
			expect( logoutUser ).toHaveBeenCalled();
		} );

		test( 'should redirect to jetpack login page for woo onboarding', async () => {
			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authQuery={ {
						...DEFAULT_PROPS.authQuery,
						alreadyAuthorized: true,
						from: 'woocommerce-onboarding',
					} }
					isAlreadyOnSitesList
					isFetchingSites
				/>
			);

			await userEvent.click( screen.getByText( 'Sign in as a different user' ) );
			expect( global.window.location.href ).toBe(
				'https://example.com/log-in/jetpack?redirect_to=http%3A%2F%2Fwwww.example.com&from=woocommerce-onboarding'
			);
		} );

		test( 'should redirect to jetpack login page for woo core profiler', async () => {
			renderWithRedux(
				<JetpackAuthorize
					{ ...DEFAULT_PROPS }
					authQuery={ {
						...DEFAULT_PROPS.authQuery,
						alreadyAuthorized: true,
						from: 'woocommerce-core-profiler',
					} }
					isAlreadyOnSitesList
					isFetchingSites
				/>
			);

			await userEvent.click( screen.getByText( 'Sign in as a different user' ) );
			expect( global.window.location.href ).toBe(
				'https://example.com/log-in/jetpack?redirect_to=http%3A%2F%2Fwwww.example.com&from=woocommerce-core-profiler'
			);
		} );
	} );
} );