File size: 8,012 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
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
 * @jest-environment jsdom
 */
// @ts-nocheck - TODO: Fix TypeScript issues
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react';
import nock from 'nock';
import React from 'react';
import { logToLogstash } from 'calypso/lib/logstash';
import { usePluginAutoInstallation } from '../use-plugin-auto-installation';
import { replyWithEnvelope } from './helpers/nock';

const Wrapper =
	( queryClient: QueryClient ) =>
	( { children } ) => {
		return <QueryClientProvider client={ queryClient }>{ children }</QueryClientProvider>;
	};

const PLUGIN = { name: 'migrate-guru/migrateguru', slug: 'migrate-guru' };
const getSitePluginsEndpoint = ( siteId: number ) =>
	`/rest/v1.2/sites/${ siteId }/plugins?http_envelope=1`;

const getJetpackReconnectionEndpoint = ( siteId: number ) =>
	`/rest/v1.2/sites/${ siteId }/migration-force-reconnection`;

const getPluginInstallationEndpoint = ( siteId: number ) =>
	`/rest/v1.2/sites/${ siteId }/plugins/migrate-guru/install?http_envelope=1`;

const getPluginActivationEndpoint = ( siteId: number ) =>
	`/rest/v1.2/sites/${ siteId }/plugins/migrate-guru%2Fmigrateguru`;

const SITE_ID = 123;

const render = ( { retry = 0, enabled = true } = {} ) => {
	const queryClient = new QueryClient();

	const renderResult = renderHook(
		() => usePluginAutoInstallation( PLUGIN, SITE_ID, { retry, enabled } ),
		{
			wrapper: Wrapper( queryClient ),
		}
	);

	return {
		...renderResult,
		queryClient,
	};
};
const installationWithSuccess = replyWithEnvelope( 200 );
const installationWithGenericError = replyWithEnvelope( 400, { error: 'any error' } );
jest.mock( 'calypso/lib/logstash' );

