1<?php
2
3namespace dokuwiki\plugin\usermanager\test;
4
5use dokuwiki\Remote\AccessDeniedException;
6use dokuwiki\Remote\Api;
7use dokuwiki\Remote\RemoteException;
8use DokuWikiTest;
9
10/**
11 * Remote API tests for the usermanager plugin
12 *
13 * @group plugin_usermanager
14 * @group plugins
15 */
16class RemoteApiTest extends DokuWikiTest
17{
18    /** @var  Api */
19    protected $remote;
20
21    public function __construct()
22    {
23        parent::__construct();
24        $this->remote = new Api();
25    }
26
27    public function setUp(): void
28    {
29        parent::setUp();
30
31        global $conf;
32        $conf['remote'] = 1;
33        $conf['useacl'] = 1;
34        $conf['remoteuser'] = 'umtestuser, admin';
35        $conf['superuser'] = 'admin';
36        $_SERVER['REMOTE_USER'] = '';
37    }
38
39    public function testCreateUserSuccess()
40    {
41        global $auth;
42        $auth = new AuthPlugin();
43
44        $params = [
45            'user' => 'user1',
46            'password' => 'password1',
47            'name' => 'user one',
48            'mail' => 'user1@localhost',
49            'groups' => [
50                'user',
51                'test'
52            ],
53            'notify' => false
54        ];
55
56        $_SERVER['REMOTE_USER'] = 'admin';
57        $this->assertTrue(
58            $this->remote->call('plugin.usermanager.createUser', $params)
59        );
60        $this->assertArrayHasKey('user1', $auth->users);
61
62        // try again should fail, because user already exists
63        $this->assertFalse(
64            $this->remote->call('plugin.usermanager.createUser', $params)
65        );
66    }
67
68    public function testCreateUserFailAccess()
69    {
70        global $auth;
71        $auth = new AuthPlugin();
72
73        $params = [
74            'user' => 'user1',
75            'password' => 'password1',
76            'name' => 'user one',
77            'mail' => 'user1@localhost',
78            'groups' => [
79                'user',
80                'test'
81            ],
82            'notify' => false
83        ];
84
85        $_SERVER['REMOTE_USER'] = 'umtestuser';
86
87        $this->expectException(AccessDeniedException::class);
88        $this->expectExceptionCode(114);
89        $this->remote->call('plugin.usermanager.createUser', $params);
90    }
91
92    public function testCreateUserFailMissingUser()
93    {
94        global $auth;
95        $auth = new AuthPlugin();
96
97        $params = [
98            'user' => '',
99            'password' => 'password1',
100            'name' => 'user one',
101            'mail' => 'user1@localhost',
102            'groups' => [
103                'user',
104                'test'
105            ],
106            'notify' => false
107        ];
108
109        $_SERVER['REMOTE_USER'] = 'admin';
110
111        $this->expectException(RemoteException::class);
112        $this->expectExceptionCode(401);
113        $this->remote->call('plugin.usermanager.createUser', $params);
114    }
115
116    public function testCreateUserFailMissingName()
117    {
118        global $auth;
119        $auth = new AuthPlugin();
120
121        $params = [
122            'user' => 'user1',
123            'password' => 'password1',
124            'name' => '',
125            'mail' => 'user1@localhost',
126            'groups' => [
127                'user',
128                'test'
129            ],
130            'notify' => false
131        ];
132
133        $_SERVER['REMOTE_USER'] = 'admin';
134
135        $this->expectException(RemoteException::class);
136        $this->expectExceptionCode(402);
137        $this->remote->call('plugin.usermanager.createUser', $params);
138    }
139
140    public function testCreateUserFailBadEmail()
141    {
142        global $auth;
143        $auth = new AuthPlugin();
144
145        $params = [
146            'user' => 'user1',
147            'password' => 'password1',
148            'name' => 'user one',
149            'mail' => 'This is not an email',
150            'groups' => [
151                'user',
152                'test'
153            ],
154            'notify' => false
155        ];
156
157        $_SERVER['REMOTE_USER'] = 'admin';
158
159        $this->expectException(RemoteException::class);
160        $this->expectExceptionCode(403);
161        $this->remote->call('plugin.usermanager.createUser', $params);
162    }
163
164    public function testCreateUserFailAuthCapability()
165    {
166        global $auth;
167        $auth = new AuthPlugin(['addUser' => false]);
168
169        $params = [
170            'user' => 'user1',
171            'password' => 'password1',
172            'name' => 'user one',
173            'mail' => 'user1@localhost',
174            'groups' => [
175                'user',
176                'test'
177            ],
178            'notify' => false
179        ];
180
181        $_SERVER['REMOTE_USER'] = 'admin';
182
183        $this->expectException(AccessDeniedException::class);
184        $this->expectExceptionCode(404);
185        $this->expectExceptionMessageMatches('/can\'t do addUser/');
186        $this->remote->call('plugin.usermanager.createUser', $params);
187    }
188
189    public function testDeleteUserSuccess()
190    {
191        global $auth;
192        $auth = new AuthPlugin();
193        $auth->users = [
194            'user1' => [
195                'pass' => 'password1',
196                'name' => 'user one',
197                'mail' => 'user1@localhost',
198                'grps' => [
199                    'user',
200                    'test'
201                ]
202            ],
203            'user2' => [
204                'pass' => 'password2',
205                'name' => 'user two',
206                'mail' => 'user2@localhost',
207                'grps' => [
208                    'user',
209                    'test'
210                ]
211            ],
212        ];
213
214        $_SERVER['REMOTE_USER'] = 'admin';
215
216        $this->assertTrue($this->remote->call('plugin.usermanager.deleteUser', ['user' => 'user1']));
217        $this->assertArrayNotHasKey('user1', $auth->users);
218        $this->assertArrayHasKey('user2', $auth->users);
219    }
220
221    public function testDeleteUserFailAccess()
222    {
223        global $auth;
224        $auth = new AuthPlugin();
225
226        $_SERVER['REMOTE_USER'] = 'umtestuser';
227
228        $this->expectException(AccessDeniedException::class);
229        $this->expectExceptionCode(114);
230        $this->remote->call('plugin.usermanager.deleteUser', ['user' => 'user1']);
231    }
232
233
234    public function testDeleteUserFailNoExist()
235    {
236        global $auth;
237        $auth = new AuthPlugin();
238
239        $_SERVER['REMOTE_USER'] = 'admin';
240
241        $this->assertFalse($this->remote->call('plugin.usermanager.deleteUser', ['user' => 'user1']));
242    }
243
244    public function testDeleteUserFailAuthCapability()
245    {
246        global $auth;
247        $auth = new AuthPlugin(['delUser' => false]);
248
249        $_SERVER['REMOTE_USER'] = 'admin';
250
251        $this->expectException(AccessDeniedException::class);
252        $this->expectExceptionCode(404);
253        $this->expectExceptionMessageMatches('/can\'t do delUser/');
254        $this->remote->call('plugin.usermanager.deleteUser', ['user' => 'user1']);
255    }
256}
257