3 namespace Tests\Wallabag\CoreBundle\Controller
;
5 use Symfony\Bundle\FrameworkBundle\Client
;
6 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
7 use Wallabag\CoreBundle\Entity\SiteCredential
;
9 class SiteCredentialControllerTest
extends WallabagCoreTestCase
11 public function testAccessDeniedBecauseFeatureDisabled()
13 $this->logInAs('admin');
14 $client = $this->getClient();
16 $client->getContainer()->get('craue_config')->set('restricted_access', 0);
18 $client->request('GET', '/site-credentials/');
20 $this->assertSame(404, $client->getResponse()->getStatusCode());
22 $client->getContainer()->get('craue_config')->set('restricted_access', 1);
25 public function testListSiteCredential()
27 $this->logInAs('admin');
28 $client = $this->getClient();
30 $crawler = $client->request('GET', '/site-credentials/');
32 $this->assertSame(200, $client->getResponse()->getStatusCode());
34 $body = $crawler->filter('body')->extract(['_text'])[0];
36 $this->assertContains('site_credential.description', $body);
37 $this->assertContains('site_credential.list.create_new_one', $body);
40 public function testNewSiteCredential()
42 $this->logInAs('admin');
43 $client = $this->getClient();
45 $crawler = $client->request('GET', '/site-credentials/new');
47 $this->assertSame(200, $client->getResponse()->getStatusCode());
49 $body = $crawler->filter('body')->extract(['_text'])[0];
51 $this->assertContains('site_credential.new_site_credential', $body);
52 $this->assertContains('site_credential.form.back_to_list', $body);
54 $form = $crawler->filter('button[id=site_credential_save]')->form();
57 'site_credential[host]' => 'google.io',
58 'site_credential[username]' => 'sergei',
59 'site_credential[password]' => 'microsoft',
62 $client->submit($form, $data);
64 $this->assertSame(302, $client->getResponse()->getStatusCode());
66 $crawler = $client->followRedirect();
68 $this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]);
71 public function testEditSiteCredential()
73 $this->logInAs('admin');
74 $client = $this->getClient();
76 $credential = $this->createSiteCredential($client);
78 $crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
80 $this->assertSame(200, $client->getResponse()->getStatusCode());
82 $body = $crawler->filter('body')->extract(['_text'])[0];
84 $this->assertContains('site_credential.edit_site_credential', $body);
85 $this->assertContains('site_credential.form.back_to_list', $body);
87 $form = $crawler->filter('button[id=site_credential_save]')->form();
90 'site_credential[host]' => 'google.io',
91 'site_credential[username]' => 'larry',
92 'site_credential[password]' => 'microsoft',
95 $client->submit($form, $data);
97 $this->assertSame(302, $client->getResponse()->getStatusCode());
99 $crawler = $client->followRedirect();
101 $this->assertContains('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]);
104 public function testEditFromADifferentUserSiteCredential()
106 $this->logInAs('admin');
107 $client = $this->getClient();
109 $credential = $this->createSiteCredential($client);
111 $this->logInAs('bob');
113 $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
115 $this->assertSame(403, $client->getResponse()->getStatusCode());
118 public function testDeleteSiteCredential()
120 $this->logInAs('admin');
121 $client = $this->getClient();
123 $credential = $this->createSiteCredential($client);
125 $crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
127 $this->assertSame(200, $client->getResponse()->getStatusCode());
129 $deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form();
131 $client->submit($deleteForm, []);
133 $this->assertSame(302, $client->getResponse()->getStatusCode());
135 $crawler = $client->followRedirect();
137 $this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]);
140 private function createSiteCredential(Client
$client)
142 $credential = new SiteCredential($this->getLoggedInUser());
143 $credential->setHost('google.io');
144 $credential->setUsername('sergei');
145 $credential->setPassword('microsoft');
147 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
148 $em->persist($credential);