describe( 'usePluginAutoInstallation', () => {
	beforeAll( () => {
		nock.disableNetConnect();
	} );
	beforeEach( () => nock.cleanAll() );

	it( 'returns success when the plugin is already installed and activated', async () => {
		nock( 'https://public-api.wordpress.com:443' )
			.get( getSitePluginsEndpoint( SITE_ID ) )
			.once()
			.reply( 200, { plugins: [ { ...PLUGIN, active: true } ] } );

		const { result } = render();

		await waitFor( () => {
			expect( result.current ).toEqual( {
				status: 'success',
				error: null,
				completed: true,
			} );
		} );
	} );

	it( 'returns status "pending" when a retry is required to get the plugin list', async () => {
		const siteId = 123;

		nock( 'https://public-api.wordpress.com:443' )
			.get( getSitePluginsEndpoint( siteId ) )
			.times( 4 )
			.reply( 500, new Error( 'Error fetching plugins list' ) );

		const { result } = render( { retry: 4 } );

		await waitFor(
			() => {
				expect( result.current ).toEqual( {
					status: 'pending',
					error: null,
					completed: false,
				} );
			},
			{ timeout: 3000 }
		);
	} );

	it( 'returns "pending" when a retry is required to install a plugin', async () => {
		nock( 'https://public-api.wordpress.com:443' )
			.get( getSitePluginsEndpoint( SITE_ID ) )
			.once()
			.reply( 200, { plugins: [] } )
			.post( getPluginInstallationEndpoint( SITE_ID ) )
			.reply( installationWithGenericError() );
		const { result } = render( { retry: 2 } );

		await waitFor(
			() => {
				expect( result.current ).toEqual( {
					status: 'pending',
					error: null,
					completed: false,
				} );
			},
			{ timeout: 3000 }
		);
	} );

	it( 'returns "pending" when a retry is required to active a plugin', async () => {
		nock( 'https://public-api.wordpress.com:443' )
			.get( getSitePluginsEndpoint( SITE_ID ) )
			.once()
			.reply( 200, { plugins: [] } )
			.post( getPluginInstallationEndpoint( SITE_ID ) )
			.reply( installationWithSuccess() )
			.post( getPluginActivationEndpoint( SITE_ID ) )
			.reply( 500, new Error( 'Error activating plugin' ) );

		const { result } = render( { retry: 2 } );

		await waitFor(
			() => {
				expect( result.current ).toEqual( {
					status: 'pending',
					error: null,
					completed: false,
				} );
			},
			{ timeout: 3000 }
		);
	} );

	it( 'returns completed and status success when all steps are successful', async () => {
		nock( 'https://public-api.wordpress.com:443' )
			.get( getSitePluginsEndpoint( SITE_ID ) )
			.once()
			.reply( 200, { plugins: [] } )
			.post( getPluginInstallationEndpoint( SITE_ID ) )
			.reply( installationWithSuccess() )
			.post( getPluginActivationEndpoint( SITE_ID ) )
			.reply( 200 );

		const { result } = render( { retry: 0 } );

		await waitFor(
			() => {
				expect( result.current ).toEqual( {
					status: 'success',
					error: null,
					completed: true,
				} );
			},
			{ timeout: 3000 }
		);
	} );

	it( 'returns error after all fetching plugin list retries failed', async () => {
		nock( 'https://public-api.wordpress.com:443' )
			.get( getSitePluginsEndpoint( SITE_ID ) )
			.once()
			.times( 2 )
			.reply( 500, new Error( 'Error fetching plugins list' ) );

		nock( 'https://public-api.wordpress.com:443' )
			.post( getJetpackReconnectionEndpoint( SITE_ID ) )
			.reply( 200 )
			.persist();

		const { result } = render( { retry: 1 } );

		await waitFor(
			() => {
				expect( result.current ).toEqual( {
					status: 'error',
					error: expect.any( Error ),
					completed: false,
				} );
			},
			{ timeout: 3000 }
		);
	} );

	it( 'refresh the jetpack connnection after all plugin list retries', async () => {
		// First 3 calls to get the plugins list will fail
		// The reconnection endpoint will be called after the 3rd failure
		// The 4th call to get the plugins list will succeed
		nock( 'https://public-api.wordpress.com:443' )
			.get( getSitePluginsEndpoint( SITE_ID ) )
			.times( 3 )
			.reply( 500, new Error( 'Error fetching plugins list' ) )
			.get( getSitePluginsEndpoint( SITE_ID ) )
			.reply( 200, { plugins: [ { ...PLUGIN, active: true } ] } );

		const refreshtokenCall = nock( 'https://public-api.wordpress.com:443' )
			.post( getJetpackReconnectionEndpoint( SITE_ID ) )
			.once()
			.reply( 200 );

		const { result } = render( { retry: 2 } );

		await waitFor(
			() => {
				expect( refreshtokenCall.isDone() ).toBe( true );
				expect( result.current ).toEqual( {
					status: 'success',
					error: null,
					completed: true,
				} );

				expect( logToLogstash ).toHaveBeenCalledWith(
					expect.objectContaining( { message: 'restoring jetpack connection' } )
				);
				expect( logToLogstash ).toHaveBeenCalledWith(
					expect.objectContaining( { message: 'jetpack connection restored' } )
				);
			},
			{ timeout: 3000 }
		);
	} );

	it( 'logs to logstash when all jetpack connection were exhausted', async () => {
		nock( 'https://public-api.wordpress.com:443' )
			.get( getSitePluginsEndpoint( SITE_ID ) )
			.times( 9 )
			.reply( 500, new Error( 'Error fetching plugins list' ) );

		nock( 'https://public-api.wordpress.com:443' )
			.post( getJetpackReconnectionEndpoint( SITE_ID ) )
			.times( 3 )
			.reply( 200 );

		render( { retry: 1 } );

		await waitFor(
			() => {
				expect( logToLogstash ).toHaveBeenCalledWith(
					expect.objectContaining( { message: 'jetpack connection restoration attempts failed' } )
				);
			},
			{ timeout: 3000 }
		);
	} );

	it( 'returns error after all installation retries', async () => {
		nock( 'https://public-api.wordpress.com:443' )
			.get( getSitePluginsEndpoint( SITE_ID ) )
			.once()
			.reply( 200, { plugins: [] } )
			.post( getPluginInstallationEndpoint( SITE_ID ) )
			.times( 4 )
			.reply( installationWithGenericError )
			.post( getPluginActivationEndpoint( SITE_ID ) )
			.reply( 200 );

		const { result } = render( { retry: 1 } );

		await waitFor(
			() => {
				expect( result.current ).toEqual( {
					status: 'error',
					error: expect.any( Error ),
					completed: false,
				} );
			},
			{ timeout: 3000 }
		);
	} );

	it( "doesn't start the operation when the enabled is set to false", async () => {
		const { result } = render( { enabled: false } );

		expect( result.current ).toEqual( {
			status: 'idle',
			error: null,
			completed: false,
		} );
	} );
} );