Commit 5c4ff16b authored by Arthur Suzuki's avatar Arthur Suzuki

hotline#86543: SIGB Koha: password gets updated with new value in bokeh even if not saved in koha

parent c3134f6f
Pipeline #9643 passed with stage
in 44 minutes and 40 seconds
- ticket #86543 : SIGB Koha : Prise en compte des contraintes de mot de passe fort
\ No newline at end of file
......@@ -41,6 +41,16 @@ class Class_WebService_SIGB_Koha_RestfulService
}
protected function _getErrorFromCode($error_code, $default_message) {
$error = ['password_too_short' => $this->_('Mot de passe trop court'),
'password_too_weak' => $this->_('Mot de passe trop faible')];
if (array_key_exists($error_code,$error))
return $default_message . " : " . $error[$error_code];
return $default_message;
}
public function providesSuggestions() {
return true;
}
......@@ -162,11 +172,22 @@ class Class_WebService_SIGB_Koha_RestfulService
public function saveEmprunteur($emprunteur) {
$this->restfulPut('auth/change_password',
$response = $this->restfulPut('auth/change_password',
['user_name' => $emprunteur->getLogin(),
'new_password' => $emprunteur->getPassword()]);
return $emprunteur;
if (null === ($json = json_decode($response,true))) {
throw new Class_WebService_Exception($this->_("saveEmprunteur() s'attends à une réponse au format JSON"));
}
if (!array_key_exists('reasons', $json)
|| !is_array($json['reasons'])
|| (!$reasons = array_keys($json['reasons'])))
return $emprunteur;
$error = implode(', ', array_map([$this, '_getErrorFromCode'],
$reasons, $reasons));
throw new Class_WebService_Exception($this->_('Erreur de mise à jour du mot de passe : %s', $error));
}
......
......@@ -102,13 +102,21 @@ class AbonneControllerChangePasswordOnKohaWithRestfulTest
->whenCalled('putData')
->with('http://plage.com/cgi-bin/koha/rest.pl/auth/change_password',
['user_name' => 'testingtest',
'new_password' => 'ce1bnm2p'])
->answers(json_encode([['success' => ['password' => '$2a$08$o0BrhvBevJBbWcYJElH3IevoytrdwnkZUQLwNcxQt3GhAjtht3RoK']]]))
'new_password' => 'Ce1bnm2p'])
->answers(json_encode(['success' => 1, 'reasons' => []]))
->beStrict();
->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]]))
$this->postDispatch('/abonne/edit', ['password' => 'ce1bnm2p',
'confirm_password' => 'ce1bnm2p']);
->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();
}
......@@ -116,8 +124,28 @@ class AbonneControllerChangePasswordOnKohaWithRestfulTest
/** @test */
public function passwordShouldHaveBeenUpdatedToCe1bnm2p() {
$this->postDispatch('/abonne/edit', ['password' => 'Ce1bnm2p',
'confirm_password' => 'Ce1bnm2p']);
Class_Users::clearCache();
$this->assertEquals('ce1bnm2p', Class_Users::getIdentity()->getPassword());
$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");
}
}
......
......@@ -530,13 +530,16 @@ class KohaRestGetUserHistoryTest extends KohaRestfulTestCase {
class KohaRestfulChangePasswordTest extends KohaRestfulTestCase {
protected $_user, $_borrower;
public function setUp() {
parent::setUp();
$borrower = new Class_WebService_SIGB_Emprunteur('harlock', 'harlock');
$borrower->setService($this->service);
$this->_borrower = new Class_WebService_SIGB_Emprunteur('harlock', 'harlock');
$this->_borrower->setService($this->service);
$user = $this->fixture('Class_Users',
$this->_user = $this->fixture('Class_Users',
['id' => 34,
'login' => 'harlock',
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
......@@ -544,33 +547,62 @@ class KohaRestfulChangePasswordTest extends KohaRestfulTestCase {
'password' => 'arcadia',
'idabon' => 'AO989IE']);
$user->setFicheSIGB(['fiche' => $borrower]);
$this->_user->setFicheSIGB(['fiche' => $this->_borrower]);
}
public function setPasswordWaitReply($password,$response){
$this->mock_web_client
->whenCalled('putData')
->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/rest.pl/auth/change_password',
['user_name' => 'harlock',
'new_password' => 'ce1snm2p'])
->answers(json_encode([['success' => ['password' => '$2a$08$o0BrhvBevJBbWcYJElH3IevoytrdwnkZUQLwNcxQt3GhAjtht3RoK']]]))
'new_password' => $password])
->answers($response)
->beStrict();
$user->setPassword('ce1snm2p');
$borrower->updateFromUser($user);
$user->save();
$this->_user->setPassword($password);
$this->_borrower->updateFromUser($this->_user);
Class_Users::clearCache();
$borrower->save();
$this->_borrower->save();
$this->_user->save();
}
/** @test */
public function userPasswordShouldBeCe1snm2p() {
$this->assertEquals('ce1snm2p', Class_Users::find(34)->getPassword());
public function withNotJSONReplyShouldNotUpdatePasswordAndTriggerException() {
$this->expectException(Class_WebService_Exception::class);
$password = 'tst';
$response = "NotAJSON";
$this->setPasswordWaitReply($password,$response);
$this->assertEquals('arcadia', Class_Users::find(34)->getPassword());
}
/** @test */
public function kohaShouldHaveBeenCalled() {
$this->assertTrue($this->mock_web_client->methodHasBeenCalled('putData'));
public function withPasswordTooShortShouldNotUpdatePasswordAndTriggerException() {
$this->expectException(Class_WebService_Exception::class);
$password = 'tst';
$response = ['success' => 0,
'reasons' => ['password_too_short' => 1] ];
$this->setPasswordWaitReply($password, json_encode($response));
$this->assertEquals('arcadia', Class_Users::find(34)->getPassword());
}
/** @test */
public function withPasswordTooWeakShouldNotUpdatePasswordAndTriggerException() {
$this->expectException(Class_WebService_Exception::class);
$password = 'testingsimplestuff';
$response = [ 'success' => 0, 'reasons' => ['password_too_weak' => 1] ];
$this->setPasswordWaitReply($password,json_encode($response));
$this->assertEquals('arcadia', Class_Users::find(34)->getPassword());
}
/** @test */
public function withValidPasswordShouldUpdateQuietly() {
$password = 'testingComplex35tUff!';
$response = [ 'success' => 1, 'reasons' => [] ];
$this->setPasswordWaitReply($password,json_encode($response));
$this->assertEquals($password, Class_Users::find(34)->getPassword());
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment