1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-08-01 17:38:38 +00:00

Hash the urls to check if they exist

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2017-05-28 14:53:04 +02:00 committed by Jeremy Benoist
parent 3620dae1e6
commit bfe02a0b48
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
7 changed files with 306 additions and 14 deletions

View file

@ -987,6 +987,8 @@ class EntryRestControllerTest extends WallabagApiTestCase
{
$this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
$this->client->request('GET', '/api/entries/exists?hashedurl=' . hash('md5', 'http://0.0.0.0/entry2'));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
@ -994,10 +996,22 @@ class EntryRestControllerTest extends WallabagApiTestCase
$this->assertTrue($content['exists']);
}
public function testGetEntriesExistsWithHash()
{
$this->client->request('GET', '/api/entries/exists?hashedurl=' . hash('md5', 'http://0.0.0.0/entry2'));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertSame(2, $content['exists']);
}
public function testGetEntriesExistsWithManyUrls()
{
$url1 = 'http://0.0.0.0/entry2';
$url2 = 'http://0.0.0.0/entry10';
$this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
@ -1027,9 +1041,46 @@ class EntryRestControllerTest extends WallabagApiTestCase
$this->assertFalse($content[$url2]);
}
public function testGetEntriesExistsWithManyUrlsHashed()
{
$url1 = 'http://0.0.0.0/entry2';
$url2 = 'http://0.0.0.0/entry10';
$this->client->request('GET', '/api/entries/exists?hashedurls[]='.hash('md5',$url1).'&hashedurls[]='.hash('md5',$url2) . '&return_id=1');
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey($url1, $content);
$this->assertArrayHasKey($url2, $content);
$this->assertSame(2, $content[$url1]);
$this->assertNull($content[$url2]);
$this->assertArrayHasKey(hash('md5', $url1), $content);
$this->assertArrayHasKey(hash('md5', $url2), $content);
$this->assertEquals(2, $content[hash('md5', $url1)]);
$this->assertEquals(false, $content[hash('md5', $url2)]);
}
public function testGetEntriesExistsWithManyUrlsHashedReturnBool()
{
$url1 = 'http://0.0.0.0/entry2';
$url2 = 'http://0.0.0.0/entry10';
$this->client->request('GET', '/api/entries/exists?hashedurls[]='.hash('md5',$url1).'&hashedurls[]='.hash('md5',$url2));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey($url1, $content);
$this->assertArrayHasKey($url2, $content);
$this->assertTrue($content[$url1]);
$this->assertFalse($content[$url2]);
}
public function testGetEntriesExistsWhichDoesNotExists()
{
$this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
$this->client->request('GET', '/api/entries/exists?hashedurl='.hash('md5','http://google.com/entry2'));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
@ -1040,7 +1091,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
public function testGetEntriesExistsWithNoUrl()
{
$this->client->request('GET', '/api/entries/exists?url=');
$this->client->request('GET', '/api/entries/exists?hashedurl=');
$this->assertSame(403, $this->client->getResponse()->getStatusCode());
}

View file

@ -0,0 +1,101 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Command\GenerateUrlHashesCommand;
use Wallabag\CoreBundle\Entity\Entry;
class GenerateUrlHashesCommandTest extends WallabagCoreTestCase
{
public function testRunGenerateUrlHashesCommand()
{
$application = new Application($this->getClient()->getKernel());
$application->add(new GenerateUrlHashesCommand());
$command = $application->find('wallabag:generate-hashed-urls');
$tester = new CommandTester($command);
$tester->execute([
'command' => $command->getName(),
]);
$this->assertContains('Generating hashed urls for the 3 user account entries', $tester->getDisplay());
$this->assertContains('Finished generated hashed urls', $tester->getDisplay());
}
public function testRunGenerateUrlHashesCommandWithBadUsername()
{
$application = new Application($this->getClient()->getKernel());
$application->add(new GenerateUrlHashesCommand());
$command = $application->find('wallabag:generate-hashed-urls');
$tester = new CommandTester($command);
$tester->execute([
'command' => $command->getName(),
'username' => 'unknown',
]);
$this->assertContains('User "unknown" not found', $tester->getDisplay());
}
public function testRunGenerateUrlHashesCommandForUser()
{
$application = new Application($this->getClient()->getKernel());
$application->add(new GenerateUrlHashesCommand());
$command = $application->find('wallabag:generate-hashed-urls');
$tester = new CommandTester($command);
$tester->execute([
'command' => $command->getName(),
'username' => 'admin',
]);
$this->assertContains('Generated hashed urls for user admin', $tester->getDisplay());
}
public function testGenerateUrls()
{
$url = 'http://www.lemonde.fr/sport/visuel/2017/05/05/rondelle-prison-blanchissage-comprendre-le-hockey-sur-glace_5122587_3242.html';
$client = $this->getClient();
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$this->logInAs('admin');
$user = $em->getRepository('WallabagUserBundle:User')->findOneById($this->getLoggedInUserId());
$entry1 = new Entry($user);
$entry1->setUrl($url);
$em->persist($entry1);
$em->flush();
$this->assertNull($entry1->getHashedUrl());
$application = new Application($this->getClient()->getKernel());
$application->add(new GenerateUrlHashesCommand());
$command = $application->find('wallabag:generate-hashed-urls');
$tester = new CommandTester($command);
$tester->execute([
'command' => $command->getName(),
'username' => 'admin',
]);
$this->assertContains('Generated hashed urls for user admin', $tester->getDisplay());
$entry = $em->getRepository('WallabagCoreBundle:Entry')->findOneByUrl($url);
$this->assertEquals($entry->getHashedUrl(), hash('sha512', $url));
$query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url');
$query->setParameter('url', $url);
$query->execute();
}
}