From 29bb7d492fd0170bee90574a767579df6ef78adb Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Thu, 11 Jun 2020 13:09:54 -0400 Subject: [PATCH 1/6] Fix installing blocks from the block directory. A block installation error is now expressed by returning an error response, this means looking for a success property no longer works since we are returning the installed plugin information instead. Also switches to making an OPTIONS request to the search controller instead of making an "empty" search request as a search term is now required. --- packages/block-directory/src/store/actions.js | 5 +--- .../block-directory/src/store/resolvers.js | 27 ++++++++++++++----- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/block-directory/src/store/actions.js b/packages/block-directory/src/store/actions.js index beea3d05ab01e9..f14fb51b177454 100644 --- a/packages/block-directory/src/store/actions.js +++ b/packages/block-directory/src/store/actions.js @@ -66,16 +66,13 @@ export function* installBlockType( block ) { throw new Error( __( 'Block has no assets.' ) ); } yield setIsInstalling( block.id, true ); - const response = yield apiFetch( { + yield apiFetch( { path: '__experimental/block-directory/install', data: { slug: block.id, }, method: 'POST', } ); - if ( response.success !== true ) { - throw new Error( __( 'Unable to install this block.' ) ); - } yield addInstalledBlockType( block ); yield loadAssets( assets ); diff --git a/packages/block-directory/src/store/resolvers.js b/packages/block-directory/src/store/resolvers.js index d2ac2f167ce4ba..0f747424523d09 100644 --- a/packages/block-directory/src/store/resolvers.js +++ b/packages/block-directory/src/store/resolvers.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { camelCase, mapKeys } from 'lodash'; +import { camelCase, get, hasIn, includes, mapKeys } from 'lodash'; /** * WordPress dependencies @@ -43,14 +43,27 @@ export default { }, *hasInstallBlocksPermission() { try { - yield apiFetch( { - path: `__experimental/block-directory/search?term=`, + const response = yield apiFetch( { + method: 'OPTIONS', + path: `__experimental/block-directory/search`, + parse: false, } ); - yield setInstallBlocksPermission( true ); - } catch ( error ) { - if ( error.code === 'rest_user_cannot_view' ) { - yield setInstallBlocksPermission( false ); + + let allowHeader; + if ( hasIn( response, [ 'headers', 'get' ] ) ) { + // If the request is fetched using the fetch api, the header can be + // retrieved using the 'get' method. + allowHeader = response.headers.get( 'allow' ); + } else { + // If the request was preloaded server-side and is returned by the + // preloading middleware, the header will be a simple property. + allowHeader = get( response, [ 'headers', 'Allow' ], '' ); } + + const isAllowed = includes( allowHeader, 'GET' ); + yield setInstallBlocksPermission( isAllowed ); + } catch ( error ) { + yield setInstallBlocksPermission( false ); } }, }; From 22ef3f9476c235824fb56bd8a0a1bbe7c63faaa1 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Thu, 11 Jun 2020 15:17:11 -0400 Subject: [PATCH 2/6] Fix unit test failure. An error is now indicated by a failed response which throws --- packages/block-directory/src/store/test/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-directory/src/store/test/actions.js b/packages/block-directory/src/store/test/actions.js index eaac933fb0a65c..4ecb769260b448 100644 --- a/packages/block-directory/src/store/test/actions.js +++ b/packages/block-directory/src/store/test/actions.js @@ -109,7 +109,7 @@ describe( 'actions', () => { message: 'Plugin not found.', data: null, }; - expect( generator.next( apiError ).value ).toMatchObject( { + expect( generator.throw( apiError ).value ).toMatchObject( { type: 'SET_ERROR_NOTICE', blockId: item.id, } ); From 0e104a80516f979948e5a8b1d08470725956d6f4 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Thu, 11 Jun 2020 17:16:20 -0400 Subject: [PATCH 3/6] Update mock block install response --- .../experiments/block-directory-add.test.js | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/e2e-tests/specs/experiments/block-directory-add.test.js b/packages/e2e-tests/specs/experiments/block-directory-add.test.js index 7d21159c4b2562..6e9d905fb89c0f 100644 --- a/packages/e2e-tests/specs/experiments/block-directory-add.test.js +++ b/packages/e2e-tests/specs/experiments/block-directory-add.test.js @@ -47,6 +47,24 @@ const MOCK_BLOCK1 = { humanized_updated: '5 months ago', }; +const MOCK_PLUGIN = { + plugin: 'block-directory-test-block', + status: 'active', + name: 'Block Directory', + plugin_uri: '', + author: 'No Author', + author_uri: '', + description: { + raw: 'This plugin is useful for the block.', + rendered: 'This plugin is useful for the block.', + }, + version: '1.0', + network_only: false, + requires_wp: '', + requires_php: '', + text_domain: 'block-directory-test-block', +}; + const MOCK_BLOCK2 = { ...MOCK_BLOCK1, name: 'block-directory-test-block/secondary-block', @@ -89,7 +107,7 @@ const MOCK_BLOCKS_RESPONSES = [ { // Mock response for install match: ( request ) => matchUrl( request.url(), INSTALL_URLS ), - onRequestMatch: createJSONResponse( { success: true } ), + onRequestMatch: createJSONResponse( MOCK_PLUGIN ), }, { // Mock the response for the js asset once it gets injected From 3df1107495f5cfbf01f77862cdd3abca6ade4574 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Fri, 12 Jun 2020 13:38:41 -0400 Subject: [PATCH 4/6] Mock the OPTIONS request used for a permissions check. --- .../block-directory/src/store/resolvers.js | 2 +- .../experiments/block-directory-add.test.js | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/block-directory/src/store/resolvers.js b/packages/block-directory/src/store/resolvers.js index 0f747424523d09..5ca5f025e791a1 100644 --- a/packages/block-directory/src/store/resolvers.js +++ b/packages/block-directory/src/store/resolvers.js @@ -36,7 +36,7 @@ export default { yield receiveDownloadableBlocks( blocks, filterValue ); } catch ( error ) { - if ( error.code === 'rest_user_cannot_view' ) { + if ( error.code === 'rest_block_directory_cannot_view' ) { yield setInstallBlocksPermission( false ); } } diff --git a/packages/e2e-tests/specs/experiments/block-directory-add.test.js b/packages/e2e-tests/specs/experiments/block-directory-add.test.js index 6e9d905fb89c0f..b1398607f1ed13 100644 --- a/packages/e2e-tests/specs/experiments/block-directory-add.test.js +++ b/packages/e2e-tests/specs/experiments/block-directory-add.test.js @@ -91,7 +91,42 @@ const block = `( function() { } ); } )();`; +const MOCK_OPTIONS = { + namespace: '__experimental', + methods: [ 'GET' ], + endpoints: [ + { + methods: [ 'GET' ], + args: {}, + }, + ], + schema: { + $schema: 'http://json-schema.org/draft-04/schema#', + title: 'block-directory-item', + type: 'object', + properties: {}, + }, +}; + +const MOCK_OPTIONS_RESPONSE = { + match: ( request ) => + matchUrl( request.url(), SEARCH_URLS ) && + request.method() === 'OPTIONS', + onRequestMatch: async ( request ) => { + const response = { + content: 'application/json', + body: JSON.stringify( MOCK_OPTIONS ), + headers: { + Allow: 'GET', + }, + }; + + return request.respond( response ); + }, +}; + const MOCK_EMPTY_RESPONSES = [ + MOCK_OPTIONS_RESPONSE, { match: ( request ) => matchUrl( request.url(), SEARCH_URLS ), onRequestMatch: createJSONResponse( [] ), @@ -99,6 +134,7 @@ const MOCK_EMPTY_RESPONSES = [ ]; const MOCK_BLOCKS_RESPONSES = [ + MOCK_OPTIONS_RESPONSE, { // Mock response for search with the block match: ( request ) => matchUrl( request.url(), SEARCH_URLS ), From b6689ebe893c9a226c9cef7a00c98a23375cf877 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Sun, 14 Jun 2020 20:12:32 -0400 Subject: [PATCH 5/6] Improve name of mock plugin data constant. --- .../e2e-tests/specs/experiments/block-directory-add.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/e2e-tests/specs/experiments/block-directory-add.test.js b/packages/e2e-tests/specs/experiments/block-directory-add.test.js index b1398607f1ed13..f14c9ee0d97ca8 100644 --- a/packages/e2e-tests/specs/experiments/block-directory-add.test.js +++ b/packages/e2e-tests/specs/experiments/block-directory-add.test.js @@ -47,7 +47,7 @@ const MOCK_BLOCK1 = { humanized_updated: '5 months ago', }; -const MOCK_PLUGIN = { +const MOCK_INSTALLED_BLOCK_PLUGIN_DETAILS = { plugin: 'block-directory-test-block', status: 'active', name: 'Block Directory', @@ -143,7 +143,7 @@ const MOCK_BLOCKS_RESPONSES = [ { // Mock response for install match: ( request ) => matchUrl( request.url(), INSTALL_URLS ), - onRequestMatch: createJSONResponse( MOCK_PLUGIN ), + onRequestMatch: createJSONResponse( MOCK_INSTALLED_BLOCK_PLUGIN_DETAILS ), }, { // Mock the response for the js asset once it gets injected From 465b9ab288f3d6efb44720e500b1b20fd50ad111 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Sun, 14 Jun 2020 20:53:12 -0400 Subject: [PATCH 6/6] Fix formatting --- .../e2e-tests/specs/experiments/block-directory-add.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/e2e-tests/specs/experiments/block-directory-add.test.js b/packages/e2e-tests/specs/experiments/block-directory-add.test.js index f14c9ee0d97ca8..c3ad73ea7ea655 100644 --- a/packages/e2e-tests/specs/experiments/block-directory-add.test.js +++ b/packages/e2e-tests/specs/experiments/block-directory-add.test.js @@ -143,7 +143,9 @@ const MOCK_BLOCKS_RESPONSES = [ { // Mock response for install match: ( request ) => matchUrl( request.url(), INSTALL_URLS ), - onRequestMatch: createJSONResponse( MOCK_INSTALLED_BLOCK_PLUGIN_DETAILS ), + onRequestMatch: createJSONResponse( + MOCK_INSTALLED_BLOCK_PLUGIN_DETAILS + ), }, { // Mock the response for the js asset once it gets injected