|
35 | 35 | namespace tests\units\Glpi\Api\HL\Controller; |
36 | 36 |
|
37 | 37 | use AuthLDAP; |
| 38 | +use Config; |
38 | 39 | use Glpi\Api\HL\Middleware\InternalAuthMiddleware; |
39 | 40 | use Glpi\Http\Request; |
40 | 41 |
|
@@ -156,4 +157,173 @@ public function testCRUDNoRights() |
156 | 157 | }); |
157 | 158 | }); |
158 | 159 | } |
| 160 | + |
| 161 | + public function testCRUDConfigValues() |
| 162 | + { |
| 163 | + $this->loginWeb(); |
| 164 | + |
| 165 | + $this->api->getRouter()->registerAuthMiddleware(new InternalAuthMiddleware()); |
| 166 | + // Can get a config value |
| 167 | + $this->api->call(new Request('GET', '/Setup/Config/core/priority_1'), function ($call) { |
| 168 | + /** @var \HLAPICallAsserter $call */ |
| 169 | + $call->response |
| 170 | + ->isOK() |
| 171 | + ->jsonContent(function ($content) { |
| 172 | + $this->assertEquals('priority_1', $content['name']); |
| 173 | + $this->assertEquals('core', $content['context']); |
| 174 | + $this->assertEquals('#fff2f2', $content['value']); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + // Get an undisclosable config value |
| 179 | + Config::setConfigurationValues('core', ['smtp_passwd' => 'test']); |
| 180 | + $this->api->call(new Request('GET', '/Setup/Config/core/smtp_passwd'), function ($call) { |
| 181 | + /** @var \HLAPICallAsserter $call */ |
| 182 | + $call->response->isAccessDenied(); |
| 183 | + }); |
| 184 | + |
| 185 | + // Not existing config value |
| 186 | + $this->api->call(new Request('GET', '/Setup/Config/core/notrealconfig'), function ($call) { |
| 187 | + /** @var \HLAPICallAsserter $call */ |
| 188 | + $call->response->isNotFoundError(); |
| 189 | + }); |
| 190 | + |
| 191 | + // Can update a config value |
| 192 | + $request = new Request('PATCH', '/Setup/Config/core/priority_1'); |
| 193 | + $request->setParameter('value', '#ffffff'); |
| 194 | + $this->api->call($request, function ($call) { |
| 195 | + /** @var \HLAPICallAsserter $call */ |
| 196 | + $call->response |
| 197 | + ->isOK() |
| 198 | + ->jsonContent(function ($content) { |
| 199 | + $this->assertEquals('priority_1', $content['name']); |
| 200 | + $this->assertEquals('core', $content['context']); |
| 201 | + $this->assertEquals('#ffffff', $content['value']); |
| 202 | + }); |
| 203 | + }); |
| 204 | + $this->api->call(new Request('GET', '/Setup/Config/core/priority_1'), function ($call) { |
| 205 | + /** @var \HLAPICallAsserter $call */ |
| 206 | + $call->response |
| 207 | + ->isOK() |
| 208 | + ->jsonContent(function ($content) { |
| 209 | + $this->assertEquals('priority_1', $content['name']); |
| 210 | + $this->assertEquals('core', $content['context']); |
| 211 | + $this->assertEquals('#ffffff', $content['value']); |
| 212 | + }); |
| 213 | + }); |
| 214 | + |
| 215 | + // Can update an undisclosable config value |
| 216 | + $request = new Request('PATCH', '/Setup/Config/core/smtp_passwd'); |
| 217 | + $request->setParameter('value', 'newtest'); |
| 218 | + $this->api->call($request, function ($call) { |
| 219 | + /** @var \HLAPICallAsserter $call */ |
| 220 | + $call->response |
| 221 | + ->status(static fn($status) => $status === 204); |
| 222 | + }); |
| 223 | + |
| 224 | + // Can delete a config value |
| 225 | + $this->api->call(new Request('DELETE', '/Setup/Config/core/priority_1'), function ($call) { |
| 226 | + /** @var \HLAPICallAsserter $call */ |
| 227 | + $call->response |
| 228 | + ->status(static fn($status) => $status === 204); |
| 229 | + }); |
| 230 | + $this->api->call(new Request('GET', '/Setup/Config/core/priority_1'), function ($call) { |
| 231 | + /** @var \HLAPICallAsserter $call */ |
| 232 | + $call->response->isNotFoundError(); |
| 233 | + }); |
| 234 | + |
| 235 | + // Can delete an undisclosable config value |
| 236 | + $this->api->call(new Request('DELETE', '/Setup/Config/core/smtp_passwd'), function ($call) { |
| 237 | + /** @var \HLAPICallAsserter $call */ |
| 238 | + $call->response |
| 239 | + ->status(static fn($status) => $status === 204); |
| 240 | + }); |
| 241 | + |
| 242 | + // Can get a config value using GraphQL |
| 243 | + $request = new Request('POST', '/GraphQL', [], 'query { Config(filter: "context==core;name==priority_2") { context, name, value } }'); |
| 244 | + $this->api->call($request, function ($call) { |
| 245 | + /** @var \HLAPICallAsserter $call */ |
| 246 | + $call->response |
| 247 | + ->isOK() |
| 248 | + ->jsonContent(function ($content) { |
| 249 | + $this->assertArrayHasKey('data', $content); |
| 250 | + $this->assertArrayHasKey('Config', $content['data']); |
| 251 | + $this->assertCount(1, $content['data']['Config']); |
| 252 | + $config = $content['data']['Config'][0]; |
| 253 | + $this->assertEquals('core', $config['context']); |
| 254 | + $this->assertEquals('priority_2', $config['name']); |
| 255 | + $this->assertEquals('#ffe0e0', $config['value']); |
| 256 | + }); |
| 257 | + }); |
| 258 | + |
| 259 | + // Cannot get an undisclosable config value using GraphQL |
| 260 | + $request = new Request('POST', '/GraphQL', [], 'query { Config(filter: "context==core;name==smtp_passwd") { context, name, value } }'); |
| 261 | + $this->api->call($request, function ($call) { |
| 262 | + /** @var \HLAPICallAsserter $call */ |
| 263 | + $call->response |
| 264 | + ->isOK() |
| 265 | + ->jsonContent(function ($content) { |
| 266 | + $this->assertArrayHasKey('data', $content); |
| 267 | + $this->assertArrayHasKey('Config', $content['data']); |
| 268 | + $this->assertEmpty($content['data']['Config']); |
| 269 | + }); |
| 270 | + }); |
| 271 | + |
| 272 | + // Can search config values |
| 273 | + $request = new Request('GET', '/Setup/Config'); |
| 274 | + $request->setParameter('filter', 'name==priority_2'); |
| 275 | + $this->api->call($request, function ($call) { |
| 276 | + /** @var \HLAPICallAsserter $call */ |
| 277 | + $call->response |
| 278 | + ->isOK() |
| 279 | + ->jsonContent(function ($content) { |
| 280 | + $this->assertCount(1, $content); |
| 281 | + $config = $content[0]; |
| 282 | + $this->assertEquals('core', $config['context']); |
| 283 | + $this->assertEquals('priority_2', $config['name']); |
| 284 | + $this->assertEquals('#ffe0e0', $config['value']); |
| 285 | + }); |
| 286 | + }); |
| 287 | + |
| 288 | + // Cannot search undisclosable config values |
| 289 | + $request = new Request('GET', '/Setup/Config'); |
| 290 | + $request->setParameter('filter', 'name==smtp_passwd'); |
| 291 | + $this->api->call($request, function ($call) { |
| 292 | + /** @var \HLAPICallAsserter $call */ |
| 293 | + $call->response |
| 294 | + ->isOK() |
| 295 | + ->jsonContent(function ($content) { |
| 296 | + $this->assertEmpty($content); |
| 297 | + }); |
| 298 | + }); |
| 299 | + } |
| 300 | + |
| 301 | + public function testConfigNotIn2_0() |
| 302 | + { |
| 303 | + $this->login(); |
| 304 | + |
| 305 | + $v2_api = $this->api->withVersion('2.0.0'); |
| 306 | + $v2_api->call(new Request('GET', '/Setup/Config/core/test'), function ($call) { |
| 307 | + /** @var \HLAPICallAsserter $call */ |
| 308 | + $call->response->isNotFoundError(); |
| 309 | + }); |
| 310 | + $v2_api->call(new Request('PATCH', '/Setup/Config/core/test'), function ($call) { |
| 311 | + /** @var \HLAPICallAsserter $call */ |
| 312 | + $call->response->isNotFoundError(); |
| 313 | + }); |
| 314 | + $v2_api->call(new Request('DELETE', '/Setup/Config/core/test'), function ($call) { |
| 315 | + /** @var \HLAPICallAsserter $call */ |
| 316 | + $call->response->isNotFoundError(); |
| 317 | + }); |
| 318 | + |
| 319 | + $request = new Request('POST', '/GraphQL', [], 'query { Config(filter: "context==core;name==test") { context, name, value } }'); |
| 320 | + $v2_api->call($request, function ($call) { |
| 321 | + /** @var \HLAPICallAsserter $call */ |
| 322 | + $call->response |
| 323 | + ->isOK() |
| 324 | + ->jsonContent(function ($content) { |
| 325 | + $this->assertArrayHasKey('errors', $content); |
| 326 | + }); |
| 327 | + }); |
| 328 | + } |
159 | 329 | } |
0 commit comments