|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the package t3g/intercept. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please read the |
| 9 | + * LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace App\Tests\Unit\Service; |
| 13 | + |
| 14 | +use App\Entity\DocumentationQuarantine; |
| 15 | +use App\Enum\DocumentationRenderingTrigger; |
| 16 | +use App\Exception\DocumentationRenderingRequestDeclinedException; |
| 17 | +use App\Exception\UnknownComposerJsonUrlException; |
| 18 | +use App\Extractor\PushEvent; |
| 19 | +use App\Repository\RepositoryBlacklistEntryRepository; |
| 20 | +use App\Service\DocumentationBuildInformationService; |
| 21 | +use App\Service\DocumentationQuarantineService; |
| 22 | +use App\Service\GithubService; |
| 23 | +use App\Service\HistoryService; |
| 24 | +use App\Service\MailService; |
| 25 | +use App\Service\RenderDocumentationService; |
| 26 | +use Doctrine\ORM\EntityManagerInterface; |
| 27 | +use PHPUnit\Framework\TestCase; |
| 28 | +use Psr\Log\NullLogger; |
| 29 | +use Symfony\Bundle\SecurityBundle\Security; |
| 30 | + |
| 31 | +class RenderDocumentationServiceTest extends TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * The quarantined domain is what an admin later allowlists, so it has to be |
| 35 | + * the host the request was blocked on. That is the host of the composer.json |
| 36 | + * url, which for Github is never the host of the clone url. |
| 37 | + */ |
| 38 | + public function testTheBlockedDomainIsHandedToTheQuarantine(): void |
| 39 | + { |
| 40 | + $pushEvent = new PushEvent( |
| 41 | + 'https://github.com/acme/coolextension.git', |
| 42 | + 'main', |
| 43 | + 'https://raw.githubusercontent.com/acme/coolextension/main/composer.json', |
| 44 | + '{}' |
| 45 | + ); |
| 46 | + |
| 47 | + $buildInformationService = $this->createMock(DocumentationBuildInformationService::class); |
| 48 | + $buildInformationService->method('fetchRemoteComposerJson')->willThrowException( |
| 49 | + new UnknownComposerJsonUrlException('', 1782290340, null, $pushEvent->getUrlToComposerFile(), 'raw.githubusercontent.com') |
| 50 | + ); |
| 51 | + |
| 52 | + $lastHitDomain = null; |
| 53 | + $buildInformationService->method('updateLastHit')->willReturnCallback( |
| 54 | + static function (string $domain) use (&$lastHitDomain): void { |
| 55 | + $lastHitDomain = $domain; |
| 56 | + } |
| 57 | + ); |
| 58 | + |
| 59 | + $quarantinedDomain = null; |
| 60 | + $quarantineService = $this->createMock(DocumentationQuarantineService::class); |
| 61 | + $quarantineService->method('isQuarantined')->willReturn(false); |
| 62 | + $quarantineService->method('quarantine')->willReturnCallback( |
| 63 | + static function (PushEvent $event, string $domain) use (&$quarantinedDomain): DocumentationQuarantine { |
| 64 | + $quarantinedDomain = $domain; |
| 65 | + |
| 66 | + return new DocumentationQuarantine(); |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + $subject = new RenderDocumentationService( |
| 71 | + $buildInformationService, |
| 72 | + $this->createMock(GithubService::class), |
| 73 | + new HistoryService($this->createMock(EntityManagerInterface::class)), |
| 74 | + new NullLogger(), |
| 75 | + $quarantineService, |
| 76 | + $this->createMock(RepositoryBlacklistEntryRepository::class), |
| 77 | + $this->createMock(MailService::class), |
| 78 | + $this->createMock(Security::class), |
| 79 | + ); |
| 80 | + |
| 81 | + try { |
| 82 | + $subject->requestDocumentationRendering($pushEvent, DocumentationRenderingTrigger::API); |
| 83 | + $this->fail('An unknown domain has to decline the rendering request.'); |
| 84 | + } catch (DocumentationRenderingRequestDeclinedException) { |
| 85 | + // expected, the assertions below are what this test is about |
| 86 | + } |
| 87 | + |
| 88 | + $this->assertSame('raw.githubusercontent.com', $quarantinedDomain, 'The quarantine has to record the host the request was blocked on.'); |
| 89 | + $this->assertSame('raw.githubusercontent.com', $lastHitDomain, 'The last hit belongs to the domain row the check looks up.'); |
| 90 | + } |
| 91 | +} |
0 commit comments