aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag')
-rw-r--r--tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php140
-rw-r--r--tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php50
2 files changed, 187 insertions, 3 deletions
diff --git a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php
new file mode 100644
index 00000000..47bf0907
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php
@@ -0,0 +1,140 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Controller;
4
5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6
7class SiteCredentialControllerTest extends WallabagCoreTestCase
8{
9 public function testListSiteCredential()
10 {
11 $this->logInAs('admin');
12 $client = $this->getClient();
13
14 $crawler = $client->request('GET', '/site-credentials/');
15
16 $this->assertEquals(200, $client->getResponse()->getStatusCode());
17
18 $body = $crawler->filter('body')->extract(['_text'])[0];
19
20 $this->assertContains('site_credential.description', $body);
21 $this->assertContains('site_credential.list.create_new_one', $body);
22 }
23
24 public function testNewSiteCredential()
25 {
26 $this->logInAs('admin');
27 $client = $this->getClient();
28
29 $crawler = $client->request('GET', '/site-credentials/new');
30
31 $this->assertEquals(200, $client->getResponse()->getStatusCode());
32
33 $body = $crawler->filter('body')->extract(['_text'])[0];
34
35 $this->assertContains('site_credential.new_site_credential', $body);
36 $this->assertContains('site_credential.form.back_to_list', $body);
37
38 $form = $crawler->filter('button[id=site_credential_save]')->form();
39
40 $data = [
41 'site_credential[host]' => 'google.io',
42 'site_credential[username]' => 'sergei',
43 'site_credential[password]' => 'microsoft',
44 ];
45
46 $client->submit($form, $data);
47
48 $this->assertEquals(302, $client->getResponse()->getStatusCode());
49
50 $crawler = $client->followRedirect();
51
52 $this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]);
53 }
54
55 /**
56 * @depends testNewSiteCredential
57 */
58 public function testEditSiteCredential()
59 {
60 $this->logInAs('admin');
61 $client = $this->getClient();
62
63 $credential = $client->getContainer()
64 ->get('doctrine.orm.entity_manager')
65 ->getRepository('WallabagCoreBundle:SiteCredential')
66 ->findOneByHost('google.io');
67
68 $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
69
70 $this->assertEquals(200, $client->getResponse()->getStatusCode());
71
72 $body = $crawler->filter('body')->extract(['_text'])[0];
73
74 $this->assertContains('site_credential.edit_site_credential', $body);
75 $this->assertContains('site_credential.form.back_to_list', $body);
76
77 $form = $crawler->filter('button[id=site_credential_save]')->form();
78
79 $data = [
80 'site_credential[host]' => 'google.io',
81 'site_credential[username]' => 'larry',
82 'site_credential[password]' => 'microsoft',
83 ];
84
85 $client->submit($form, $data);
86
87 $this->assertEquals(302, $client->getResponse()->getStatusCode());
88
89 $crawler = $client->followRedirect();
90
91 $this->assertContains('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]);
92 $this->assertContains('larry', $crawler->filter('input[id=site_credential_username]')->attr('value'));
93 }
94
95 /**
96 * @depends testNewSiteCredential
97 */
98 public function testEditFromADifferentUserSiteCredential()
99 {
100 $this->logInAs('bob');
101 $client = $this->getClient();
102
103 $credential = $client->getContainer()
104 ->get('doctrine.orm.entity_manager')
105 ->getRepository('WallabagCoreBundle:SiteCredential')
106 ->findOneByHost('google.io');
107
108 $client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
109
110 $this->assertEquals(403, $client->getResponse()->getStatusCode());
111 }
112
113 /**
114 * @depends testNewSiteCredential
115 */
116 public function testDeleteSiteCredential()
117 {
118 $this->logInAs('admin');
119 $client = $this->getClient();
120
121 $credential = $client->getContainer()
122 ->get('doctrine.orm.entity_manager')
123 ->getRepository('WallabagCoreBundle:SiteCredential')
124 ->findOneByHost('google.io');
125
126 $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
127
128 $this->assertEquals(200, $client->getResponse()->getStatusCode());
129
130 $deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form();
131
132 $client->submit($deleteForm, []);
133
134 $this->assertEquals(302, $client->getResponse()->getStatusCode());
135
136 $crawler = $client->followRedirect();
137
138 $this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]);
139 }
140}
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
index 8b50bce9..980f7579 100644
--- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
+++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
@@ -8,6 +8,8 @@ use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
8use Graby\SiteConfig\SiteConfig as GrabySiteConfig; 8use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
9use PHPUnit_Framework_TestCase; 9use PHPUnit_Framework_TestCase;
10use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; 10use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder;
11use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
12use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
11 13
12class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase 14class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
13{ 15{
@@ -17,7 +19,7 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
17 public function testBuildConfigExists() 19 public function testBuildConfigExists()
18 { 20 {
19 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ 21 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
20 $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') 22 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
21 ->disableOriginalConstructor() 23 ->disableOriginalConstructor()
22 ->getMock(); 24 ->getMock();
23 25
@@ -38,9 +40,30 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
38 $handler = new TestHandler(); 40 $handler = new TestHandler();
39 $logger->pushHandler($handler); 41 $logger->pushHandler($handler);
40 42
43 $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
44 ->disableOriginalConstructor()
45 ->getMock();
46 $siteCrentialRepo->expects($this->once())
47 ->method('findOneByHostAndUser')
48 ->with('example.com', 1)
49 ->willReturn(['username' => 'foo', 'password' => 'bar']);
50
51 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
52 ->disableOriginalConstructor()
53 ->getMock();
54 $user->expects($this->once())
55 ->method('getId')
56 ->willReturn(1);
57
58 $token = new UsernamePasswordToken($user, 'pass', 'provider');
59
60 $tokenStorage = new TokenStorage();
61 $tokenStorage->setToken($token);
62
41 $this->builder = new GrabySiteConfigBuilder( 63 $this->builder = new GrabySiteConfigBuilder(
42 $grabyConfigBuilderMock, 64 $grabyConfigBuilderMock,
43 ['example.com' => ['username' => 'foo', 'password' => 'bar']], 65 $tokenStorage,
66 $siteCrentialRepo,
44 $logger 67 $logger
45 ); 68 );
46 69
@@ -82,9 +105,30 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
82 $handler = new TestHandler(); 105 $handler = new TestHandler();
83 $logger->pushHandler($handler); 106 $logger->pushHandler($handler);
84 107
108 $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
109 ->disableOriginalConstructor()
110 ->getMock();
111 $siteCrentialRepo->expects($this->once())
112 ->method('findOneByHostAndUser')
113 ->with('unknown.com', 1)
114 ->willReturn(null);
115
116 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
117 ->disableOriginalConstructor()
118 ->getMock();
119 $user->expects($this->once())
120 ->method('getId')
121 ->willReturn(1);
122
123 $token = new UsernamePasswordToken($user, 'pass', 'provider');
124
125 $tokenStorage = new TokenStorage();
126 $tokenStorage->setToken($token);
127
85 $this->builder = new GrabySiteConfigBuilder( 128 $this->builder = new GrabySiteConfigBuilder(
86 $grabyConfigBuilderMock, 129 $grabyConfigBuilderMock,
87 [], 130 $tokenStorage,
131 $siteCrentialRepo,
88 $logger 132 $logger
89 ); 133 );
90 134