forked from wallabag/wallabag
Merge pull request #2683 from wallabag/credentials-in-db
Store credentials in DB
This commit is contained in:
@ -5,6 +5,7 @@ namespace Tests\Wallabag\CoreBundle\Controller;
|
||||
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
|
||||
use Wallabag\CoreBundle\Entity\Config;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
use Wallabag\CoreBundle\Entity\SiteCredential;
|
||||
|
||||
class EntryControllerTest extends WallabagCoreTestCase
|
||||
{
|
||||
@ -1335,4 +1336,56 @@ class EntryControllerTest extends WallabagCoreTestCase
|
||||
$this->assertEquals($url, $content->getUrl());
|
||||
$this->assertEquals($expectedLanguage, $content->getLanguage());
|
||||
}
|
||||
|
||||
/**
|
||||
* This test will require an internet connection.
|
||||
*/
|
||||
public function testRestrictedArticle()
|
||||
{
|
||||
$url = 'http://www.monde-diplomatique.fr/2017/05/BONNET/57475';
|
||||
$this->logInAs('admin');
|
||||
$client = $this->getClient();
|
||||
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
|
||||
|
||||
// enable restricted access
|
||||
$client->getContainer()->get('craue_config')->set('restricted_access', 1);
|
||||
|
||||
// create a new site_credential
|
||||
$user = $client->getContainer()->get('security.token_storage')->getToken()->getUser();
|
||||
$credential = new SiteCredential($user);
|
||||
$credential->setHost('monde-diplomatique.fr');
|
||||
$credential->setUsername($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('foo'));
|
||||
$credential->setPassword($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('bar'));
|
||||
|
||||
$em->persist($credential);
|
||||
$em->flush();
|
||||
|
||||
$crawler = $client->request('GET', '/new');
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
|
||||
$form = $crawler->filter('form[name=entry]')->form();
|
||||
|
||||
$data = [
|
||||
'entry[url]' => $url,
|
||||
];
|
||||
|
||||
$client->submit($form, $data);
|
||||
|
||||
$this->assertEquals(302, $client->getResponse()->getStatusCode());
|
||||
|
||||
$crawler = $client->followRedirect();
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
$this->assertContains('flashes.entry.notice.entry_saved', $crawler->filter('body')->extract(['_text'])[0]);
|
||||
|
||||
$content = $em
|
||||
->getRepository('WallabagCoreBundle:Entry')
|
||||
->findByUrlAndUserId($url, $this->getLoggedInUserId());
|
||||
|
||||
$this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content);
|
||||
$this->assertSame('Crimes et réformes aux Philippines', $content->getTitle());
|
||||
|
||||
$client->getContainer()->get('craue_config')->set('restricted_access', 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Wallabag\CoreBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Client;
|
||||
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
|
||||
use Wallabag\CoreBundle\Entity\SiteCredential;
|
||||
|
||||
class SiteCredentialControllerTest extends WallabagCoreTestCase
|
||||
{
|
||||
public function testListSiteCredential()
|
||||
{
|
||||
$this->logInAs('admin');
|
||||
$client = $this->getClient();
|
||||
|
||||
$crawler = $client->request('GET', '/site-credentials/');
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
|
||||
$body = $crawler->filter('body')->extract(['_text'])[0];
|
||||
|
||||
$this->assertContains('site_credential.description', $body);
|
||||
$this->assertContains('site_credential.list.create_new_one', $body);
|
||||
}
|
||||
|
||||
public function testNewSiteCredential()
|
||||
{
|
||||
$this->logInAs('admin');
|
||||
$client = $this->getClient();
|
||||
|
||||
$crawler = $client->request('GET', '/site-credentials/new');
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
|
||||
$body = $crawler->filter('body')->extract(['_text'])[0];
|
||||
|
||||
$this->assertContains('site_credential.new_site_credential', $body);
|
||||
$this->assertContains('site_credential.form.back_to_list', $body);
|
||||
|
||||
$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);
|
||||
|
||||
$this->assertEquals(302, $client->getResponse()->getStatusCode());
|
||||
|
||||
$crawler = $client->followRedirect();
|
||||
|
||||
$this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]);
|
||||
}
|
||||
|
||||
public function testEditSiteCredential()
|
||||
{
|
||||
$this->logInAs('admin');
|
||||
$client = $this->getClient();
|
||||
|
||||
$credential = $this->createSiteCredential($client);
|
||||
|
||||
$crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
|
||||
$body = $crawler->filter('body')->extract(['_text'])[0];
|
||||
|
||||
$this->assertContains('site_credential.edit_site_credential', $body);
|
||||
$this->assertContains('site_credential.form.back_to_list', $body);
|
||||
|
||||
$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);
|
||||
|
||||
$this->assertEquals(302, $client->getResponse()->getStatusCode());
|
||||
|
||||
$crawler = $client->followRedirect();
|
||||
|
||||
$this->assertContains('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]);
|
||||
}
|
||||
|
||||
public function testEditFromADifferentUserSiteCredential()
|
||||
{
|
||||
$this->logInAs('admin');
|
||||
$client = $this->getClient();
|
||||
|
||||
$credential = $this->createSiteCredential($client);
|
||||
|
||||
$this->logInAs('bob');
|
||||
|
||||
$client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
|
||||
|
||||
$this->assertEquals(403, $client->getResponse()->getStatusCode());
|
||||
}
|
||||
|
||||
public function testDeleteSiteCredential()
|
||||
{
|
||||
$this->logInAs('admin');
|
||||
$client = $this->getClient();
|
||||
|
||||
$credential = $this->createSiteCredential($client);
|
||||
|
||||
$crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
|
||||
$deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form();
|
||||
|
||||
$client->submit($deleteForm, []);
|
||||
|
||||
$this->assertEquals(302, $client->getResponse()->getStatusCode());
|
||||
|
||||
$crawler = $client->followRedirect();
|
||||
|
||||
$this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]);
|
||||
}
|
||||
|
||||
private function createSiteCredential(Client $client)
|
||||
{
|
||||
$credential = new SiteCredential($this->getLoggedInUser());
|
||||
$credential->setHost('google.io');
|
||||
$credential->setUsername('sergei');
|
||||
$credential->setPassword('microsoft');
|
||||
|
||||
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
|
||||
$em->persist($credential);
|
||||
$em->flush();
|
||||
|
||||
return $credential;
|
||||
}
|
||||
}
|
||||
@ -6,10 +6,11 @@ use Monolog\Handler\TestHandler;
|
||||
use Monolog\Logger;
|
||||
use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
|
||||
use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
|
||||
use PHPUnit_Framework_TestCase;
|
||||
use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
||||
|
||||
class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
|
||||
class GrabySiteConfigBuilderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */
|
||||
protected $builder;
|
||||
@ -17,13 +18,13 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
|
||||
public function testBuildConfigExists()
|
||||
{
|
||||
/* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
|
||||
$grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder')
|
||||
$grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$grabySiteConfig = new GrabySiteConfig();
|
||||
$grabySiteConfig->requires_login = true;
|
||||
$grabySiteConfig->login_uri = 'http://example.com/login';
|
||||
$grabySiteConfig->login_uri = 'http://www.example.com/login';
|
||||
$grabySiteConfig->login_username_field = 'login';
|
||||
$grabySiteConfig->login_password_field = 'password';
|
||||
$grabySiteConfig->login_extra_fields = ['field=value'];
|
||||
@ -38,19 +39,40 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
|
||||
$handler = new TestHandler();
|
||||
$logger->pushHandler($handler);
|
||||
|
||||
$siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$siteCrentialRepo->expects($this->once())
|
||||
->method('findOneByHostAndUser')
|
||||
->with('example.com', 1)
|
||||
->willReturn(['username' => 'foo', 'password' => 'bar']);
|
||||
|
||||
$user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$user->expects($this->once())
|
||||
->method('getId')
|
||||
->willReturn(1);
|
||||
|
||||
$token = new UsernamePasswordToken($user, 'pass', 'provider');
|
||||
|
||||
$tokenStorage = new TokenStorage();
|
||||
$tokenStorage->setToken($token);
|
||||
|
||||
$this->builder = new GrabySiteConfigBuilder(
|
||||
$grabyConfigBuilderMock,
|
||||
['example.com' => ['username' => 'foo', 'password' => 'bar']],
|
||||
$tokenStorage,
|
||||
$siteCrentialRepo,
|
||||
$logger
|
||||
);
|
||||
|
||||
$config = $this->builder->buildForHost('example.com');
|
||||
$config = $this->builder->buildForHost('www.example.com');
|
||||
|
||||
$this->assertEquals(
|
||||
new SiteConfig([
|
||||
'host' => 'example.com',
|
||||
'requiresLogin' => true,
|
||||
'loginUri' => 'http://example.com/login',
|
||||
'loginUri' => 'http://www.example.com/login',
|
||||
'usernameField' => 'login',
|
||||
'passwordField' => 'password',
|
||||
'extraFields' => ['field' => 'value'],
|
||||
@ -82,9 +104,30 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
|
||||
$handler = new TestHandler();
|
||||
$logger->pushHandler($handler);
|
||||
|
||||
$siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$siteCrentialRepo->expects($this->once())
|
||||
->method('findOneByHostAndUser')
|
||||
->with('unknown.com', 1)
|
||||
->willReturn(null);
|
||||
|
||||
$user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$user->expects($this->once())
|
||||
->method('getId')
|
||||
->willReturn(1);
|
||||
|
||||
$token = new UsernamePasswordToken($user, 'pass', 'provider');
|
||||
|
||||
$tokenStorage = new TokenStorage();
|
||||
$tokenStorage->setToken($token);
|
||||
|
||||
$this->builder = new GrabySiteConfigBuilder(
|
||||
$grabyConfigBuilderMock,
|
||||
[],
|
||||
$tokenStorage,
|
||||
$siteCrentialRepo,
|
||||
$logger
|
||||
);
|
||||
|
||||
|
||||
40
tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php
Normal file
40
tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Wallabag\CoreBundle\Helper;
|
||||
|
||||
use Psr\Log\NullLogger;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\TestHandler;
|
||||
use Wallabag\CoreBundle\Helper\CryptoProxy;
|
||||
|
||||
class CryptoProxyTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCrypto()
|
||||
{
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$crypto = new CryptoProxy(sys_get_temp_dir().'/'.uniqid('', true).'.txt', $logger);
|
||||
$crypted = $crypto->crypt('test');
|
||||
$decrypted = $crypto->decrypt($crypted);
|
||||
|
||||
$this->assertSame('test', $decrypted);
|
||||
|
||||
$records = $logHandler->getRecords();
|
||||
$this->assertCount(2, $records);
|
||||
$this->assertContains('Crypto: crypting value', $records[0]['message']);
|
||||
$this->assertContains('Crypto: decrypting value', $records[1]['message']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException RuntimeException
|
||||
* @expectedExceptionMessage Decrypt fail
|
||||
*
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function testDecryptBadValue()
|
||||
{
|
||||
$crypto = new CryptoProxy(sys_get_temp_dir().'/'.uniqid('', true).'.txt', new NullLogger());
|
||||
$crypto->decrypt('badvalue');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user