diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-06-20 16:40:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-20 16:40:48 +0200 |
commit | 80784b782becfaa297e6d9cbb0584e27739cffc8 (patch) | |
tree | fc201969597b16070d890b0703568618a81a76bc /tests | |
parent | 604cca1f4247f9f905e57b9276cf2543cfa41a5d (diff) | |
parent | f44dba22fc1a566cb156d9e6eda5afc353163eda (diff) | |
download | wallabag-80784b782becfaa297e6d9cbb0584e27739cffc8.tar.gz wallabag-80784b782becfaa297e6d9cbb0584e27739cffc8.tar.zst wallabag-80784b782becfaa297e6d9cbb0584e27739cffc8.zip |
Merge pull request #2683 from wallabag/credentials-in-db
Store credentials in DB
Diffstat (limited to 'tests')
4 files changed, 283 insertions, 8 deletions
diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 853f37f2..7cf28bfe 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php | |||
@@ -5,6 +5,7 @@ namespace Tests\Wallabag\CoreBundle\Controller; | |||
5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | 5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; |
6 | use Wallabag\CoreBundle\Entity\Config; | 6 | use Wallabag\CoreBundle\Entity\Config; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\CoreBundle\Entity\SiteCredential; | ||
8 | 9 | ||
9 | class EntryControllerTest extends WallabagCoreTestCase | 10 | class EntryControllerTest extends WallabagCoreTestCase |
10 | { | 11 | { |
@@ -1335,4 +1336,56 @@ class EntryControllerTest extends WallabagCoreTestCase | |||
1335 | $this->assertEquals($url, $content->getUrl()); | 1336 | $this->assertEquals($url, $content->getUrl()); |
1336 | $this->assertEquals($expectedLanguage, $content->getLanguage()); | 1337 | $this->assertEquals($expectedLanguage, $content->getLanguage()); |
1337 | } | 1338 | } |
1339 | |||
1340 | /** | ||
1341 | * This test will require an internet connection. | ||
1342 | */ | ||
1343 | public function testRestrictedArticle() | ||
1344 | { | ||
1345 | $url = 'http://www.monde-diplomatique.fr/2017/05/BONNET/57475'; | ||
1346 | $this->logInAs('admin'); | ||
1347 | $client = $this->getClient(); | ||
1348 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | ||
1349 | |||
1350 | // enable restricted access | ||
1351 | $client->getContainer()->get('craue_config')->set('restricted_access', 1); | ||
1352 | |||
1353 | // create a new site_credential | ||
1354 | $user = $client->getContainer()->get('security.token_storage')->getToken()->getUser(); | ||
1355 | $credential = new SiteCredential($user); | ||
1356 | $credential->setHost('monde-diplomatique.fr'); | ||
1357 | $credential->setUsername($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('foo')); | ||
1358 | $credential->setPassword($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('bar')); | ||
1359 | |||
1360 | $em->persist($credential); | ||
1361 | $em->flush(); | ||
1362 | |||
1363 | $crawler = $client->request('GET', '/new'); | ||
1364 | |||
1365 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
1366 | |||
1367 | $form = $crawler->filter('form[name=entry]')->form(); | ||
1368 | |||
1369 | $data = [ | ||
1370 | 'entry[url]' => $url, | ||
1371 | ]; | ||
1372 | |||
1373 | $client->submit($form, $data); | ||
1374 | |||
1375 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | ||
1376 | |||
1377 | $crawler = $client->followRedirect(); | ||
1378 | |||
1379 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
1380 | $this->assertContains('flashes.entry.notice.entry_saved', $crawler->filter('body')->extract(['_text'])[0]); | ||
1381 | |||
1382 | $content = $em | ||
1383 | ->getRepository('WallabagCoreBundle:Entry') | ||
1384 | ->findByUrlAndUserId($url, $this->getLoggedInUserId()); | ||
1385 | |||
1386 | $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content); | ||
1387 | $this->assertSame('Crimes et réformes aux Philippines', $content->getTitle()); | ||
1388 | |||
1389 | $client->getContainer()->get('craue_config')->set('restricted_access', 0); | ||
1390 | } | ||
1338 | } | 1391 | } |
diff --git a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php new file mode 100644 index 00000000..e73a9743 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php | |||
@@ -0,0 +1,139 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Controller; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Client; | ||
6 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | ||
7 | use Wallabag\CoreBundle\Entity\SiteCredential; | ||
8 | |||
9 | class SiteCredentialControllerTest extends WallabagCoreTestCase | ||
10 | { | ||
11 | public function testListSiteCredential() | ||
12 | { | ||
13 | $this->logInAs('admin'); | ||
14 | $client = $this->getClient(); | ||
15 | |||
16 | $crawler = $client->request('GET', '/site-credentials/'); | ||
17 | |||
18 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
19 | |||
20 | $body = $crawler->filter('body')->extract(['_text'])[0]; | ||
21 | |||
22 | $this->assertContains('site_credential.description', $body); | ||
23 | $this->assertContains('site_credential.list.create_new_one', $body); | ||
24 | } | ||
25 | |||
26 | public function testNewSiteCredential() | ||
27 | { | ||
28 | $this->logInAs('admin'); | ||
29 | $client = $this->getClient(); | ||
30 | |||
31 | $crawler = $client->request('GET', '/site-credentials/new'); | ||
32 | |||
33 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
34 | |||
35 | $body = $crawler->filter('body')->extract(['_text'])[0]; | ||
36 | |||
37 | $this->assertContains('site_credential.new_site_credential', $body); | ||
38 | $this->assertContains('site_credential.form.back_to_list', $body); | ||
39 | |||
40 | $form = $crawler->filter('button[id=site_credential_save]')->form(); | ||
41 | |||
42 | $data = [ | ||
43 | 'site_credential[host]' => 'google.io', | ||
44 | 'site_credential[username]' => 'sergei', | ||
45 | 'site_credential[password]' => 'microsoft', | ||
46 | ]; | ||
47 | |||
48 | $client->submit($form, $data); | ||
49 | |||
50 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | ||
51 | |||
52 | $crawler = $client->followRedirect(); | ||
53 | |||
54 | $this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]); | ||
55 | } | ||
56 | |||
57 | public function testEditSiteCredential() | ||
58 | { | ||
59 | $this->logInAs('admin'); | ||
60 | $client = $this->getClient(); | ||
61 | |||
62 | $credential = $this->createSiteCredential($client); | ||
63 | |||
64 | $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); | ||
65 | |||
66 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
67 | |||
68 | $body = $crawler->filter('body')->extract(['_text'])[0]; | ||
69 | |||
70 | $this->assertContains('site_credential.edit_site_credential', $body); | ||
71 | $this->assertContains('site_credential.form.back_to_list', $body); | ||
72 | |||
73 | $form = $crawler->filter('button[id=site_credential_save]')->form(); | ||
74 | |||
75 | $data = [ | ||
76 | 'site_credential[host]' => 'google.io', | ||
77 | 'site_credential[username]' => 'larry', | ||
78 | 'site_credential[password]' => 'microsoft', | ||
79 | ]; | ||
80 | |||
81 | $client->submit($form, $data); | ||
82 | |||
83 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | ||
84 | |||
85 | $crawler = $client->followRedirect(); | ||
86 | |||
87 | $this->assertContains('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]); | ||
88 | } | ||
89 | |||
90 | public function testEditFromADifferentUserSiteCredential() | ||
91 | { | ||
92 | $this->logInAs('admin'); | ||
93 | $client = $this->getClient(); | ||
94 | |||
95 | $credential = $this->createSiteCredential($client); | ||
96 | |||
97 | $this->logInAs('bob'); | ||
98 | |||
99 | $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); | ||
100 | |||
101 | $this->assertEquals(403, $client->getResponse()->getStatusCode()); | ||
102 | } | ||
103 | |||
104 | public function testDeleteSiteCredential() | ||
105 | { | ||
106 | $this->logInAs('admin'); | ||
107 | $client = $this->getClient(); | ||
108 | |||
109 | $credential = $this->createSiteCredential($client); | ||
110 | |||
111 | $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); | ||
112 | |||
113 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
114 | |||
115 | $deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form(); | ||
116 | |||
117 | $client->submit($deleteForm, []); | ||
118 | |||
119 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | ||
120 | |||
121 | $crawler = $client->followRedirect(); | ||
122 | |||
123 | $this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]); | ||
124 | } | ||
125 | |||
126 | private function createSiteCredential(Client $client) | ||
127 | { | ||
128 | $credential = new SiteCredential($this->getLoggedInUser()); | ||
129 | $credential->setHost('google.io'); | ||
130 | $credential->setUsername('sergei'); | ||
131 | $credential->setPassword('microsoft'); | ||
132 | |||
133 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | ||
134 | $em->persist($credential); | ||
135 | $em->flush(); | ||
136 | |||
137 | return $credential; | ||
138 | } | ||
139 | } | ||
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index 8b50bce9..b0c81e7b 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php | |||
@@ -6,10 +6,11 @@ use Monolog\Handler\TestHandler; | |||
6 | use Monolog\Logger; | 6 | use Monolog\Logger; |
7 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; | 7 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; |
8 | use Graby\SiteConfig\SiteConfig as GrabySiteConfig; | 8 | use Graby\SiteConfig\SiteConfig as GrabySiteConfig; |
9 | use PHPUnit_Framework_TestCase; | ||
10 | use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; | 9 | use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; |
10 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
11 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | ||
11 | 12 | ||
12 | class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | 13 | class GrabySiteConfigBuilderTest extends \PHPUnit_Framework_TestCase |
13 | { | 14 | { |
14 | /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */ | 15 | /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */ |
15 | protected $builder; | 16 | protected $builder; |
@@ -17,13 +18,13 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
17 | public function testBuildConfigExists() | 18 | public function testBuildConfigExists() |
18 | { | 19 | { |
19 | /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ | 20 | /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ |
20 | $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') | 21 | $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder') |
21 | ->disableOriginalConstructor() | 22 | ->disableOriginalConstructor() |
22 | ->getMock(); | 23 | ->getMock(); |
23 | 24 | ||
24 | $grabySiteConfig = new GrabySiteConfig(); | 25 | $grabySiteConfig = new GrabySiteConfig(); |
25 | $grabySiteConfig->requires_login = true; | 26 | $grabySiteConfig->requires_login = true; |
26 | $grabySiteConfig->login_uri = 'http://example.com/login'; | 27 | $grabySiteConfig->login_uri = 'http://www.example.com/login'; |
27 | $grabySiteConfig->login_username_field = 'login'; | 28 | $grabySiteConfig->login_username_field = 'login'; |
28 | $grabySiteConfig->login_password_field = 'password'; | 29 | $grabySiteConfig->login_password_field = 'password'; |
29 | $grabySiteConfig->login_extra_fields = ['field=value']; | 30 | $grabySiteConfig->login_extra_fields = ['field=value']; |
@@ -38,19 +39,40 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
38 | $handler = new TestHandler(); | 39 | $handler = new TestHandler(); |
39 | $logger->pushHandler($handler); | 40 | $logger->pushHandler($handler); |
40 | 41 | ||
42 | $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository') | ||
43 | ->disableOriginalConstructor() | ||
44 | ->getMock(); | ||
45 | $siteCrentialRepo->expects($this->once()) | ||
46 | ->method('findOneByHostAndUser') | ||
47 | ->with('example.com', 1) | ||
48 | ->willReturn(['username' => 'foo', 'password' => 'bar']); | ||
49 | |||
50 | $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') | ||
51 | ->disableOriginalConstructor() | ||
52 | ->getMock(); | ||
53 | $user->expects($this->once()) | ||
54 | ->method('getId') | ||
55 | ->willReturn(1); | ||
56 | |||
57 | $token = new UsernamePasswordToken($user, 'pass', 'provider'); | ||
58 | |||
59 | $tokenStorage = new TokenStorage(); | ||
60 | $tokenStorage->setToken($token); | ||
61 | |||
41 | $this->builder = new GrabySiteConfigBuilder( | 62 | $this->builder = new GrabySiteConfigBuilder( |
42 | $grabyConfigBuilderMock, | 63 | $grabyConfigBuilderMock, |
43 | ['example.com' => ['username' => 'foo', 'password' => 'bar']], | 64 | $tokenStorage, |
65 | $siteCrentialRepo, | ||
44 | $logger | 66 | $logger |
45 | ); | 67 | ); |
46 | 68 | ||
47 | $config = $this->builder->buildForHost('example.com'); | 69 | $config = $this->builder->buildForHost('www.example.com'); |
48 | 70 | ||
49 | $this->assertEquals( | 71 | $this->assertEquals( |
50 | new SiteConfig([ | 72 | new SiteConfig([ |
51 | 'host' => 'example.com', | 73 | 'host' => 'example.com', |
52 | 'requiresLogin' => true, | 74 | 'requiresLogin' => true, |
53 | 'loginUri' => 'http://example.com/login', | 75 | 'loginUri' => 'http://www.example.com/login', |
54 | 'usernameField' => 'login', | 76 | 'usernameField' => 'login', |
55 | 'passwordField' => 'password', | 77 | 'passwordField' => 'password', |
56 | 'extraFields' => ['field' => 'value'], | 78 | 'extraFields' => ['field' => 'value'], |
@@ -82,9 +104,30 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase | |||
82 | $handler = new TestHandler(); | 104 | $handler = new TestHandler(); |
83 | $logger->pushHandler($handler); | 105 | $logger->pushHandler($handler); |
84 | 106 | ||
107 | $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository') | ||
108 | ->disableOriginalConstructor() | ||
109 | ->getMock(); | ||
110 | $siteCrentialRepo->expects($this->once()) | ||
111 | ->method('findOneByHostAndUser') | ||
112 | ->with('unknown.com', 1) | ||
113 | ->willReturn(null); | ||
114 | |||
115 | $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') | ||
116 | ->disableOriginalConstructor() | ||
117 | ->getMock(); | ||
118 | $user->expects($this->once()) | ||
119 | ->method('getId') | ||
120 | ->willReturn(1); | ||
121 | |||
122 | $token = new UsernamePasswordToken($user, 'pass', 'provider'); | ||
123 | |||
124 | $tokenStorage = new TokenStorage(); | ||
125 | $tokenStorage->setToken($token); | ||
126 | |||
85 | $this->builder = new GrabySiteConfigBuilder( | 127 | $this->builder = new GrabySiteConfigBuilder( |
86 | $grabyConfigBuilderMock, | 128 | $grabyConfigBuilderMock, |
87 | [], | 129 | $tokenStorage, |
130 | $siteCrentialRepo, | ||
88 | $logger | 131 | $logger |
89 | ); | 132 | ); |
90 | 133 | ||
diff --git a/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php b/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php new file mode 100644 index 00000000..cede8696 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php | |||
@@ -0,0 +1,40 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Psr\Log\NullLogger; | ||
6 | use Monolog\Logger; | ||
7 | use Monolog\Handler\TestHandler; | ||
8 | use Wallabag\CoreBundle\Helper\CryptoProxy; | ||
9 | |||
10 | class CryptoProxyTest extends \PHPUnit_Framework_TestCase | ||
11 | { | ||
12 | public function testCrypto() | ||
13 | { | ||
14 | $logHandler = new TestHandler(); | ||
15 | $logger = new Logger('test', [$logHandler]); | ||
16 | |||
17 | $crypto = new CryptoProxy(sys_get_temp_dir().'/'.uniqid('', true).'.txt', $logger); | ||
18 | $crypted = $crypto->crypt('test'); | ||
19 | $decrypted = $crypto->decrypt($crypted); | ||
20 | |||
21 | $this->assertSame('test', $decrypted); | ||
22 | |||
23 | $records = $logHandler->getRecords(); | ||
24 | $this->assertCount(2, $records); | ||
25 | $this->assertContains('Crypto: crypting value', $records[0]['message']); | ||
26 | $this->assertContains('Crypto: decrypting value', $records[1]['message']); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * @expectedException RuntimeException | ||
31 | * @expectedExceptionMessage Decrypt fail | ||
32 | * | ||
33 | * @return [type] [description] | ||
34 | */ | ||
35 | public function testDecryptBadValue() | ||
36 | { | ||
37 | $crypto = new CryptoProxy(sys_get_temp_dir().'/'.uniqid('', true).'.txt', new NullLogger()); | ||
38 | $crypto->decrypt('badvalue'); | ||
39 | } | ||
40 | } | ||