3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Config\ConfigManager
;
8 use Shaarli\Front\Exception\OpenShaarliPasswordException
;
9 use Shaarli\Front\Exception\WrongTokenException
;
10 use Shaarli\Security\SessionManager
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
15 class PasswordControllerTest
extends TestCase
17 use FrontAdminControllerMockHelper
;
19 /** @var PasswordController */
20 protected $controller;
22 /** @var mixed[] Variables assigned to the template */
23 protected $assignedVariables = [];
25 public function setUp(): void
27 $this->createContainer();
28 $this->assignTemplateVars($this->assignedVariables
);
30 $this->controller
= new PasswordController($this->container
);
34 * Test displaying the change password page.
36 public function testGetPage(): void
38 $request = $this->createMock(Request
::class);
39 $response = new Response();
41 $result = $this->controller
->index($request, $response);
43 static::assertSame(200, $result->getStatusCode());
44 static::assertSame('changepassword', (string) $result->getBody());
45 static::assertSame('Change password - Shaarli', $this->assignedVariables
['pagetitle']);
49 * Change the password with valid parameters
51 public function testPostNewPasswordDefault(): void
53 $request = $this->createMock(Request
::class);
54 $request->method('getParam')->willReturnCallback(function (string $key): string {
55 if ('oldpassword' === $key) {
58 if ('setpassword' === $key) {
64 $response = new Response();
66 $this->container
->conf
= $this->createMock(ConfigManager
::class);
67 $this->container
->conf
->method('get')->willReturnCallback(function (string $key, $default) {
68 if ('credentials.hash' === $key) {
69 return sha1('old' . 'credentials.login' . 'credentials.salt');
72 return strpos($key, 'credentials') !== false ? $key : $default;
74 $this->container
->conf
->expects(static::once())->method('write')->with(true);
76 $this->container
->conf
78 ->willReturnCallback(function (string $key, string $value) {
79 if ('credentials.hash' === $key) {
80 static::assertSame(sha1('new' . 'credentials.login' . 'credentials.salt'), $value);
85 $result = $this->controller
->change($request, $response);
87 static::assertSame(200, $result->getStatusCode());
88 static::assertSame('changepassword', (string) $result->getBody());
89 static::assertSame('Change password - Shaarli', $this->assignedVariables
['pagetitle']);
93 * Change the password with a wrong existing password
95 public function testPostNewPasswordWrongOldPassword(): void
97 $request = $this->createMock(Request
::class);
98 $request->method('getParam')->willReturnCallback(function (string $key): string {
99 if ('oldpassword' === $key) {
102 if ('setpassword' === $key) {
108 $response = new Response();
110 $this->container
->conf
= $this->createMock(ConfigManager
::class);
111 $this->container
->conf
->method('get')->willReturnCallback(function (string $key, $default) {
112 if ('credentials.hash' === $key) {
113 return sha1('old' . 'credentials.login' . 'credentials.salt');
116 return strpos($key, 'credentials') !== false ? $key : $default;
119 $this->container
->conf
->expects(static::never())->method('set');
120 $this->container
->conf
->expects(static::never())->method('write');
122 $this->container
->sessionManager
123 ->expects(static::once())
124 ->method('setSessionParameter')
125 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['The old password is not correct.'])
128 $result = $this->controller
->change($request, $response);
130 static::assertSame(400, $result->getStatusCode());
131 static::assertSame('changepassword', (string) $result->getBody());
132 static::assertSame('Change password - Shaarli', $this->assignedVariables
['pagetitle']);
136 * Change the password with a wrong existing password
138 public function testPostNewPasswordWrongToken(): void
140 $this->container
->sessionManager
= $this->createMock(SessionManager
::class);
141 $this->container
->sessionManager
->method('checkToken')->willReturn(false);
143 $this->container
->conf
->expects(static::never())->method('set');
144 $this->container
->conf
->expects(static::never())->method('write');
146 $request = $this->createMock(Request
::class);
147 $response = new Response();
149 $this->expectException(WrongTokenException
::class);
151 $this->controller
->change($request, $response);
155 * Change the password with an empty new password
157 public function testPostNewEmptyPassword(): void
159 $this->container
->sessionManager
160 ->expects(static::once())
161 ->method('setSessionParameter')
162 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['You must provide the current and new password to change it.'])
165 $this->container
->conf
->expects(static::never())->method('set');
166 $this->container
->conf
->expects(static::never())->method('write');
168 $request = $this->createMock(Request
::class);
169 $request->method('getParam')->willReturnCallback(function (string $key): string {
170 if ('oldpassword' === $key) {
173 if ('setpassword' === $key) {
179 $response = new Response();
181 $result = $this->controller
->change($request, $response);
183 static::assertSame(400, $result->getStatusCode());
184 static::assertSame('changepassword', (string) $result->getBody());
185 static::assertSame('Change password - Shaarli', $this->assignedVariables
['pagetitle']);
189 * Change the password on an open shaarli
191 public function testPostNewPasswordOnOpenShaarli(): void
193 $this->container
->conf
= $this->createMock(ConfigManager
::class);
194 $this->container
->conf
->method('get')->with('security.open_shaarli')->willReturn(true);
196 $request = $this->createMock(Request
::class);
197 $response = new Response();
199 $this->expectException(OpenShaarliPasswordException
::class);
201 $this->controller
->change($request, $response);