]>
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 | 10 | { |
359b3f43 NL |
11 | public function testRegister() |
12 | { | |
13 | $client = $this->getClient(); | |
14 | ||
15 | $crawler = $client->request('GET', '/register/'); | |
16 | ||
17 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
18 | $this->assertContains('Register', $client->getResponse()->getContent()); | |
19 | } | |
20 | ||
21 | public function dataForCreateAccountFailed() | |
22 | { | |
23 | return array( | |
24 | array( | |
25 | array( | |
26 | 'fos_user_registration_form[email]' => '', | |
27 | 'fos_user_registration_form[username]' => 'newuser', | |
28 | 'fos_user_registration_form[plainPassword][first]' => 'mypassword', | |
29 | 'fos_user_registration_form[plainPassword][second]' => 'mypassword', | |
30 | ), | |
31 | 'Please enter an email', | |
32 | ), | |
33 | array( | |
34 | array( | |
35 | 'fos_user_registration_form[email]' => 'newuser@wallabag.org', | |
36 | 'fos_user_registration_form[username]' => 'admin', | |
37 | 'fos_user_registration_form[plainPassword][first]' => 'mypassword', | |
38 | 'fos_user_registration_form[plainPassword][second]' => 'mypassword', | |
39 | ), | |
40 | 'The username is already used', | |
41 | ), | |
42 | array( | |
43 | array( | |
44 | 'fos_user_registration_form[email]' => 'newuser@wallabag.org', | |
45 | 'fos_user_registration_form[username]' => 'newuser', | |
46 | 'fos_user_registration_form[plainPassword][first]' => 'mypassword1', | |
47 | 'fos_user_registration_form[plainPassword][second]' => 'mypassword2', | |
48 | ), | |
49 | 'The entered passwords don't match', | |
50 | ), | |
51 | ); | |
52 | } | |
53 | ||
54 | /** | |
55 | * @dataProvider dataForCreateAccountFailed | |
56 | */ | |
57 | public function testCreateAccountFailed($data, $expectedMessage) | |
58 | { | |
59 | $client = $this->getClient(); | |
60 | ||
61 | $crawler = $client->request('GET', '/register/'); | |
62 | ||
63 | $form = $crawler->filter('input[type=submit]')->form(); | |
64 | ||
65 | $client->submit($form, $data); | |
66 | ||
67 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
68 | $this->assertContains($expectedMessage, $client->getResponse()->getContent()); | |
69 | } | |
70 | ||
71 | public function dataForCreateAccountSuccess() | |
72 | { | |
73 | return array( | |
74 | array( | |
75 | array( | |
76 | 'fos_user_registration_form[email]' => 'newuser@wallabag.org', | |
77 | 'fos_user_registration_form[username]' => 'newuser', | |
78 | 'fos_user_registration_form[plainPassword][first]' => 'mypassword', | |
79 | 'fos_user_registration_form[plainPassword][second]' => 'mypassword', | |
80 | ), | |
81 | ) | |
82 | ); | |
83 | } | |
84 | ||
85 | /** | |
86 | * @dataProvider dataForCreateAccountSuccess | |
87 | */ | |
88 | public function testCreateAccountSuccess($data) | |
89 | { | |
90 | $client = $this->getClient(); | |
91 | ||
92 | $crawler = $client->request('GET', '/register/'); | |
93 | ||
94 | $form = $crawler->filter('input[type=submit]')->form(); | |
95 | ||
96 | $client->submit($form, $data); | |
97 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
98 | ||
99 | $crawler = $client->followRedirect(); | |
100 | ||
101 | $this->assertContains('The user has been created successfully', $client->getResponse()->getContent()); | |
102 | } | |
103 | ||
3b815d2d J |
104 | public function testLogin() |
105 | { | |
106 | $client = $this->getClient(); | |
107 | ||
108 | $crawler = $client->request('GET', '/new'); | |
109 | ||
110 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
111 | $this->assertContains('login', $client->getResponse()->headers->get('location')); | |
112 | } | |
113 | ||
114 | public function testLoginFail() | |
115 | { | |
116 | $client = $this->getClient(); | |
117 | ||
118 | $crawler = $client->request('GET', '/login'); | |
119 | ||
120 | $form = $crawler->filter('button[type=submit]')->form(); | |
121 | $data = array( | |
122 | '_username' => 'admin', | |
123 | '_password' => 'admin', | |
124 | ); | |
125 | ||
126 | $client->submit($form, $data); | |
127 | ||
128 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
129 | $this->assertContains('login', $client->getResponse()->headers->get('location')); | |
130 | ||
131 | $crawler = $client->followRedirect(); | |
132 | ||
133 | $this->assertContains('Bad credentials', $client->getResponse()->getContent()); | |
134 | } | |
6894d48e | 135 | |
7fc14130 NL |
136 | public function testRedirectionAfterLogin() |
137 | { | |
138 | $client = $this->getClient(); | |
139 | $client->followRedirects(); | |
140 | ||
141 | $crawler = $client->request('GET', '/config'); | |
142 | ||
143 | $form = $crawler->filter('button[type=submit]')->form(); | |
144 | ||
145 | $data = array( | |
146 | '_username' => 'admin', | |
147 | '_password' => 'mypassword', | |
148 | ); | |
149 | ||
150 | $client->submit($form, $data); | |
151 | ||
152 | $this->assertContains('RSS', $client->getResponse()->getContent()); | |
153 | } | |
154 | ||
6894d48e J |
155 | public function testForgotPassword() |
156 | { | |
157 | $client = $this->getClient(); | |
158 | ||
159 | $crawler = $client->request('GET', '/forgot-password'); | |
160 | ||
161 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
162 | ||
163 | $this->assertContains('Forgot password', $client->getResponse()->getContent()); | |
164 | ||
165 | $form = $crawler->filter('button[type=submit]'); | |
166 | ||
167 | $this->assertCount(1, $form); | |
168 | ||
169 | return array( | |
170 | 'form' => $form->form(), | |
171 | 'client' => $client, | |
172 | ); | |
173 | } | |
174 | ||
175 | /** | |
176 | * @depends testForgotPassword | |
177 | */ | |
178 | public function testSubmitForgotPasswordFail($parameters) | |
179 | { | |
180 | $form = $parameters['form']; | |
181 | $client = $parameters['client']; | |
182 | ||
183 | $data = array( | |
4ab58dcf | 184 | 'forgot_password[email]' => 'material', |
6894d48e J |
185 | ); |
186 | ||
187 | $client->submit($form, $data); | |
188 | ||
189 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
190 | $this->assertContains('No user found with this email', $client->getResponse()->getContent()); | |
191 | } | |
192 | ||
193 | /** | |
194 | * @depends testForgotPassword | |
195 | * | |
196 | * Instead of using collector which slow down the test suite | |
197 | * http://symfony.com/doc/current/cookbook/email/testing.html | |
198 | * | |
199 | * Use a different way where Swift store email as file | |
200 | */ | |
201 | public function testSubmitForgotPassword($parameters) | |
202 | { | |
203 | $form = $parameters['form']; | |
204 | $client = $parameters['client']; | |
205 | ||
206 | $spoolDir = $client->getKernel()->getContainer()->getParameter('swiftmailer.spool.default.file.path'); | |
207 | ||
208 | // cleanup pool dir | |
209 | $filesystem = new Filesystem(); | |
210 | $filesystem->remove($spoolDir); | |
211 | ||
212 | // to use `getCollector` since `collect: false` in config_test.yml | |
213 | $client->enableProfiler(); | |
214 | ||
215 | $data = array( | |
216 | 'forgot_password[email]' => 'bobby@wallabag.org', | |
217 | ); | |
218 | ||
219 | $client->submit($form, $data); | |
220 | ||
221 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
222 | ||
223 | $crawler = $client->followRedirect(); | |
224 | ||
225 | $this->assertContains('An email has been sent to', $client->getResponse()->getContent()); | |
226 | ||
227 | // find every files (ie: emails) inside the spool dir except hidden files | |
228 | $finder = new Finder(); | |
229 | $finder | |
230 | ->in($spoolDir) | |
231 | ->ignoreDotFiles(true) | |
232 | ->files(); | |
233 | ||
234 | $this->assertCount(1, $finder, 'Only one email has been sent'); | |
235 | ||
236 | foreach ($finder as $file) { | |
237 | $message = unserialize(file_get_contents($file)); | |
238 | ||
239 | $this->assertInstanceOf('Swift_Message', $message); | |
240 | $this->assertEquals('Reset Password', $message->getSubject()); | |
241 | $this->assertEquals('no-reply@wallabag.org', key($message->getFrom())); | |
242 | $this->assertEquals('bobby@wallabag.org', key($message->getTo())); | |
243 | $this->assertContains( | |
244 | 'To reset your password - please visit', | |
245 | $message->getBody() | |
246 | ); | |
247 | } | |
248 | } | |
d0c2243b J |
249 | |
250 | public function testReset() | |
251 | { | |
252 | $client = $this->getClient(); | |
253 | $user = $client->getContainer() | |
254 | ->get('doctrine.orm.entity_manager') | |
255 | ->getRepository('WallabagCoreBundle:User') | |
256 | ->findOneByEmail('bobby@wallabag.org'); | |
257 | ||
258 | $crawler = $client->request('GET', '/forgot-password/'.$user->getConfirmationToken()); | |
259 | ||
260 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
261 | $this->assertCount(2, $crawler->filter('input[type=password]')); | |
262 | $this->assertCount(1, $form = $crawler->filter('button[type=submit]')); | |
263 | $this->assertCount(1, $form); | |
264 | ||
265 | $data = array( | |
266 | 'change_passwd[new_password][first]' => 'mypassword', | |
267 | 'change_passwd[new_password][second]' => 'mypassword', | |
268 | ); | |
269 | ||
270 | $client->submit($form->form(), $data); | |
271 | ||
272 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
273 | $this->assertContains('login', $client->getResponse()->headers->get('location')); | |
274 | } | |
275 | ||
276 | public function testResetBadToken() | |
277 | { | |
278 | $client = $this->getClient(); | |
279 | ||
280 | $client->request('GET', '/forgot-password/UIZOAU29UE902IEPZO'); | |
281 | ||
282 | $this->assertEquals(404, $client->getResponse()->getStatusCode()); | |
283 | } | |
284 | ||
285 | public function testCheckEmailWithoutEmail() | |
286 | { | |
287 | $client = $this->getClient(); | |
288 | ||
289 | $client->request('GET', '/forgot-password/check-email'); | |
290 | ||
291 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
292 | $this->assertContains('forgot-password', $client->getResponse()->headers->get('location')); | |
293 | } | |
3b815d2d | 294 | } |