69 lines
2.3 KiB
PHP
69 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Controller;
|
|
|
|
use App\Entity\ScriptMapping;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\ORM\Tools\SchemaTool;
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
|
|
|
final class PublicScriptControllerTest extends WebTestCase
|
|
{
|
|
private KernelBrowser $client;
|
|
private EntityManagerInterface $entityManager;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
self::ensureKernelShutdown();
|
|
$this->client = self::createClient();
|
|
|
|
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
|
|
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
|
|
$schemaTool = new SchemaTool($this->entityManager);
|
|
$schemaTool->dropSchema($metadata);
|
|
$schemaTool->createSchema($metadata);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
|
|
$this->entityManager->close();
|
|
}
|
|
|
|
public function testUnknownPathReturnsNotFound(): void
|
|
{
|
|
$this->client->request('GET', '/missing/install.sh');
|
|
|
|
self::assertResponseStatusCodeSame(404);
|
|
}
|
|
|
|
public function testCachedScriptIsServed(): void
|
|
{
|
|
$container = static::getContainer();
|
|
$cacheDir = $container->getParameter('app.cache_dir');
|
|
if (!str_starts_with($cacheDir, '/')) {
|
|
$cacheDir = $container->getParameter('kernel.project_dir').'/'.$cacheDir;
|
|
}
|
|
@mkdir($cacheDir.'/scripts', 0775, true);
|
|
file_put_contents($cacheDir.'/scripts/test.sh', "#!/bin/sh\necho ok\n");
|
|
|
|
$mapping = (new ScriptMapping())
|
|
->setPublicPath('mcp/graylog/install.sh')
|
|
->setRepositoryUrl('https://forge.lclr.dev/AI/graylog-mcp.git')
|
|
->setGitRef('main')
|
|
->setRepositoryFilePath('install.sh')
|
|
->setCacheKey('scripts/test.sh');
|
|
$mapping->markSyncSucceeded('scripts/test.sh');
|
|
|
|
$this->entityManager->persist($mapping);
|
|
$this->entityManager->flush();
|
|
|
|
$this->client->request('GET', '/mcp/graylog/install.sh');
|
|
|
|
self::assertResponseIsSuccessful();
|
|
self::assertResponseHeaderSame('content-type', 'text/x-shellscript; charset=UTF-8');
|
|
self::assertStringContainsString('echo ok', (string) $this->client->getResponse()->getContent());
|
|
}
|
|
}
|