Add integration tests for settings API endpoint

This commit is contained in:
Alexander Skvortsov 2021-11-16 16:48:09 -05:00
parent 6d4c7d15a6
commit 28d6471877
2 changed files with 87 additions and 8 deletions

View File

@ -13,11 +13,6 @@ use Flarum\Foundation\AbstractValidator;
class SettingsValidator extends AbstractValidator
{
/**
* @var array
*/
protected $rules = [];
/**
* These rules apply to all attributes.
*
@ -45,9 +40,7 @@ class SettingsValidator extends AbstractValidator
// Apply attribute specific rules.
foreach ($rules as $key => $value) {
if (array_key_exists($key, $this->rules)) {
$rules[$key] = array_merge($rules[$key], $this->rules[$key]);
}
$rules[$key] = array_merge($rules[$key], $this->rules[$key] ?? []);
}
$validator = $this->validator->make($attributes, $rules, $this->getMessages());

View File

@ -0,0 +1,86 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\api\settings;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
class SetTest extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->prepareDatabase([
'users' => [
$this->normalUser(),
],
]);
}
/**
* @test
*/
public function settings_cant_be_updated_by_user()
{
$response = $this->send(
$this->request('POST', '/api/settings', [
'authenticatedAs' => 2,
'json' => [
'hello' => 'world',
],
])
);
// Test for successful response and that the email is included in the response
$this->assertEquals(200, $response->getStatusCode());
}
/**
* @test
*/
public function settings_can_be_updated_by_admin()
{
$response = $this->send(
$this->request('POST', '/api/settings', [
'authenticatedAs' => 1,
'json' => [
'hello' => 'world',
],
])
);
// Test for successful response and that the email is included in the response
$this->assertEquals(200, $response->getStatusCode());
}
/**
* @test
*/
public function max_setting_length_validated()
{
$response = $this->send(
$this->request('POST', '/api/settings', [
'authenticatedAs' => 1,
'json' => [
'hello' => str_repeat('a', 66000),
],
])
);
// Test for successful response and that the email is included in the response
$this->assertEquals(422, $response->getStatusCode());
}
}