]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php
Disable controller access if feature disabled
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / SiteCredentialControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Controller;
4
5 use Symfony\Bundle\FrameworkBundle\Client;
6 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
7 use Wallabag\CoreBundle\Entity\SiteCredential;
8
9 class SiteCredentialControllerTest extends WallabagCoreTestCase
10 {
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
25 public function testListSiteCredential()
26 {
27 $this->logInAs('admin');
28 $client = $this->getClient();
29
30 $crawler = $client->request('GET', '/site-credentials/');
31
32 $this->assertSame(200, $client->getResponse()->getStatusCode());
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
47 $this->assertSame(200, $client->getResponse()->getStatusCode());
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
64 $this->assertSame(302, $client->getResponse()->getStatusCode());
65
66 $crawler = $client->followRedirect();
67
68 $this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]);
69 }
70
71 public function testEditSiteCredential()
72 {
73 $this->logInAs('admin');
74 $client = $this->getClient();
75
76 $credential = $this->createSiteCredential($client);
77
78 $crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
79
80 $this->assertSame(200, $client->getResponse()->getStatusCode());
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
97 $this->assertSame(302, $client->getResponse()->getStatusCode());
98
99 $crawler = $client->followRedirect();
100
101 $this->assertContains('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]);
102 }
103
104 public function testEditFromADifferentUserSiteCredential()
105 {
106 $this->logInAs('admin');
107 $client = $this->getClient();
108
109 $credential = $this->createSiteCredential($client);
110
111 $this->logInAs('bob');
112
113 $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
114
115 $this->assertSame(403, $client->getResponse()->getStatusCode());
116 }
117
118 public function testDeleteSiteCredential()
119 {
120 $this->logInAs('admin');
121 $client = $this->getClient();
122
123 $credential = $this->createSiteCredential($client);
124
125 $crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
126
127 $this->assertSame(200, $client->getResponse()->getStatusCode());
128
129 $deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form();
130
131 $client->submit($deleteForm, []);
132
133 $this->assertSame(302, $client->getResponse()->getStatusCode());
134
135 $crawler = $client->followRedirect();
136
137 $this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]);
138 }
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 }
153 }