]>
Commit | Line | Data |
---|---|---|
b8427f22 JB |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Controller; | |
4 | ||
9de9f1e5 | 5 | use Symfony\Bundle\FrameworkBundle\Client; |
b8427f22 | 6 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; |
9de9f1e5 | 7 | use Wallabag\CoreBundle\Entity\SiteCredential; |
b8427f22 JB |
8 | |
9 | class SiteCredentialControllerTest extends WallabagCoreTestCase | |
10 | { | |
ef2b4041 JB |
11 | public function testAccessDeniedBecauseFeatureDisabled() |
12 | { | |
13 | $this->logInAs('admin'); | |
14 | $client = $this->getClient(); | |
15 | ||
16 | $client->getContainer()->get('craue_config')->set('restricted_access', 0); | |
17 | ||
18 | $client->request('GET', '/site-credentials/'); | |
19 | ||
20 | $this->assertSame(404, $client->getResponse()->getStatusCode()); | |
21 | ||
22 | $client->getContainer()->get('craue_config')->set('restricted_access', 1); | |
23 | } | |
24 | ||
b8427f22 JB |
25 | public function testListSiteCredential() |
26 | { | |
27 | $this->logInAs('admin'); | |
28 | $client = $this->getClient(); | |
29 | ||
30 | $crawler = $client->request('GET', '/site-credentials/'); | |
31 | ||
f808b016 | 32 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
b8427f22 JB |
33 | |
34 | $body = $crawler->filter('body')->extract(['_text'])[0]; | |
35 | ||
36 | $this->assertContains('site_credential.description', $body); | |
37 | $this->assertContains('site_credential.list.create_new_one', $body); | |
38 | } | |
39 | ||
40 | public function testNewSiteCredential() | |
41 | { | |
42 | $this->logInAs('admin'); | |
43 | $client = $this->getClient(); | |
44 | ||
45 | $crawler = $client->request('GET', '/site-credentials/new'); | |
46 | ||
f808b016 | 47 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
b8427f22 JB |
48 | |
49 | $body = $crawler->filter('body')->extract(['_text'])[0]; | |
50 | ||
51 | $this->assertContains('site_credential.new_site_credential', $body); | |
52 | $this->assertContains('site_credential.form.back_to_list', $body); | |
53 | ||
54 | $form = $crawler->filter('button[id=site_credential_save]')->form(); | |
55 | ||
56 | $data = [ | |
57 | 'site_credential[host]' => 'google.io', | |
58 | 'site_credential[username]' => 'sergei', | |
59 | 'site_credential[password]' => 'microsoft', | |
60 | ]; | |
61 | ||
62 | $client->submit($form, $data); | |
63 | ||
f808b016 | 64 | $this->assertSame(302, $client->getResponse()->getStatusCode()); |
b8427f22 JB |
65 | |
66 | $crawler = $client->followRedirect(); | |
67 | ||
68 | $this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]); | |
69 | } | |
70 | ||
b8427f22 JB |
71 | public function testEditSiteCredential() |
72 | { | |
73 | $this->logInAs('admin'); | |
74 | $client = $this->getClient(); | |
75 | ||
9de9f1e5 | 76 | $credential = $this->createSiteCredential($client); |
b8427f22 | 77 | |
f808b016 | 78 | $crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit'); |
b8427f22 | 79 | |
f808b016 | 80 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
b8427f22 JB |
81 | |
82 | $body = $crawler->filter('body')->extract(['_text'])[0]; | |
83 | ||
84 | $this->assertContains('site_credential.edit_site_credential', $body); | |
85 | $this->assertContains('site_credential.form.back_to_list', $body); | |
86 | ||
87 | $form = $crawler->filter('button[id=site_credential_save]')->form(); | |
88 | ||
89 | $data = [ | |
90 | 'site_credential[host]' => 'google.io', | |
91 | 'site_credential[username]' => 'larry', | |
92 | 'site_credential[password]' => 'microsoft', | |
93 | ]; | |
94 | ||
95 | $client->submit($form, $data); | |
96 | ||
f808b016 | 97 | $this->assertSame(302, $client->getResponse()->getStatusCode()); |
b8427f22 JB |
98 | |
99 | $crawler = $client->followRedirect(); | |
100 | ||
101 | $this->assertContains('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]); | |
b8427f22 JB |
102 | } |
103 | ||
b8427f22 JB |
104 | public function testEditFromADifferentUserSiteCredential() |
105 | { | |
9de9f1e5 | 106 | $this->logInAs('admin'); |
b8427f22 JB |
107 | $client = $this->getClient(); |
108 | ||
9de9f1e5 JB |
109 | $credential = $this->createSiteCredential($client); |
110 | ||
111 | $this->logInAs('bob'); | |
b8427f22 | 112 | |
f808b016 | 113 | $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit'); |
b8427f22 | 114 | |
f808b016 | 115 | $this->assertSame(403, $client->getResponse()->getStatusCode()); |
b8427f22 JB |
116 | } |
117 | ||
b8427f22 JB |
118 | public function testDeleteSiteCredential() |
119 | { | |
120 | $this->logInAs('admin'); | |
121 | $client = $this->getClient(); | |
122 | ||
9de9f1e5 | 123 | $credential = $this->createSiteCredential($client); |
b8427f22 | 124 | |
f808b016 | 125 | $crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit'); |
b8427f22 | 126 | |
f808b016 | 127 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
b8427f22 JB |
128 | |
129 | $deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form(); | |
130 | ||
131 | $client->submit($deleteForm, []); | |
132 | ||
f808b016 | 133 | $this->assertSame(302, $client->getResponse()->getStatusCode()); |
b8427f22 JB |
134 | |
135 | $crawler = $client->followRedirect(); | |
136 | ||
137 | $this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]); | |
138 | } | |
9de9f1e5 JB |
139 | |
140 | private function createSiteCredential(Client $client) | |
141 | { | |
142 | $credential = new SiteCredential($this->getLoggedInUser()); | |
143 | $credential->setHost('google.io'); | |
144 | $credential->setUsername('sergei'); | |
145 | $credential->setPassword('microsoft'); | |
146 | ||
147 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
148 | $em->persist($credential); | |
149 | $em->flush(); | |
150 | ||
151 | return $credential; | |
152 | } | |
b8427f22 | 153 | } |