aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Tests/Controller/SecurityControllerTest.php
blob: 7af9d5c45747e7f932c4c657f0406ad6abb8e566 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php

namespace Wallabag\CoreBundle\Tests\Controller;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Wallabag\CoreBundle\Tests\WallabagCoreTestCase;

class SecurityControllerTest extends WallabagCoreTestCase
{
    public function testRegister()
    {
        $client = $this->getClient();

        $crawler = $client->request('GET', '/register/');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertContains('Register', $client->getResponse()->getContent());
    }

    public function dataForCreateAccountFailed()
    {
        return array(
            array(
                array(
                    'fos_user_registration_form[email]' => '',
                    'fos_user_registration_form[username]' => 'newuser',
                    'fos_user_registration_form[plainPassword][first]' => 'mypassword',
                    'fos_user_registration_form[plainPassword][second]' => 'mypassword',
                ),
                'Please enter an email',
            ),
            array(
                array(
                    'fos_user_registration_form[email]' => 'newuser@wallabag.org',
                    'fos_user_registration_form[username]' => 'admin',
                    'fos_user_registration_form[plainPassword][first]' => 'mypassword',
                    'fos_user_registration_form[plainPassword][second]' => 'mypassword',
                ),
                'The username is already used',
            ),
            array(
                array(
                    'fos_user_registration_form[email]' => 'newuser@wallabag.org',
                    'fos_user_registration_form[username]' => 'newuser',
                    'fos_user_registration_form[plainPassword][first]' => 'mypassword1',
                    'fos_user_registration_form[plainPassword][second]' => 'mypassword2',
                ),
                'The entered passwords don&#039;t match',
            ),
        );
    }

    /**
     * @dataProvider dataForCreateAccountFailed
     */
    public function testCreateAccountFailed($data, $expectedMessage)
    {
        $client = $this->getClient();

        $crawler = $client->request('GET', '/register/');

        $form = $crawler->filter('input[type=submit]')->form();

        $client->submit($form, $data);

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertContains($expectedMessage, $client->getResponse()->getContent());
    }

    public function dataForCreateAccountSuccess()
    {
        return array(
            array(
                array(
                    'fos_user_registration_form[email]' => 'newuser@wallabag.org',
                    'fos_user_registration_form[username]' => 'newuser',
                    'fos_user_registration_form[plainPassword][first]' => 'mypassword',
                    'fos_user_registration_form[plainPassword][second]' => 'mypassword',
                ),
            ),
        );
    }

    /**
     * @dataProvider dataForCreateAccountSuccess
     */
    public function testCreateAccountSuccess($data)
    {
        $client = $this->getClient();

        $crawler = $client->request('GET', '/register/');

        $form = $crawler->filter('input[type=submit]')->form();

        $client->submit($form, $data);
        $this->assertEquals(302, $client->getResponse()->getStatusCode());

        $crawler = $client->followRedirect();

        $this->assertContains('The user has been created successfully', $client->getResponse()->getContent());
    }

    public function testRegistrationConfirmation()
    {
        $client = $this->getClient();
        $client->followRedirects();

        $user = $client->getContainer()
            ->get('doctrine.orm.entity_manager')
            ->getRepository('WallabagCoreBundle:User')
            ->findOneByUsername('newuser');

        $this->assertNull($user->getConfig());

        $client->request('GET', '/register/confirm/b4dT0k3n');
        $this->assertEquals(404, $client->getResponse()->getStatusCode());

        $crawler = $client->request('GET', '/register/confirm/'.$user->getConfirmationToken());
        $this->assertEquals(200, $client->getResponse()->getStatusCode());

        $user = $client->getContainer()
            ->get('doctrine.orm.entity_manager')
            ->getRepository('WallabagCoreBundle:User')
            ->findOneByUsername('newuser');
        $this->assertNotNull($user->getConfig());
    }

    public function testLogin()
    {
        $client = $this->getClient();

        $crawler = $client->request('GET', '/new');

        $this->assertEquals(302, $client->getResponse()->getStatusCode());
        $this->assertContains('login', $client->getResponse()->headers->get('location'));
    }

    public function testLoginFail()
    {
        $client = $this->getClient();

        $crawler = $client->request('GET', '/login');

        $form = $crawler->filter('button[type=submit]')->form();
        $data = array(
            '_username' => 'admin',
            '_password' => 'admin',
        );

        $client->submit($form, $data);

        $this->assertEquals(302, $client->getResponse()->getStatusCode());
        $this->assertContains('login', $client->getResponse()->headers->get('location'));

        $crawler = $client->followRedirect();

        $this->assertContains('Bad credentials', $client->getResponse()->getContent());
    }

