From 430578f184a6dae5038b77d0d12d8426000cc5a5 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Tue, 18 Oct 2022 15:26:21 +0200 Subject: [PATCH] Fixed: Require Mailman settings to be filled --- app/Mailman/Actions/SettingSaveAction.php | 20 +++++++++++++++++++- tests/Feature/Mailman/SettingTest.php | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/Mailman/Actions/SettingSaveAction.php b/app/Mailman/Actions/SettingSaveAction.php index 96d07761..0ec3d620 100644 --- a/app/Mailman/Actions/SettingSaveAction.php +++ b/app/Mailman/Actions/SettingSaveAction.php @@ -29,9 +29,27 @@ class SettingSaveAction $settings->save(); } + /** + * @return array + */ + public function rules(): array + { + return [ + 'base_url' => 'required', + 'username' => 'required', + 'password' => 'required', + ]; + } + public function afterValidator(Validator $validator, ActionRequest $request): void { - if (!app(MailmanService::class)->setCredentials($request->input('base_url'), $request->input('username'), $request->input('password'))->check()) { + if (!$request->filled(['base_url', 'username', 'password'])) { + return; + } + + $result = app(MailmanService::class)->setCredentials($request->input('base_url'), $request->input('username'), $request->input('password'))->check(); + + if (!$result) { $validator->errors()->add('mailman', 'Verbindung fehlgeschlagen.'); } } diff --git a/tests/Feature/Mailman/SettingTest.php b/tests/Feature/Mailman/SettingTest.php index 7b6c9da5..c38eca0b 100644 --- a/tests/Feature/Mailman/SettingTest.php +++ b/tests/Feature/Mailman/SettingTest.php @@ -72,4 +72,19 @@ class SettingTest extends TestCase Phake::verify(app(MailmanService::class))->setCredentials('http://mailman.test/api', 'user', 'secret'); Phake::verify(app(MailmanService::class))->check(); } + + public function testItValidatesPassword(): void + { + $this->stubIo(MailmanService::class, fn ($mock) => $mock); + $this->login()->loginNami(); + + $response = $this->from('/setting/mailman')->post('/setting/mailman', [ + 'base_url' => 'http://mailman.test/api', + 'username' => 'user', + 'password' => '', + ]); + + $response->assertSessionHasErrors(['password' => 'Passwort ist erforderlich.']); + Phake::verifyNoInteraction(app(MailmanService::class)); + } }