Implement TempUrlLocalStorageGenerator and its tests

Added the full implementation for TempUrlLocalStorageGenerator, including methods to generate signed URLs and POST requests with expiration and signature logic. Introduced corresponding unit tests to validate functionality using mocked dependencies.
This commit is contained in:
2024-12-16 23:20:08 +01:00
parent 3a2548ed89
commit 1f6de3cb11
2 changed files with 126 additions and 2 deletions

View File

@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Tests\AsyncUpload\Driver\LocalStorage;
use Chill\DocStoreBundle\AsyncUpload\Driver\LocalStorage\TempUrlLocalStorageGenerator;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* @internal
*
* @coversNothing
*/
class TempUrlLocalStorageGeneratorTest extends TestCase
{
use ProphecyTrait;
public function testGenerate(): void
{
$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$urlGenerator->generate('chill_docstore_stored_object_operate', [
'object_name' => 'testABC',
'exp' => $exp = 1734307200 + 180,
'sig' => '528a426aebea86ada86035e9f3299788a074b77d3f926c6c15492457f81e88cfa98b270ba489d5d7588612eadaeeb7717a57887b33932d6d449b5f441252e600',
], UrlGeneratorInterface::ABSOLUTE_URL)
->shouldBeCalled()
->willReturn($url = 'http://example.com/public/doc-store/stored-object/operate/testABC');
$generator = $this->buildGenerator($urlGenerator->reveal());
$signedUrl = $generator->generate('GET', 'testABC');
self::assertEquals($url, $signedUrl->url);
self::assertEquals('testABC', $signedUrl->object_name);
self::assertEquals($exp, $signedUrl->expires->getTimestamp());
self::assertEquals('GET', $signedUrl->method);
}
public function testGeneratePost(): void
{
$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$urlGenerator->generate('chill_docstore_storedobject_post', [
'prefix' => 'prefixABC',
], UrlGeneratorInterface::ABSOLUTE_URL)
->shouldBeCalled()
->willReturn($url = 'http://example.com/public/doc-store/stored-object/prefixABC');
$generator = $this->buildGenerator($urlGenerator->reveal());
$signedUrl = $generator->generatePost(object_name: 'prefixABC');
self::assertEquals($url, $signedUrl->url);
self::assertEquals('prefixABC', $signedUrl->object_name);
self::assertEquals(1734307200 + 180 + 180, $signedUrl->expires->getTimestamp());
self::assertEquals('POST', $signedUrl->method);
self::assertEquals('47e9271917c6c9149a47248d88eeadcbc1f804138e445f3406d39f6aa51b2d976c2ee6e5b1196d2bf88852b627b330596c2fbf6f31b06837e9f41cf56103a4e4', $signedUrl->signature);
}
private function buildGenerator(UrlGeneratorInterface $urlGenerator): TempUrlLocalStorageGenerator
{
return new TempUrlLocalStorageGenerator(
'abc',
new MockClock('2024-12-16T00:00:00+00:00'),
$urlGenerator,
);
}
}