    public function testRedirectionAfterLogin()
    {
        $client = $this->getClient();
        $client->followRedirects();

        $crawler = $client->request('GET', '/config');

        $form = $crawler->filter('button[type=submit]')->form();

        $data = array(
            '_username' => 'admin',
            '_password' => 'mypassword',
        );

        $client->submit($form, $data);

        $this->assertContains('RSS', $client->getResponse()->getContent());
    }

    public function testForgotPassword()
    {
        $client = $this->getClient();

        $crawler = $client->request('GET', '/forgot-password');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());

        $this->assertContains('Forgot password', $client->getResponse()->getContent());

        $form = $crawler->filter('button[type=submit]');

        $this->assertCount(1, $form);

        return array(
            'form' => $form->form(),
            'client' => $client,
        );
    }

    /**
     * @depends testForgotPassword
     */
    public function testSubmitForgotPasswordFail($parameters)
    {
        $form = $parameters['form'];
        $client = $parameters['client'];

        $data = array(
            'forgot_password[email]' => 'material',
        );

        $client->submit($form, $data);

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertContains('No user found with this email', $client->getResponse()->getContent());
    }

    /**
     * @depends testForgotPassword
     *
     * Instead of using collector which slow down the test suite
     * http://symfony.com/doc/current/cookbook/email/testing.html
     *
     * Use a different way where Swift store email as file
     */
    public function testSubmitForgotPassword($parameters)
    {
        $form = $parameters['form'];
        $client = $parameters['client'];

        $spoolDir = $client->getKernel()->getContainer()->getParameter('swiftmailer.spool.default.file.path');

        // cleanup pool dir
        $filesystem = new Filesystem();
        $filesystem->remove($spoolDir);

        // to use `getCollector` since `collect: false` in config_test.yml
        $client->enableProfiler();

        $data = array(
            'forgot_password[email]' => 'bobby@wallabag.org',
        );

        $client->submit($form, $data);

        $this->assertEquals(302, $client->getResponse()->getStatusCode());

        $crawler = $client->followRedirect();

        $this->assertContains('An email has been sent to', $client->getResponse()->getContent());

        // find every files (ie: emails) inside the spool dir except hidden files
        $finder = new Finder();
        $finder
            ->in($spoolDir)
            ->ignoreDotFiles(true)
            ->files();

        $this->assertCount(1, $finder, 'Only one email has been sent');

        foreach ($finder as $file) {
            $message = unserialize(file_get_contents($file));

            $this->assertInstanceOf('Swift_Message', $message);
            $this->assertEquals('Reset Password', $message->getSubject());
            $this->assertEquals('no-reply@wallabag.org', key($message->getFrom()));
            $this->assertEquals('bobby@wallabag.org', key($message->getTo()));
            $this->assertContains(
                'To reset your password - please visit',
                $message->getBody()
            );
        }
    }

    public function testReset()
    {
        $client = $this->getClient();
        $user = $client->getContainer()
            ->get('doctrine.orm.entity_manager')
            ->getRepository('WallabagCoreBundle:User')
            ->findOneByEmail('bobby@wallabag.org');

        $crawler = $client->request('GET', '/forgot-password/'.$user->getConfirmationToken());

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertCount(2, $crawler->filter('input[type=password]'));
        $this->assertCount(1, $form = $crawler->filter('button[type=submit]'));
        $this->assertCount(1, $form);

        $data = array(
            'change_passwd[new_password][first]' => 'mypassword',
            'change_passwd[new_password][second]' => 'mypassword',
        );

        $client->submit($form->form(), $data);

        $this->assertEquals(302, $client->getResponse()->getStatusCode());
        $this->assertContains('login', $client->getResponse()->headers->get('location'));
    }

    public function testResetBadToken()
    {
        $client = $this->getClient();

        $client->request('GET', '/forgot-password/UIZOAU29UE902IEPZO');

        $this->assertEquals(404, $client->getResponse()->getStatusCode());
    }

    public function testCheckEmailWithoutEmail()
    {
        $client = $this->getClient();

        $client->request('GET', '/forgot-password/check-email');

        $this->assertEquals(302, $client->getResponse()->getStatusCode());
        $this->assertContains('forgot-password', $client->getResponse()->headers->get('location'));
    }
}