File tree Expand file tree Collapse file tree 4 files changed +60
-2
lines changed
includes/Core/Authentication
integration/Core/Authentication/Clients Expand file tree Collapse file tree 4 files changed +60
-2
lines changed Original file line number Diff line number Diff line change @@ -465,6 +465,9 @@ private function refresh_auth_token_on_login() {
465
465
// If 'invalid_grant' error, disconnect the account.
466
466
if ( 'invalid_grant ' === $ this ->user_options ->get ( Clients \OAuth_Client::OPTION_ERROR_CODE ) ) {
467
467
$ this ->disconnect ();
468
+
469
+ // We need to re-set this error so that it is displayed to the user.
470
+ $ this ->user_options ->set ( Clients \OAuth_Client::OPTION_ERROR_CODE , 'invalid_grant ' );
468
471
}
469
472
}
470
473
Original file line number Diff line number Diff line change @@ -246,7 +246,11 @@ public function refresh_token() {
246
246
$ this ->user_options ->set ( self ::OPTION_PROXY_ACCESS_CODE , $ e ->getAccessCode () );
247
247
return ;
248
248
} catch ( \Exception $ e ) {
249
- $ this ->user_options ->set ( self ::OPTION_ERROR_CODE , 'invalid_grant ' );
249
+ $ error_code = 'invalid_grant ' ;
250
+ if ( $ this ->using_proxy () ) { // Only the Google_Proxy_Client exposes the real error response.
251
+ $ error_code = $ e ->getMessage ();
252
+ }
253
+ $ this ->user_options ->set ( self ::OPTION_ERROR_CODE , $ error_code );
250
254
return ;
251
255
}
252
256
Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * Fake HTTP Client
4
+ *
5
+ * @package Google\Site_Kit\Tests
6
+ * @copyright 2019 Google LLC
7
+ * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
8
+ * @link https://sitekit.withgoogle.com
9
+ */
10
+
11
+ namespace Google \Site_Kit \Tests ;
12
+
13
+ use Google \Site_Kit_Dependencies \GuzzleHttp \Client ;
14
+ use Google \Site_Kit_Dependencies \GuzzleHttp \Message \RequestInterface ;
15
+ use Google \Site_Kit_Dependencies \GuzzleHttp \Message \Response ;
16
+
17
+ /**
18
+ * Class FakeHttpClient
19
+ */
20
+ class FakeHttpClient extends Client {
21
+ /**
22
+ * Handler function for overriding requests.
23
+ *
24
+ * @var callable
25
+ */
26
+ protected $ request_handler ;
27
+
28
+ /**
29
+ * Sets the handler for all requests.
30
+ *
31
+ * @param callable $handler
32
+ */
33
+ public function set_request_handler ( callable $ handler ) {
34
+ $ this ->request_handler = $ handler ;
35
+ }
36
+
37
+ /**
38
+ * @param RequestInterface $request
39
+ *
40
+ * @return \Google\Site_Kit_Dependencies\GuzzleHttp\Message\ResponseInterface
41
+ */
42
+ public function send ( RequestInterface $ request ) {
43
+ if ( $ this ->request_handler ) {
44
+ return call_user_func ( $ this ->request_handler , $ request );
45
+ }
46
+
47
+ return new Response ( 200 );
48
+ }
49
+ }
Original file line number Diff line number Diff line change 13
13
use Google \Site_Kit \Context ;
14
14
use Google \Site_Kit \Core \Authentication \Clients \OAuth_Client ;
15
15
use Google \Site_Kit \Tests \Exception \RedirectException ;
16
+ use Google \Site_Kit \Tests \FakeHttpClient ;
16
17
use Google \Site_Kit \Tests \TestCase ;
17
18
18
19
/**
@@ -30,6 +31,7 @@ public function test_get_client() {
30
31
}
31
32
32
33
public function test_refresh_token () {
34
+ $ this ->fake_authentication ();
33
35
$ user_id = $ this ->factory ()->user ->create ();
34
36
wp_set_current_user ( $ user_id );
35
37
$ client = new OAuth_Client ( new Context ( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) );
@@ -49,7 +51,7 @@ public function test_refresh_token() {
49
51
// Google client must be initialized first
50
52
$ this ->assertEquals ( 'refresh_token_not_exist ' , get_user_option ( OAuth_Client::OPTION_ERROR_CODE , $ user_id ) );
51
53
52
- $ client ->get_client ();
54
+ $ client ->get_client ()-> setHttpClient ( new FakeHttpClient () ) ;
53
55
$ client ->refresh_token ();
54
56
55
57
// At this point an error is triggered internally due to undefined indexes on $authentication_token
You can’t perform that action at this time.
0 commit comments