From c593096bf9533b24fc767b3f720c1f27dfa7c004 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 20 Oct 2022 03:22:50 +0200 Subject: [PATCH] Add is_active setting for mailman --- app/Mailman/Actions/SettingIndexAction.php | 7 +++ app/Mailman/Actions/SettingSaveAction.php | 18 +++++-- app/Mailman/MailmanSettings.php | 2 + .../2022_10_18_123918_mailman_is_active.php | 11 ++++ resources/img/svg/disabled.svg | 1 + resources/js/views/setting/Mailman.vue | 42 ++++++++++++++- tests/Feature/Mailman/SettingTest.php | 53 ++++++++++++++++++- 7 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 database/settings/2022_10_18_123918_mailman_is_active.php create mode 100644 resources/img/svg/disabled.svg diff --git a/app/Mailman/Actions/SettingIndexAction.php b/app/Mailman/Actions/SettingIndexAction.php index 51b6d2ed..ccd35409 100644 --- a/app/Mailman/Actions/SettingIndexAction.php +++ b/app/Mailman/Actions/SettingIndexAction.php @@ -3,6 +3,7 @@ namespace App\Mailman\Actions; use App\Mailman\MailmanSettings; +use App\Mailman\Support\MailmanService; use Inertia\Inertia; use Inertia\Response; use Lorisleiva\Actions\Concerns\AsAction; @@ -17,6 +18,7 @@ class SettingIndexAction public function handle(MailmanSettings $settings): array { return [ + 'is_active' => $settings->is_active, 'base_url' => $settings->base_url, 'username' => $settings->username, 'password' => '', @@ -28,8 +30,13 @@ class SettingIndexAction session()->put('menu', 'setting'); session()->put('title', 'Mailman-Einstellungen'); + $state = $settings->base_url && $settings->username && $settings->password && $settings->is_active + ? app(MailmanService::class)->setCredentials($settings->base_url, $settings->username, $settings->password)->check() + : null; + return Inertia::render('setting/Mailman', [ 'data' => $this->handle($settings), + 'state' => $state, ]); } } diff --git a/app/Mailman/Actions/SettingSaveAction.php b/app/Mailman/Actions/SettingSaveAction.php index 0ec3d620..f4b33cd9 100644 --- a/app/Mailman/Actions/SettingSaveAction.php +++ b/app/Mailman/Actions/SettingSaveAction.php @@ -24,6 +24,7 @@ class SettingSaveAction 'base_url' => $input['base_url'] ?? null, 'username' => $input['username'] ?? null, 'password' => $input['password'] ?? null, + 'is_active' => $input['is_active'] ?? false, ]); $settings->save(); @@ -35,15 +36,24 @@ class SettingSaveAction public function rules(): array { return [ - 'base_url' => 'required', - 'username' => 'required', - 'password' => 'required', + 'base_url' => 'required_if:is_active,true', + 'username' => 'required_if:is_active,true', + 'password' => 'required_if:is_active,true', + ]; + } + + public function getValidationMessages(): array + { + return [ + 'base_url.required_if' => 'URL ist erforderlich.', + 'username.required_if' => 'Benutzername ist erforderlich.', + 'password.required_if' => 'Passwort ist erforderlich.', ]; } public function afterValidator(Validator $validator, ActionRequest $request): void { - if (!$request->filled(['base_url', 'username', 'password'])) { + if (false === $request->is_active || !$request->filled(['base_url', 'username', 'password'])) { return; } diff --git a/app/Mailman/MailmanSettings.php b/app/Mailman/MailmanSettings.php index 6a70995c..a3269a43 100644 --- a/app/Mailman/MailmanSettings.php +++ b/app/Mailman/MailmanSettings.php @@ -14,6 +14,8 @@ class MailmanSettings extends LocalSettings public ?string $password; + public bool $is_active; + public static function group(): string { return 'mailman'; diff --git a/database/settings/2022_10_18_123918_mailman_is_active.php b/database/settings/2022_10_18_123918_mailman_is_active.php new file mode 100644 index 00000000..d6c247a9 --- /dev/null +++ b/database/settings/2022_10_18_123918_mailman_is_active.php @@ -0,0 +1,11 @@ +migrator->add('mailman.is_active', false); + } +} diff --git a/resources/img/svg/disabled.svg b/resources/img/svg/disabled.svg new file mode 100644 index 00000000..54698703 --- /dev/null +++ b/resources/img/svg/disabled.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources/js/views/setting/Mailman.vue b/resources/js/views/setting/Mailman.vue index b7bce7fa..274d0a21 100644 --- a/resources/js/views/setting/Mailman.vue +++ b/resources/js/views/setting/Mailman.vue @@ -10,9 +10,16 @@ Scoutrobot wird nach der Ersteinrichtung deine Mitglieder zu bestehenden E-Mail-Verteilern hinzufügen.

+
+ +
+
+ + +
- +
@@ -34,10 +41,41 @@ export default { }, props: { data: {}, + state: {}, }, + computed: { + stateDisplay() { + if (this.state === null) { + return { + text: 'text-gray-500', + icon: 'disabled', + label: 'Deaktiviert', + }; + } + + return this.state + ? { + text: 'text-green-500', + icon: 'check', + label: 'Verbindung erfolgreich.', + } + : { + text: 'text-red-500', + icon: 'close', + label: 'Verbindung fehlgeschlagen.', + }; + }, + }, + methods: { submit() { - this.$inertia.post('/setting/mailman', this.inner); + var _self = this; + + this.$inertia.post('/setting/mailman', this.inner, { + onSuccess(page) { + _self.inner = page.props.data; + }, + }); }, }, created() { diff --git a/tests/Feature/Mailman/SettingTest.php b/tests/Feature/Mailman/SettingTest.php index c38eca0b..6a685044 100644 --- a/tests/Feature/Mailman/SettingTest.php +++ b/tests/Feature/Mailman/SettingTest.php @@ -15,10 +15,15 @@ class SettingTest extends TestCase public function testItGetsMailSettings(): void { $this->withoutExceptionHandling()->login()->loginNami(); + $this->stubIo(MailmanService::class, function ($mock) { + Phake::when($mock)->setCredentials('http://mailman.test/api', 'user', 'secret')->thenReturn($mock); + Phake::when($mock)->check()->thenReturn(true); + }); MailmanSettings::fake([ 'base_url' => 'http://mailman.test/api', 'username' => 'user', 'password' => 'secret', + 'is_active' => true, ]); $response = $this->get('/setting/mailman'); @@ -28,10 +33,53 @@ class SettingTest extends TestCase 'base_url' => 'http://mailman.test/api', 'username' => 'user', 'password' => '', + 'is_active' => true, ], $response, 'data'); + $this->assertInertiaHas(true, $response, 'state'); } - public function testItSaesMailmanSettings(): void + public function testItReturnsWrongStateWhenLoginFailed(): void + { + $this->withoutExceptionHandling()->login()->loginNami(); + $this->stubIo(MailmanService::class, function ($mock) { + Phake::when($mock)->setCredentials('http://mailman.test/api', 'user', 'secret')->thenReturn($mock); + Phake::when($mock)->check()->thenReturn(false); + }); + MailmanSettings::fake([ + 'base_url' => 'http://mailman.test/api', + 'username' => 'user', + 'password' => 'secret', + 'is_active' => true, + ]); + + $response = $this->get('/setting/mailman'); + + $response->assertOk(); + $this->assertInertiaHas(false, $response, 'state'); + } + + public function testItDoesntReturnAnyStateWhenMailmanIsInactive(): void + { + $this->withoutExceptionHandling()->login()->loginNami(); + $this->stubIo(MailmanService::class, function ($mock) { + Phake::when($mock)->setCredentials('http://mailman.test/api', 'user', 'secret')->thenReturn($mock); + Phake::when($mock)->check()->thenReturn(false); + }); + MailmanSettings::fake([ + 'base_url' => 'http://mailman.test/api', + 'username' => 'user', + 'password' => 'secret', + 'is_active' => false, + ]); + + $response = $this->get('/setting/mailman'); + + $response->assertOk(); + $this->assertInertiaHas(null, $response, 'state'); + Phake::verifyNoInteraction(app(MailmanService::class)); + } + + public function testItSetsMailmanSettings(): void { $this->stubIo(MailmanService::class, function ($mock) { Phake::when($mock)->setCredentials('http://mailman.test/api', 'user', 'secret')->thenReturn($mock); @@ -43,6 +91,7 @@ class SettingTest extends TestCase 'base_url' => 'http://mailman.test/api', 'username' => 'user', 'password' => 'secret', + 'is_active' => true, ]); $response->assertRedirect('/setting/mailman'); @@ -66,6 +115,7 @@ class SettingTest extends TestCase 'base_url' => 'http://mailman.test/api', 'username' => 'user', 'password' => 'secret', + 'is_active' => true, ]); $response->assertSessionHasErrors(['mailman' => 'Verbindung fehlgeschlagen.']); @@ -82,6 +132,7 @@ class SettingTest extends TestCase 'base_url' => 'http://mailman.test/api', 'username' => 'user', 'password' => '', + 'is_active' => true, ]); $response->assertSessionHasErrors(['password' => 'Passwort ist erforderlich.']);