Skip to content
Snippets Groups Projects
AbonneControllerChangePasswordTest.php 13.04 KiB
<?php
/**
 * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */


abstract class AbonneControllerChangePasswordOnKohaTestCase extends AbstractControllerTestCase {
  protected
    $_storm_default_to_volatile = true,
    $_web_client,
    $_int_bib,
    $_patron;


  public function setUp() {
    parent::setUp();

    $this->_prepareFixtures();

    $borrower = Class_WebService_SIGB_Emprunteur::newInstance('ml34');
    $this->_patron->setFicheSigb(['type_comm' => Class_IntBib::COM_KOHA,
                                  'fiche' => $borrower])
                  ->setIdSigb('123456');

    ZendAfi_Auth::getInstance()->logUser($this->_patron);

    $logger = $this->mock()
                   ->whenCalled('log')->answers(true)

                   ->whenCalled('logError')
                   ->willDo(
                            function($url, $message) {
                              var_dump($url . ' :: ' . $message);
                              exit;
                            });

    Class_WebService_SIGB_AbstractService::setLogger($logger);

    $this->_web_client = $this->mock();
    $this->_int_bib->getSIGBComm()->setWebClient($this->_web_client);
  }


  protected function _prepareFixtures() {
    $this->_int_bib = $this->fixture('Class_IntBib',
                                     ['id' => 3,
                                      'comm_params' => ['url_serveur' => 'http://plage.com/cgi-bin/koha/ilsdi.pl',
                                                        'restful' => '1'],
                                      'comm_sigb' => Class_IntBib::COM_KOHA
                                     ]);

    $this->fixture('Class_Bib', ['id' => 11,
                                 'libelle' => 'Bib de la plage',
                                 'int_bib' => $this->_int_bib]);

    $this->_patron = $this->fixture('Class_Users',
                                    ['id' => 5,
                                     'login' => 'testingtest',
                                     'password' => 'achanger',
                                     'id_site' => 11,
                                     'int_bib' => $this->_int_bib,
                                     'idabon' => '123456',
                                     'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
                                     'disable_newsletter' => 0]);
  }


  public function tearDown() {
    Class_WebService_SIGB_AbstractService::setLogger(null);
    Class_IntBib::find(3)->getSIGBComm()->setWebClient(null);
    parent::tearDown();
  }
}



class AbonneControllerChangePasswordOnKohaWithRestfulTest
  extends AbonneControllerChangePasswordOnKohaTestCase {

  public function setUp() {
    parent::setUp();

    $this->_web_client
      ->whenCalled('open_url')
      ->with('http://plage.com/cgi-bin/koha/ilsdi.pl?service=AuthenticatePatron&username=ml34&password=achanger')
      ->answers(true)

      ->whenCalled('putData')
      ->with('http://plage.com/cgi-bin/koha/rest.pl/auth/change_password',
             ['user_name' => 'testingtest',
              'new_password' => 'Ce1bnm2p'])
      ->answers(json_encode(['success' => 1, 'reasons' => []]))

      ->whenCalled('putData')
      ->with('http://plage.com/cgi-bin/koha/rest.pl/auth/change_password',
             ['user_name' => 'testingtest',
              'new_password' => 'test'])
      ->answers(json_encode(['success' => '0', 'reasons' => ['password_too_short' => 1]]))

      ->whenCalled('putData')
      ->with('http://plage.com/cgi-bin/koha/rest.pl/user/testingtest',
             ['data' => "[]"])
      ->answers(json_encode(['success' => '1', 'modified_fields' => []]))

      ->whenCalled('putData')
      ->with('http://plage.com/cgi-bin/koha/rest.pl/auth/change_password',
             ['user_name' => 'testingtest',
              'new_password' => 'testingsimple'])
      ->answers(json_encode(['success' => '0', 'reasons' => ['password_too_weak' => 1]]))
      ->beStrict();
  }


  /** @test */
  public function passwordShouldHaveBeenUpdatedToCe1bnm2p() {
    $this->postDispatch('/abonne/edit', ['password' => 'Ce1bnm2p',
                                         'confirm_password' => 'Ce1bnm2p']);
    Class_Users::clearCache();
    $this->assertEquals('Ce1bnm2p', Class_Users::getIdentity()->getPassword());
  }


  /** @test */
  public function shortPasswordShouldTriggerError() {
    $this->postDispatch('/abonne/edit', ['password' => 'test',
                                         'confirm_password' => 'test']);
    Class_Users::clearCache();
    $this->assertXPathContentContains('//ul[@class="errors"]//li',
                                      "Mot de passe trop court");
  }


  /** @test */
  public function weakPasswordShouldTriggerError() {
    $this->postDispatch('/abonne/edit', ['password' => 'testingsimple',
                                         'confirm_password' => 'testingsimple']);
    Class_Users::clearCache();
    $this->assertXPathContentContains('//ul[@class="errors"]//li',
                                      "Mot de passe trop faible");
  }
}




class AbonneControllerChangePasswordOnKohaWithCommunityServiceTest
  extends AbonneControllerChangePasswordOnKohaTestCase {

  public function setUp() {
    parent::setUp();

    $auth = [$this->_patron->getLogin(),
             $this->_patron->getPassword()];

    $this->_web_client
      ->whenCalled('open_url')
      ->with('http://plage.com/cgi-bin/koha/ilsdi.pl?service=AuthenticatePatron&username=ml34&password=achanger')
      ->answers(true)

      ->whenCalled('postRawDataResponse')
      ->with('http://plage.com/api/v1/patrons/123456/password',
             json_encode(['password' => 'Ce1bnm2p',
                          'password_2' => 'Ce1bnm2p']),
             Zend_Http_Client::ENC_URLENCODED,
             ['auth' => $auth ])
      ->answers(new Zend_Http_Response(200,[],''))

      ->whenCalled('postRawDataResponse')
      ->with('http://plage.com/api/v1/patrons/123456/password',

             json_encode(['password' => 'test',
                          'password_2' => 'test']),
             Zend_Http_Client::ENC_URLENCODED,
             ['auth' => $auth ])
      ->answers(new Zend_Http_Response(200,
                                       [],
                                       json_encode(['error' => 'Password length (4) is shorter than required (5)'])))

      ->whenCalled('postRawDataResponse')
      ->with('http://plage.com/api/v1/patrons/123456/password',
             json_encode(['password' => 'testingsimple',
                          'password_2' => 'testingsimple']),
             Zend_Http_Client::ENC_URLENCODED,
             ['auth' => $auth ])
      ->answers(new Zend_Http_Response(200,
                                       [],
                                       json_encode(['error' => '[Passwords is too weak]'])))

      ->beStrict();
  }


  protected function _prepareFixtures() {
    parent::_prepareFixtures();
    $comm_params = $this->_int_bib->getCommParamsAsArray();
    $comm_params['api_community'] = '1';
    $this->_int_bib->setCommParams($comm_params);
  }


  /** @test */
  public function passwordShouldHaveBeenUpdatedToCe1bnm2p() {
    $this->postDispatch('/abonne/edit', ['password' => 'Ce1bnm2p',
                                         'confirm_password' => 'Ce1bnm2p']);
    Class_Users::clearCache();
    $this->assertEquals('Ce1bnm2p', Class_Users::getIdentity()->getPassword());
  }

  /** @test */
  public function shortPasswordShouldTriggerError() {
    $this->postDispatch('/abonne/edit', ['password' => 'test',
                                         'confirm_password' => 'test']);
    Class_Users::clearCache();
    $this->assertXPathContentContains('//ul[@class="errors"]//li',
                                      "Mot de passe trop court");
  }

  /** @test */
  public function weakPasswordShouldTriggerError() {
    $this->postDispatch('/abonne/edit', ['password' => 'testingsimple',
                                         'confirm_password' => 'testingsimple']);
    Class_Users::clearCache();
    $this->assertXPathContentContains('//ul[@class="errors"]//li',
                                      "Mot de passe trop faible");
  }
}




