diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2019-04-25 13:28:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-25 13:28:09 +0200 |
commit | 522e37ad274361dde697da13a92ff3f846599822 (patch) | |
tree | a2b9302d885d886e013a6c33e800f5b39293e861 /tests | |
parent | 3620dae1e6b3fab5a4ba4001b4581ce7ed795996 (diff) | |
parent | 76bc05ebc02408b213b536fec44e94b092889118 (diff) | |
download | wallabag-522e37ad274361dde697da13a92ff3f846599822.tar.gz wallabag-522e37ad274361dde697da13a92ff3f846599822.tar.zst wallabag-522e37ad274361dde697da13a92ff3f846599822.zip |
Merge pull request #3158 from wallabag/hash-exist-url
Hash exist url
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | 88 | ||||
-rw-r--r-- | tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php | 98 |
2 files changed, 175 insertions, 11 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 2151f587..8cc12ed3 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | |||
@@ -971,33 +971,49 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
971 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); | 971 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); |
972 | } | 972 | } |
973 | 973 | ||
974 | public function testGetEntriesExistsWithReturnId() | 974 | public function dataForEntriesExistWithUrl() |
975 | { | 975 | { |
976 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1'); | 976 | $url = hash('sha1', 'http://0.0.0.0/entry2'); |
977 | 977 | ||
978 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | 978 | return [ |
979 | 979 | 'with_id' => [ | |
980 | $content = json_decode($this->client->getResponse()->getContent(), true); | 980 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1', |
981 | 981 | 'expectedValue' => 2, | |
982 | // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value | 982 | ], |
983 | $this->assertGreaterThan(1, $content['exists']); | 983 | 'without_id' => [ |
984 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2', | ||
985 | 'expectedValue' => true, | ||
986 | ], | ||
987 | 'hashed_url_with_id' => [ | ||
988 | 'url' => '/api/entries/exists?hashed_url=' . $url . '&return_id=1', | ||
989 | 'expectedValue' => 2, | ||
990 | ], | ||
991 | 'hashed_url_without_id' => [ | ||
992 | 'url' => '/api/entries/exists?hashed_url=' . $url . '', | ||
993 | 'expectedValue' => true, | ||
994 | ], | ||
995 | ]; | ||
984 | } | 996 | } |
985 | 997 | ||
986 | public function testGetEntriesExistsWithoutReturnId() | 998 | /** |
999 | * @dataProvider dataForEntriesExistWithUrl | ||
1000 | */ | ||
1001 | public function testGetEntriesExists($url, $expectedValue) | ||
987 | { | 1002 | { |
988 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | 1003 | $this->client->request('GET', $url); |
989 | 1004 | ||
990 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | 1005 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
991 | 1006 | ||
992 | $content = json_decode($this->client->getResponse()->getContent(), true); | 1007 | $content = json_decode($this->client->getResponse()->getContent(), true); |
993 | 1008 | ||
994 | $this->assertTrue($content['exists']); | 1009 | $this->assertSame($expectedValue, $content['exists']); |
995 | } | 1010 | } |
996 | 1011 | ||
997 | public function testGetEntriesExistsWithManyUrls() | 1012 | public function testGetEntriesExistsWithManyUrls() |
998 | { | 1013 | { |
999 | $url1 = 'http://0.0.0.0/entry2'; | 1014 | $url1 = 'http://0.0.0.0/entry2'; |
1000 | $url2 = 'http://0.0.0.0/entry10'; | 1015 | $url2 = 'http://0.0.0.0/entry10'; |
1016 | |||
1001 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1'); | 1017 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1'); |
1002 | 1018 | ||
1003 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | 1019 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
@@ -1027,6 +1043,38 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
1027 | $this->assertFalse($content[$url2]); | 1043 | $this->assertFalse($content[$url2]); |
1028 | } | 1044 | } |
1029 | 1045 | ||
1046 | public function testGetEntriesExistsWithManyUrlsHashed() | ||
1047 | { | ||
1048 | $url1 = 'http://0.0.0.0/entry2'; | ||
1049 | $url2 = 'http://0.0.0.0/entry10'; | ||
1050 | $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2) . '&return_id=1'); | ||
1051 | |||
1052 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1053 | |||
1054 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1055 | |||
1056 | $this->assertArrayHasKey(hash('sha1', $url1), $content); | ||
1057 | $this->assertArrayHasKey(hash('sha1', $url2), $content); | ||
1058 | $this->assertSame(2, $content[hash('sha1', $url1)]); | ||
1059 | $this->assertNull($content[hash('sha1', $url2)]); | ||
1060 | } | ||
1061 | |||
1062 | public function testGetEntriesExistsWithManyUrlsHashedReturnBool() | ||
1063 | { | ||
1064 | $url1 = 'http://0.0.0.0/entry2'; | ||
1065 | $url2 = 'http://0.0.0.0/entry10'; | ||
1066 | $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2)); | ||
1067 | |||
1068 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1069 | |||
1070 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1071 | |||
1072 | $this->assertArrayHasKey(hash('sha1', $url1), $content); | ||
1073 | $this->assertArrayHasKey(hash('sha1', $url2), $content); | ||
1074 | $this->assertTrue($content[hash('sha1', $url1)]); | ||
1075 | $this->assertFalse($content[hash('sha1', $url2)]); | ||
1076 | } | ||
1077 | |||
1030 | public function testGetEntriesExistsWhichDoesNotExists() | 1078 | public function testGetEntriesExistsWhichDoesNotExists() |
1031 | { | 1079 | { |
1032 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | 1080 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); |
@@ -1038,6 +1086,17 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
1038 | $this->assertFalse($content['exists']); | 1086 | $this->assertFalse($content['exists']); |
1039 | } | 1087 | } |
1040 | 1088 | ||
1089 | public function testGetEntriesExistsWhichDoesNotExistsWithHashedUrl() | ||
1090 | { | ||
1091 | $this->client->request('GET', '/api/entries/exists?hashed_url=' . hash('sha1', 'http://google.com/entry2')); | ||
1092 | |||
1093 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | ||
1094 | |||
1095 | $content = json_decode($this->client->getResponse()->getContent(), true); | ||
1096 | |||
1097 | $this->assertFalse($content['exists']); | ||
1098 | } | ||
1099 | |||
1041 | public function testGetEntriesExistsWithNoUrl() | 1100 | public function testGetEntriesExistsWithNoUrl() |
1042 | { | 1101 | { |
1043 | $this->client->request('GET', '/api/entries/exists?url='); | 1102 | $this->client->request('GET', '/api/entries/exists?url='); |
@@ -1045,6 +1104,13 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
1045 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); | 1104 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); |
1046 | } | 1105 | } |
1047 | 1106 | ||
1107 | public function testGetEntriesExistsWithNoHashedUrl() | ||
1108 | { | ||
1109 | $this->client->request('GET', '/api/entries/exists?hashed_url='); | ||
1110 | |||
1111 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); | ||
1112 | } | ||
1113 | |||
1048 | public function testReloadEntryErrorWhileFetching() | 1114 | public function testReloadEntryErrorWhileFetching() |
1049 | { | 1115 | { |
1050 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | 1116 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
diff --git a/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php b/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php new file mode 100644 index 00000000..17eed210 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php | |||
@@ -0,0 +1,98 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Command; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
6 | use Symfony\Component\Console\Tester\CommandTester; | ||
7 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | ||
8 | use Wallabag\CoreBundle\Command\GenerateUrlHashesCommand; | ||
9 | use Wallabag\CoreBundle\Entity\Entry; | ||
10 | |||
11 | class GenerateUrlHashesCommandTest extends WallabagCoreTestCase | ||
12 | { | ||
13 | public function testRunGenerateUrlHashesCommand() | ||
14 | { | ||
15 | $application = new Application($this->getClient()->getKernel()); | ||
16 | $application->add(new GenerateUrlHashesCommand()); | ||
17 | |||
18 | $command = $application->find('wallabag:generate-hashed-urls'); | ||
19 | |||
20 | $tester = new CommandTester($command); | ||
21 | $tester->execute([ | ||
22 | 'command' => $command->getName(), | ||
23 | ]); | ||
24 | |||
25 | $this->assertContains('Generating hashed urls for "3" users', $tester->getDisplay()); | ||
26 | $this->assertContains('Finished generated hashed urls', $tester->getDisplay()); | ||
27 | } | ||
28 | |||
29 | public function testRunGenerateUrlHashesCommandWithBadUsername() | ||
30 | { | ||
31 | $application = new Application($this->getClient()->getKernel()); | ||
32 | $application->add(new GenerateUrlHashesCommand()); | ||
33 | |||
34 | $command = $application->find('wallabag:generate-hashed-urls'); | ||
35 | |||
36 | $tester = new CommandTester($command); | ||
37 | $tester->execute([ | ||
38 | 'command' => $command->getName(), | ||
39 | 'username' => 'unknown', | ||
40 | ]); | ||
41 | |||
42 | $this->assertContains('User "unknown" not found', $tester->getDisplay()); | ||
43 | } | ||
44 | |||
45 | public function testRunGenerateUrlHashesCommandForUser() | ||
46 | { | ||
47 | $application = new Application($this->getClient()->getKernel()); | ||
48 | $application->add(new GenerateUrlHashesCommand()); | ||
49 | |||
50 | $command = $application->find('wallabag:generate-hashed-urls'); | ||
51 | |||
52 | $tester = new CommandTester($command); | ||
53 | $tester->execute([ | ||
54 | 'command' => $command->getName(), | ||
55 | 'username' => 'admin', | ||
56 | ]); | ||
57 | |||
58 | $this->assertContains('Generated hashed urls for user: admin', $tester->getDisplay()); | ||
59 | } | ||
60 | |||
61 | public function testGenerateUrls() | ||
62 | { | ||
63 | $url = 'http://www.lemonde.fr/sport/visuel/2017/05/05/rondelle-prison-blanchissage-comprendre-le-hockey-sur-glace_5122587_3242.html'; | ||
64 | $client = $this->getClient(); | ||
65 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | ||
66 | |||
67 | $this->logInAs('admin'); | ||
68 | |||
69 | $user = $em->getRepository('WallabagUserBundle:User')->findOneById($this->getLoggedInUserId()); | ||
70 | |||
71 | $entry1 = new Entry($user); | ||
72 | $entry1->setUrl($url); | ||
73 | |||
74 | $em->persist($entry1); | ||
75 | $em->flush(); | ||
76 | |||
77 | $application = new Application($this->getClient()->getKernel()); | ||
78 | $application->add(new GenerateUrlHashesCommand()); | ||
79 | |||
80 | $command = $application->find('wallabag:generate-hashed-urls'); | ||
81 | |||
82 | $tester = new CommandTester($command); | ||
83 | $tester->execute([ | ||
84 | 'command' => $command->getName(), | ||
85 | 'username' => 'admin', | ||
86 | ]); | ||
87 | |||
88 | $this->assertContains('Generated hashed urls for user: admin', $tester->getDisplay()); | ||
89 | |||
90 | $entry = $em->getRepository('WallabagCoreBundle:Entry')->findOneByUrl($url); | ||
91 | |||
92 | $this->assertSame($entry->getHashedUrl(), hash('sha1', $url)); | ||
93 | |||
94 | $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url'); | ||
95 | $query->setParameter('url', $url); | ||
96 | $query->execute(); | ||
97 | } | ||
98 | } | ||