Skip to content

Commit

Permalink
Add JWECollector tests for builders, decrypters, and loaders
Browse files Browse the repository at this point in the history
Introduce functional tests for `JWECollector` to verify support for JWE builders, decrypters, and loaders without requiring a compression method manager. These tests ensure proper data collection and structure validation for the `JWECollector` in various configurations.
  • Loading branch information
Spomky committed Dec 12, 2024
1 parent 4884c36 commit fc1dbeb
Showing 1 changed file with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Jose\Tests\Bundle\JoseFramework\Functional\Encryption;

use Jose\Bundle\JoseFramework\DataCollector\JWECollector;
use Jose\Bundle\JoseFramework\Services\JWEBuilderFactory as JWEBuilderFactoryService;
use Jose\Bundle\JoseFramework\Services\JWEDecrypterFactory as JWEDecrypterFactoryService;
use Jose\Bundle\JoseFramework\Services\JWELoaderFactory as JWELoaderFactoryAlias;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @internal
*/
final class JWECollectorTest extends WebTestCase
{
#[Test]
public function aJWEBuilderCanBeCollectedWithoutACompressionMethodManager(): void
{
static::ensureKernelShutdown();
$client = static::createClient();
$container = $client->getContainer();
static::assertInstanceOf(ContainerInterface::class, $container);

$jweFactory = $container->get(JWEBuilderFactoryService::class);
static::assertInstanceOf(JWEBuilderFactoryService::class, $jweFactory);

$jweBuilder = $jweFactory->create(['RSA1_5', 'A256GCM']);

$jweCollector = new JWECollector();
$jweCollector->addJWEBuilder('builder2', $jweBuilder);

$data = [];
$jweCollector->collect($data, new Request(), new Response());

static::assertArrayHasKey('jwe', $data);
static::assertArrayNotHasKey('compression_methods', $data['jwe']);
static::assertArrayHasKey('jwe_builders', $data['jwe']);
static::assertArrayHasKey('builder2', $data['jwe']['jwe_builders']);
}

#[Test]
public function aJWEDecrypterCanBeCollectedWithoutACompressionMethodManager(): void
{
static::ensureKernelShutdown();
$client = static::createClient();
$container = $client->getContainer();
static::assertInstanceOf(ContainerInterface::class, $container);

$jweDecrypterFactory = $container->get(JWEDecrypterFactoryService::class);
static::assertInstanceOf(JWEDecrypterFactoryService::class, $jweDecrypterFactory);

$jweDecrypter = $jweDecrypterFactory->create(['RSA1_5', 'A256GCM']);

$jweCollector = new JWECollector();
$jweCollector->addJWEDecrypter('decrypter2', $jweDecrypter);

$data = [];
$jweCollector->collect($data, new Request(), new Response());

static::assertArrayHasKey('jwe', $data);
static::assertArrayNotHasKey('compression_methods', $data['jwe']);

static::assertArrayHasKey('jwe_decrypters', $data['jwe']);
static::assertArrayHasKey('decrypter2', $data['jwe']['jwe_decrypters']);
}

#[Test]
public function aJWELoaderCanBeCollectedWithoutACompressionMethodManager(): void
{
static::ensureKernelShutdown();
$client = static::createClient();
$container = $client->getContainer();
static::assertInstanceOf(ContainerInterface::class, $container);

$jweLoaderFactory = $container->get(JWELoaderFactoryAlias::class);
static::assertInstanceOf(JWELoaderFactoryAlias::class, $jweLoaderFactory);

$jweLoader = $jweLoaderFactory->create(['jwe_compact'], ['RSA1_5', 'A256GCM']);

$jweCollector = new JWECollector();
$jweCollector->addJWELoader('loader2', $jweLoader);

$data = [];
$jweCollector->collect($data, new Request(), new Response());

static::assertArrayHasKey('jwe', $data);
static::assertArrayNotHasKey('compression_methods', $data['jwe']);
static::assertArrayHasKey('jwe_loaders', $data['jwe']);
static::assertArrayHasKey('loader2', $data['jwe']['jwe_loaders']);
}
}

0 comments on commit fc1dbeb

Please sign in to comment.