|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for /wpcom/v2/application-password-extras endpoints. |
| 4 | + * |
| 5 | + * @package automattic/jetpack |
| 6 | + */ |
| 7 | + |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | +use WpOrg\Requests\Requests; |
| 11 | + |
| 12 | +require_once dirname( __DIR__, 2 ) . '/lib/Jetpack_REST_TestCase.php'; |
| 13 | +require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-application-password-extras.php'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class WPCOM_REST_API_V2_Endpoint_Application_Password_Extras_Test |
| 17 | + * |
| 18 | + * @covers \WPCOM_REST_API_V2_Endpoint_Application_Password_Extras |
| 19 | + */ |
| 20 | +#[CoversClass( WPCOM_REST_API_V2_Endpoint_Application_Password_Extras::class )] |
| 21 | +class WPCOM_REST_API_V2_Endpoint_Application_Password_Extras_Test extends Jetpack_REST_TestCase { |
| 22 | + |
| 23 | + /** |
| 24 | + * Mock user ID. |
| 25 | + * |
| 26 | + * @var int |
| 27 | + */ |
| 28 | + private static $user_id = 0; |
| 29 | + |
| 30 | + /** |
| 31 | + * Mock editor user ID. |
| 32 | + * |
| 33 | + * @var int |
| 34 | + */ |
| 35 | + private static $editor_id = 0; |
| 36 | + |
| 37 | + /** |
| 38 | + * Create shared database fixtures. |
| 39 | + * |
| 40 | + * @param WP_UnitTest_Factory $factory Fixture factory. |
| 41 | + */ |
| 42 | + public static function wpSetUpBeforeClass( $factory ) { |
| 43 | + static::$user_id = $factory->user->create( array( 'role' => 'administrator' ) ); |
| 44 | + static::$editor_id = $factory->user->create( array( 'role' => 'editor' ) ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Setup the environment for a test. |
| 49 | + */ |
| 50 | + public function set_up() { |
| 51 | + parent::set_up(); |
| 52 | + |
| 53 | + // Initialize the class to ensure it's available |
| 54 | + Jetpack_Application_Password_Extras::init(); |
| 55 | + |
| 56 | + wp_set_current_user( static::$user_id ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test that routes are registered correctly. |
| 61 | + */ |
| 62 | + public function test_routes_registered() { |
| 63 | + $routes = rest_get_server()->get_routes(); |
| 64 | + |
| 65 | + $this->assertArrayHasKey( '/wpcom/v2/application-password-extras/abilities', $routes ); |
| 66 | + $this->assertArrayHasKey( '/wpcom/v2/application-password-extras/admin-ajax', $routes ); |
| 67 | + $this->assertArrayHasKey( '/wpcom/v2/application-password-extras/post-previews', $routes ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test permission check for unauthenticated users. |
| 72 | + */ |
| 73 | + public function test_unauthenticated_user_cannot_access_abilities() { |
| 74 | + wp_set_current_user( 0 ); |
| 75 | + |
| 76 | + $request = new WP_REST_Request( Requests::GET, '/wpcom/v2/application-password-extras/abilities' ); |
| 77 | + $response = $this->server->dispatch( $request ); |
| 78 | + |
| 79 | + $this->assertErrorResponse( 'rest_forbidden', $response, 401 ); |
| 80 | + $data = $response->get_data(); |
| 81 | + $this->assertEquals( 'Sorry, you must be logged in to access this endpoint.', $data['message'] ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Test permission check for authenticated users. |
| 86 | + */ |
| 87 | + public function test_authenticated_user_can_access_abilities() { |
| 88 | + wp_set_current_user( static::$user_id ); |
| 89 | + |
| 90 | + $request = new WP_REST_Request( Requests::GET, '/wpcom/v2/application-password-extras/abilities' ); |
| 91 | + $response = $this->server->dispatch( $request ); |
| 92 | + |
| 93 | + $this->assertEquals( 200, $response->get_status() ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Test that get_abilities endpoint returns correct data. |
| 98 | + */ |
| 99 | + public function test_get_abilities_endpoint_returns_correct_data() { |
| 100 | + wp_set_current_user( static::$user_id ); |
| 101 | + |
| 102 | + $request = new WP_REST_Request( Requests::GET, '/wpcom/v2/application-password-extras/abilities' ); |
| 103 | + $response = $this->server->dispatch( $request ); |
| 104 | + $data = $response->get_data(); |
| 105 | + |
| 106 | + $this->assertEquals( 200, $response->get_status() ); |
| 107 | + $this->assertIsArray( $data ); |
| 108 | + $this->assertArrayHasKey( 'admin-ajax', $data ); |
| 109 | + $this->assertArrayHasKey( 'post-previews', $data ); |
| 110 | + $this->assertTrue( $data['admin-ajax'] ); |
| 111 | + $this->assertTrue( $data['post-previews'] ); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Data provider for endpoint tests |
| 116 | + * |
| 117 | + * @return array |
| 118 | + */ |
| 119 | + public static function endpoint_provider() { |
| 120 | + return array( |
| 121 | + 'abilities endpoint' => array( '/wpcom/v2/application-password-extras/abilities' ), |
| 122 | + 'admin-ajax endpoint' => array( '/wpcom/v2/application-password-extras/admin-ajax' ), |
| 123 | + 'post-previews endpoint' => array( '/wpcom/v2/application-password-extras/post-previews' ), |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Test that all endpoints work correctly for authenticated users. |
| 129 | + * |
| 130 | + * @dataProvider endpoint_provider |
| 131 | + * @param string $endpoint The endpoint to test. |
| 132 | + */ |
| 133 | + #[DataProvider( 'endpoint_provider' )] |
| 134 | + public function test_endpoints_work_for_authenticated_users( $endpoint ) { |
| 135 | + wp_set_current_user( static::$user_id ); |
| 136 | + |
| 137 | + $request = new WP_REST_Request( Requests::GET, $endpoint ); |
| 138 | + $response = $this->server->dispatch( $request ); |
| 139 | + $data = $response->get_data(); |
| 140 | + |
| 141 | + $this->assertEquals( 200, $response->get_status() ); |
| 142 | + $this->assertIsArray( $data ); |
| 143 | + $this->assertArrayHasKey( 'admin-ajax', $data ); |
| 144 | + $this->assertArrayHasKey( 'post-previews', $data ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Test that all endpoints require authentication. |
| 149 | + * |
| 150 | + * @dataProvider endpoint_provider |
| 151 | + * @param string $endpoint The endpoint to test. |
| 152 | + */ |
| 153 | + #[DataProvider( 'endpoint_provider' )] |
| 154 | + public function test_endpoints_require_authentication( $endpoint ) { |
| 155 | + wp_set_current_user( 0 ); |
| 156 | + |
| 157 | + $request = new WP_REST_Request( Requests::GET, $endpoint ); |
| 158 | + $response = $this->server->dispatch( $request ); |
| 159 | + |
| 160 | + $this->assertErrorResponse( 'rest_forbidden', $response, 401 ); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Test that editor users can access abilities. |
| 165 | + */ |
| 166 | + public function test_editor_can_access_abilities() { |
| 167 | + wp_set_current_user( static::$editor_id ); |
| 168 | + |
| 169 | + $request = new WP_REST_Request( Requests::GET, '/wpcom/v2/application-password-extras/abilities' ); |
| 170 | + $response = $this->server->dispatch( $request ); |
| 171 | + |
| 172 | + $this->assertEquals( 200, $response->get_status() ); |
| 173 | + $data = $response->get_data(); |
| 174 | + $this->assertIsArray( $data ); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Test that only GET requests are allowed. |
| 179 | + */ |
| 180 | + public function test_only_get_requests_allowed() { |
| 181 | + wp_set_current_user( static::$user_id ); |
| 182 | + |
| 183 | + $disallowed_methods = array( |
| 184 | + Requests::POST, |
| 185 | + Requests::PUT, |
| 186 | + Requests::DELETE, |
| 187 | + Requests::PATCH, |
| 188 | + ); |
| 189 | + |
| 190 | + foreach ( $disallowed_methods as $method ) { |
| 191 | + $request = new WP_REST_Request( $method, '/wpcom/v2/application-password-extras/abilities' ); |
| 192 | + $response = $this->server->dispatch( $request ); |
| 193 | + |
| 194 | + // Routes that don't support a method typically return 404 (not found) in WordPress REST API |
| 195 | + $this->assertContains( |
| 196 | + $response->get_status(), |
| 197 | + array( 404, 405 ), |
| 198 | + "Method $method should not be allowed (status should be 404 or 405)" |
| 199 | + ); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Test response headers are set correctly. |
| 205 | + */ |
| 206 | + public function test_response_headers() { |
| 207 | + wp_set_current_user( static::$user_id ); |
| 208 | + |
| 209 | + $request = new WP_REST_Request( Requests::GET, '/wpcom/v2/application-password-extras/abilities' ); |
| 210 | + $response = $this->server->dispatch( $request ); |
| 211 | + |
| 212 | + $headers = $response->get_headers(); |
| 213 | + |
| 214 | + // Standard REST API headers should be present |
| 215 | + $this->assertIsArray( $headers ); |
| 216 | + } |
| 217 | +} |
0 commit comments