From 76e00d6f0e32facfff5b5159bfde4b41fd6f0b45 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Wed, 1 Aug 2012 12:17:30 +0200 Subject: [PATCH 1/5] Remove constructor as defining the constructor function breaks the functionality of data providers --- tests/PhraseanetPHPUnitAbstract.class.inc | 30 ++++++++--------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/tests/PhraseanetPHPUnitAbstract.class.inc b/tests/PhraseanetPHPUnitAbstract.class.inc index ecd625d10a..f56312bd62 100644 --- a/tests/PhraseanetPHPUnitAbstract.class.inc +++ b/tests/PhraseanetPHPUnitAbstract.class.inc @@ -83,6 +83,16 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase self::$time_start = microtime(true); } + //check if app is set up + if ( ! setup::is_installed()) { + exit("\033[0;31mPhraseanet is not set up\033[0;37m\r\n"); + } + + //init core + if (null === self::$core) { + self::$core = \bootstrap::getCore(); + } + self::updateTablesSchema(); self::createSetOfUserTests(); @@ -102,26 +112,6 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase parent::tearDownAfterClass(); } - /** - * Delete temporay sqlite database - * Create schema using $this->classesMetatdas - * Load DoctrineTestServices - * - * @return - */ - public function __construct() - { - //check if app is set up - if ( ! setup::is_installed()) { - exit("\033[0;31mPhraseanet is not set up\033[0;37m\r\n"); - } - - //init core - if (null === self::$core) { - self::$core = \bootstrap::getCore(); - } - } - /** * Delete all ressources created during the test */ From 9419c7e589dc9fc4ada1f09c84b66bc0a70a85c6 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Wed, 1 Aug 2012 12:18:03 +0200 Subject: [PATCH 2/5] add /account/* tests --- .../Phrasea/Controller/Root/Account.php | 43 +- templates/web/account/access.html.twig | 17 +- templates/web/account/reset-email.html.twig | 2 +- .../web/account/reset-password.html.twig | 2 +- .../Phrasea/Controller/Root/AccountTest.php | 434 ++++++++++++++++-- 5 files changed, 422 insertions(+), 76 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Root/Account.php b/lib/Alchemy/Phrasea/Controller/Root/Account.php index ded8cf54b8..87b641db7e 100644 --- a/lib/Alchemy/Phrasea/Controller/Root/Account.php +++ b/lib/Alchemy/Phrasea/Controller/Root/Account.php @@ -154,21 +154,6 @@ class Account implements ControllerProviderInterface */ $controllers->get('/access/', $this->call('accountAccess'))->bind('account_access'); -// /** -// * Give account open sessions -// * -// * name : register_account -// * -// * description : Display form to create a new account -// * -// * method : GET -// * -// * parameters : none -// * -// * return : HTML Response -// */ -// $controllers->get('/register/', $this->call('registerAccount'))->bind('register_account'); - /** * Give authorized applications that can access user informations * @@ -182,7 +167,7 @@ class Account implements ControllerProviderInterface * * return : HTML Response */ - $controllers->get('/reset-email/', $this->call('resetEmail'))->bind('reset_email'); + $controllers->post('/reset-email/', $this->call('resetEmail'))->bind('reset_email'); /** * Grant access to an authorized app @@ -251,11 +236,13 @@ class Account implements ControllerProviderInterface return $controllers; } - public function registerAccount(Application $app, Request $request) - { - return new Response($app['twig']->render('account/register.html.twig')); - } - + /** + * Reset Password + * + * @param \Silex\Application $app + * @param \Symfony\Component\HttpFoundation\Request $request + * @return \Symfony\Component\HttpFoundation\Response + */ public function resetPassword(Application $app, Request $request) { if (null !== $passwordMsg = $request->get('pass-error')) { @@ -278,7 +265,7 @@ class Account implements ControllerProviderInterface } /** - * Reset email + * Reset Email * * @param \Silex\Application $app * @param \Symfony\Component\HttpFoundation\Request $request @@ -443,12 +430,12 @@ class Account implements ControllerProviderInterface , new \API_OAuth2_Application($appbox, $application_id) , $app['phraseanet.core']->getAuthenticatedUser() ); + + $account->set_revoked((bool) $request->get('revoke'), false); } catch (\Exception_NotFound $e) { $error = true; } - $account->set_revoked((bool) $request->get('revoke'), false); - return $app->json(array('success' => ! $error)); } @@ -555,7 +542,7 @@ class Account implements ControllerProviderInterface $demands = (array) $request->get('demand', array()); - if (0 === count($demands)) { + if (0 !== count($demands)) { $register = new \appbox_register($appbox); foreach ($demands as $baseId) { @@ -563,7 +550,7 @@ class Account implements ControllerProviderInterface $register->add_request($user, \collection::get_from_base_id($baseId)); $notice = 'demand-ok'; } catch (\Exception $e) { - + exit($e->getMessage()); } } } @@ -640,10 +627,10 @@ class Account implements ControllerProviderInterface foreach ($evtMngr->list_notifications_available($user->get_id()) as $notifications) { foreach ($notifications as $notification) { - $notifId = (int) $notification['id']; + $notifId = $notification['id']; $notifName = sprintf('notification_%d', $notifId); - if (isset($requestedNotifications[$notifId])) { + if (in_array($notifId, $requestedNotifications)) { $user->setPrefs($notifName, '1'); } else { $user->setPrefs($notifName, '0'); diff --git a/templates/web/account/access.html.twig b/templates/web/account/access.html.twig index d1471f0f4d..afa68d7610 100644 --- a/templates/web/account/access.html.twig +++ b/templates/web/account/access.html.twig @@ -18,7 +18,7 @@ {% if baseInsc['CollsRegistered'] is not none %} {% for base in baseInsc['CollsRegistered']%} {% for collId, isTrue in base %} - {{ base_id == sbasId |base_from_coll(collId) }} + {% set base_id = sbasId |base_from_coll(collId) %} {% trans 'login::register: acces authorise sur la collection ' %}{{ sbasId |sbas_names }} @@ -37,7 +37,7 @@ {% if baseInsc['CollsRefuse'] %} {% for collId, isTrue in baseInsc['CollsRefuse'] %} - {{ base_id == sbasId |base_from_coll(collId) }} + {% set base_id = sbasId |base_from_coll(collId) %} {% trans 'login::register: acces refuse sur la collection ' %}{{ sbasId |sbas_names }} @@ -55,7 +55,7 @@ {% if baseInsc['CollsWait'] %} {% for collId, isTrue in baseInsc['CollsWait'] %} - {{ base_id == sbasId |base_from_coll(collId) }} + {% set base_id = sbasId |base_from_coll(collId) %} {% trans 'login::register: en attente d\'acces sur' %} {{ sbasId |sbas_names }} @@ -70,7 +70,7 @@ {% if baseInsc['CollsIntime'] %} {% for collId, isTrue in baseInsc['CollsIntime'] %} - {{ base_id == sbasId |base_from_coll(collId) }} + {% set base_id = sbasId |base_from_coll(collId) %} {% trans 'login::register: acces temporaire sur' %} {{ sbasId |sbas_names }} @@ -85,7 +85,7 @@ {% if baseInsc['CollsOuttime'] %} {% for collId, isTrue in baseInsc['CollsOuttime'] %} - {{ base_id == sbasId |base_from_coll(collId) }} + {% set base_id = sbasId |base_from_coll(collId) %} {% trans 'login::register: acces temporaire termine sur ' %}{{ sbasId |sbas_names }} @@ -100,7 +100,7 @@ {% if baseInsc['CollsNonactif'] %} {% for collId, isTrue in baseInsc['CollsNonactif'] %} - {{ base_id == (sbasId |base_from_coll(collId)) }} + {% set base_id = sbasId |base_from_coll(collId) %} {% trans 'login::register: acces supendu sur' %} {{ sbasId |sbas_names }} @@ -114,7 +114,6 @@ {% endif %} {% if (baseInsc['CollsCGU'] or baseInsc['Colls']) and baseInsc['inscript'] %} - {{ noDemand == false }} {% if baseInsc['Colls'] %} {% if baseInsc['CGU'] %} @@ -125,7 +124,7 @@ {% endif %} {% for collId, collName in baseInsc['Colls'] %} - {{ base_id == sbasId |base_from_coll(collId) }} + {% set base_id = sbasId |base_from_coll(collId) %} {{ collName }} @@ -138,7 +137,7 @@ {% endif %} {% if baseInsc['CollsCGU'] %} {% for collId, collDesc in baseInsc['CollsCGU'] %} - {{ base_id == sbasId |base_from_coll(collId) }} + {% set base_id = sbasId |base_from_coll(collId) %}
diff --git a/templates/web/account/reset-email.html.twig b/templates/web/account/reset-email.html.twig index 3e6f348d4a..6c2a17cfd0 100644 --- a/templates/web/account/reset-email.html.twig +++ b/templates/web/account/reset-email.html.twig @@ -48,7 +48,7 @@ $(document).ready(function() { {% block content %} {% if updateMsg is not none %} -
{{ updateMsg }}
+
{{ updateMsg }}
{% trans 'admin::compte-utilisateur retour a mon compte'%} {% else %} diff --git a/templates/web/account/reset-password.html.twig b/templates/web/account/reset-password.html.twig index 036fec84ed..1b7b1b9152 100644 --- a/templates/web/account/reset-password.html.twig +++ b/templates/web/account/reset-password.html.twig @@ -56,7 +56,7 @@ {% block content %}
{% if passwordMsg is not none %} -

{{ passwordMsg }}

+

{{ passwordMsg }}

{% endif %}
diff --git a/tests/Alchemy/Phrasea/Controller/Root/AccountTest.php b/tests/Alchemy/Phrasea/Controller/Root/AccountTest.php index 737705c9c4..a3b895ef20 100644 --- a/tests/Alchemy/Phrasea/Controller/Root/AccountTest.php +++ b/tests/Alchemy/Phrasea/Controller/Root/AccountTest.php @@ -4,6 +4,27 @@ require_once __DIR__ . '/../../../../PhraseanetWebTestCaseAuthenticatedAbstract. class AccountTest extends \PhraseanetWebTestCaseAuthenticatedAbstract { + protected static $authorizedApp; + + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + try { + self::$authorizedApp = \API_OAuth2_Application::create(\appbox::get_instance(\bootstrap::getCore()), self::$user, 'test API v1'); + } catch (\Exception $e) { + + } + } + + public static function tearDownAfterClass() + { + parent::tearDownAfterClass(); + + if (self::$authorizedApp) { + self::$authorizedApp->delete(); + } + } public function setUp() { @@ -22,19 +43,53 @@ class AccountTest extends \PhraseanetWebTestCaseAuthenticatedAbstract } /** - * @covers \Alchemy\Phrasea\Controller\Root/Account::displayAccount + * @covers \Alchemy\Phrasea\Controller\Root\Account::displayAccount + * @covers \Alchemy\Phrasea\Controller\Root\Account::call */ public function testGetAccount() { - $this->client->request('GET', '/account/'); + $crawler = $this->client->request('GET', '/account/'); $response = $this->client->getResponse(); $this->assertTrue($response->isOk()); + + $actionForm = $crawler->filter('form[name=account]')->attr('action'); + $methodForm = $crawler->filter('form[name=account]')->attr('method'); + + $this->assertEquals('/account/', $actionForm); + $this->assertEquals('post', $methodForm); } /** - * @covers \Alchemy\Phrasea\Controller\Root/Account::accountAccess + * @dataProvider msgProvider + */ + public function testGetAccountNotice($msg) + { + $crawler = $this->client->request('GET', '/account/', array( + 'notice' => $msg + )); + + $response = $this->client->getResponse(); + + $this->assertTrue($response->isOk()); + + $this->assertEquals(1, $crawler->filter('.notice')->count()); + } + + public function msgProvider() + { + return array( + array('pass-ok'), + array('pass-ko'), + array('account-update-ok'), + array('account-update-bad'), + array('demand-ok'), + ); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Account::accountAccess */ public function testGetAccountAccess() { @@ -46,19 +101,165 @@ class AccountTest extends \PhraseanetWebTestCaseAuthenticatedAbstract } /** - * @covers \Alchemy\Phrasea\Controller\Root/Account::resetEmail + * @covers \Alchemy\Phrasea\Controller\Root\Account::resetEmail */ - public function testGetResetMail() + public function testPostResetMailWithToken() { - $this->client->request('GET', '/account/reset-email/'); - + $token = \random::getUrlToken(\random::TYPE_EMAIL, self::$user->get_id(), null, 'new_email@email.com'); + $this->client->request('POST', '/account/reset-email/', array('token' => $token)); $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/account/reset-email/?update=ok', $response->headers->get('location')); - $this->assertTrue($response->isOk()); + $this->assertEquals('new_email@email.com', self::$user->get_email()); + + try { + \random::helloToken($token); + $this->fail('TOken has not been removed'); + } catch (\Exception_NotFound $e) { + + } } /** - * @covers \Alchemy\Phrasea\Controller\Root/Account::accountSessionsAccess + * @covers \Alchemy\Phrasea\Controller\Root\Account::resetEmail + */ + public function testPostResetMailWithBadToken() + { + $this->client->request('POST', '/account/reset-email/', array('token' => '134dT0k3n')); + $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/account/reset-email/?update=ko', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Account::resetEmail + * @expectedException Symfony\Component\HttpKernel\Exception\HttpException + */ + public function testPostResetMailBadRequest() + { + $this->client->request('POST', '/account/reset-email/'); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Account::resetEmail + */ + public function testPostResetMailBadPassword() + { + $this->client->request('POST', '/account/reset-email/', array( + 'form_password' => 'changeme', + 'form_email' => 'new@email.com', + 'form_email_confirm' => 'new@email.com', + )); + + $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/account/reset-email/?notice=bad-password', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Account::resetEmail + */ + public function testPostResetMailBadEmail() + { + $password = \random::generatePassword(); + self::$user->set_password($password); + $this->client->request('POST', '/account/reset-email/', array( + 'form_password' => $password, + 'form_email' => "invalid#!&&@@email.x", + 'form_email_confirm' => 'invalid#!&&@@email.x', + )); + + $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/account/reset-email/?notice=mail-invalid', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Account::resetEmail + */ + public function testPostResetMailEmailNotIdentical() + { + $password = \random::generatePassword(); + self::$user->set_password($password); + $this->client->request('POST', '/account/reset-email/', array( + 'form_password' => $password, + 'form_email' => 'email1@email.com', + 'form_email_confirm' => 'email2@email.com', + )); + + $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/account/reset-email/?notice=mail-match', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Account::resetEmail + */ + public function testPostResetMailEmail() + { + $password = \random::generatePassword(); + self::$user->set_password($password); + $this->client->request('POST', '/account/reset-email/', array( + 'form_password' => $password, + 'form_email' => 'email1@email.com', + 'form_email_confirm' => 'email1@email.com', + )); + + $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/account/reset-email/?update=mail-send', $response->headers->get('location')); + } + + /** + * @dataProvider noticeProvider + */ + public function testGetResetMailNotice($notice) + { + $crawler = $this->client->request('GET', '/account/reset-email/', array( + 'notice' => $notice + )); + + $this->assertTrue($this->client->getResponse()->isOk()); + + $this->assertEquals(2, $crawler->filter('.notice')->count()); + } + + public function noticeProvider() + { + return array( + array('mail-server'), + array('mail-match'), + array('mail-invalid'), + array('bad-password'), + ); + } + + /** + * @dataProvider updateMsgProvider + */ + public function testGetResetMailUpdate($updateMessage) + { + $crawler = $this->client->request('GET', '/account/reset-email/', array( + 'update' => $updateMessage + )); + + $this->assertTrue($this->client->getResponse()->isOk()); + + $this->assertEquals(1, $crawler->filter('.update-msg')->count()); + } + + public function updateMsgProvider() + { + return array( + array('ok'), + array('ko'), + array('mail-send'), + ); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Account::accountSessionsAccess */ public function testGetAccountSecuritySessions() { @@ -70,7 +271,7 @@ class AccountTest extends \PhraseanetWebTestCaseAuthenticatedAbstract } /** - * @covers \Alchemy\Phrasea\Controller\Root/Account::accountAuthorizedApps + * @covers \Alchemy\Phrasea\Controller\Root\Account::accountAuthorizedApps */ public function testGetAccountSecurityApplications() { @@ -82,7 +283,7 @@ class AccountTest extends \PhraseanetWebTestCaseAuthenticatedAbstract } /** - * @covers \Alchemy\Phrasea\Controller\Root/Account::resetPassword + * @covers \Alchemy\Phrasea\Controller\Root\Account::resetPassword */ public function testGetResetPassword() { @@ -94,50 +295,209 @@ class AccountTest extends \PhraseanetWebTestCaseAuthenticatedAbstract } /** - * @covers \Alchemy\Phrasea\Controller\Root/Account::renewPassword + * @dataProvider passwordMsgProvider + */ + public function testGetResetPasswordPassError($msg) + { + $crawler = $this->client->request('GET', '/account/reset-password/', array( + 'pass-error' => $msg + )); + + $response = $this->client->getResponse(); + + $this->assertTrue($response->isOk()); + + $this->assertEquals(1, $crawler->filter('.password-msg')->count()); + } + + public function passwordMsgProvider() + { + return array( + array('pass-match'), + array('pass-short'), + array('pass-invalid'), + ); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Account::updateAccount */ public function testUpdateAccount() { - $core = \bootstrap::getCore(); - $appbox = \appbox::get_instance($core); + $evtMngr = \eventsmanager_broker::getInstance($this->app['phraseanet.appbox'], $this->app['phraseanet.core']); + $register = new \appbox_register($this->app['phraseanet.appbox']); + $bases = $notifs = array(); - $bases = array(); - foreach ($appbox->get_databoxes() as $databox) { + foreach ($this->app['phraseanet.appbox']->get_databoxes() as $databox) { foreach ($databox->get_collections() as $collection) { $bases[] = $collection->get_base_id(); } } - if(0 === count($bases)) { + if (0 === count($bases)) { $this->markTestSkipped('No collections'); } + foreach ($evtMngr->list_notifications_available($this->app['phraseanet.core']->getAUthenticatedUser()->get_id()) as $notifications) { + foreach ($notifications as $notification) { + $notifs[] = $notification['id']; + } + } + + array_shift($notifs); + $this->client->request('POST', '/account/', array( - 'demand' => $bases, - 'form_gender' => 'M', - 'form_firstname' => 'gros', - 'form_lastname' => 'minet', - 'form_address' => 'rue du lac', - 'form_zip' => '75005', - 'form_phone' => '+33645787878', - 'form_fax' => '+33145787845', - 'form_function' => 'astronaute', - 'form_company' => 'NASA', - 'form_activity' => 'Space', - 'form_geonameid' => '', - 'form_addrFTP' => '', - 'form_loginFTP' => '', - 'form_pwdFTP' => '', - 'form_destFTP' => '', - 'form_prefixFTPfolder' => '', - 'form_defaultdataFTP' => array('document', 'preview', 'caption'), - 'mail_notifications' => '1' + 'demand' => $bases, + 'form_gender' => 'M', + 'form_firstname' => 'gros', + 'form_lastname' => 'minet', + 'form_address' => 'rue du lac', + 'form_zip' => '75005', + 'form_phone' => '+33645787878', + 'form_fax' => '+33145787845', + 'form_function' => 'astronaute', + 'form_company' => 'NASA', + 'form_activity' => 'Space', + 'form_geonameid' => '', + 'form_addrFTP' => '', + 'form_loginFTP' => '', + 'form_pwdFTP' => '', + 'form_destFTP' => '', + 'form_prefixFTPfolder' => '', + 'notifications' => $notifs, + 'form_defaultdataFTP' => array('document', 'preview', 'caption'), + 'mail_notifications' => '1' )); $response = $this->client->getResponse(); $this->assertTrue($response->isRedirect()); - $this->assertEquals('minet', $core->getAUthenticatedUser()->get_lastname()); + $this->assertEquals('minet', $this->app['phraseanet.core']->getAUthenticatedUser()->get_lastname()); + + $ret = $register->get_collection_awaiting_for_user(self::$user); + + $this->assertEquals(count($ret), count($bases)); } + /** + * @expectedException Symfony\Component\HttpKernel\Exception\HttpException + */ + public function testAUthorizedAppGrantAccessBadRequest() + { + $this->client->request('GET', '/account/security/application/3/grant/'); + } + public function testAUthorizedAppGrantAccessNotSuccessfull() + { + $this->client->request('GET', '/account/security/application/3/grant/', array(), array(), array('HTTP_ACCEPT' => 'application/json', 'HTTP_X-Requested-With' => 'XMLHttpRequest')); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isOk()); + $json = json_decode($response->getContent()); + $this->assertInstanceOf('StdClass', $json); + $this->assertObjectHasAttribute('success', $json); + $this->assertFalse($json->success); + } + + /** + * @dataProvider revokeProvider + */ + public function testAUthorizedAppGrantAccessSuccessfull($revoke, $expected) + { + if (null === self::$authorizedApp) { + $this->markTestSkipped('Application could not be created'); + } + + $this->client->request('GET', '/account/security/application/' . self::$authorizedApp->get_id() . '/grant/', array( + 'revoke' => $revoke + ), array(), array( + 'HTTP_ACCEPT' => 'application/json', + 'HTTP_X-Requested-With' => 'XMLHttpRequest' + )); + + $response = $this->client->getResponse(); + + $this->assertTrue($response->isOk()); + $json = json_decode($response->getContent()); + $this->assertInstanceOf('StdClass', $json); + $this->assertObjectHasAttribute('success', $json); + $this->assertTrue($json->success); + + $account = \API_OAuth2_Account::load_with_user( + $this->app['phraseanet.appbox'] + , self::$authorizedApp + , self::$user + ); + + $this->assertEquals($expected, $account->is_revoked()); + } + + public function revokeProvider() + { + return array( + array('1', true), + array('0', false), + array(null, false), + array('titi', true), + ); + } + + /** + * @dataProvider passwordProvider + */ + public function testPostRenewPasswordBadArguments($oldPassword, $password, $passwordConfirm, $redirect) + { + self::$user->set_password($oldPassword); + + $this->client->request('POST', '/account/forgot-password/', array( + 'form_password' => $password, + 'form_password_confirm' => $passwordConfirm, + 'form_old_password' => $oldPassword + )); + + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals($redirect, $response->headers->get('location')); + } + + public function testPostRenewPasswordBadOldPassword() + { + $this->client->request('POST', '/account/forgot-password/', array( + 'form_password' => 'password', + 'form_password_confirm' => 'password', + 'form_old_password' => 'oulala' + )); + + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/account/?notice=pass-ko', $response->headers->get('location')); + } + + public function testPostRenewPassword() + { + $password = \random::generatePassword(); + + self::$user->set_password($password); + + $this->client->request('POST', '/account/forgot-password/', array( + 'form_password' => 'password', + 'form_password_confirm' => 'password', + 'form_old_password' => $password + )); + + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/account/?notice=pass-ok', $response->headers->get('location')); + } + + public function passwordProvider() + { + return array( + array(\random::generatePassword(), 'password', 'not_identical_password', '/account/reset-password/?pass-error=pass-match'), + array(\random::generatePassword(), 'min', 'min', '/account/reset-password/?pass-error=pass-short'), + array(\random::generatePassword(), 'invalid password \n', 'invalid password \n', '/account/reset-password/?pass-error=pass-invalid'), + ); + } } From a44df4389aeba4356fe5425a215bf202524904c5 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Wed, 1 Aug 2012 12:19:44 +0200 Subject: [PATCH 3/5] add /login/* tests delete unused template add random email --- lib/Alchemy/Phrasea/Controller/Root/Login.php | 51 +- templates/web/login/forgot-password.html.twig | 2 +- templates/web/user/forgotpwd.html.twig | 176 ----- .../Phrasea/Controller/Root/LoginTest.php | 606 +++++++++++++++++- 4 files changed, 617 insertions(+), 218 deletions(-) delete mode 100644 templates/web/user/forgotpwd.html.twig diff --git a/lib/Alchemy/Phrasea/Controller/Root/Login.php b/lib/Alchemy/Phrasea/Controller/Root/Login.php index 58193893fc..0311494f2e 100644 --- a/lib/Alchemy/Phrasea/Controller/Root/Login.php +++ b/lib/Alchemy/Phrasea/Controller/Root/Login.php @@ -29,8 +29,29 @@ class Login implements ControllerProviderInterface { $controllers = $app['controllers_factory']; + /** + * Login + * + * name : homepage + * + * description : Login from phraseanet + * + * method : GET + * + * parameters : none + * + * return : HTML Response + */ $controllers->get('/', $this->call('login')) ->before(function() use ($app) { +// +// if ( ! $app['phraseanet.appbox']->get_session()->isset_postlog() +// && $app['phraseanet.core']->isAuthenticated() +// && $app['request']->get('error') != 'no-connection') { +// +// return $app->redirect($app['request']->get('redirect', '/prod/')); +// } + return $app['phraseanet.core']['Firewall']->requireNotAuthenticated($app); }) ->bind('homepage'); @@ -208,7 +229,6 @@ class Login implements ControllerProviderInterface return $app->redirect('/login/?redirect=prod¬ice=already'); } - $user->set_mail_locked(false); \random::removeToken($code); if (\PHPMailer::ValidateAddress($user->get_email())) { @@ -250,7 +270,7 @@ class Login implements ControllerProviderInterface { $appbox = $app['phraseanet.appbox']; - if (null !== $mail = trim($request->get('mail'))) { + if (null !== $mail = $request->get('mail')) { if ( ! \PHPMailer::ValidateAddress($mail)) { return $app->redirect('/login/forgot-password/?error=invalidmail'); } @@ -272,8 +292,6 @@ class Login implements ControllerProviderInterface return $app->redirect('/login/forgot-password/?error=mailserver'); } } - - return $app->redirect('/login/forgot-password/?error=noaccount'); } if ((null !== $token = $request->get('token')) @@ -301,7 +319,7 @@ class Login implements ControllerProviderInterface return $app->redirect('/login/?notice=password-update-ok'); } catch (\Exception_NotFound $e) { - + return $app->redirect('/login/forgot-password/?error=token'); } } } @@ -356,15 +374,15 @@ class Login implements ControllerProviderInterface } if (null !== $passwordMsg = $request->get('pass-error')) { - switch ($sentMsg) { + switch ($passwordMsg) { case 'pass-match': - $sentMsg = _('forms::les mots de passe ne correspondent pas'); + $passwordMsg = _('forms::les mots de passe ne correspondent pas'); break; case 'pass-short': - $sentMsg = _('forms::la valeur donnee est trop courte'); + $passwordMsg = _('forms::la valeur donnee est trop courte'); break; case 'pass-invalid': - $sentMsg = _('forms::la valeur donnee contient des caracteres invalides'); + $passwordMsg = _('forms::la valeur donnee contient des caracteres invalides'); break; } } @@ -472,7 +490,7 @@ class Login implements ControllerProviderInterface $needed['form_password'] = 'pass-invalid'; } - if (false !== \PHPMailer::ValidateAddress($email = $request->get('form_email'))) { + if (false === \PHPMailer::ValidateAddress($email = $request->get('form_email'))) { $needed['form_email'] = 'mail-invalid'; } @@ -498,7 +516,7 @@ class Login implements ControllerProviderInterface } if (sizeof($needed) > 0) { - $app->redirect(sprintf('/register/?%s', http_build_query(array('needed' => $needed)))); + return $app->redirect(sprintf('/register/?%s', http_build_query(array('needed' => $needed)))); } require_once($app['phraseanet.core']['Registry']->get('GV_RootPath') . 'lib/classes/deprecated/inscript.api.php'); @@ -619,6 +637,13 @@ class Login implements ControllerProviderInterface return $app->redirect("/login/?logged_out=user" . ($appRedirect ? sprintf("&redirect=/%s", $appRedirect) : "")); } + /** + * Login into Phraseanet + * + * @param \Silex\Application $app + * @param \Symfony\Component\HttpFoundation\Request $request + * @return \Symfony\Component\HttpFoundation\Response + */ public function login(Application $app, Request $request) { $appbox = $app['phraseanet.appbox']; @@ -636,10 +661,6 @@ class Login implements ControllerProviderInterface return $app->redirect("/login/?redirect=" . $request->get('redirect')); } - if ( ! $session->isset_postlog() && $session->is_authenticated() && $request->get('error') != 'no-connection') { - return $app->redirect($request->get('redirect', '/prod/')); - } - $warning = $request->get('error', ''); try { diff --git a/templates/web/login/forgot-password.html.twig b/templates/web/login/forgot-password.html.twig index ce4fe1f14a..4ffde1c0f3 100644 --- a/templates/web/login/forgot-password.html.twig +++ b/templates/web/login/forgot-password.html.twig @@ -124,7 +124,7 @@ {% if not tokenize %} {% if errorMsg is not none %} -
{{ errorMsg }}
+
{{ errorMsg }}
{% endif %} {% if sentMsg is not none %} diff --git a/templates/web/user/forgotpwd.html.twig b/templates/web/user/forgotpwd.html.twig deleted file mode 100644 index 70f7d06b8c..0000000000 --- a/templates/web/user/forgotpwd.html.twig +++ /dev/null @@ -1,176 +0,0 @@ - - - - - {% trans 'admin::compte-utilisateur changer mon mot de passe' %} - - -
-
- - - - - -
get('GV_homeTitle') ?>
-
-
-
-
- -
-
- - {% if tokenize %} - - - - - - - {% if parm['salt'] %} -
- {% trans 'Pour ameliorer la securite de l\'application, vous devez mettre a jour votre mot de passe.' %}
- {% trans 'Cette tache ne pouvant etre automatisee, merci de bien vouloir la realiser.' %} -
- {% endif %} - - - - - - - - - - - - - - - - - - -
-
- -
-
-
- -
-
 
-
-
-
-
-
-
- -
- -
-
-
- -
-
- - - - {% trans 'login:: Retour a l\'accueil' %} -
- - - {% endif %} - - {% if tokenError %} - {% set parm = {'error': 'token'} %} - {% endif %} - - {% if not tokenize and not tokenError %} -
- {% if not parm['error'] == null %} - switch ($parm['error']) { - case 'mailserver': - echo '
{% trans 'phraseanet::erreur: Echec du serveur mail' %}
- break; - case 'noaccount': - echo '
{% trans 'phraseanet::erreur: Le compte n\'a pas ete trouve' %}
- break; - case 'mail': - echo '
{% trans 'phraseanet::erreur: Echec du serveur mail' %}
- break; - case 'token': - echo '
{% trans 'phraseanet::erreur: l\'url n\'est plus valide' %}
- break; - } - {% endif %} - {% if not parm['sent'] == null %} - switch ($parm['sent']) { - case 'ok': - echo '
{% trans 'phraseanet:: Un email vient de vous etre envoye' %}
- break; - } - {% endif %} -
- {% trans 'login:: Forgot your password' %} -
-
- {% trans 'login:: Entrez votre adresse email' %} -
-
- -
- -
- {% endif %} -
-
-
-
- © Copyright Alchemy 2005-{{ "now"|date("Y") }} -
-
-
- - diff --git a/tests/Alchemy/Phrasea/Controller/Root/LoginTest.php b/tests/Alchemy/Phrasea/Controller/Root/LoginTest.php index c3143e24f1..823a687a6c 100644 --- a/tests/Alchemy/Phrasea/Controller/Root/LoginTest.php +++ b/tests/Alchemy/Phrasea/Controller/Root/LoginTest.php @@ -22,64 +22,618 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract } /** - * @covers \Alchemy\Phrasea\Controller\Root/Login::sendConfirmMail + * @covers \Alchemy\Phrasea\Controller\Root\Login::login + * @covers \Alchemy\Phrasea\Controller\Root\Login::connect */ - public function testGetConfirMail() + public function testLoginAlreadyAthenticated() { - $this->markTestIncomplete(); + $this->client->request('GET', '/login/'); + $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/prod/', $response->headers->get('location')); } /** - * @covers \Alchemy\Phrasea\Controller\Root/Login::registerConfirm + * @covers \Alchemy\Phrasea\Controller\Root\Login::login + */ + public function testLoginRedirectPostLog() + { + $this->app['phraseanet.appbox']->get_session()->logout(); + + $this->client->request('GET', '/login/', array('postlog' => '1', 'redirect' => 'prod')); + $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/?redirect=prod', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::login + * @dataProvider errorAndNoticeMsgProvider + */ + public function testLoginError($warning, $notice) + { + $this->app['phraseanet.appbox']->get_session()->logout(); + + $this->client->request('GET', '/login/', array( + 'error' => $warning, + 'notice' => $notice + )); + + $response = $this->client->getResponse(); + $this->assertTrue($response->isOk()); + } + + public function errorAndNoticeMsgProvider() + { + return array( + array('auth', 'ok'), + array('maintenance', 'already'), + array('no-connection', 'mail-sent'), + array('captcha', 'register-ok'), + array('mail-not-confirmed', 'register-ok-wait'), + array('no-base', 'password-update-ok'), + array('session', 'no-register-available') + ); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::registerConfirm + */ + public function testRegisterConfirmMailNoCode() + { + $this->client->request('GET', '/login/register-confirm/'); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/?redirect=/prod&error=code-not-found', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::registerConfirm + */ + public function testRegisterConfirmMailWrongCode() + { + $this->client->request('GET', '/login/register-confirm/', array('code' => '34dT0k3n')); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/?redirect=/prod&error=token-not-found', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::registerConfirm + */ + public function testRegisterConfirmMailUserNotFound() + { + $email = \random::generatePassword() . '_email@email.com'; + $token = \random::getUrlToken(\random::TYPE_EMAIL, 0, null, $email); + $this->client->request('GET', '/login/register-confirm/', array('code' => $token)); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/?redirect=/prod&error=user-not-found', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::registerConfirm + */ + public function testRegisterConfirmMailUnlocked() + { + $email = \random::generatePassword() . '_email@email.com'; + $token = \random::getUrlToken(\random::TYPE_EMAIL, self::$user->get_id(), null, $email); + + self::$user->set_mail_locked(false); + + $this->client->request('GET', '/login/register-confirm/', array('code' => $token)); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/?redirect=prod¬ice=already', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::registerConfirm */ public function testRegisterConfirmMail() { - $this->markTestIncomplete(); + $email = \random::generatePassword() . '_email@email.com'; + $appboxRegister = new \appbox_register($this->app['phraseanet.appbox']); + $token = \random::getUrlToken(\random::TYPE_EMAIL, self::$user->get_id(), null, $email); + + self::$user->set_mail_locked(true); + $appboxRegister->add_request(self::$user, self::$collection); + $this->client->request('GET', '/login/register-confirm/', array('code' => $token)); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/?redirect=prod¬ice=confirm-ok-wait', $response->headers->get('location')); + $this->assertEquals($email, self::$user->get_email()); + self::$user->set_email('noone@example.com'); } /** - * @covers \Alchemy\Phrasea\Controller\Root/Login::renewPassword + * @covers \Alchemy\Phrasea\Controller\Root\Login::registerConfirm + */ + public function testRegisterConfirmMailNoCollAwait() + { + $email = \random::generatePassword() . '_email@email.com'; + $token = \random::getUrlToken(\random::TYPE_EMAIL, self::$user->get_id(), null, $email); + + self::$user->set_mail_locked(true); + + $sql = "DELETE FROM demand WHERE usr_id = :usr_id"; + $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); + $stmt->execute(array(':usr_id' => self::$user->get_id())); + $stmt->closeCursor(); + + $this->client->request('GET', '/login/register-confirm/', array('code' => $token)); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + + $this->assertEquals('/login/?redirect=prod¬ice=confirm-ok', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::renewPassword + */ + public function testRenewPasswordInvalidEmail() + { + $this->client->request('POST', '/login/forgot-password/', array('mail' => 'invalid.email.com')); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/forgot-password/?error=invalidmail', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::renewPassword + */ + public function testRenewPasswordUnknowEmail() + { + $this->client->request('POST', '/login/forgot-password/', array('mail' => 'invalid_email@test.com')); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/forgot-password/?error=noaccount', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::renewPassword + */ + public function testRenewPasswordMail() + { + $this->client->request('POST', '/login/forgot-password/', array('mail' => self::$user->get_email())); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/forgot-password/?sent=ok', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::renewPassword + * @dataProvider passwordProvider + */ + public function testRenewPasswordBadArguments($password, $passwordConfirm, $redirect) + { + $this->client->request('POST', '/login/forgot-password/', array( + 'token' => '1Cx6Z7', + 'form_password' => $password, + 'form_password_confirm' => $passwordConfirm + ) + ); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals($redirect, $response->headers->get('location')); + } + + public function testRenewPasswordBadToken() + { + $this->client->request('POST', '/login/forgot-password/', array( + 'token' => 'badToken', + 'form_password' => 'password', + 'form_password_confirm' => 'password' + ) + ); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/forgot-password/?error=token', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::renewPassword + * @dataProvider passwordProvider */ public function testRenewPassword() { - $this->markTestIncomplete(); + $token = \random::getUrlToken(\random::TYPE_PASSWORD, self::$user->get_id()); + + $this->client->request('POST', '/login/forgot-password/', array( + 'token' => $token, + 'form_password' => 'password', + 'form_password_confirm' => 'password' + ) + ); + $response = $this->client->getResponse(); + + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/?notice=password-update-ok', $response->headers->get('location')); + } + + public function passwordProvider() + { + return array( + array('password', 'password_not_identical', '/login/forgot-password/?pass-error=pass-match'), + array('min', 'min', '/login/forgot-password/?pass-error=pass-short'), + array('in valid password', 'in valid password', '/login/forgot-password/?pass-error=pass-invalid'), + ); } /** - * @covers \Alchemy\Phrasea\Controller\Root/Login::displayForgotPasswordForm + * @covers \Alchemy\Phrasea\Controller\Root\Login::displayForgotPasswordForm */ - public function testGetForgotPassword() + public function testGetForgotPasswordSendMsg() { - $this->markTestSkipped('Update rewrite rules'); + $this->client->request('GET', '/login/forgot-password/', array( + 'sent' => 'ok', + )); - $this->client->request('GET', '/login/forgot-password/'); - - $response = $this->client->getResponse(); - - $this->assertTrue($response->isOk()); + $this->assertTrue($this->client->getResponse()->isOk()); } /** - * @covers \Alchemy\Phrasea\Controller\Root/Login::displayRegisterForm + * @covers \Alchemy\Phrasea\Controller\Root\Login::displayForgotPasswordForm */ - public function testGetRegister() + public function testGetForgotBadToken() { - $this->markTestSkipped('Update rewrite rules'); - - $this->client->request('GET', '/login/register/'); + $crawler = $this->client->request('GET', '/login/forgot-password/', array( + 'token' => 'one-token' + )); - $response = $this->client->getResponse(); - - $this->assertTrue($response->isOk()); + $this->assertTrue($this->client->getResponse()->isOk()); + $this->assertEquals(1, $crawler->filter('.error-msg')->count()); } - /** - * @covers \Alchemy\Phrasea\Controller\Root/Login::logout + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::displayForgotPasswordForm + * @dataProvider errorMessageProvider + */ + public function testGetForgotPasswordErrorMsg($errorMsg) + { + $crawler = $this->client->request('GET', '/login/forgot-password/', array( + 'error' => $errorMsg + )); + + $response = $this->client->getResponse(); + $this->assertTrue($response->isOk()); + $this->assertEquals(1, $crawler->filter('.error-msg')->count()); + } + + public function errorMessageProvider() + { + return array( + array('invalidmail'), + array('mailserver'), + array('noaccount'), + array('mail'), + array('token'), + ); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::displayForgotPasswordForm + * @dataProvider badPasswordMsgProvider + */ + public function testGetForgotPasswordBadPassword($msg) + { + $this->client->request('GET', '/login/forgot-password/', array( + 'pass-error' => $msg, + )); + + $this->assertTrue($this->client->getResponse()->isOk()); + } + + public function badPasswordMsgProvider() + { + return array( + array('pass-match'), + array('pass-short'), + array('pass-invalid'), + ); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::displayRegisterForm + * @covers \Alchemy\Phrasea\Controller\Root\Login::getRegisterFieldConfiguration + * @dataProvider fieldErrorProvider + */ + public function testGetRegister($error) + { + $this->client->request('GET', '/login/register/', array( + 'needed' => array( + 'field_name' => $error, + ) + )); + + $this->assertTrue($this->client->getResponse()->isOk()); + } + + public function fieldErrorProvider() + { + return array( + array('required-field'), + array('pass-match'), + array('pass-short'), + array('pass-invalid'), + array('email-invalid'), + array('login-short'), + array('login-mail-exists'), + array('user-mail-exists'), + array('no-collections'), + ); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::register + * @expectedException Symfony\Component\HttpKernel\Exception\HttpException + */ + public function testPostRegisterBadRequest() + { + $this->client->request('POST', '/login/register/'); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::register + * @dataProvider parametersProvider + */ + public function testPostRegisterbadArguments($parameters) + { + $this->client->request('POST', '/login/register/', $parameters); + + $this->assertTrue($this->client->getResponse()->isRedirect()); + } + + public function parametersProvider() + { + return array( + array(array(//required field + "form_login" => '', + "form_password" => 'password', + "form_password_confirm" => 'password', + "form_gender" => 'M', + "form_lastname" => 'lastname', + "form_firstname" => 'firstname', + "form_email" => 'email@email.com', + "form_job" => 'job', + "form_company" => 'company', + "form_activity" => 'activity', + "form_phone" => 'phone', + "form_fax" => 'fax', + "form_address" => 'adress', + "form_zip" => 'zip', + "form_geonameid" => 'geoname_id', + "demand" => array() + )), + array(array(//password mismatch + "form_login" => 'login', + "form_password" => 'password', + "form_password_confirm" => 'passwordmismatch', + "form_gender" => 'M', + "form_lastname" => 'lastname', + "form_firstname" => 'firstname', + "form_email" => 'email@email.com', + "form_job" => 'job', + "form_company" => 'company', + "form_activity" => 'activity', + "form_phone" => 'phone', + "form_fax" => 'fax', + "form_address" => 'adress', + "form_zip" => 'zip', + "form_geonameid" => 'geoname_id', + "demand" => array() + )), + array(array(//password tooshort + "form_login" => 'login', + "form_password" => 'min', + "form_password_confirm" => 'min', + "form_gender" => 'M', + "form_lastname" => 'lastname', + "form_firstname" => 'firstname', + "form_email" => 'email@email.com', + "form_job" => 'job', + "form_company" => 'company', + "form_activity" => 'activity', + "form_phone" => 'phone', + "form_fax" => 'fax', + "form_address" => 'adress', + "form_zip" => 'zip', + "form_geonameid" => 'geoname_id', + "demand" => array() + )), + array(array(//password invalid + "form_login" => 'login', + "form_password" => 'invalid pass word', + "form_password_confirm" => 'invalid pass word', + "form_gender" => 'M', + "form_lastname" => 'lastname', + "form_firstname" => 'firstname', + "form_email" => 'email@email.com', + "form_job" => 'job', + "form_company" => 'company', + "form_activity" => 'activity', + "form_phone" => 'phone', + "form_fax" => 'fax', + "form_address" => 'adress', + "form_zip" => 'zip', + "form_geonameid" => 'geoname_id', + "demand" => array() + )), + array(array(//email invalid + "form_login" => 'login', + "form_password" => 'password', + "form_password_confirm" => 'password', + "form_gender" => 'M', + "form_lastname" => 'lastname', + "form_firstname" => 'firstname', + "form_email" => 'email@com', + "form_job" => 'job', + "form_company" => 'company', + "form_activity" => 'activity', + "form_phone" => 'phone', + "form_fax" => 'fax', + "form_address" => 'adress', + "form_zip" => 'zip', + "form_geonameid" => 'geoname_id', + "demand" => array() + )), + array(array(//login exists + "form_login" => 'test_phpunit', + "form_password" => 'invalid pass word', + "form_password_confirm" => 'invalid pass word', + "form_gender" => 'M', + "form_lastname" => 'lastname', + "form_firstname" => 'firstname', + "form_email" => 'email@email.com', + "form_job" => 'job', + "form_company" => 'company', + "form_activity" => 'activity', + "form_phone" => 'phone', + "form_fax" => 'fax', + "form_address" => 'adress', + "form_zip" => 'zip', + "form_geonameid" => 'geoname_id', + "demand" => array() + )), + array(array(//mails exists + "form_login" => 'login', + "form_password" => 'invalid pass word', + "form_password_confirm" => 'noone@example.com', + "form_gender" => 'M', + "form_lastname" => 'lastname', + "form_firstname" => 'firstname', + "form_email" => 'email@email.com', + "form_job" => 'job', + "form_company" => 'company', + "form_activity" => 'activity', + "form_phone" => 'phone', + "form_fax" => 'fax', + "form_address" => 'adress', + "form_zip" => 'zip', + "form_geonameid" => 'geoname_id', + "demand" => array() + )), + array(array(//no demands + "form_login" => 'login', + "form_password" => 'invalid pass word', + "form_password_confirm" => 'email@email.com', + "form_gender" => 'M', + "form_lastname" => 'lastname', + "form_firstname" => 'firstname', + "form_email" => 'email@email.com', + "form_job" => 'job', + "form_company" => 'company', + "form_activity" => 'activity', + "form_phone" => 'phone', + "form_fax" => 'fax', + "form_address" => 'adress', + "form_zip" => 'zip', + "form_geonameid" => 'geoname_id', + "demand" => array() + )) + ); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::register + */ + public function testPostRegister() + { + $bases = array(); + + foreach ($this->app['phraseanet.appbox']->get_databoxes() as $databox) { + foreach ($databox->get_collections() as $collection) { + $bases[] = $collection->get_base_id(); + } + } + + $login = \random::generatePassword(); + $email = $login . '@google.com'; + + $this->client->request('POST', '/login/register/', array( + "form_login" => $login, + "form_password" => 'password', + "form_password_confirm" => 'password', + "form_gender" => 'M', + "form_lastname" => 'lastname', + "form_firstname" => 'firstname', + "form_email" => $email, + "form_job" => 'job', + "form_company" => 'company', + "form_activity" => 'activity', + "form_phone" => 'phone', + "form_fax" => 'fax', + "form_address" => 'adress', + "form_zip" => 'zip', + "form_geonameid" => 'geoname_id', + "demand" => $bases + )); + + if ( ! $userId = \User_Adapter::get_usr_id_from_login($login)) { + $this->fail('User not created'); + } + + $user = new User_Adapter((int) $userId, $this->app['phraseanet.appbox']); + + $user->delete(); + + $this->assertTrue($this->client->getResponse()->isRedirect()); + $this->assertEquals('/login/?notice=mail-sent', $this->client->getResponse()->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::logout */ public function testGetLogout() { - $this->markTestIncomplete(); + $this->assertTrue($this->app['phraseanet.core']->isAuthenticated()); + $this->client->request('GET', '/login/logout/', array('app' => 'prod')); + $this->assertFalse($this->app['phraseanet.core']->isAuthenticated()); + + $this->assertTrue($this->client->getResponse()->isRedirect()); } + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::sendConfirmMail + * @expectedException Symfony\Component\HttpKernel\Exception\HttpException + */ + public function testSendConfirmMailBadRequest() + { + $this->client->request('GET', '/login/send-mail-confirm/'); + } + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::sendConfirmMail + */ + public function testSendConfirmMail() + { + $this->client->request('GET', '/login/send-mail-confirm/', array('usr_id' => self::$user->get_id())); + + $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/?notice=mail-sent', $response->headers->get('location')); + } + + /** + * @covers \Alchemy\Phrasea\Controller\Root\Login::sendConfirmMail + */ + public function testSendConfirmMailWrongUser() + { + $this->client->request('GET', '/login/send-mail-confirm/', array('usr_id' => 0)); + + $response = $this->client->getResponse(); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('/login/?error=user-not-found', $response->headers->get('location')); + } } From 0ecdf87260295a80a1779bc86fac859a7a8c2ef8 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Wed, 1 Aug 2012 15:44:42 +0200 Subject: [PATCH 4/5] add /developers/* tests add /developers/* test add /developers/* test fix tests add developer test fix Typo fix tests --- .../Phrasea/Controller/Root/Developers.php | 15 +- .../web/developers/application_form.html.twig | 1 - .../Phrasea/Controller/Root/AccountTest.php | 2 +- .../Controller/Root/DevelopersTest.php | 320 ++++++++++++++++++ .../Phrasea/Controller/Root/LoginTest.php | 26 +- 5 files changed, 344 insertions(+), 20 deletions(-) create mode 100644 tests/Alchemy/Phrasea/Controller/Root/DevelopersTest.php diff --git a/lib/Alchemy/Phrasea/Controller/Root/Developers.php b/lib/Alchemy/Phrasea/Controller/Root/Developers.php index 30be4a8ce0..573d6ac6d4 100644 --- a/lib/Alchemy/Phrasea/Controller/Root/Developers.php +++ b/lib/Alchemy/Phrasea/Controller/Root/Developers.php @@ -289,12 +289,11 @@ class Developers implements ControllerProviderInterface try { $clientApp = new \API_OAuth2_Application($app['phraseanet.appbox'], $id); + $clientApp->set_grant_password((bool) $request->get('grant', false)); } catch (\Exception_NotFound $e) { $error = true; } - $clientApp->set_grant_password((bool) $request->get('grant', false)); - return $app->json(array('success' => ! $error)); } @@ -308,9 +307,7 @@ class Developers implements ControllerProviderInterface */ public function newApp(Application $app, Request $request) { - $error = false; - - if ($request->get("type") == "desktop") { + if ($request->get('type') == "desktop") { $form = new \API_OAuth2_Form_DevAppDesktop($app['request']); } else { $form = new \API_OAuth2_Form_DevAppInternet($app['request']); @@ -318,11 +315,7 @@ class Developers implements ControllerProviderInterface $violations = $app['validator']->validate($form); - if ($violations->count() == 0) { - $error = true; - } - - if ($error) { + if ($violations->count() === 0) { $application = \API_OAuth2_Application::create($app['phraseanet.appbox'], $app['phraseanet.core']->getAuthenticatedUser(), $form->getName()); $application ->set_description($form->getDescription()) @@ -338,7 +331,7 @@ class Developers implements ControllerProviderInterface "form" => $form ); - return $app['twig']->render('/developers/application.html.twig', $var); + return $app['twig']->render('/developers/application_form.html.twig', $var); } /** diff --git a/templates/web/developers/application_form.html.twig b/templates/web/developers/application_form.html.twig index ae62a9a555..f6b3db5fd3 100644 --- a/templates/web/developers/application_form.html.twig +++ b/templates/web/developers/application_form.html.twig @@ -95,7 +95,6 @@