Updated logic to generate random short codes, increasing entropy

This commit is contained in:
Alejandro Celaya
2019-10-11 09:14:25 +02:00
parent c8d950e04d
commit 2f09ff456c
13 changed files with 91 additions and 149 deletions

View File

@@ -90,7 +90,7 @@ class CreateShortUrlActionTest extends ApiTestCase
$this->assertEquals(self::STATUS_OK, $statusCode);
// Request to the short URL will return a 404 since ist' not valid yet
// Request to the short URL will return a 404 since it's not valid yet
$lastResp = $this->callShortUrl($shortCode);
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode());
}

View File

@@ -20,13 +20,15 @@ class ShortUrlsFixture extends AbstractFixture
*/
public function load(ObjectManager $manager): void
{
$abcShortUrl = $this->setShortUrlDate(new ShortUrl('https://shlink.io'))->setShortCode('abc123');
$abcShortUrl = $this->setShortUrlDate(
new ShortUrl('https://shlink.io', ShortUrlMeta::createFromRawData(['customSlug' => 'abc123']))
);
$manager->persist($abcShortUrl);
$defShortUrl = $this->setShortUrlDate(new ShortUrl(
'https://blog.alejandrocelaya.com/2017/12/09/acmailer-7-0-the-most-important-release-in-a-long-time/',
ShortUrlMeta::createFromParams(Chronos::parse('2020-05-01'))
))->setShortCode('def456');
ShortUrlMeta::createFromParams(Chronos::parse('2020-05-01'), null, 'def456')
));
$manager->persist($defShortUrl);
$customShortUrl = $this->setShortUrlDate(new ShortUrl(
@@ -37,14 +39,14 @@ class ShortUrlsFixture extends AbstractFixture
$withDomainShortUrl = $this->setShortUrlDate(new ShortUrl(
'https://blog.alejandrocelaya.com/2019/04/27/considerations-to-properly-use-open-source-software-projects/',
ShortUrlMeta::createFromRawData(['domain' => 'example.com'])
))->setShortCode('ghi789');
ShortUrlMeta::createFromRawData(['domain' => 'example.com', 'customSlug' => 'ghi789'])
));
$manager->persist($withDomainShortUrl);
$withDomainAndSlugShortUrl = $this->setShortUrlDate(new ShortUrl(
'https://google.com',
ShortUrlMeta::createFromRawData(['domain' => 'some-domain.com'])
))->setShortCode('custom-with-domain');
ShortUrlMeta::createFromRawData(['domain' => 'some-domain.com', 'customSlug' => 'custom-with-domain'])
));
$manager->persist($withDomainAndSlugShortUrl);
$manager->flush();

View File

@@ -22,6 +22,11 @@ use function strpos;
class CreateShortUrlActionTest extends TestCase
{
private const DOMAIN_CONFIG = [
'schema' => 'http',
'hostname' => 'foo.com',
];
/** @var CreateShortUrlAction */
private $action;
/** @var ObjectProphecy */
@@ -30,10 +35,7 @@ class CreateShortUrlActionTest extends TestCase
public function setUp(): void
{
$this->urlShortener = $this->prophesize(UrlShortener::class);
$this->action = new CreateShortUrlAction($this->urlShortener->reveal(), [
'schema' => 'http',
'hostname' => 'foo.com',
]);
$this->action = new CreateShortUrlAction($this->urlShortener->reveal(), self::DOMAIN_CONFIG);
}
/** @test */
@@ -46,10 +48,9 @@ class CreateShortUrlActionTest extends TestCase
/** @test */
public function properShortcodeConversionReturnsData(): void
{
$shortUrl = new ShortUrl('');
$this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
->willReturn(
(new ShortUrl(''))->setShortCode('abc123')
)
->willReturn($shortUrl)
->shouldBeCalledOnce();
$request = (new ServerRequest())->withParsedBody([
@@ -57,7 +58,7 @@ class CreateShortUrlActionTest extends TestCase
]);
$response = $this->action->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), 'http://foo.com/abc123') > 0);
$this->assertTrue(strpos($response->getBody()->getContents(), $shortUrl->toString(self::DOMAIN_CONFIG)) > 0);
}
/** @test */