Skip to content

Commit 7c00526

Browse files
committed
Signature Bundle tests
1 parent 775fad1 commit 7c00526

File tree

17 files changed

+420
-37
lines changed

17 files changed

+420
-37
lines changed

src/Bundle/Checker/Tests/TestBundle/Resources/config/services.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ services:
44
autoconfigure: true
55
public: true
66

7-
Jose\Bundle\Checker\Tests\TestBundle\Converter\CustomJsonConverter: ~
87
Jose\Bundle\Checker\Tests\TestBundle\Checker\CustomChecker:
98
class:
109
public: false

src/Bundle/Encryption/phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
<directory>./Tests/</directory>
1616
</testsuite>
1717
</testsuites>
18-
18+
<php>
19+
<ini name="error_reporting" value="-1" />
20+
<server name="KERNEL_DIR" value="./Tests/" />
21+
</php>
1922
<filter>
2023
<whitelist>
2124
<directory suffix=".php">./</directory>

src/Bundle/KeyManagement/phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
<directory>./Tests/</directory>
1616
</testsuite>
1717
</testsuites>
18-
18+
<php>
19+
<ini name="error_reporting" value="-1" />
20+
<server name="KERNEL_DIR" value="./Tests/" />
21+
</php>
1922
<filter>
2023
<whitelist>
2124
<directory suffix=".php">./</directory>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
use Symfony\Component\Config\Loader\LoaderInterface;
15+
use Symfony\Component\HttpKernel\Kernel;
16+
17+
/**
18+
* Class AppKernel.
19+
*/
20+
final class AppKernel extends Kernel
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function registerBundles()
26+
{
27+
$bundles = [
28+
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
29+
new Jose\Bundle\JoseFramework\JoseFrameworkBundle(),
30+
new Jose\Bundle\Checker\CheckerBundle(),
31+
new Jose\Bundle\Signature\SignatureBundle(),
32+
new Jose\Bundle\Signature\Tests\TestBundle\TestBundle(),
33+
];
34+
35+
return $bundles;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getCacheDir()
42+
{
43+
return sys_get_temp_dir().'/Jose/Bundle/Test';
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function registerContainerConfiguration(LoaderInterface $loader)
50+
{
51+
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
52+
}
53+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Signature\Tests;
15+
16+
use Jose\Component\Signature\JWSBuilder;
17+
use Jose\Component\Signature\JWSBuilderFactory;
18+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
19+
20+
/**
21+
* @group Bundle
22+
* @group Functional
23+
*/
24+
final class JWSBuilderTest extends WebTestCase
25+
{
26+
public function testJWSBuilderFactoryIsAvailable()
27+
{
28+
$client = static::createClient();
29+
$container = $client->getContainer();
30+
self::assertNotNull($container);
31+
self::assertTrue($container->has(JWSBuilderFactory::class));
32+
}
33+
34+
public function testJWSBuilderFactoryCanCreateAJWSBuilder()
35+
{
36+
$client = static::createClient();
37+
38+
/** @var JWSBuilderFactory $jwsFactory */
39+
$jwsFactory = $client->getContainer()->get(JWSBuilderFactory::class);
40+
41+
$jws = $jwsFactory->create(['none']);
42+
43+
self::assertInstanceOf(JWSBuilder::class, $jws);
44+
}
45+
46+
public function testJWSBuilderFromConfigurationIsAvailable()
47+
{
48+
$client = static::createClient();
49+
$container = $client->getContainer();
50+
self::assertTrue($container->has('jose.jws_builder.builder1'));
51+
52+
$jws = $container->get('jose.jws_builder.builder1');
53+
self::assertInstanceOf(JWSBuilder::class, $jws);
54+
}
55+
56+
public function testJWSBuilderFromExternalBundleExtensionIsAvailable()
57+
{
58+
$client = static::createClient();
59+
$container = $client->getContainer();
60+
self::assertTrue($container->has('jose.jws_builder.builder2'));
61+
62+
$jws = $container->get('jose.jws_builder.builder2');
63+
self::assertInstanceOf(JWSBuilder::class, $jws);
64+
}
65+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Signature\Tests;
15+
16+
use Jose\Component\Core\JWK;
17+
use Jose\Component\Signature\JWSBuilder;
18+
use Jose\Component\Signature\JWSLoader;
19+
use Jose\Component\Signature\Serializer\CompactSerializer;
20+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
21+
22+
/**
23+
* @group Bundle
24+
* @group Functional
25+
*/
26+
final class JWSComputationTest extends WebTestCase
27+
{
28+
public function testCreateAndLoadAToken()
29+
{
30+
$client = static::createClient();
31+
$container = $client->getContainer();
32+
33+
$jwk = JWK::create([
34+
'kty' => 'oct',
35+
'k' => '3pWc2vAZpHoV7XmCT-z2hWhdQquwQwW5a3XTojbf87c',
36+
]);
37+
38+
/** @var JWSBuilder $builder */
39+
$builder = $container->get('jose.jws_builder.builder1');
40+
41+
/** @var JWSLoader $loader */
42+
$loader = $container->get('jose.jws_loader.loader1');
43+
44+
/** @var CompactSerializer $serializer */
45+
$serializer = $container->get(CompactSerializer::class);
46+
47+
$jws = $builder
48+
->create()
49+
->withPayload('Hello World!')
50+
->addSignature($jwk, [
51+
'alg' => 'HS256',
52+
'exp' => time() + 3600,
53+
])
54+
->build();
55+
$token = $serializer->serialize($jws, 0);
56+
57+
$loaded = $loader->load($token);
58+
$index = $loader->verifyWithKey($loaded, $jwk);
59+
self::assertEquals(0, $index);
60+
}
61+
62+
/**
63+
* @expectedException \InvalidArgumentException
64+
* @expectedExceptionMessage Unable to verify the JWS.
65+
*/
66+
public function testUnableToLoadAnExpiredToken()
67+
{
68+
$client = static::createClient();
69+
$container = $client->getContainer();
70+
71+
$jwk = JWK::create([
72+
'kty' => 'oct',
73+
'k' => '3pWc2vAZpHoV7XmCT-z2hWhdQquwQwW5a3XTojbf87c',
74+
]);
75+
76+
/** @var JWSBuilder $builder */
77+
$builder = $container->get('jose.jws_builder.builder1');
78+
79+
/** @var JWSLoader $loader */
80+
$loader = $container->get('jose.jws_loader.loader1');
81+
82+
/** @var CompactSerializer $serializer */
83+
$serializer = $container->get(CompactSerializer::class);
84+
85+
$jws = $builder
86+
->create()
87+
->withPayload('Hello World!')
88+
->addSignature($jwk, [
89+
'alg' => 'HS256',
90+
'exp' => time() - 3600,
91+
])
92+
->build();
93+
$token = $serializer->serialize($jws, 0);
94+
95+
$loaded = $loader->load($token);
96+
$loader->verifyWithKey($loaded, $jwk);
97+
}
98+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Signature\Tests;
15+
16+
use Jose\Component\Signature\JWSLoader;
17+
use Jose\Component\Signature\JWSLoaderFactory;
18+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
19+
20+
/**
21+
* @group Bundle
22+
* @group Functional
23+
*/
24+
final class JWSLoaderTest extends WebTestCase
25+
{
26+
public function testJWSLoaderFactoryIsAvailable()
27+
{
28+
$client = static::createClient();
29+
$container = $client->getContainer();
30+
self::assertNotNull($container);
31+
self::assertTrue($container->has(JWSLoaderFactory::class));
32+
}
33+
34+
public function testJWSLoaderFactoryCanCreateAJWSLoader()
35+
{
36+
$client = static::createClient();
37+
38+
/** @var JWSLoaderFactory $jwsFactory */
39+
$jwsFactory = $client->getContainer()->get(JWSLoaderFactory::class);
40+
41+
$jws = $jwsFactory->create(['none'], ['iat', 'exp', 'nbf'], ['jws_compact', 'jws_json_general', 'jws_json_flattened']);
42+
43+
self::assertInstanceOf(JWSLoader::class, $jws);
44+
}
45+
46+
public function testJWSLoaderFromConfigurationIsAvailable()
47+
{
48+
$client = static::createClient();
49+
$container = $client->getContainer();
50+
self::assertTrue($container->has('jose.jws_loader.loader1'));
51+
52+
$jws = $container->get('jose.jws_loader.loader1');
53+
self::assertInstanceOf(JWSLoader::class, $jws);
54+
}
55+
56+
public function testJWSLoaderFromExternalBundleExtensionIsAvailable()
57+
{
58+
$client = static::createClient();
59+
$container = $client->getContainer();
60+
self::assertTrue($container->has('jose.jws_loader.loader2'));
61+
62+
$jws = $container->get('jose.jws_loader.loader2');
63+
self::assertInstanceOf(JWSLoader::class, $jws);
64+
}
65+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Signature\Tests\TestBundle\DependencyInjection;
15+
16+
use Jose\Bundle\JoseFramework\Helper\ConfigurationHelper;
17+
use Symfony\Component\Config\FileLocator;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
20+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
21+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22+
23+
/**
24+
* Class TestExtension.
25+
*/
26+
final class TestExtension extends Extension implements PrependExtensionInterface
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function load(array $configs, ContainerBuilder $container)
32+
{
33+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
34+
$loader->load('services.yml');
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function prepend(ContainerBuilder $container)
41+
{
42+
ConfigurationHelper::addJWSBuilder($container, 'builder2', ['RS512', 'HS512', 'ES512'], true);
43+
ConfigurationHelper::addJWSLoader($container, 'loader2', ['RS512', 'HS512', 'ES512'], ['exp', 'iat', 'nbf'], ['jws_compact', 'jws_json_general', 'jws_json_flattened'], true);
44+
}
45+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
public: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2017 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Bundle\Signature\Tests\TestBundle;
15+
16+
use Symfony\Component\HttpKernel\Bundle\Bundle;
17+
18+
/**
19+
* Class TestBundle.
20+
*/
21+
final class TestBundle extends Bundle
22+
{
23+
}

0 commit comments

Comments
 (0)