File size: 6,165 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
/**
 * @jest-environment jsdom
 */

import page from '@automattic/calypso-router';
import { navigate } from 'calypso/lib/navigate';
import { JPC_PATH_PLANS, JPC_PATH_REMOTE_INSTALL, REMOTE_PATH_AUTH } from '../constants';
import {
	addCalypsoEnvQueryArg,
	cleanUrl,
	getRoleFromScope,
	parseAuthorizationQuery,
	redirect,
} from '../utils';

jest.mock( '@automattic/calypso-config', () => ( input ) => {
	const lookupTable = {
		env_id: 'mocked-test-env-id',
	};
	if ( input in lookupTable ) {
		return lookupTable[ input ];
	}
	throw new Error( 'Unrecognized input to mocked config' );
} );

describe( 'addCalypsoEnvQueryArg', () => {
	test( 'should add config env_id as calypso_env', () => {
		expect( addCalypsoEnvQueryArg( 'http://example.com' ) ).toBe(
			'http://example.com/?calypso_env=mocked-test-env-id'
		);
	} );
} );

describe( 'cleanUrl', () => {
	test( 'should prepare entered urls for network access', () => {
		const results = [
			{ input: '', expected: '' },
			{ input: 'a', expected: 'http://a' },
			{ input: 'example.com', expected: 'http://example.com' },
			{ input: '  example.com   ', expected: 'http://example.com' },
			{ input: 'http://example.com/', expected: 'http://example.com' },
			{ input: 'eXAmple.com', expected: 'http://example.com' },
			{ input: 'example.com/wp-admin', expected: 'http://example.com' },
		];

		results.forEach( ( { input, expected } ) => expect( cleanUrl( input ) ).toBe( expected ) );
	} );
} );

describe( 'getRoleFromScope', () => {
	test( 'should return role from scope', () => {
		const result = getRoleFromScope( 'role:e8ae7346d1a0f800b64e' );
		expect( result ).toBe( 'role' );
	} );

	test( 'should return null if no role is found', () => {
		const result = getRoleFromScope( ':e8ae7346d1a0f800b64e' );
		expect( result ).toBe( null );
	} );

	test( 'should return null if no hash is found', () => {
		const result = getRoleFromScope( 'role' );
		expect( result ).toBe( null );
	} );

	test( 'should return null if scope is malformed', () => {
		const result = getRoleFromScope( 'rolee8ae7346d1a0f800b64e' );
		expect( result ).toBe( null );
	} );
} );

describe( 'parseAuthorizationQuery', () => {
	test( 'should return transformed data on valid input', () => {
		const data = {
			_wp_nonce: 'foobar',
			blogname: 'Just Another WordPress.com Site',
			client_id: '12345',
			close_window_after_login: '0',
			close_window_after_auth: '0',
			home_url: 'https://yourjetpack.blog',
			redirect_uri: 'https://yourjetpack.blog/wp-admin/admin.php',
			scope: 'administrator:34579bf2a3185a47d1b31aab30125d',
			secret: '640fdbd69f96a8ca9e61',
			site: 'https://yourjetpack.blog',
			site_url: 'https://yourjetpack.blog',
			state: '1',
			allow_site_connection: '1',
			installed_ext_success: '1',
		};
		const result = parseAuthorizationQuery( data );
		expect( result ).not.toBeNull();
		expect( result ).toMatchSnapshot();
	} );

	test( 'isPopup, closeWindowAfterLogin, closeWindowAfterAuthorize should be true if string is 1', () => {
		const data = {
			_wp_nonce: 'foobar',
			blogname: 'Just Another WordPress.com Site',
			client_id: '12345',
			close_window_after_login: '1',
			close_window_after_auth: '1',
			home_url: 'https://yourjetpack.blog',
			is_popup: '1',
			redirect_uri: 'https://yourjetpack.blog/wp-admin/admin.php',
			scope: 'administrator:34579bf2a3185a47d1b31aab30125d',
			secret: '640fdbd69f96a8ca9e61',
			site: 'https://yourjetpack.blog',
			site_url: 'https://yourjetpack.blog',
			state: '1',
		};
		const result = parseAuthorizationQuery( data );
		expect( result.isPopup ).toBe( true );
		expect( result.closeWindowAfterLogin ).toBe( true );
		expect( result.closeWindowAfterAuthorize ).toBe( true );
	} );

	test( 'isPopup, closeWindowAfterLogin, closeWindowAfterAuthorize should be false if string is not 1', () => {
		const data = {
			_wp_nonce: 'foobar',
			blogname: 'Just Another WordPress.com Site',
			client_id: '12345',
			close_window_after_login: '0',
			close_window_after_auth: '0',
			home_url: 'https://yourjetpack.blog',
			is_popup: 'FALSE',
			redirect_uri: 'https://yourjetpack.blog/wp-admin/admin.php',
			scope: 'administrator:34579bf2a3185a47d1b31aab30125d',
			secret: '640fdbd69f96a8ca9e61',
			site: 'https://yourjetpack.blog',
			site_url: 'https://yourjetpack.blog',
			state: '1',
		};
		const result = parseAuthorizationQuery( data );
		expect( result.isPopup ).toBe( false );
		expect( result.closeWindowAfterLogin ).toBe( false );
		expect( result.closeWindowAfterAuthorize ).toBe( false );
	} );

	test( 'should return null data on valid input', () => {
		expect( parseAuthorizationQuery( {} ) ).toBeNull();
	} );
} );

jest.mock( 'calypso/lib/navigate', () => ( {
	navigate: jest.fn(),
} ) );

jest.mock( '@automattic/calypso-router', () => ( {
	redirect: jest.fn(),
} ) );

describe( 'redirect', () => {
	beforeEach( () => {
		navigate.mockReset();
		page.redirect.mockReset();
	} );
	test( 'should fire redirect', () => {
		const results = [
			{ type: 'plans_selection', url: 'example.com', expected: JPC_PATH_PLANS + '/example.com' },
			{ type: 'remote_install', url: 'example.com', expected: JPC_PATH_REMOTE_INSTALL },
			{
				type: 'install_instructions',
				url: 'example.com',
				expected: '/jetpack/connect/instructions?url=example.com',
			},
			{
				type: 'remote_auth',
				url: 'example.com',
				expected: decodeURI( addCalypsoEnvQueryArg( 'example.com' + REMOTE_PATH_AUTH ) ),
			},
		];

		results.forEach( ( { type, url, expected } ) =>
			expect( redirect( type, url ) ).toBe( expected )
		);
	} );

	test( 'external redirect should be called once during remote authorization', () => {
		redirect( 'remote_auth', 'example.com' );
		expect( navigate ).toHaveBeenCalledTimes( 1 );
	} );

	test( 'should redirect once', () => {
		redirect( 'plans_selection', 'example.com' );
		expect( page.redirect ).toHaveBeenCalledTimes( 1 );
		page.redirect.mockReset();

		redirect( 'remote_install', 'example.com' );
		expect( page.redirect ).toHaveBeenCalledTimes( 1 );
		page.redirect.mockReset();

		redirect( 'install_instructions', 'example.com' );
		expect( page.redirect ).toHaveBeenCalledTimes( 1 );
	} );
} );