]>
Commit | Line | Data |
---|---|---|
3b815d2d J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Tests\Controller; | |
4 | ||
6894d48e J |
5 | use Symfony\Component\Filesystem\Filesystem; |
6 | use Symfony\Component\Finder\Finder; | |
769e19dc | 7 | use Wallabag\CoreBundle\Tests\WallabagCoreTestCase; |
3b815d2d | 8 | |
769e19dc | 9 | class SecurityControllerTest extends WallabagCoreTestCase |
3b815d2d J |
10 | { |
11 | public function testLogin() | |
12 | { | |
13 | $client = $this->getClient(); | |
14 | ||
15 | $crawler = $client->request('GET', '/new'); | |
16 | ||
17 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
18 | $this->assertContains('login', $client->getResponse()->headers->get('location')); | |
19 | } | |
20 | ||
21 | public function testLoginFail() | |
22 | { | |
23 | $client = $this->getClient(); | |
24 | ||
25 | $crawler = $client->request('GET', '/login'); | |
26 | ||
27 | $form = $crawler->filter('button[type=submit]')->form(); | |
28 | $data = array( | |
29 | '_username' => 'admin', | |
30 | '_password' => 'admin', | |
31 | ); | |
32 | ||
33 | $client->submit($form, $data); | |
34 | ||
35 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
36 | $this->assertContains('login', $client->getResponse()->headers->get('location')); | |
37 | ||
38 | $crawler = $client->followRedirect(); | |
39 | ||
40 | $this->assertContains('Bad credentials', $client->getResponse()->getContent()); | |
41 | } | |
6894d48e | 42 | |
7fc14130 NL |
43 | public function testRedirectionAfterLogin() |
44 | { | |
45 | $client = $this->getClient(); | |
46 | $client->followRedirects(); | |
47 | ||
48 | $crawler = $client->request('GET', '/config'); | |
49 | ||
50 | $form = $crawler->filter('button[type=submit]')->form(); | |
51 | ||
52 | $data = array( | |
53 | '_username' => 'admin', | |
54 | '_password' => 'mypassword', | |
55 | ); | |
56 | ||
57 | $client->submit($form, $data); | |
58 | ||
59 | $this->assertContains('RSS', $client->getResponse()->getContent()); | |
60 | } | |
61 | ||
6894d48e J |
62 | public function testForgotPassword() |
63 | { | |
64 | $client = $this->getClient(); | |
65 | ||
66 | $crawler = $client->request('GET', '/forgot-password'); | |
67 | ||
68 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
69 | ||
70 | $this->assertContains('Forgot password', $client->getResponse()->getContent()); | |
71 | ||
72 | $form = $crawler->filter('button[type=submit]'); | |
73 | ||
74 | $this->assertCount(1, $form); | |
75 | ||
76 | return array( | |
77 | 'form' => $form->form(), | |
78 | 'client' => $client, | |
79 | ); | |
80 | } | |
81 | ||
82 | /** | |
83 | * @depends testForgotPassword | |
84 | */ | |
85 | public function testSubmitForgotPasswordFail($parameters) | |
86 | { | |
87 | $form = $parameters['form']; | |
88 | $client = $parameters['client']; | |
89 | ||
90 | $data = array( | |
4ab58dcf | 91 | 'forgot_password[email]' => 'material', |
6894d48e J |
92 | ); |
93 | ||
94 | $client->submit($form, $data); | |
95 | ||
96 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
97 | $this->assertContains('No user found with this email', $client->getResponse()->getContent()); | |
98 | } | |
99 | ||
100 | /** | |
101 | * @depends testForgotPassword | |
102 | * | |
103 | * Instead of using collector which slow down the test suite | |
104 | * http://symfony.com/doc/current/cookbook/email/testing.html | |
105 | * | |
106 | * Use a different way where Swift store email as file | |
107 | */ | |
108 | public function testSubmitForgotPassword($parameters) | |
109 | { | |
110 | $form = $parameters['form']; | |
111 | $client = $parameters['client']; | |
112 | ||
113 | $spoolDir = $client->getKernel()->getContainer()->getParameter('swiftmailer.spool.default.file.path'); | |
114 | ||
115 | // cleanup pool dir | |
116 | $filesystem = new Filesystem(); | |
117 | $filesystem->remove($spoolDir); | |
118 | ||
119 | // to use `getCollector` since `collect: false` in config_test.yml | |
120 | $client->enableProfiler(); | |
121 | ||
122 | $data = array( | |
123 | 'forgot_password[email]' => 'bobby@wallabag.org', | |
124 | ); | |
125 | ||
126 | $client->submit($form, $data); | |
127 | ||
128 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
129 | ||
130 | $crawler = $client->followRedirect(); | |
131 | ||
132 | $this->assertContains('An email has been sent to', $client->getResponse()->getContent()); | |
133 | ||
134 | // find every files (ie: emails) inside the spool dir except hidden files | |
135 | $finder = new Finder(); | |
136 | $finder | |
137 | ->in($spoolDir) | |
138 | ->ignoreDotFiles(true) | |
139 | ->files(); | |
140 | ||
141 | $this->assertCount(1, $finder, 'Only one email has been sent'); | |
142 | ||
143 | foreach ($finder as $file) { | |
144 | $message = unserialize(file_get_contents($file)); | |
145 | ||
146 | $this->assertInstanceOf('Swift_Message', $message); | |
147 | $this->assertEquals('Reset Password', $message->getSubject()); | |
148 | $this->assertEquals('no-reply@wallabag.org', key($message->getFrom())); | |
149 | $this->assertEquals('bobby@wallabag.org', key($message->getTo())); | |
150 | $this->assertContains( | |
151 | 'To reset your password - please visit', | |
152 | $message->getBody() | |
153 | ); | |
154 | } | |
155 | } | |
d0c2243b J |
156 | |
157 | public function testReset() | |
158 | { | |
159 | $client = $this->getClient(); | |
160 | $user = $client->getContainer() | |
161 | ->get('doctrine.orm.entity_manager') | |
162 | ->getRepository('WallabagCoreBundle:User') | |
163 | ->findOneByEmail('bobby@wallabag.org'); | |
164 | ||
165 | $crawler = $client->request('GET', '/forgot-password/'.$user->getConfirmationToken()); | |
166 | ||
167 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
168 | $this->assertCount(2, $crawler->filter('input[type=password]')); | |
169 | $this->assertCount(1, $form = $crawler->filter('button[type=submit]')); | |
170 | $this->assertCount(1, $form); | |
171 | ||
172 | $data = array( | |
173 | 'change_passwd[new_password][first]' => 'mypassword', | |
174 | 'change_passwd[new_password][second]' => 'mypassword', | |
175 | ); | |
176 | ||
177 | $client->submit($form->form(), $data); | |
178 | ||
179 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
180 | $this->assertContains('login', $client->getResponse()->headers->get('location')); | |
181 | } | |
182 | ||
183 | public function testResetBadToken() | |
184 | { | |
185 | $client = $this->getClient(); | |
186 | ||
187 | $client->request('GET', '/forgot-password/UIZOAU29UE902IEPZO'); | |
188 | ||
189 | $this->assertEquals(404, $client->getResponse()->getStatusCode()); | |
190 | } | |
191 | ||
192 | public function testCheckEmailWithoutEmail() | |
193 | { | |
194 | $client = $this->getClient(); | |
195 | ||
196 | $client->request('GET', '/forgot-password/check-email'); | |
197 | ||
198 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
199 | $this->assertContains('forgot-password', $client->getResponse()->headers->get('location')); | |
200 | } | |
3b815d2d | 201 | } |