|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for Login::handle_logout(). |
| 4 | + * |
| 5 | + * @package WorkOS\Tests\Wpunit |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WorkOS\Tests\Wpunit; |
| 9 | + |
| 10 | +use lucatume\WPBrowser\TestCase\WPTestCase; |
| 11 | +use WorkOS\Auth\Login; |
| 12 | + |
| 13 | +/** |
| 14 | + * Logout session cleanup and server-side revocation tests. |
| 15 | + */ |
| 16 | +class LoginLogoutTest extends WPTestCase { |
| 17 | + |
| 18 | + /** |
| 19 | + * Login instance under test. |
| 20 | + * |
| 21 | + * @var Login |
| 22 | + */ |
| 23 | + private Login $login; |
| 24 | + |
| 25 | + /** |
| 26 | + * Captured HTTP requests. |
| 27 | + * |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + private array $captured_requests = []; |
| 31 | + |
| 32 | + /** |
| 33 | + * Set up each test. |
| 34 | + */ |
| 35 | + public function setUp(): void { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + \WorkOS\Config::set_active_environment( 'production' ); |
| 39 | + update_option( 'workos_production', [ |
| 40 | + 'api_key' => 'sk_test_fake', |
| 41 | + 'client_id' => 'client_fake', |
| 42 | + 'environment_id' => 'environment_test', |
| 43 | + ] ); |
| 44 | + \WorkOS\App::container()->get( \WorkOS\Options\Production::class )->reset(); |
| 45 | + |
| 46 | + add_filter( 'pre_http_request', [ $this, 'intercept_http' ], 10, 3 ); |
| 47 | + |
| 48 | + remove_all_actions( 'user_register' ); |
| 49 | + |
| 50 | + // Remove constructor hooks so tests only exercise handle_logout directly. |
| 51 | + remove_all_actions( 'login_init' ); |
| 52 | + remove_all_filters( 'authenticate' ); |
| 53 | + remove_all_actions( 'wp_logout' ); |
| 54 | + |
| 55 | + $this->login = new Login(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Tear down each test. |
| 60 | + */ |
| 61 | + public function tearDown(): void { |
| 62 | + remove_filter( 'pre_http_request', [ $this, 'intercept_http' ], 10 ); |
| 63 | + delete_option( 'workos_production' ); |
| 64 | + \WorkOS\Config::set_active_environment( 'staging' ); |
| 65 | + \WorkOS\App::container()->get( \WorkOS\Options\Production::class )->reset(); |
| 66 | + |
| 67 | + $this->captured_requests = []; |
| 68 | + |
| 69 | + parent::tearDown(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Intercept outbound HTTP requests. |
| 74 | + * |
| 75 | + * @param false|array $preempt Response override. |
| 76 | + * @param array $args Request args. |
| 77 | + * @param string $url Request URL. |
| 78 | + * |
| 79 | + * @return array Fake response. |
| 80 | + */ |
| 81 | + public function intercept_http( $preempt, array $args, string $url ): array { |
| 82 | + $this->captured_requests[] = [ |
| 83 | + 'url' => $url, |
| 84 | + 'method' => $args['method'] ?? 'GET', |
| 85 | + 'body' => $args['body'] ?? '', |
| 86 | + ]; |
| 87 | + |
| 88 | + return [ |
| 89 | + 'response' => [ 'code' => 200, 'message' => 'OK' ], |
| 90 | + 'body' => '{}', |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Build a fake JWT with a given payload. |
| 96 | + * |
| 97 | + * No real signature — extract_session_id only decodes the payload. |
| 98 | + * |
| 99 | + * @param array $payload JWT payload claims. |
| 100 | + * |
| 101 | + * @return string Fake JWT string. |
| 102 | + */ |
| 103 | + private function build_fake_jwt( array $payload ): string { |
| 104 | + $header = rtrim( strtr( base64_encode( '{"alg":"RS256","typ":"JWT"}' ), '+/', '-_' ), '=' ); |
| 105 | + $body = rtrim( strtr( base64_encode( wp_json_encode( $payload ) ), '+/', '-_' ), '=' ); |
| 106 | + $sig = 'fake_signature'; |
| 107 | + |
| 108 | + return "{$header}.{$body}.{$sig}"; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Create a user with a stored WorkOS access token. |
| 113 | + * |
| 114 | + * @param string $session_id Session ID to embed in the JWT. |
| 115 | + * |
| 116 | + * @return int WordPress user ID. |
| 117 | + */ |
| 118 | + private function create_user_with_token( string $session_id ): int { |
| 119 | + $user_id = self::factory()->user->create( [ 'role' => 'subscriber' ] ); |
| 120 | + $token = $this->build_fake_jwt( [ 'sid' => $session_id, 'sub' => 'user_test' ] ); |
| 121 | + |
| 122 | + update_user_meta( $user_id, '_workos_access_token', $token ); |
| 123 | + update_user_meta( $user_id, '_workos_refresh_token', 'refresh_fake' ); |
| 124 | + update_user_meta( $user_id, '_workos_session_id', $session_id ); |
| 125 | + |
| 126 | + return $user_id; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Test tokens are deleted from usermeta on logout. |
| 131 | + */ |
| 132 | + public function test_clears_tokens_on_logout(): void { |
| 133 | + $user_id = $this->create_user_with_token( 'session_abc' ); |
| 134 | + |
| 135 | + $this->login->handle_logout( $user_id ); |
| 136 | + |
| 137 | + $this->assertEmpty( get_user_meta( $user_id, '_workos_access_token', true ) ); |
| 138 | + $this->assertEmpty( get_user_meta( $user_id, '_workos_refresh_token', true ) ); |
| 139 | + $this->assertEmpty( get_user_meta( $user_id, '_workos_session_id', true ) ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Test server-side session revocation is called with the correct session ID. |
| 144 | + */ |
| 145 | + public function test_revokes_session_server_side(): void { |
| 146 | + $user_id = $this->create_user_with_token( 'session_xyz' ); |
| 147 | + |
| 148 | + $this->login->handle_logout( $user_id ); |
| 149 | + |
| 150 | + $this->assertCount( 1, $this->captured_requests ); |
| 151 | + $this->assertStringContainsString( |
| 152 | + '/user_management/sessions/session_xyz/revoke', |
| 153 | + $this->captured_requests[0]['url'] |
| 154 | + ); |
| 155 | + $this->assertSame( 'POST', $this->captured_requests[0]['method'] ); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Test no API call is made when user has no access token. |
| 160 | + */ |
| 161 | + public function test_skips_revocation_when_no_access_token(): void { |
| 162 | + $user_id = self::factory()->user->create( [ 'role' => 'subscriber' ] ); |
| 163 | + |
| 164 | + $this->login->handle_logout( $user_id ); |
| 165 | + |
| 166 | + $this->assertCount( 0, $this->captured_requests ); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Test no API call is made when JWT has no sid claim. |
| 171 | + */ |
| 172 | + public function test_skips_revocation_when_token_has_no_sid(): void { |
| 173 | + $user_id = self::factory()->user->create( [ 'role' => 'subscriber' ] ); |
| 174 | + $token = $this->build_fake_jwt( [ 'sub' => 'user_test' ] ); |
| 175 | + update_user_meta( $user_id, '_workos_access_token', $token ); |
| 176 | + |
| 177 | + $this->login->handle_logout( $user_id ); |
| 178 | + |
| 179 | + $this->assertCount( 0, $this->captured_requests ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test no logout_redirect filter is registered (redirect stays with WordPress). |
| 184 | + */ |
| 185 | + public function test_does_not_register_logout_redirect_filter(): void { |
| 186 | + $user_id = $this->create_user_with_token( 'session_abc' ); |
| 187 | + |
| 188 | + $this->login->handle_logout( $user_id ); |
| 189 | + |
| 190 | + $this->assertFalse( has_filter( 'logout_redirect', [ $this->login, 'wrap_logout_redirect' ] ) ); |
| 191 | + } |
| 192 | +} |
0 commit comments