From 28d6471877f8b10c16faf4304caf423705ba4316 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Tue, 16 Nov 2021 16:48:09 -0500 Subject: [PATCH] Add integration tests for settings API endpoint --- .../core/src/Settings/SettingsValidator.php | 9 +- .../integration/api/settings/SetTest.php | 86 +++++++++++++++++++ 2 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 framework/core/tests/integration/api/settings/SetTest.php diff --git a/framework/core/src/Settings/SettingsValidator.php b/framework/core/src/Settings/SettingsValidator.php index cf73aa5be..f967cf40f 100644 --- a/framework/core/src/Settings/SettingsValidator.php +++ b/framework/core/src/Settings/SettingsValidator.php @@ -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()); diff --git a/framework/core/tests/integration/api/settings/SetTest.php b/framework/core/tests/integration/api/settings/SetTest.php new file mode 100644 index 000000000..3ecbfbeb0 --- /dev/null +++ b/framework/core/tests/integration/api/settings/SetTest.php @@ -0,0 +1,86 @@ +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()); + } +}