|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * phpMyAdmin Error reporting server |
| 5 | + * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * For full copyright and license information, please see the LICENSE.txt |
| 9 | + * Redistributions of files must retain the above copyright notice. |
| 10 | + * |
| 11 | + * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/) |
| 12 | + * @license https://opensource.org/licenses/mit-license.php MIT License |
| 13 | + * |
| 14 | + * @see https://www.phpmyadmin.net/ |
| 15 | + */ |
| 16 | + |
| 17 | +namespace App\Test\TestCase\Controller\Component; |
| 18 | + |
| 19 | +use App\Controller\Component\GithubApiComponent; |
| 20 | +use Cake\Controller\ComponentRegistry; |
| 21 | +use Cake\Http\TestSuite\HttpClientTrait; |
| 22 | +use Cake\Log\Engine\ArrayLog; |
| 23 | +use Cake\Log\Log; |
| 24 | +use Cake\TestSuite\TestCase; |
| 25 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 26 | + |
| 27 | +use function is_array; |
| 28 | +use function json_encode; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for the write-access check of the GitHub API component. |
| 32 | + */ |
| 33 | +class GithubApiComponentTest extends TestCase |
| 34 | +{ |
| 35 | + use HttpClientTrait; |
| 36 | + |
| 37 | + private const string PERMISSION_URL |
| 38 | + = 'https://api.github.com/repos/phpmyadmin/phpmyadmin/collaborators/pma-bot/permission'; |
| 39 | + |
| 40 | + protected GithubApiComponent $GithubApi; |
| 41 | + |
| 42 | + private ArrayLog $logCapture; |
| 43 | + |
| 44 | + private mixed $originalErrorLogConfig; |
| 45 | + |
| 46 | + public function setUp(): void |
| 47 | + { |
| 48 | + parent::setUp(); |
| 49 | + |
| 50 | + $this->loadRoutes(); |
| 51 | + $this->GithubApi = new GithubApiComponent(new ComponentRegistry()); |
| 52 | + |
| 53 | + $this->originalErrorLogConfig = Log::getConfig('error'); |
| 54 | + Log::drop('error'); |
| 55 | + Log::setConfig('error', [ |
| 56 | + 'className' => ArrayLog::class, |
| 57 | + 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], |
| 58 | + ]); |
| 59 | + $errorLog = Log::engine('error'); |
| 60 | + $this->assertInstanceOf(ArrayLog::class, $errorLog); |
| 61 | + $this->logCapture = $errorLog; |
| 62 | + $this->logCapture->clear(); |
| 63 | + } |
| 64 | + |
| 65 | + public function tearDown(): void |
| 66 | + { |
| 67 | + $this->cleanupMockResponses(); |
| 68 | + |
| 69 | + Log::drop('error'); |
| 70 | + if (is_array($this->originalErrorLogConfig)) { |
| 71 | + Log::setConfig('error', $this->originalErrorLogConfig); |
| 72 | + } |
| 73 | + |
| 74 | + parent::tearDown(); |
| 75 | + } |
| 76 | + |
| 77 | + /** @return array<string, array{string}> */ |
| 78 | + public static function writePermissionProvider(): array |
| 79 | + { |
| 80 | + return [ |
| 81 | + 'admin' => ['admin'], |
| 82 | + 'maintain' => ['maintain'], |
| 83 | + 'write' => ['write'], |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + /** @return array<string, array{string}> */ |
| 88 | + public static function readOnlyPermissionProvider(): array |
| 89 | + { |
| 90 | + return [ |
| 91 | + 'read' => ['read'], |
| 92 | + 'triage' => ['triage'], |
| 93 | + 'none' => ['none'], |
| 94 | + 'unknown' => ['some-new-role'], |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * A collaborator with a writing permission is allowed to commit. |
| 100 | + */ |
| 101 | + #[DataProvider('writePermissionProvider')] |
| 102 | + public function testCanCommitToAllowsWritingPermissions(string $permission): void |
| 103 | + { |
| 104 | + $this->mockClientGet( |
| 105 | + self::PERMISSION_URL, |
| 106 | + $this->newClientResponse(200, [], json_encode(['permission' => $permission, 'role_name' => $permission])), |
| 107 | + ); |
| 108 | + |
| 109 | + $this->assertTrue($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token')); |
| 110 | + $this->assertSame([], $this->logCapture->read()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * A collaborator with a read only permission may not commit. |
| 115 | + * |
| 116 | + * The permission endpoint is the only one called: the collaborators endpoint |
| 117 | + * answers 204 for every collaborator, read-only ones included. |
| 118 | + */ |
| 119 | + #[DataProvider('readOnlyPermissionProvider')] |
| 120 | + public function testCanCommitToRejectsReadOnlyPermissions(string $permission): void |
| 121 | + { |
| 122 | + $this->mockClientGet( |
| 123 | + self::PERMISSION_URL, |
| 124 | + $this->newClientResponse(200, [], json_encode(['permission' => $permission, 'role_name' => $permission])), |
| 125 | + ); |
| 126 | + |
| 127 | + $this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token')); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Role names are accepted when the permission field is not available. |
| 132 | + */ |
| 133 | + public function testCanCommitToUsesRoleNameWhenPermissionIsMissing(): void |
| 134 | + { |
| 135 | + $this->mockClientGet( |
| 136 | + self::PERMISSION_URL, |
| 137 | + $this->newClientResponse(200, [], json_encode(['role_name' => 'write'])), |
| 138 | + ); |
| 139 | + |
| 140 | + $this->assertTrue($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token')); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Any answer that is not a 200 means no access and is logged. |
| 145 | + */ |
| 146 | + public function testCanCommitToRejectsAndLogsNonOkAnswers(): void |
| 147 | + { |
| 148 | + $this->mockClientGet( |
| 149 | + self::PERMISSION_URL, |
| 150 | + $this->newClientResponse(404, [], json_encode(['message' => 'Not Found'])), |
| 151 | + ); |
| 152 | + |
| 153 | + $this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token')); |
| 154 | + |
| 155 | + $logs = $this->logCapture->read(); |
| 156 | + $this->assertCount(1, $logs); |
| 157 | + $this->assertStringContainsString('404', $logs[0]); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * A server error is not mistaken for a granted permission either. |
| 162 | + */ |
| 163 | + public function testCanCommitToRejectsServerError(): void |
| 164 | + { |
| 165 | + $this->mockClientGet( |
| 166 | + self::PERMISSION_URL, |
| 167 | + $this->newClientResponse(500, [], json_encode(['message' => 'Server Error'])), |
| 168 | + ); |
| 169 | + |
| 170 | + $this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token')); |
| 171 | + $this->assertCount(1, $this->logCapture->read()); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * The collaborator endpoint must not be used any more. |
| 176 | + */ |
| 177 | + public function testCanCommitToDoesNotCallTheCollaboratorEndpoint(): void |
| 178 | + { |
| 179 | + $this->mockClientGet( |
| 180 | + 'https://api.github.com/repos/phpmyadmin/phpmyadmin/collaborators/pma-bot', |
| 181 | + $this->newClientResponse(204, [], ''), |
| 182 | + ); |
| 183 | + $this->mockClientGet( |
| 184 | + self::PERMISSION_URL, |
| 185 | + $this->newClientResponse(200, [], json_encode(['permission' => 'read'])), |
| 186 | + ); |
| 187 | + |
| 188 | + $this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token')); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * The authorize url can carry the OAuth state. |
| 193 | + */ |
| 194 | + public function testGetRedirectUrlContainsTheState(): void |
| 195 | + { |
| 196 | + $url = $this->GithubApi->getRedirectUrl('public_repo', 'the-state'); |
| 197 | + |
| 198 | + $this->assertStringContainsString('state=the-state', $url); |
| 199 | + } |
| 200 | +} |
0 commit comments