aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/admin/PasswordControllerTest.php
blob: 9a01089e2ddecd3747e54a968b846e35c091bdaa (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller\Admin;

use PHPUnit\Framework\TestCase;
use Shaarli\Config\ConfigManager;
use Shaarli\Front\Exception\OpenShaarliPasswordException;
use Shaarli\Front\Exception\WrongTokenException;
use Shaarli\Security\SessionManager;
use Slim\Http\Request;
use Slim\Http\Response;

class PasswordControllerTest extends TestCase
{
    use FrontAdminControllerMockHelper;

    /** @var PasswordController */
    protected $controller;

    /** @var mixed[] Variables assigned to the template */
    protected $assignedVariables = [];

    public function setUp(): void
    {
        $this->createContainer();
        $this->assignTemplateVars($this->assignedVariables);

        $this->controller = new PasswordController($this->container);
    }

    /**
     * Test displaying the change password page.
     */
    public function testGetPage(): void
    {
        $request = $this->createMock(Request::class);
        $response = new Response();

        $result = $this->controller->index($request, $response);

        static::assertSame(200, $result->getStatusCode());
        static::assertSame('changepassword', (string) $result->getBody());
        static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']);
    }

    /**
     * Change the password with valid parameters
     */
    public function testPostNewPasswordDefault(): void
    {
        $request = $this->createMock(Request::class);
        $request->method('getParam')->willReturnCallback(function (string $key): string {
             if ('oldpassword' === $key) {
                 return 'old';
             }
             if ('setpassword' === $key) {
                 return 'new';
             }

             return $key;
        });
        $response = new Response();

        $this->container->conf = $this->createMock(ConfigManager::class);
        $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) {
            if ('credentials.hash' === $key) {
                return sha1('old' . 'credentials.login' . 'credentials.salt');
            }

            return strpos($key, 'credentials') !== false ? $key : $default;
        });
        $this->container->conf->expects(static::once())->method('write')->with(true);

        $this->container->conf
            ->method('set')
            ->willReturnCallback(function (string $key, string $value) {
                if ('credentials.hash' === $key) {
                    static::assertSame(sha1('new' . 'credentials.login' . 'credentials.salt'), $value);
                }
            })
        ;

        $result = $this->controller->change($request, $response);

        static::assertSame(200, $result->getStatusCode());
        static::assertSame('changepassword', (string) $result->getBody());
        static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']);
    }

    /**
     * Change the password with a wrong existing password
     */
    public function testPostNewPasswordWrongOldPassword(): void
    {
        $request = $this->createMock(Request::class);
        $request->method('getParam')->willReturnCallback(function (string $key): string {
            if ('oldpassword' === $key) {
                return 'wrong';
            }
            if ('setpassword' === $key) {
                return 'new';
            }

            return $key;
        });
        $response = new Response();

        $this->container->conf = $this->createMock(ConfigManager::class);
        $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) {
            if ('credentials.hash' === $key) {
                return sha1('old' . 'credentials.login' . 'credentials.salt');
            }

            return strpos($key, 'credentials') !== false ? $key : $default;
        });

        $this->container->conf->expects(static::never())->method('set');
        $this->container->conf->expects(static::never())->method('write');

        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_ERROR_MESSAGES, ['The old password is not correct.'])
        ;

        $result = $this->controller->change($request, $response);

        static::assertSame(400, $result->getStatusCode());
        static::assertSame('changepassword', (string) $result->getBody());
        static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']);
    }

    /**
     * Change the password with a wrong existing password
     */
    public function testPostNewPasswordWrongToken(): void
    {
        $this->container->sessionManager = $this->createMock(SessionManager::class);
        $this->container->sessionManager->method('checkToken')->willReturn(false);

        $this->container->conf->expects(static::never())->method('set');
        $this->container->conf->expects(static::never())->method('write');

        $request = $this->createMock(Request::class);
        $response = new Response();

        $this->expectException(WrongTokenException::class);

        $this->controller->change($request, $response);
    }

    /**
     * Change the password with an empty new password
     */
    public function testPostNewEmptyPassword(): void
    {
        $this->container->sessionManager
            ->expects(static::once())
            ->method('setSessionParameter')
            ->with(SessionManager::KEY_ERROR_MESSAGES, ['You must provide the current and new password to change it.'])
        ;

        $this->container->conf->expects(static::never())->method('set');
        $this->container->conf->expects(static::never())->method('write');

        $request = $this->createMock(Request::class);
        $request->method('getParam')->willReturnCallback(function (string $key): string {
            if ('oldpassword' === $key) {
                return 'old';
            }
            if ('setpassword' === $key) {
                return '';
            }

            return $key;
        });
        $response = new Response();

        $result = $this->controller->change($request, $response);

        static::assertSame(400, $result->getStatusCode());
        static::assertSame('changepassword', (string) $result->getBody());
        static::assertSame('Change password - Shaarli', $this->assignedVariables['pagetitle']);
    }

    /**
     * Change the password on an open shaarli
     */
    public function testPostNewPasswordOnOpenShaarli(): void
    {
        $this->container->conf = $this->createMock(ConfigManager::class);
        $this->container->conf->method('get')->with('security.open_shaarli')->willReturn(true);

        $request = $this->createMock(Request::class);
        $response = new Response();

        $this->expectException(OpenShaarliPasswordException::class);

        $this->controller->change($request, $response);
    }
}