Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use remote-site-status to check the WPCOM Auth status #2547

Merged
merged 8 commits into from
Sep 5, 2024

Conversation

puntope
Copy link
Contributor

@puntope puntope commented Aug 20, 2024

Changes proposed in this Pull Request:

Context: p1724064639654099/1722414955.111429-slack-C02BB3F30TG

When authorizing Google to fetch data from the Shop as well as generating a token we set an option gla_wpcom_rest_api_status as approved. If the token is revoked outside the extension, the option will still be approved and the notifications will be sent.

This PR handles that and checks the status of the token using the WPCOM REST API.

Screenshots:

Detailed test instructions:

  1. Disable fetch data from Settings or disconnect it from the Connection Test page
  2. Grant access from scratch still works.
  3. You can send notifications in the Connection test page for example
  4. Now disconnect WPCOM Access from Connection Test page
  5. You CANNOT send notifications anymore
  6. Set gla_wpcom_rest_api_status as approved to simulate a granted access that has no token.
  7. You CANNOT send notifications anymore
  8. Go to settings. You see the warning notice in the MC Card
  9. Grant access again
  10. Now You can send notifications again in the Connection test page

Additional details:

ℹ️ I was thinking about removing gla_wpcom_rest_api_status and only relying on the transient + WPCOM REST API. status endpoint for considering the WPCOM API as ready. I would appreciate thoughts on this.

Changelog entry

Tweak - Use remote-site-status to check the WPCOM Auth status

@github-actions github-actions bot added the changelog: tweak Small change, that isn't actually very important. label Aug 20, 2024
@puntope puntope self-assigned this Aug 20, 2024
@puntope puntope requested a review from a team August 20, 2024 11:31
@puntope puntope marked this pull request as ready for review August 20, 2024 11:32
Copy link

codecov bot commented Aug 20, 2024

Codecov Report

Attention: Patch coverage is 56.25000% with 14 lines in your changes missing coverage. Please review.

Project coverage is 65.0%. Comparing base (cc158ab) to head (fd5d8aa).
Report is 115 commits behind head on develop.

Files with missing lines Patch % Lines
src/MerchantCenter/AccountService.php 54.2% 11 Missing ⚠️
src/ConnectionTest.php 0.0% 2 Missing ⚠️
...ernal/DependencyManagement/CoreServiceProvider.php 0.0% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             develop   #2547     +/-   ##
===========================================
- Coverage       65.0%   65.0%   -0.0%     
- Complexity      4585    4602     +17     
===========================================
  Files            475     475             
  Lines          17898   17932     +34     
===========================================
+ Hits           11640   11660     +20     
- Misses          6258    6272     +14     
Flag Coverage Δ
php-unit-tests 65.0% <56.2%> (-<0.1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/API/WP/NotificationsService.php 95.2% <100.0%> (+0.1%) ⬆️
src/Settings/SyncerHooks.php 100.0% <100.0%> (ø)
...ernal/DependencyManagement/CoreServiceProvider.php 0.0% <0.0%> (ø)
src/ConnectionTest.php 0.0% <0.0%> (ø)
src/MerchantCenter/AccountService.php 94.4% <54.2%> (-3.9%) ⬇️

... and 3 files with indirect coverage changes

Copy link
Contributor

@jorgemd24 jorgemd24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @puntope, for working on this. It works as you described in the PR, but I’ve left some concerns about how we’re handling the errors returned by the remote status endpoint.

Comment on lines 665 to 668
if ( is_wp_error( $integration_remote_request_response ) ) {
$this->delete_wpcom_api_status_transient();
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the token isn't healthy or if the user disconnect Jetpack, does that mean we'll still make a request every time , even though we know the token isn't valid? Shouldn't we store that status in the transient as well?

I also noticed that the SyncerHooks class is registered every time the plugin is initialized, which means we could end up calling this endpoint repeatedly without end.

public function register(): void {
if ( ! $this->notifications_service->is_ready() ) {
return;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes, but I still see this issue. If the response is wp_error, we will keep querying the endpoint. Wouldn't it be better to update the transient with the status error instead?

@@ -5,6 +5,8 @@

use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerService;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\OAuthService;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused dependency


$status = json_decode( wp_remote_retrieve_body( $integration_remote_request_response ), true ) ?? [];

if ( isset( $status['error'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response from the remote status endpoints returns errors instead of error, should it be error?

@puntope
Copy link
Contributor Author

puntope commented Aug 22, 2024

Thanks for your useful review @jorgemd24 I addressed your comments so now I believe this is ready for a new round

@puntope puntope requested a review from jorgemd24 August 22, 2024 15:21
Copy link
Contributor

@jorgemd24 jorgemd24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes, @puntope! However I’m still seeing some possible issues—see my comments. Thanks!

@@ -227,6 +229,11 @@ public function get_connected_status(): array {
$id = $this->options->get_merchant_id();
$wpcom_rest_api_status = $this->options->get( OptionsInterface::WPCOM_REST_API_STATUS );

// If token is revoked outside the extension. Set the status as error to force the merchant to grant access again.
if ( $wpcom_rest_api_status === 'approved' && ! $this->is_wpcom_api_status_healthy() ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think if we update the WPCOM_REST_API_STATUS accordingly? Or Maybe is better just to delete the option so then the user can start the onboarding again.

src/API/WP/NotificationsService.php Show resolved Hide resolved
src/MerchantCenter/AccountService.php Outdated Show resolved Hide resolved
Comment on lines 665 to 668
if ( is_wp_error( $integration_remote_request_response ) ) {
$this->delete_wpcom_api_status_transient();
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes, but I still see this issue. If the response is wp_error, we will keep querying the endpoint. Wouldn't it be better to update the transient with the status error instead?

@puntope
Copy link
Contributor Author

puntope commented Sep 2, 2024

Hi @jorgemd24 for the review. I updated again the code with your suggestions:

Can we do a new round of review?

Thanks

Copy link
Contributor

@jorgemd24 jorgemd24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the adjustments, @puntope! One last thing: if the token isn't valid, shouldn't we reset the gla_wpcom_rest_api_status option? Currently, if I disconnect the GFW app, it doesn't send the notification, but the status still shows as "approved."

image

@puntope
Copy link
Contributor Author

puntope commented Sep 4, 2024

if the token isn't valid, shouldn't we reset the gla_wpcom_rest_api_status option? Currently, if I disconnect the GFW app, it doesn't send the notification, but the status still shows as "approved."

Hi @jorgemd24 Thanks for the review. Can you elaborate the steps for this use case? As far as I understand, When the token is invalid we set the transient and option as "error". (See AccountService::get_connected_status and AccuntService::is_wpcom_api_status_healthy)

Copy link
Contributor

@jorgemd24 jorgemd24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jorgemd24 Thanks for the review. Can you elaborate the steps for this use case? As far as I understand, When the token is invalid we set the transient and option as "error". (See AccountService::get_connected_status and AccuntService::is_wpcom_api_status_healthy)

Thanks for the clarification! I think this happened because I had the transient set to healthy. Then, I disconnected the Google App and refreshed the settings page, but since the transient was still set to "healthy" it appeared as if it was approved. But now I realize this was just a coincidence when I was doing the review and that the issue is temporary. So, I think we can move forward with this PR!

@puntope puntope merged commit 7210b14 into develop Sep 5, 2024
12 checks passed
@puntope puntope deleted the tweak/check-wpcom-api-auth branch September 5, 2024 07:42
@martynmjones martynmjones mentioned this pull request Sep 5, 2024
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: tweak Small change, that isn't actually very important.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants