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