site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Fix REST API 404 response during Client Credentials setup step

Open lilybonney opened this issue 7 years ago • 2 comments

Bug Description

Several REST API endpoints (i.e. credentials, API key, GCP project) return a 404 if no value is set for them yet (being most apparent during the setup flow when Site Kit checks for whether OAuth credentials are already set). This was intentionally designed like that originally, but revisiting it doesn't actually make sense. That response code should only be expected if 404 is seen as "the data doesn't exist", however not in the more RESTful way of 404 meaning that "the resource doesn't exist". The resource does in fact exist as it is the endpoint itself. The only place in REST where 404s should be used are route URIs containing variables where the passed variable name doesn't identity an existing resource. For all these endpoints in Site Kit, that is however not the case, so the response for all of them should be turned into a 200.

Steps to reproduce

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Screenshots

Additional Context

  • PHP Version:
  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Plugin Version [e.g. 22]
  • Device: [e.g. iPhone6]

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • No 404 error should be given for any request in the console during the client credentials step in the setup wizard.
  • The routes core/site/data/gcpproject, core/site/data/apikey, and core/site/data/credentials can never return a 404, but instead a 200 with an empty dataset following the required format (object with expected properties for gcpproject and credentials, falsy scalar value for apikey).
  • All JavaScript code relying on one of these values must be ensured to correctly check whether the respective value is present and not empty, to replace the "falsy" lookup previously covered by the 404.

Implementation Brief

  • Add parse_defaults() method to Authentication\Credentials class, which works similar to the one in GCP_Project\Credentials class, ensuring oauth2_client_id and oauth2_client_secret are always present in the returned object.
  • Remove the ! $gcp_project->has() if clause from the GET core/site/data/gcpproject route in REST_API\REST_Routes.
  • Remove the ! $api_key->has() if clause from the GET core/site/data/apikey route in REST_API\REST_Routes.
  • Remove the ! $credentials->has() if clause from the GET core/site/data/credentials route in REST_API\REST_Routes.
  • Change https://github.com/google/site-kit-wp/blob/develop/assets/js/components/setup-wizard/wizard-step-client-credentials.js#L67 to check for response && response.oauth2_client_id && response.oauth2_client_secret instead, to account for the new response.
  • The other two changed routes aren't actually called by the plugin currently and therefore don't need adjustment.

Changelog entry

  • Fix inaccurate 404 error response of client credentials REST API call during setup.

lilybonney avatar Jul 18 '19 11:07 lilybonney

IB LGTM!

ThierryA avatar Aug 12 '19 12:08 ThierryA

QA PASS

Went through setup flow a couple times and did not see any 404s in console or network.

circlecube avatar Aug 22 '19 17:08 circlecube