diff options
Diffstat (limited to 'tests/front/controller/admin/PasswordControllerTest.php')
-rw-r--r-- | tests/front/controller/admin/PasswordControllerTest.php | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/tests/front/controller/admin/PasswordControllerTest.php b/tests/front/controller/admin/PasswordControllerTest.php new file mode 100644 index 00000000..7262243e --- /dev/null +++ b/tests/front/controller/admin/PasswordControllerTest.php | |||
@@ -0,0 +1,186 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
7 | use PHPUnit\Framework\TestCase; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Shaarli\Front\Exception\WrongTokenException; | ||
10 | use Shaarli\Security\SessionManager; | ||
11 | use Slim\Http\Request; | ||
12 | use Slim\Http\Response; | ||
13 | |||
14 | class PasswordControllerTest extends TestCase | ||
15 | { | ||
16 | use FrontAdminControllerMockHelper; | ||
17 | |||
18 | /** @var PasswordController */ | ||
19 | protected $controller; | ||
20 | |||
21 | /** @var mixed[] Variables assigned to the template */ | ||
22 | protected $assignedVariables = []; | ||
23 | |||
24 | public function setUp(): void | ||
25 | { | ||
26 | $this->createContainer(); | ||
27 | $this->assignTemplateVars($this->assignedVariables); | ||
28 | |||
29 | $this->controller = new PasswordController($this->container); | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * Test displaying the change password page. | ||
34 | */ | ||
35 | public function testGetPage(): void | ||
36 | { | ||
37 | $request = $this->createMock(Request::class); | ||
38 | $response = new Response(); | ||
39 | |||
40 | $result = $this->controller->index($request, $response); | ||
41 | |||
42 | static::assertSame(200, $result->getStatusCode()); | ||
43 | static::assertSame('changepassword', (string) $result->getBody()); | ||
44 | static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']); | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * Change the password with valid parameters | ||
49 | */ | ||
50 | public function testPostNewPasswordDefault(): void | ||
51 | { | ||
52 | $request = $this->createMock(Request::class); | ||
53 | $request->method('getParam')->willReturnCallback(function (string $key): string { | ||
54 | if ('oldpassword' === $key) { | ||
55 | return 'old'; | ||
56 | } | ||
57 | if ('setpassword' === $key) { | ||
58 | return 'new'; | ||
59 | } | ||
60 | |||
61 | return $key; | ||
62 | }); | ||
63 | $response = new Response(); | ||
64 | |||
65 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
66 | $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) { | ||
67 | if ('credentials.hash' === $key) { | ||
68 | return sha1('old' . 'credentials.login' . 'credentials.salt'); | ||
69 | } | ||
70 | |||
71 | return strpos($key, 'credentials') !== false ? $key : $default; | ||
72 | }); | ||
73 | $this->container->conf->expects(static::once())->method('write')->with(true); | ||
74 | |||
75 | $this->container->conf | ||
76 | ->method('set') | ||
77 | ->willReturnCallback(function (string $key, string $value) { | ||
78 | if ('credentials.hash' === $key) { | ||
79 | static::assertSame(sha1('new' . 'credentials.login' . 'credentials.salt'), $value); | ||
80 | } | ||
81 | }) | ||
82 | ; | ||
83 | |||
84 | $result = $this->controller->change($request, $response); | ||
85 | |||
86 | static::assertSame(200, $result->getStatusCode()); | ||
87 | static::assertSame('changepassword', (string) $result->getBody()); | ||
88 | static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']); | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * Change the password with a wrong existing password | ||
93 | */ | ||
94 | public function testPostNewPasswordWrongOldPassword(): void | ||
95 | { | ||
96 | $request = $this->createMock(Request::class); | ||
97 | $request->method('getParam')->willReturnCallback(function (string $key): string { | ||
98 | if ('oldpassword' === $key) { | ||
99 | return 'wrong'; | ||
100 | } | ||
101 | if ('setpassword' === $key) { | ||
102 | return 'new'; | ||
103 | } | ||
104 | |||
105 | return $key; | ||
106 | }); | ||
107 | $response = new Response(); | ||
108 | |||
109 | $this->container->conf = $this->createMock(ConfigManager::class); | ||
110 | $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) { | ||
111 | if ('credentials.hash' === $key) { | ||
112 | return sha1('old' . 'credentials.login' . 'credentials.salt'); | ||
113 | } | ||
114 | |||
115 | return strpos($key, 'credentials') !== false ? $key : $default; | ||
116 | }); | ||
117 | |||
118 | $this->container->conf->expects(static::never())->method('set'); | ||
119 | $this->container->conf->expects(static::never())->method('write'); | ||
120 | |||
121 | $this->container->sessionManager | ||
122 | ->expects(static::once()) | ||
123 | ->method('setSessionParameter') | ||
124 | ->with(SessionManager::KEY_ERROR_MESSAGES, ['The old password is not correct.']) | ||
125 | ; | ||
126 | |||
127 | $result = $this->controller->change($request, $response); | ||
128 | |||
129 | static::assertSame(400, $result->getStatusCode()); | ||
130 | static::assertSame('changepassword', (string) $result->getBody()); | ||
131 | static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Change the password with a wrong existing password | ||
136 | */ | ||
137 | public function testPostNewPasswordWrongToken(): void | ||
138 | { | ||
139 | $this->container->sessionManager = $this->createMock(SessionManager::class); | ||
140 | $this->container->sessionManager->method('checkToken')->willReturn(false); | ||
141 | |||
142 | $this->container->conf->expects(static::never())->method('set'); | ||
143 | $this->container->conf->expects(static::never())->method('write'); | ||
144 | |||
145 | $request = $this->createMock(Request::class); | ||
146 | $response = new Response(); | ||
147 | |||
148 | $this->expectException(WrongTokenException::class); | ||
149 | |||
150 | $this->controller->change($request, $response); | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * Change the password with an empty new password | ||
155 | */ | ||
156 | public function testPostNewEmptyPassword(): void | ||
157 | { | ||
158 | $this->container->sessionManager | ||
159 | ->expects(static::once()) | ||
160 | ->method('setSessionParameter') | ||
161 | ->with(SessionManager::KEY_ERROR_MESSAGES, ['You must provide the current and new password to change it.']) | ||
162 | ; | ||
163 | |||
164 | $this->container->conf->expects(static::never())->method('set'); | ||
165 | $this->container->conf->expects(static::never())->method('write'); | ||
166 | |||
167 | $request = $this->createMock(Request::class); | ||
168 | $request->method('getParam')->willReturnCallback(function (string $key): string { | ||
169 | if ('oldpassword' === $key) { | ||
170 | return 'old'; | ||
171 | } | ||
172 | if ('setpassword' === $key) { | ||
173 | return ''; | ||
174 | } | ||
175 | |||
176 | return $key; | ||
177 | }); | ||
178 | $response = new Response(); | ||
179 | |||
180 | $result = $this->controller->change($request, $response); | ||
181 | |||
182 | static::assertSame(400, $result->getStatusCode()); | ||
183 | static::assertSame('changepassword', (string) $result->getBody()); | ||
184 | static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']); | ||
185 | } | ||
186 | } | ||