class AbonneControllerChangePasswordOnKohaWithoutRestfulTest
  extends AbonneControllerChangePasswordOnKohaTestCase {

  protected function _prepareFixtures() {
    parent::_prepareFixtures();
    $comm_params = $this->_int_bib->getCommParamsAsArray();
    $comm_params['restful'] = '';
    $this->_int_bib->setCommParams($comm_params);
  }


  public function setUp() {
    parent::setUp();

    $this->_web_client
      ->whenCalled('open_url')
      ->with('http://plage.com/cgi-bin/koha/ilsdi.pl?service=AuthenticatePatron&username=ml34&password=achanger')
      ->answers(true)

      ->beStrict();

    $this->postDispatch('/abonne/edit', ['password' => 'ce1bnm2p',
                                         'confirm_password' => 'ce1bnm2p']);
  }


  /** @test */
  public function passwordShouldHaveBeenUpdatedToCe1bnm2p() {
    Class_Users::clearCache();
    $this->assertEquals('ce1bnm2p', Class_Users::getIdentity()->getPassword());
  }
}




class AbonneControllerChangePasswordOnNanookTest extends AbstractControllerTestCase {
  protected
    $_storm_default_to_volatile = true,
    $_mock_web_client;


  public function setUp() {
    parent::setUp();

    $sigb_plage = $this->fixture('Class_IntBib',
                                 ['id' => 3,
                                  'comm_params' => ['url_serveur' => 'http://plage.com/cgi-bin/nanook/ilsdi.pl'],
                                  'comm_sigb' => Class_IntBib::COM_NANOOK
                                 ]);

    $this->fixture('Class_Bib', ['id' => 11,
                                 'libelle' => 'Bib de la plage',
                                 'int_bib' => $sigb_plage]);

    $nanook_user = $this->fixture('Class_Users',
                                ['id' => 5,
                                 'login' => 'ml34',
                                 'password' => 'achanger',
                                 'id_site' => 11,
                                 'int_bib' => $sigb_plage,
                                 'idabon' => '123456',
                                 'id_sigb' => '111',
                                 'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
                                 'disable_newsletter' => 0]);

    $nanook_user->setFicheSigb(['type_comm' => Class_IntBib::COM_KOHA,
                                'fiche' => Class_WebService_SIGB_Emprunteur::nullInstance()]);


    $this->_mock_web_client = $this->mock()
                                   ->whenCalled('postData')
                                   ->with('http://plage.com/cgi-bin/nanook/ilsdi.pl/service/UpdatePatronInfo/patronId/111',
                                          ['password' => 'ce1bnm2p',
                                           'mail' => '',
                                           'phoneNumber' => ''])
                                   ->answers(true)

                                   ->beStrict();

    $sigb_comm = Class_IntBib::find(3)->getSIGBComm();
    $sigb_comm->setWebClient($this->_mock_web_client);
    ZendAfi_Auth::getInstance()->logUser($nanook_user);

    $logger = $this->mock()
                   ->whenCalled('log')->answers(true)

                   ->whenCalled('logError')
                   ->willDo(
                            function($url, $message) {
                              var_dump($url . ' :: ' . $message);
                              exit;
                            });

    Class_WebService_SIGB_AbstractService::setLogger($logger);
    $this->postDispatch('/abonne/edit', ['password' => 'ce1bnm2p',
                                         'confirm_password' => 'ce1bnm2p']);
  }


  public function tearDown() {
    Class_WebService_SIGB_AbstractService::setLogger(null);
    Class_IntBib::find(3)->getSIGBComm()->setWebClient(null);
    parent::tearDown();
  }


  /** @test */
  public function passwordShouldHaveBeenUpdatedToCe1bnm2p() {
    Class_Users::clearCache();
    $this->assertEquals('ce1bnm2p', Class_Users::getIdentity()->getPassword());
  }


  /** @test */
  public function responseShouldBeARedirect() {
    $this->assertRedirectTo('/abonne/fiche');
  }


  /** @test */
  public function serviceShouldHaveBeenCalledOnNanookServer() {
    $this->assertTrue($this->_mock_web_client->methodHasBeenCalled('postData'));
  }
}