@@ -491,6 +491,32 @@ public function testRefreshTokenWithInvalidToken()
491
491
$ this ->assertEquals ($ result ->get_error_code (), 'convertkit_api_error ' );
492
492
}
493
493
494
+ /**
495
+ * Test that making a call with an expired access token results in refresh_token()
496
+ * not being automatically called, when the WordPress site isn't a production site.
497
+ *
498
+ * @since 2.0.2
499
+ *
500
+ * @return void
501
+ */
502
+ public function testRefreshTokenWhenAccessTokenExpiredErrorOnNonProductionSite ()
503
+ {
504
+ // If the refresh token action in the libraries is triggered when calling get_account(), the test failed.
505
+ add_action (
506
+ 'convertkit_api_refresh_token ' ,
507
+ function () {
508
+ $ this ->fail ('`convertkit_api_refresh_token` was triggered when calling `get_account` with an expired access token on a non-production site. ' );
509
+ }
510
+ );
511
+
512
+ // Filter requests to mock the token expiry and refreshing the token.
513
+ add_filter ( 'pre_http_request ' , array ( $ this , 'mockAccessTokenExpiredResponse ' ), 10 , 3 );
514
+ add_filter ( 'pre_http_request ' , array ( $ this , 'mockRefreshTokenResponse ' ), 10 , 3 );
515
+
516
+ // Run request, which will trigger the above filters as if the token expired and refreshes automatically.
517
+ $ result = $ this ->api ->get_account ();
518
+ }
519
+
494
520
/**
495
521
* Test that supplying no API credentials to the API class returns a WP_Error.
496
522
*
@@ -6240,6 +6266,90 @@ function( $response ) use ( $httpCode, $httpMessage, $body ) { // phpcs:ignore G
6240
6266
);
6241
6267
}
6242
6268
6269
+ /**
6270
+ * Mocks an API response as if the Access Token expired.
6271
+ *
6272
+ * @since 2.0.2
6273
+ *
6274
+ * @param mixed $response HTTP Response.
6275
+ * @param array $parsed_args Request arguments.
6276
+ * @param string $url Request URL.
6277
+ * @return mixed
6278
+ */
6279
+ public function mockAccessTokenExpiredResponse ( $ response , $ parsed_args , $ url )
6280
+ {
6281
+ // Only mock requests made to the /account endpoint.
6282
+ if ( strpos ( $ url , 'https://api.convertkit.com/v4/account ' ) === false ) {
6283
+ return $ response ;
6284
+ }
6285
+
6286
+ // Remove this filter, so we don't end up in a loop when retrying the request.
6287
+ remove_filter ( 'pre_http_request ' , array ( $ this , 'mockAccessTokenExpiredResponse ' ) );
6288
+
6289
+ // Return a 401 unauthorized response with the errors body as if the API
6290
+ // returned "The access token expired".
6291
+ return array (
6292
+ 'headers ' => array (),
6293
+ 'body ' => wp_json_encode (
6294
+ array (
6295
+ 'errors ' => array (
6296
+ 'The access token expired ' ,
6297
+ ),
6298
+ )
6299
+ ),
6300
+ 'response ' => array (
6301
+ 'code ' => 401 ,
6302
+ 'message ' => 'The access token expired ' ,
6303
+ ),
6304
+ 'cookies ' => array (),
6305
+ 'http_response ' => null ,
6306
+ );
6307
+ }
6308
+
6309
+ /**
6310
+ * Mocks an API response as if a refresh token was used to fetch new tokens.
6311
+ *
6312
+ * @since 2.0.2
6313
+ *
6314
+ * @param mixed $response HTTP Response.
6315
+ * @param array $parsed_args Request arguments.
6316
+ * @param string $url Request URL.
6317
+ * @return mixed
6318
+ */
6319
+ public function mockRefreshTokenResponse ( $ response , $ parsed_args , $ url )
6320
+ {
6321
+ // Only mock requests made to the /token endpoint.
6322
+ if ( strpos ( $ url , 'https://api.convertkit.com/oauth/token ' ) === false ) {
6323
+ return $ response ;
6324
+ }
6325
+
6326
+ // Remove this filter, so we don't end up in a loop when retrying the request.
6327
+ remove_filter ( 'pre_http_request ' , array ( $ this , 'mockRefreshTokenResponse ' ) );
6328
+
6329
+ // Return a mock access and refresh token for this API request, as calling
6330
+ // refresh_token results in a new access and refresh token being provided,
6331
+ // which would result in other tests breaking due to changed tokens.
6332
+ return array (
6333
+ 'headers ' => array (),
6334
+ 'body ' => wp_json_encode (
6335
+ array (
6336
+ 'access_token ' => 'new- ' . $ _ENV ['CONVERTKIT_OAUTH_ACCESS_TOKEN ' ],
6337
+ 'refresh_token ' => 'new- ' . $ _ENV ['CONVERTKIT_OAUTH_REFRESH_TOKEN ' ],
6338
+ 'token_type ' => 'bearer ' ,
6339
+ 'created_at ' => strtotime ( 'now ' ),
6340
+ 'expires_in ' => 10000 ,
6341
+ 'scope ' => 'public ' ,
6342
+ )
6343
+ ),
6344
+ 'response ' => array (
6345
+ 'code ' => 200 ,
6346
+ 'message ' => 'OK ' ,
6347
+ ),
6348
+ 'cookies ' => array (),
6349
+ 'http_response ' => null ,
6350
+ );
6351
+ }
6352
+
6243
6353
/**
6244
6354
* Helper method to assert the given key exists as an array
6245
6355
* in the API response.
0 commit comments