-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathChangePasswordTest.php
More file actions
94 lines (72 loc) · 2.72 KB
/
Copy pathChangePasswordTest.php
File metadata and controls
94 lines (72 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/*
* Copyright (c) 2026 Netresearch DTT GmbH
* SPDX-License-Identifier: AGPL-3.0-only
*/
declare(strict_types=1);
namespace Tests\Controller;
use Symfony\Component\HttpFoundation\Request;
use Tests\AbstractWebTestCase;
use Tests\Traits\LocalPasswordTestTrait;
use function is_string;
use function password_hash;
use function password_verify;
use const PASSWORD_DEFAULT;
/**
* Self-service password change (ADR-018 D2): only local accounts, current
* password re-verified, minimum length enforced.
*
* @internal
*
* @coversNothing
*/
final class ChangePasswordTest extends AbstractWebTestCase
{
use LocalPasswordTestTrait;
public function testLdapAccountCannotChangePassword(): void
{
$this->setStoredPassword(1, null); // LDAP account: no local password
$this->logInSession('unittest');
$this->post(['currentPassword' => 'anything', 'newPassword' => 'newpass12']);
$this->assertStatusCode(403);
}
public function testLocalAccountChangesPasswordWithCorrectCurrent(): void
{
$this->setStoredPassword(1, password_hash('oldpass12', PASSWORD_DEFAULT));
$this->logInSession('unittest');
$this->post(['currentPassword' => 'oldpass12', 'newPassword' => 'brandnew34']);
$this->assertStatusCode(200);
$stored = $this->storedPassword(1);
self::assertNotNull($stored);
self::assertTrue(password_verify('brandnew34', $stored), 'the new password is stored');
self::assertFalse(password_verify('oldpass12', $stored), 'the old password no longer works');
}
public function testWrongCurrentPasswordIsRejected(): void
{
$this->setStoredPassword(1, password_hash('oldpass12', PASSWORD_DEFAULT));
$this->logInSession('unittest');
$this->post(['currentPassword' => 'WRONG', 'newPassword' => 'brandnew34']);
$this->assertStatusCode(422);
}
public function testTooShortNewPasswordIsRejected(): void
{
$this->setStoredPassword(1, password_hash('oldpass12', PASSWORD_DEFAULT));
$this->logInSession('unittest');
$this->post(['currentPassword' => 'oldpass12', 'newPassword' => 'short']);
$this->assertStatusCode(422);
}
/**
* @param array<string, string> $body
*/
private function post(array $body): void
{
$this->client->request(Request::METHOD_POST, '/settings/password', $body, [], ['HTTP_ACCEPT' => 'application/json']);
}
private function storedPassword(int $userId): ?string
{
$connection = $this->connection;
self::assertNotNull($connection);
$value = $connection->fetchOne('SELECT password FROM users WHERE id = ?', [$userId]);
return is_string($value) ? $value : null;
}
}