2017-05-01 22:13:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Wallabag\CoreBundle\Controller;
|
|
|
|
|
2022-08-28 02:01:46 +02:00
|
|
|
use Craue\ConfigBundle\Util\Config;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2023-08-08 01:21:56 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
2017-05-01 22:13:35 +02:00
|
|
|
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
|
2017-05-03 10:23:49 +02:00
|
|
|
use Wallabag\CoreBundle\Entity\SiteCredential;
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
class SiteCredentialControllerTest extends WallabagCoreTestCase
|
|
|
|
{
|
2017-11-22 09:59:11 +01:00
|
|
|
public function testAccessDeniedBecauseFeatureDisabled()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
2022-11-23 17:09:32 +01:00
|
|
|
$client = $this->getTestClient();
|
2017-11-22 09:59:11 +01:00
|
|
|
|
2022-08-28 02:01:46 +02:00
|
|
|
$client->getContainer()->get(Config::class)->set('restricted_access', 0);
|
2017-11-22 09:59:11 +01:00
|
|
|
|
|
|
|
$client->request('GET', '/site-credentials/');
|
|
|
|
|
|
|
|
$this->assertSame(404, $client->getResponse()->getStatusCode());
|
|
|
|
|
2022-08-28 02:01:46 +02:00
|
|
|
$client->getContainer()->get(Config::class)->set('restricted_access', 1);
|
2017-11-22 09:59:11 +01:00
|
|
|
}
|
|
|
|
|
2017-05-01 22:13:35 +02:00
|
|
|
public function testListSiteCredential()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
2022-11-23 17:09:32 +01:00
|
|
|
$client = $this->getTestClient();
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$crawler = $client->request('GET', '/site-credentials/');
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$body = $crawler->filter('body')->extract(['_text'])[0];
|
|
|
|
|
2020-06-15 13:37:50 +02:00
|
|
|
$this->assertStringContainsString('site_credential.description', $body);
|
|
|
|
$this->assertStringContainsString('site_credential.list.create_new_one', $body);
|
2017-05-01 22:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewSiteCredential()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
2022-11-23 17:09:32 +01:00
|
|
|
$client = $this->getTestClient();
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$crawler = $client->request('GET', '/site-credentials/new');
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$body = $crawler->filter('body')->extract(['_text'])[0];
|
|
|
|
|
2020-06-15 13:37:50 +02:00
|
|
|
$this->assertStringContainsString('site_credential.new_site_credential', $body);
|
|
|
|
$this->assertStringContainsString('site_credential.form.back_to_list', $body);
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$form = $crawler->filter('button[id=site_credential_save]')->form();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'site_credential[host]' => 'google.io',
|
|
|
|
'site_credential[username]' => 'sergei',
|
|
|
|
'site_credential[password]' => 'microsoft',
|
|
|
|
];
|
|
|
|
|
|
|
|
$client->submit($form, $data);
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(302, $client->getResponse()->getStatusCode());
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$crawler = $client->followRedirect();
|
|
|
|
|
2020-06-15 13:37:50 +02:00
|
|
|
$this->assertStringContainsString('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]);
|
2017-05-01 22:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEditSiteCredential()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
2022-11-23 17:09:32 +01:00
|
|
|
$client = $this->getTestClient();
|
2017-05-01 22:13:35 +02:00
|
|
|
|
2017-05-03 10:23:49 +02:00
|
|
|
$credential = $this->createSiteCredential($client);
|
2017-05-01 22:13:35 +02:00
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
|
2017-05-01 22:13:35 +02:00
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$body = $crawler->filter('body')->extract(['_text'])[0];
|
|
|
|
|
2020-06-15 13:37:50 +02:00
|
|
|
$this->assertStringContainsString('site_credential.edit_site_credential', $body);
|
|
|
|
$this->assertStringContainsString('site_credential.form.back_to_list', $body);
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$form = $crawler->filter('button[id=site_credential_save]')->form();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'site_credential[host]' => 'google.io',
|
|
|
|
'site_credential[username]' => 'larry',
|
|
|
|
'site_credential[password]' => 'microsoft',
|
|
|
|
];
|
|
|
|
|
|
|
|
$client->submit($form, $data);
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(302, $client->getResponse()->getStatusCode());
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$crawler = $client->followRedirect();
|
|
|
|
|
2020-06-15 13:37:50 +02:00
|
|
|
$this->assertStringContainsString('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]);
|
2017-05-01 22:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEditFromADifferentUserSiteCredential()
|
|
|
|
{
|
2017-05-03 10:23:49 +02:00
|
|
|
$this->logInAs('admin');
|
2022-11-23 17:09:32 +01:00
|
|
|
$client = $this->getTestClient();
|
2017-05-01 22:13:35 +02:00
|
|
|
|
2017-05-03 10:23:49 +02:00
|
|
|
$credential = $this->createSiteCredential($client);
|
|
|
|
|
|
|
|
$this->logInAs('bob');
|
2017-05-01 22:13:35 +02:00
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
|
2017-05-01 22:13:35 +02:00
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(403, $client->getResponse()->getStatusCode());
|
2017-05-01 22:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteSiteCredential()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
2022-11-23 17:09:32 +01:00
|
|
|
$client = $this->getTestClient();
|
2017-05-01 22:13:35 +02:00
|
|
|
|
2017-05-03 10:23:49 +02:00
|
|
|
$credential = $this->createSiteCredential($client);
|
2017-05-01 22:13:35 +02:00
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
|
2017-05-01 22:13:35 +02:00
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form();
|
|
|
|
|
|
|
|
$client->submit($deleteForm, []);
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(302, $client->getResponse()->getStatusCode());
|
2017-05-01 22:13:35 +02:00
|
|
|
|
|
|
|
$crawler = $client->followRedirect();
|
|
|
|
|
2020-06-15 13:37:50 +02:00
|
|
|
$this->assertStringContainsString('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]);
|
2017-05-01 22:13:35 +02:00
|
|
|
}
|
2017-05-03 10:23:49 +02:00
|
|
|
|
2023-08-08 01:21:56 +02:00
|
|
|
private function createSiteCredential(KernelBrowser $client)
|
2017-05-03 10:23:49 +02:00
|
|
|
{
|
|
|
|
$credential = new SiteCredential($this->getLoggedInUser());
|
|
|
|
$credential->setHost('google.io');
|
|
|
|
$credential->setUsername('sergei');
|
|
|
|
$credential->setPassword('microsoft');
|
|
|
|
|
2022-08-28 02:01:46 +02:00
|
|
|
$em = $client->getContainer()->get(EntityManagerInterface::class);
|
2017-05-03 10:23:49 +02:00
|
|
|
$em->persist($credential);
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $credential;
|
|
|
|
}
|
2017-05-01 22:13:35 +02:00
|
|
|
}
|