|
| 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\Github; |
| 18 | + |
| 19 | +use App\Github\PermissionChecker; |
| 20 | +use Cake\Http\Client; |
| 21 | +use Cake\Http\Client\Response; |
| 22 | +use Cake\TestSuite\TestCase; |
| 23 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 24 | + |
| 25 | +use function json_encode; |
| 26 | + |
| 27 | +/** |
| 28 | + * The role of a developer is read from one answer of the GitHub API. |
| 29 | + */ |
| 30 | +class PermissionCheckerTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Build a client that answers a canned response, without a connection. |
| 34 | + * |
| 35 | + * @param int $status The status code to answer |
| 36 | + * @param array<string, mixed>|null $json The body, as JSON |
| 37 | + */ |
| 38 | + protected function clientAnswering(int $status, array|null $json): Client |
| 39 | + { |
| 40 | + $response = new Response( |
| 41 | + ['HTTP/1.1 ' . $status . ' Answer', 'Content-Type: application/json'], |
| 42 | + (string) json_encode($json), |
| 43 | + ); |
| 44 | + |
| 45 | + return new class ($response) extends Client { |
| 46 | + public function __construct(private Response $canned) |
| 47 | + { |
| 48 | + } |
| 49 | + |
| 50 | + public function get(string $url, array|string $data = [], array $options = []): Response |
| 51 | + { |
| 52 | + return $this->canned; |
| 53 | + } |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + /** @return array<string, array{int, array<string, string>|null, bool|null}> */ |
| 58 | + public static function roleProvider(): array |
| 59 | + { |
| 60 | + return [ |
| 61 | + 'admin' => [200, ['permission' => 'admin'], true], |
| 62 | + 'maintain' => [200, ['permission' => 'maintain'], true], |
| 63 | + 'write' => [200, ['permission' => 'write'], true], |
| 64 | + 'write as a role_name' => [200, ['role_name' => 'write'], true], |
| 65 | + 'read' => [200, ['permission' => 'read'], false], |
| 66 | + 'triage' => [200, ['permission' => 'triage'], false], |
| 67 | + 'no role' => [200, [], false], |
| 68 | + 'a role that is not a string' => [200, ['permission' => 1], false], |
| 69 | + 'not a collaborator' => [404, null, false], |
| 70 | + 'the API is down' => [500, null, null], |
| 71 | + 'a forbidden answer' => [403, null, null], |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + #[DataProvider('roleProvider')] |
| 76 | + public function testTheRoleDecidesTheAccess(int $status, array|null $json, bool|null $expected): void |
| 77 | + { |
| 78 | + $result = PermissionChecker::canCommitTo( |
| 79 | + 'pma-bot', |
| 80 | + 'phpmyadmin/phpmyadmin', |
| 81 | + 'token', |
| 82 | + $this->clientAnswering($status, $json), |
| 83 | + ); |
| 84 | + |
| 85 | + $this->assertSame($expected, $result); |
| 86 | + } |
| 87 | + |
| 88 | + public function testNoTokenIsNotAnAnswer(): void |
| 89 | + { |
| 90 | + $this->assertNull(PermissionChecker::canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', '')); |
| 91 | + $this->assertNull(PermissionChecker::canCommitTo('', 'phpmyadmin/phpmyadmin', 'token')); |
| 92 | + } |
| 93 | +} |
0 commit comments