File size: 6,822 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
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
import { IncomingMessage } from 'http';
import config from '@automattic/calypso-config';
import nock from 'nock';
import getBootstrappedUser from '../index';

jest.mock( '@automattic/calypso-config', () => {
	const impl = jest.fn();
	impl.isEnabled = jest.fn();
	return impl;
} );

const mockRequest = ( { cookies = {}, headers = {} } = {} ) => {
	return Object.assign( new IncomingMessage(), {
		body: {},
		cookies,
		query: {},
		params: {},
		get: jest.fn().mockImplementation( ( key ) => headers[ key ] ),
	} );
};

const configureUpstreamRequest = ( {
	headers = {},
	response = { ID: '1' },
	respondWithError = false,
} = {} ) => {
	return nock( 'https://public-api.wordpress.com', {
		reqheaders: headers,
	} )
		.get( '/rest/v1/me?meta=flags' )
		.reply( respondWithError ? 500 : 200, response );
};

const withConfig = ( keys ) => {
	config.mockImplementation( ( key ) => keys[ key ] );
};

/**
 * These tests rely on nock to intercept the requets and return a pre-defined response.
 *
 * If `getBootstrappedUser()` sends a request that doesn't match any of the requests configured in `nock`, it will throw. In most
 * tests we just assert that `getBootstrappedUser()` returns a resolved promise, as it proves that it sent the expected request.
 */
describe( 'User bootstrap', () => {
	beforeEach( () => {
		// Default value for most tests. Some tests will overwrite this to simulate missing key
		withConfig( {
			wpcom_calypso_rest_api_key: 'key',
		} );
	} );

	afterEach( () => {
		nock.cleanAll();
		jest.resetAllMocks();
	} );

	it( 'throws if there is no auth cookie', async () => {
		const request = mockRequest( { cookies: {} } );

		await expect( getBootstrappedUser( request ) ).rejects.toThrow(
			new Error( 'Cannot bootstrap without an auth cookie' )
		);
	} );

	it( 'throws if there is supportSessionHeader and supportSessionCookie', async () => {
		const request = mockRequest( {
			cookies: {
				wordpress_logged_in: 'auth-cookie',
				support_session_id: 'session-id',
			},
			headers: {
				'x-support-session': 'session-id',
			},
		} );

		await expect( getBootstrappedUser( request ) ).rejects.toThrow(
			new Error( 'Cannot bootstrap with both a support session header and support session cookie.' )
		);
	} );

	it( 'sets the user agent in the upstream request', async () => {
		configureUpstreamRequest( {
			headers: {
				'User-Agent': 'WordPress.com Calypso',
			},
		} );
		const request = mockRequest( {
			cookies: {
				wordpress_logged_in: 'auth-cookie',
			},
		} );

		await expect( getBootstrappedUser( request ) ).resolves.not.toThrow();
	} );

	it( 'sets the geo country code in the upstream request', async () => {
		configureUpstreamRequest( {
			headers: {
				'X-Forwarded-GeoIP-Country-Code': 'es',
			},
		} );
		const request = mockRequest( {
			cookies: {
				wordpress_logged_in: 'auth-cookie',
			},
			headers: {
				'x-geoip-country-code': 'es',
			},
		} );

		await expect( getBootstrappedUser( request ) ).resolves.not.toThrow();
	} );

	it( 'sets the support session cookie', async () => {
		configureUpstreamRequest( {
			headers: {
				cookie: ( val ) => val.includes( 'support_session_id=session-id' ),
			},
		} );
		const request = mockRequest( {
			cookies: {
				wordpress_logged_in: 'auth-cookie',
				support_session_id: 'session-id',
			},
		} );

		await expect( getBootstrappedUser( request ) ).resolves.not.toThrow();
	} );

	describe( 'with support session header', () => {
		let request;

		beforeEach( () => {
			request = mockRequest( {
				cookies: {
					wordpress_logged_in: 'auth-cookie',
				},
				headers: {
					'x-support-session': 'session-id',
				},
			} );
			configureUpstreamRequest( {
				headers: {
					// Hardcoded value resulting from hashing "key" + "session-id"
					Authorization: 'X-WPCALYPSO-SUPPORT-SESSION bc8844a622734cad18a2ec733e11b296',
					'x-support-session': 'session-id',
				},
			} );
		} );

		it( 'throws if there is no support session API key', async () => {
			withConfig( {
				wpcom_calypso_support_session_rest_api_key: undefined,
			} );

			await expect( getBootstrappedUser( request ) ).rejects.toThrow(
				new Error(
					'Unable to bootstrap user because of invalid SUPPORT SESSION API key in secrets.json'
				)
			);
		} );

		it( 'sets the authorization header', async () => {
			withConfig( {
				wpcom_calypso_support_session_rest_api_key: 'key',
			} );

			await expect( getBootstrappedUser( request ) ).resolves.not.toThrow();
		} );
	} );

	describe( 'with auth session', () => {
		let request;

		beforeEach( () => {
			request = mockRequest( {
				cookies: {
					wordpress_logged_in: 'auth-cookie',
				},
			} );
			configureUpstreamRequest( {
				headers: {
					// Hardcoded value resulting from hashing "key" + "auth-cookie"
					Authorization: 'X-WPCALYPSO 26be6ad9e36fde3770b1e81a559db109',
					cookie: ( cookie ) => cookie.includes( 'wordpress_logged_in=auth-cookie' ),
				},
			} );
		} );

		it( 'throws if there is no API key', async () => {
			withConfig( {
				wpcom_calypso_rest_api_key: undefined,
			} );

			await expect( getBootstrappedUser( request ) ).rejects.toThrow(
				new Error( 'Unable to bootstrap user because of invalid API key in secrets.json' )
			);
		} );

		it( 'sets the authorization header and cookie', async () => {
			withConfig( {
				wpcom_calypso_rest_api_key: 'key',
			} );

			await expect( getBootstrappedUser( request ) ).resolves.not.toThrow();
		} );
	} );

	it( 'returns the user from the response', async () => {
		configureUpstreamRequest( {
			response: {
				ID: '1',
				username: 'test',
			},
		} );

		const request = mockRequest( {
			cookies: {
				wordpress_logged_in: 'auth-cookie',
			},
		} );

		const user = await getBootstrappedUser( request );

		expect( user.ID ).toBe( '1' );
		expect( user.username ).toBe( 'test' );
	} );

	it( 'filters out invalid properties', async () => {
		configureUpstreamRequest( {
			response: {
				ID: '1',
				unknown_field: 'test',
			},
		} );

		const request = mockRequest( {
			cookies: {
				wordpress_logged_in: 'auth-cookie',
			},
		} );

		const user = await getBootstrappedUser( request );

		expect( user.unknown_field ).toBe( undefined );
	} );

	it( 'adds bootstrapped property to the response', async () => {
		configureUpstreamRequest();

		const request = mockRequest( {
			cookies: {
				wordpress_logged_in: 'auth-cookie',
			},
		} );

		const user = await getBootstrappedUser( request );

		expect( user.bootstrapped ).toBe( true );
	} );

	it( "throws if the request doesn't have a response", async () => {
		configureUpstreamRequest( {
			respondWithError: true,
		} );

		const request = mockRequest( {
			cookies: {
				wordpress_logged_in: 'auth-cookie',
			},
		} );

		await expect( getBootstrappedUser( request ) ).rejects.toThrow();
	} );
} );