|
| 1 | +<?php |
| 2 | + |
| 3 | +use WP_CLI\Tests\TestCase; |
| 4 | + |
| 5 | +class PackageAuthTest extends TestCase { |
| 6 | + |
| 7 | + private $env_vars_to_restore = []; |
| 8 | + |
| 9 | + public function set_up() { |
| 10 | + parent::set_up(); |
| 11 | + |
| 12 | + // Save current environment variables |
| 13 | + $this->env_vars_to_restore = [ |
| 14 | + 'COMPOSER_AUTH' => getenv( 'COMPOSER_AUTH' ), |
| 15 | + 'GITHUB_TOKEN' => getenv( 'GITHUB_TOKEN' ), |
| 16 | + 'GITLAB_OAUTH_TOKEN' => getenv( 'GITLAB_OAUTH_TOKEN' ), |
| 17 | + 'GITLAB_TOKEN' => getenv( 'GITLAB_TOKEN' ), |
| 18 | + 'BITBUCKET_CONSUMER_KEY' => getenv( 'BITBUCKET_CONSUMER_KEY' ), |
| 19 | + 'BITBUCKET_CONSUMER_SECRET' => getenv( 'BITBUCKET_CONSUMER_SECRET' ), |
| 20 | + 'HTTP_BASIC_AUTH' => getenv( 'HTTP_BASIC_AUTH' ), |
| 21 | + ]; |
| 22 | + |
| 23 | + // Clear all auth-related environment variables |
| 24 | + foreach ( array_keys( $this->env_vars_to_restore ) as $var ) { |
| 25 | + putenv( $var ); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public function tear_down() { |
| 30 | + // Restore environment variables |
| 31 | + foreach ( $this->env_vars_to_restore as $var => $value ) { |
| 32 | + if ( false !== $value ) { |
| 33 | + putenv( "$var=$value" ); |
| 34 | + } else { |
| 35 | + putenv( $var ); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + parent::tear_down(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Test that GITHUB_TOKEN is added to COMPOSER_AUTH. |
| 44 | + */ |
| 45 | + public function test_github_token_added_to_composer_auth() { |
| 46 | + putenv( 'GITHUB_TOKEN=ghp_test123456789' ); |
| 47 | + |
| 48 | + $package = new Package_Command(); |
| 49 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 50 | + if ( PHP_VERSION_ID < 80100 ) { |
| 51 | + $method->setAccessible( true ); |
| 52 | + } |
| 53 | + $method->invoke( $package ); |
| 54 | + |
| 55 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 56 | + $this->assertNotFalse( $composer_auth ); |
| 57 | + |
| 58 | + $auth_array = json_decode( $composer_auth, true ); |
| 59 | + $this->assertIsArray( $auth_array ); |
| 60 | + $this->assertArrayHasKey( 'github-oauth', $auth_array ); |
| 61 | + $this->assertArrayHasKey( 'github.com', $auth_array['github-oauth'] ); |
| 62 | + $this->assertSame( 'ghp_test123456789', $auth_array['github-oauth']['github.com'] ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test that GITLAB_OAUTH_TOKEN is added to COMPOSER_AUTH. |
| 67 | + */ |
| 68 | + public function test_gitlab_oauth_token_added_to_composer_auth() { |
| 69 | + putenv( 'GITLAB_OAUTH_TOKEN=glpat_test123456789' ); |
| 70 | + |
| 71 | + $package = new Package_Command(); |
| 72 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 73 | + if ( PHP_VERSION_ID < 80100 ) { |
| 74 | + $method->setAccessible( true ); |
| 75 | + } |
| 76 | + $method->invoke( $package ); |
| 77 | + |
| 78 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 79 | + $this->assertNotFalse( $composer_auth ); |
| 80 | + |
| 81 | + $auth_array = json_decode( $composer_auth, true ); |
| 82 | + $this->assertIsArray( $auth_array ); |
| 83 | + $this->assertArrayHasKey( 'gitlab-oauth', $auth_array ); |
| 84 | + $this->assertArrayHasKey( 'gitlab.com', $auth_array['gitlab-oauth'] ); |
| 85 | + $this->assertSame( 'glpat_test123456789', $auth_array['gitlab-oauth']['gitlab.com'] ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test that GITLAB_TOKEN is added to COMPOSER_AUTH. |
| 90 | + */ |
| 91 | + public function test_gitlab_token_added_to_composer_auth() { |
| 92 | + putenv( 'GITLAB_TOKEN=glpat_test123456789' ); |
| 93 | + |
| 94 | + $package = new Package_Command(); |
| 95 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 96 | + if ( PHP_VERSION_ID < 80100 ) { |
| 97 | + $method->setAccessible( true ); |
| 98 | + } |
| 99 | + $method->invoke( $package ); |
| 100 | + |
| 101 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 102 | + $this->assertNotFalse( $composer_auth ); |
| 103 | + |
| 104 | + $auth_array = json_decode( $composer_auth, true ); |
| 105 | + $this->assertIsArray( $auth_array ); |
| 106 | + $this->assertArrayHasKey( 'gitlab-token', $auth_array ); |
| 107 | + $this->assertArrayHasKey( 'gitlab.com', $auth_array['gitlab-token'] ); |
| 108 | + $this->assertSame( 'glpat_test123456789', $auth_array['gitlab-token']['gitlab.com'] ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test that BITBUCKET_CONSUMER_KEY and BITBUCKET_CONSUMER_SECRET are added to COMPOSER_AUTH. |
| 113 | + */ |
| 114 | + public function test_bitbucket_oauth_added_to_composer_auth() { |
| 115 | + putenv( 'BITBUCKET_CONSUMER_KEY=test_key' ); |
| 116 | + putenv( 'BITBUCKET_CONSUMER_SECRET=test_secret' ); |
| 117 | + |
| 118 | + $package = new Package_Command(); |
| 119 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 120 | + if ( PHP_VERSION_ID < 80100 ) { |
| 121 | + $method->setAccessible( true ); |
| 122 | + } |
| 123 | + $method->invoke( $package ); |
| 124 | + |
| 125 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 126 | + $this->assertNotFalse( $composer_auth ); |
| 127 | + |
| 128 | + $auth_array = json_decode( $composer_auth, true ); |
| 129 | + $this->assertIsArray( $auth_array ); |
| 130 | + $this->assertArrayHasKey( 'bitbucket-oauth', $auth_array ); |
| 131 | + $this->assertArrayHasKey( 'bitbucket.org', $auth_array['bitbucket-oauth'] ); |
| 132 | + $this->assertArrayHasKey( 'consumer-key', $auth_array['bitbucket-oauth']['bitbucket.org'] ); |
| 133 | + $this->assertArrayHasKey( 'consumer-secret', $auth_array['bitbucket-oauth']['bitbucket.org'] ); |
| 134 | + $this->assertSame( 'test_key', $auth_array['bitbucket-oauth']['bitbucket.org']['consumer-key'] ); |
| 135 | + $this->assertSame( 'test_secret', $auth_array['bitbucket-oauth']['bitbucket.org']['consumer-secret'] ); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Test that Bitbucket OAuth is not added if only one credential is provided. |
| 140 | + */ |
| 141 | + public function test_bitbucket_oauth_requires_both_credentials() { |
| 142 | + putenv( 'BITBUCKET_CONSUMER_KEY=test_key' ); |
| 143 | + // BITBUCKET_CONSUMER_SECRET is not set |
| 144 | + |
| 145 | + $package = new Package_Command(); |
| 146 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 147 | + if ( PHP_VERSION_ID < 80100 ) { |
| 148 | + $method->setAccessible( true ); |
| 149 | + } |
| 150 | + $method->invoke( $package ); |
| 151 | + |
| 152 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 153 | + // No auth should be set because only one credential was provided |
| 154 | + $this->assertFalse( $composer_auth ); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Test that HTTP_BASIC_AUTH is added to COMPOSER_AUTH. |
| 159 | + */ |
| 160 | + public function test_http_basic_auth_added_to_composer_auth() { |
| 161 | + $http_basic = json_encode( |
| 162 | + [ |
| 163 | + 'repo.example.com' => [ |
| 164 | + 'username' => 'user', |
| 165 | + 'password' => 'pass', |
| 166 | + ], |
| 167 | + ] |
| 168 | + ); |
| 169 | + putenv( "HTTP_BASIC_AUTH=$http_basic" ); |
| 170 | + |
| 171 | + $package = new Package_Command(); |
| 172 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 173 | + if ( PHP_VERSION_ID < 80100 ) { |
| 174 | + $method->setAccessible( true ); |
| 175 | + } |
| 176 | + $method->invoke( $package ); |
| 177 | + |
| 178 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 179 | + $this->assertNotFalse( $composer_auth ); |
| 180 | + |
| 181 | + $auth_array = json_decode( $composer_auth, true ); |
| 182 | + $this->assertIsArray( $auth_array ); |
| 183 | + $this->assertArrayHasKey( 'http-basic', $auth_array ); |
| 184 | + $this->assertArrayHasKey( 'repo.example.com', $auth_array['http-basic'] ); |
| 185 | + $this->assertArrayHasKey( 'username', $auth_array['http-basic']['repo.example.com'] ); |
| 186 | + $this->assertArrayHasKey( 'password', $auth_array['http-basic']['repo.example.com'] ); |
| 187 | + $this->assertSame( 'user', $auth_array['http-basic']['repo.example.com']['username'] ); |
| 188 | + $this->assertSame( 'pass', $auth_array['http-basic']['repo.example.com']['password'] ); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Test that invalid HTTP_BASIC_AUTH JSON is ignored. |
| 193 | + */ |
| 194 | + public function test_invalid_http_basic_auth_json_ignored() { |
| 195 | + putenv( 'HTTP_BASIC_AUTH=not-valid-json' ); |
| 196 | + |
| 197 | + $package = new Package_Command(); |
| 198 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 199 | + if ( PHP_VERSION_ID < 80100 ) { |
| 200 | + $method->setAccessible( true ); |
| 201 | + } |
| 202 | + $method->invoke( $package ); |
| 203 | + |
| 204 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 205 | + // No auth should be set because the JSON was invalid |
| 206 | + $this->assertFalse( $composer_auth ); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Test that multiple auth providers can be used together. |
| 211 | + */ |
| 212 | + public function test_multiple_auth_providers() { |
| 213 | + putenv( 'GITHUB_TOKEN=ghp_test123' ); |
| 214 | + putenv( 'GITLAB_TOKEN=glpat_test456' ); |
| 215 | + |
| 216 | + $package = new Package_Command(); |
| 217 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 218 | + if ( PHP_VERSION_ID < 80100 ) { |
| 219 | + $method->setAccessible( true ); |
| 220 | + } |
| 221 | + $method->invoke( $package ); |
| 222 | + |
| 223 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 224 | + $this->assertNotFalse( $composer_auth ); |
| 225 | + |
| 226 | + $auth_array = json_decode( $composer_auth, true ); |
| 227 | + $this->assertIsArray( $auth_array ); |
| 228 | + $this->assertArrayHasKey( 'github-oauth', $auth_array ); |
| 229 | + $this->assertArrayHasKey( 'gitlab-token', $auth_array ); |
| 230 | + $this->assertSame( 'ghp_test123', $auth_array['github-oauth']['github.com'] ); |
| 231 | + $this->assertSame( 'glpat_test456', $auth_array['gitlab-token']['gitlab.com'] ); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Test that existing COMPOSER_AUTH is preserved. |
| 236 | + */ |
| 237 | + public function test_existing_composer_auth_preserved() { |
| 238 | + $existing_auth = json_encode( |
| 239 | + [ |
| 240 | + 'github-oauth' => [ |
| 241 | + 'github.com' => 'existing_token', |
| 242 | + ], |
| 243 | + ] |
| 244 | + ); |
| 245 | + putenv( "COMPOSER_AUTH=$existing_auth" ); |
| 246 | + putenv( 'GITLAB_TOKEN=glpat_new_token' ); |
| 247 | + |
| 248 | + $package = new Package_Command(); |
| 249 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 250 | + if ( PHP_VERSION_ID < 80100 ) { |
| 251 | + $method->setAccessible( true ); |
| 252 | + } |
| 253 | + $method->invoke( $package ); |
| 254 | + |
| 255 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 256 | + $this->assertNotFalse( $composer_auth ); |
| 257 | + |
| 258 | + $auth_array = json_decode( $composer_auth, true ); |
| 259 | + $this->assertIsArray( $auth_array ); |
| 260 | + // Existing github-oauth should be preserved |
| 261 | + $this->assertArrayHasKey( 'github-oauth', $auth_array ); |
| 262 | + $this->assertSame( 'existing_token', $auth_array['github-oauth']['github.com'] ); |
| 263 | + // New gitlab-token should be added |
| 264 | + $this->assertArrayHasKey( 'gitlab-token', $auth_array ); |
| 265 | + $this->assertSame( 'glpat_new_token', $auth_array['gitlab-token']['gitlab.com'] ); |
| 266 | + } |
| 267 | + |
| 268 | + /** |
| 269 | + * Test that environment variable tokens don't override existing COMPOSER_AUTH values. |
| 270 | + */ |
| 271 | + public function test_env_tokens_dont_override_composer_auth() { |
| 272 | + $existing_auth = json_encode( |
| 273 | + [ |
| 274 | + 'github-oauth' => [ |
| 275 | + 'github.com' => 'existing_token', |
| 276 | + ], |
| 277 | + ] |
| 278 | + ); |
| 279 | + putenv( "COMPOSER_AUTH=$existing_auth" ); |
| 280 | + putenv( 'GITHUB_TOKEN=new_token' ); |
| 281 | + |
| 282 | + $package = new Package_Command(); |
| 283 | + $method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' ); |
| 284 | + if ( PHP_VERSION_ID < 80100 ) { |
| 285 | + $method->setAccessible( true ); |
| 286 | + } |
| 287 | + $method->invoke( $package ); |
| 288 | + |
| 289 | + $composer_auth = getenv( 'COMPOSER_AUTH' ); |
| 290 | + $this->assertNotFalse( $composer_auth ); |
| 291 | + |
| 292 | + $auth_array = json_decode( $composer_auth, true ); |
| 293 | + $this->assertIsArray( $auth_array ); |
| 294 | + // Existing github-oauth should be preserved, not overridden by GITHUB_TOKEN |
| 295 | + $this->assertArrayHasKey( 'github-oauth', $auth_array ); |
| 296 | + $this->assertSame( 'existing_token', $auth_array['github-oauth']['github.com'] ); |
| 297 | + } |
| 298 | +} |
0 commit comments