|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\EndToEnd; |
| 4 | + |
| 5 | +use Tests\Support\EndToEndTester; |
| 6 | + |
| 7 | +/** |
| 8 | + * Tests Plugin uninstallation. |
| 9 | + * |
| 10 | + * @since 3.2.4 |
| 11 | + */ |
| 12 | +class UninstallCest |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Test that the Plugin's access and refresh tokens are revoked, and all v4 and v4 |
| 16 | + * API credentials are removed from the Plugin's settings when the Plugin is deleted. |
| 17 | + * |
| 18 | + * @since 3.2.4 |
| 19 | + * |
| 20 | + * @param EndToEndTester $I Tester. |
| 21 | + */ |
| 22 | + public function testPluginDeletionRevokesAndRemovesTokens(EndToEndTester $I) |
| 23 | + { |
| 24 | + // Activate this Plugin. |
| 25 | + $I->activateKitPlugin($I); |
| 26 | + |
| 27 | + // Generate an access token and refresh token by API key and secret. |
| 28 | + // We don't use the tokens from the environment, as revoking those |
| 29 | + // would result in later tests failing. |
| 30 | + $result = wp_remote_post( |
| 31 | + 'https://api.kit.com/wordpress/accounts/oauth_access_token', |
| 32 | + [ |
| 33 | + 'headers' => [ |
| 34 | + 'Content-Type' => 'application/json', |
| 35 | + ], |
| 36 | + 'body' => wp_json_encode( |
| 37 | + [ |
| 38 | + 'api_key' => $_ENV['CONVERTKIT_API_KEY'], |
| 39 | + 'api_secret' => $_ENV['CONVERTKIT_API_SECRET'], |
| 40 | + 'client_id' => $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], |
| 41 | + 'tenant_name' => wp_generate_password( 10, false ), // Random tenant name to produce a token for this request only. |
| 42 | + ] |
| 43 | + ), |
| 44 | + ] |
| 45 | + ); |
| 46 | + $tokens = json_decode(wp_remote_retrieve_body($result), true)['oauth']; |
| 47 | + |
| 48 | + // Store the tokens and API keys in the Plugin's settings. |
| 49 | + $I->setupKitPlugin( |
| 50 | + $I, |
| 51 | + [ |
| 52 | + 'access_token' => $tokens['access_token'], |
| 53 | + 'refresh_token' => $tokens['refresh_token'], |
| 54 | + 'token_expires' => $tokens['expires_at'], |
| 55 | + 'api_key' => $_ENV['CONVERTKIT_API_KEY'], |
| 56 | + 'api_secret' => $_ENV['CONVERTKIT_API_SECRET'], |
| 57 | + ] |
| 58 | + ); |
| 59 | + |
| 60 | + // Deactivate the Plugin. |
| 61 | + $I->deactivateKitPlugin($I); |
| 62 | + |
| 63 | + // Delete the Plugin. |
| 64 | + $I->deleteKitPlugin($I); |
| 65 | + |
| 66 | + // Confirm the credentials have been removed from the Plugin's settings. |
| 67 | + $I->wait(3); |
| 68 | + $settings = $I->grabOptionFromDatabase('_wp_convertkit_settings'); |
| 69 | + $I->assertEmpty($settings['access_token']); |
| 70 | + $I->assertEmpty($settings['refresh_token']); |
| 71 | + $I->assertEmpty($settings['token_expires']); |
| 72 | + $I->assertEmpty($settings['api_key']); |
| 73 | + $I->assertEmpty($settings['api_secret']); |
| 74 | + |
| 75 | + // Confirm attempting to use the revoked access token no longer works. |
| 76 | + $result = wp_remote_get( |
| 77 | + 'https://api.kit.com/v4/account', |
| 78 | + [ |
| 79 | + 'headers' => [ |
| 80 | + 'Authorization' => 'Bearer ' . $tokens['access_token'], |
| 81 | + ], |
| 82 | + ] |
| 83 | + ); |
| 84 | + $data = json_decode(wp_remote_retrieve_body($result), true); |
| 85 | + $I->assertArrayHasKey( 'errors', $data ); |
| 86 | + $I->assertEquals( 'The access token was revoked', $data['errors'][0] ); |
| 87 | + |
| 88 | + // Confirm attempting to use the revoked refresh token no longer works. |
| 89 | + $result = wp_remote_post( |
| 90 | + 'https://api.kit.com/v4/oauth/token', |
| 91 | + [ |
| 92 | + 'headers' => [ |
| 93 | + 'Authorization' => 'Bearer ' . $tokens['access_token'], |
| 94 | + ], |
| 95 | + 'body' => [ |
| 96 | + 'client_id' => $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], |
| 97 | + 'grant_type' => 'refresh_token', |
| 98 | + 'refresh_token' => $tokens['refresh_token'], |
| 99 | + ], |
| 100 | + ] |
| 101 | + ); |
| 102 | + $data = json_decode(wp_remote_retrieve_body($result), true); |
| 103 | + $I->assertArrayHasKey( 'error', $data ); |
| 104 | + $I->assertEquals( 'invalid_grant', $data['error'] ); |
| 105 | + } |
| 106 | +} |
0 commit comments