From ca9bf70c9a2445bcbebffaa5a70cf118ada97e63 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 22 Dec 2024 18:32:05 +0100 Subject: [PATCH] Add fileshare settings --- config/app.php | 1 + modules/Fileshare/Components/Form.php | 112 +++++++++++++ modules/Fileshare/Components/FormTest.php | 155 ++++++++++++++++++ modules/Fileshare/Components/SettingView.php | 20 +++ .../Fileshare/Components/SettingViewTest.php | 63 +++++++ .../Components/setting-view.blade.php | 36 ++++ .../Fileshare/FileshareServiceProvider.php | 38 +++++ tests/Fileshare/FileshareIndexActionTest.php | 47 ------ tests/Fileshare/FileshareStoreActionTest.php | 83 ---------- tests/Fileshare/FileshareUpdateActionTest.php | 56 ------- tests/FileshareTestCase.php | 2 +- 11 files changed, 426 insertions(+), 187 deletions(-) create mode 100644 modules/Fileshare/Components/Form.php create mode 100644 modules/Fileshare/Components/FormTest.php create mode 100644 modules/Fileshare/Components/SettingView.php create mode 100644 modules/Fileshare/Components/SettingViewTest.php create mode 100644 modules/Fileshare/Components/setting-view.blade.php create mode 100644 modules/Fileshare/FileshareServiceProvider.php delete mode 100644 tests/Fileshare/FileshareIndexActionTest.php delete mode 100644 tests/Fileshare/FileshareStoreActionTest.php delete mode 100644 tests/Fileshare/FileshareUpdateActionTest.php diff --git a/config/app.php b/config/app.php index 12fa5c15..0fb18e81 100644 --- a/config/app.php +++ b/config/app.php @@ -185,6 +185,7 @@ return [ Modules\Nami\NamiServiceProvider::class, Modules\Auth\AuthServiceProvider::class, Modules\Form\FormServiceProvider::class, + Modules\Fileshare\FileshareServiceProvider::class, ], /* diff --git a/modules/Fileshare/Components/Form.php b/modules/Fileshare/Components/Form.php new file mode 100644 index 00000000..775f4dbd --- /dev/null +++ b/modules/Fileshare/Components/Form.php @@ -0,0 +1,112 @@ + 'required|string|max:255', + 'type' => 'array|exclude', + ]; + } + + public function validationAttributes(): array + { + return [ + 'typeClass' => 'Typ', + 'name' => 'Bezeichnung', + ]; + } + + public function mount(?string $id = null): void + { + $this->types = ConnectionType::forSelect(); + + if ($id) { + $this->model = Fileshare::findOrFail($id); + $this->name = $this->model->name; + $this->typeClass = get_class($this->model->type); + $this->type = $this->model->type->toArray(); + } + } + + public function fields(): array + { + return $this->typeClass ? $this->typeClass::fields() : []; + } + + public function updatedTypeClass(?string $type): void + { + if (!$type) { + return; + } + + $this->type = $type::defaults(); + } + + #[On('onStoreFromModal')] + public function onSave(): void + { + $payload = $this->validate(); + $type = $this->typeClass::from($this->type); + + if (!$type->check()) { + throw ValidationException::withMessages(['typeClass' => 'Verbindung fehlgeschlagen']); + } + + if ($this->model) { + $this->model->update([...$payload, 'type' => $type]); + } else { + Fileshare::create([ + ...$payload, + 'type' => $type, + ]); + } + + $this->dispatch('closeModal'); + $this->dispatch('refresh-page'); + $this->dispatch('success', $this->model ? 'Verbindung aktualisiert.' : 'Verbindung erstellt.'); + } + + public function render() + { + return <<<'HTML' +
+
+ + + @foreach($this->fields() as $index => $field) + + @endforeach + +
+ HTML; + } +} diff --git a/modules/Fileshare/Components/FormTest.php b/modules/Fileshare/Components/FormTest.php new file mode 100644 index 00000000..6f6bd7cd --- /dev/null +++ b/modules/Fileshare/Components/FormTest.php @@ -0,0 +1,155 @@ +withoutExceptionHandling()->login()->loginNami(); + + Livewire::test(Form::class) + ->assertSet('name', '') + ->assertSee('Bezeichnung') + ->assertSee('Owncloud') + ->assertSee('Nextcloud') + ->assertSet('type', []); +}); + +it('it displays owncloud connection values', function () { + test()->withoutExceptionHandling()->login()->loginNami(); + + Livewire::test(Form::class) + ->set('typeClass', OwncloudConnection::class) + ->assertSet('typeClass', OwncloudConnection::class) + ->assertSee('Benutzer') + ->assertSee('URL') + ->assertSet('type.user', '') + ->assertSet('type.base_url', '') + ->assertSet('type.password', ''); +}); + +it('it saves owncloud connection', function () { + test()->login()->loginNami()->withUser('badenpowell', 'secret'); + + Livewire::test(Form::class) + ->set('name', 'lala') + ->set('typeClass', OwncloudConnection::class) + ->set('type.user', 'badenpowell') + ->set('type.password', 'secret') + ->set('type.base_url', env('TEST_OWNCLOUD_DOMAIN')) + ->call('onSave') + ->assertDispatched('success', 'Verbindung erstellt.') + ->assertDispatched('refresh-page'); + + $connection = Fileshare::firstOrFail(); + $this->assertEquals('badenpowell', $connection->type->user); + $this->assertEquals('secret', $connection->type->password); + $this->assertEquals(env('TEST_OWNCLOUD_DOMAIN'), $connection->type->baseUrl); + $this->assertEquals('lala', $connection->name); +}); + +it('it saves nextcloud connection', function () { + test()->login()->loginNami()->withUser('badenpowell', 'secret'); + + Livewire::test(Form::class) + ->set('name', 'lala') + ->set('typeClass', NextcloudConnection::class) + ->set('type.user', 'badenpowell') + ->set('type.password', 'secret') + ->set('type.base_url', env('TEST_OWNCLOUD_DOMAIN')) + ->call('onSave') + ->assertDispatched('success', 'Verbindung erstellt.') + ->assertDispatched('refresh-page'); + + $connection = Fileshare::firstOrFail(); + $this->assertEquals('badenpowell', $connection->type->user); + $this->assertEquals('secret', $connection->type->password); + $this->assertEquals(env('TEST_OWNCLOUD_DOMAIN'), $connection->type->baseUrl); + $this->assertEquals('lala', $connection->name); +}); + +it('checks nextcloud connection', function () { + test()->login()->loginNami()->withUser('badenpowell', 'secret'); + + Livewire::test(Form::class) + ->set('name', 'lala') + ->set('typeClass', NextcloudConnection::class) + ->set('type.user', 'badenpowell') + ->set('type.password', 'wrong') + ->set('type.base_url', env('TEST_OWNCLOUD_DOMAIN')) + ->call('onSave') + ->assertHasErrors(['typeClass' => 'Verbindung fehlgeschlagen']); +}); + +it('validates input', function ($attributes, $errors) { + test()->login()->loginNami()->withUser('badenpowell', 'secret'); + + Livewire::test(Form::class) + ->set('name', 'lala') + ->set('typeClass', NextcloudConnection::class) + ->set('type.user', 'badenpowell') + ->set('type.password', 'wrong') + ->set('type.base_url', env('TEST_OWNCLOUD_DOMAIN')) + ->setArray($attributes) + ->call('onSave') + ->assertHasErrors($errors); +})->with([ + [['name' => ''], ['name' => 'Bezeichnung ist erforderlich.']], + [['typeClass' => null], ['typeClass' => 'Typ ist erforderlich.']], +]); + +it('updates a connection', function () { + $this->withoutExceptionHandling()->login()->loginNami()->withUser('badenpowell', 'secretneu'); + + $connection = Fileshare::factory() + ->type(OwncloudConnection::from(['user' => 'test', 'password' => 'secret', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')])) + ->name('lokaler Server') + ->create(); + + Livewire::test(Form::class, ['id' => $connection->id]) + ->assertSet('name', 'lokaler Server') + ->assertSet('typeClass', OwncloudConnection::class) + ->assertSet('type.user', 'test') + ->assertSet('type.password', 'secret') + ->assertSet('type.base_url', env('TEST_OWNCLOUD_DOMAIN')) + ->setArray([ + 'name' => 'Lala', + 'typeClass' => OwncloudConnection::class, + 'type' => [ + 'user' => 'badenpowell', + 'password' => 'secretneu', + 'base_url' => env('TEST_OWNCLOUD_DOMAIN'), + ] + ]) + ->call('onSave') + ->assertDispatched('success', 'Verbindung aktualisiert.') + ->assertDispatched('refresh-page'); + + $connection = Fileshare::firstOrFail(); + $this->assertEquals('badenpowell', $connection->type->user); + $this->assertEquals('secretneu', $connection->type->password); + $this->assertEquals(env('TEST_OWNCLOUD_DOMAIN'), $connection->type->baseUrl); + $this->assertEquals('Lala', $connection->name); +}); + +it('checks a connection while updating', function () { + $this->withoutExceptionHandling()->login()->loginNami()->withUser('badenpowell', 'secretneu'); + + $connection = Fileshare::factory() + ->type(OwncloudConnection::from(['user' => 'test', 'password' => 'secret', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')])) + ->name('lokaler Server') + ->create(); + + Livewire::test(Form::class, ['id' => $connection->id]) + ->set('type.password', 'wrong') + ->call('onSave') + ->assertHasErrors(['typeClass' => 'Verbindung fehlgeschlagen']); +}); diff --git a/modules/Fileshare/Components/SettingView.php b/modules/Fileshare/Components/SettingView.php new file mode 100644 index 00000000..2f6fb9bb --- /dev/null +++ b/modules/Fileshare/Components/SettingView.php @@ -0,0 +1,20 @@ + Fileshare::get(), + ]); + } +} diff --git a/modules/Fileshare/Components/SettingViewTest.php b/modules/Fileshare/Components/SettingViewTest.php new file mode 100644 index 00000000..ec671215 --- /dev/null +++ b/modules/Fileshare/Components/SettingViewTest.php @@ -0,0 +1,63 @@ +withoutExceptionHandling()->login()->loginNami(); + + test()->get('/setting/fileshare')->assertSeeLivewire(SettingView::class); +}); + +it('displays overview', function () { + $this->withoutExceptionHandling()->login()->loginNami()->withUser('badenpowell', 'secret'); + Fileshare::factory() + ->type(OwncloudConnection::from(['user' => 'badenpowell', 'password' => 'secret', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')])) + ->name('lokaler Server') + ->create(); + + Livewire::test(SettingView::class) + ->assertSee('lokaler Server') + ->assertSee('Verbindung erfolgreich') + ->assertSee('Owncloud'); + // ->assertJsonPath('data.0.name', 'lokaler Server') + // ->assertJsonPath('data.0.type', OwncloudConnection::class) + // ->assertJsonPath('data.0.config.user', 'badenpowell') + // ->assertJsonPath('data.0.config.password', 'secret') + // ->assertJsonPath('data.0.config.base_url', env('TEST_OWNCLOUD_DOMAIN')) + // ->assertJsonPath('data.0.id', $connection->id) + // ->assertJsonPath('data.0.is_active', true) + // ->assertJsonPath('data.0.type_human', 'Owncloud') + // ->assertJsonPath('data.0.links.update', route('fileshare.update', ['fileshare' => $connection])) + // ->assertJsonPath('meta.default.name', '') + // ->assertJsonPath('meta.links.store', route('fileshare.store')) + // ->assertJsonPath('meta.types.0.id', NextcloudConnection::class) + // ->assertJsonPath('meta.types.0.name', 'Nextcloud') + // ->assertJsonPath('meta.types.0.defaults.base_url', '') + // ->assertJsonPath('meta.types.1.id', OwncloudConnection::class) + // ->assertJsonPath('meta.types.1.name', 'Owncloud') + // ->assertJsonPath('meta.types.1.defaults.base_url', '') + // ->assertJsonPath('meta.types.0.fields.1', ['label' => 'Benutzer', 'key' => 'user', 'type' => 'text']); +}); + +it('displays wrong connection', function () { + $this->withoutExceptionHandling()->login()->loginNami()->withUser('badenpowell', 'secret'); + Fileshare::factory() + ->type(OwncloudConnection::from(['user' => 'badenpowell', 'password' => 'wrong', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')])) + ->name('lokaler Server') + ->create(); + + Livewire::test(SettingView::class) + ->assertSee('lokaler Server') + ->assertSee('Verbindung fehlgeschlagen'); +}); diff --git a/modules/Fileshare/Components/setting-view.blade.php b/modules/Fileshare/Components/setting-view.blade.php new file mode 100644 index 00000000..2591f60a --- /dev/null +++ b/modules/Fileshare/Components/setting-view.blade.php @@ -0,0 +1,36 @@ + + + Neue Verbindung + +
+ + + Bezeichnung + Typ + Prüfung + Aktion + + + @foreach ($data as $index => $share) + + {{ $share->name }} + {{ $share->type::title() }} + + + + + Bearbeiten + + + @endforeach + +
+
diff --git a/modules/Fileshare/FileshareServiceProvider.php b/modules/Fileshare/FileshareServiceProvider.php new file mode 100644 index 00000000..ff23dc00 --- /dev/null +++ b/modules/Fileshare/FileshareServiceProvider.php @@ -0,0 +1,38 @@ +register(FileshareSettings::class); + + app(Router::class)->middleware(['web', 'auth:web'])->group(function ($router) { + $router->get('/setting/fileshare', SettingView::class)->name('setting.fileshare'); + }); + + View::addNamespace('fileshare', __DIR__ . '/Components'); + } +} diff --git a/tests/Fileshare/FileshareIndexActionTest.php b/tests/Fileshare/FileshareIndexActionTest.php deleted file mode 100644 index a398d74a..00000000 --- a/tests/Fileshare/FileshareIndexActionTest.php +++ /dev/null @@ -1,47 +0,0 @@ -withoutExceptionHandling()->login()->loginNami()->withUser('badenpowell', 'secret'); - $connection = Fileshare::factory() - ->type(OwncloudConnection::from(['user' => 'badenpowell', 'password' => 'secret', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')])) - ->name('lokaler Server') - ->create(); - - $this->get('/api/fileshare') - ->assertJsonPath('data.0.name', 'lokaler Server') - ->assertJsonPath('data.0.type', OwncloudConnection::class) - ->assertJsonPath('data.0.config.user', 'badenpowell') - ->assertJsonPath('data.0.config.password', 'secret') - ->assertJsonPath('data.0.config.base_url', env('TEST_OWNCLOUD_DOMAIN')) - ->assertJsonPath('data.0.id', $connection->id) - ->assertJsonPath('data.0.is_active', true) - ->assertJsonPath('data.0.type_human', 'Owncloud') - ->assertJsonPath('data.0.links.update', route('fileshare.update', ['fileshare' => $connection])) - ->assertJsonPath('meta.default.name', '') - ->assertJsonPath('meta.links.store', route('fileshare.store')) - ->assertJsonPath('meta.types.0.id', NextcloudConnection::class) - ->assertJsonPath('meta.types.0.name', 'Nextcloud') - ->assertJsonPath('meta.types.0.defaults.base_url', '') - ->assertJsonPath('meta.types.1.id', OwncloudConnection::class) - ->assertJsonPath('meta.types.1.name', 'Owncloud') - ->assertJsonPath('meta.types.1.defaults.base_url', '') - ->assertJsonPath('meta.types.0.fields.1', ['label' => 'Benutzer', 'key' => 'user', 'type' => 'text']); - } - - public function testItRendersComponent(): void - { - $this->withoutExceptionHandling()->login()->loginNami(); - - $this->get('/setting/fileshare')->assertComponent('setting/Fileshare'); - } -} diff --git a/tests/Fileshare/FileshareStoreActionTest.php b/tests/Fileshare/FileshareStoreActionTest.php deleted file mode 100644 index 01980ff1..00000000 --- a/tests/Fileshare/FileshareStoreActionTest.php +++ /dev/null @@ -1,83 +0,0 @@ -withoutExceptionHandling()->login()->loginNami()->withUser('badenpowell', 'secret'); - - $this->post(route('fileshare.store'), [ - 'name' => 'Lala', - 'type' => OwncloudConnection::class, - 'config' => [ - 'user' => 'badenpowell', - 'password' => 'secret', - 'base_url' => env('TEST_OWNCLOUD_DOMAIN'), - ] - ])->assertOk(); - - $connection = Fileshare::firstOrFail(); - $this->assertEquals('badenpowell', $connection->type->user); - $this->assertEquals('secret', $connection->type->password); - $this->assertEquals(env('TEST_OWNCLOUD_DOMAIN'), $connection->type->baseUrl); - $this->assertEquals('Lala', $connection->name); - } - - public function testItChecksConnection(): void - { - $this->withExceptionHandling()->login()->loginNami(); - - $this->postJson(route('fileshare.store'), [ - 'name' => 'Lala', - 'type' => OwncloudConnection::class, - 'config' => [ - 'user' => 'badenpowell', - 'password' => 'secret', - 'base_url' => env('TEST_OWNCLOUD_DOMAIN'), - ] - ])->assertJsonValidationErrors(['type' => 'Verbindung fehlgeschlagen']); - } - - public function testItNeedsName(): void - { - $this->withExceptionHandling()->login()->loginNami(); - - $this->postJson(route('fileshare.store'), [ - 'name' => '', - 'type' => OwncloudConnection::class, - 'config' => [ - 'user' => 'badenpowell', - 'password' => 'secret', - 'base_url' => env('TEST_OWNCLOUD_DOMAIN'), - ] - ])->assertJsonValidationErrors(['name' => 'Name ist erforderlich.']); - } - - public function testItStoresNextcloudConnection(): void - { - $this->withoutExceptionHandling()->login()->loginNami()->withUser('badenpowell', 'uaLeitu3eecoweePhaeGei3Oa'); - - $this->post(route('fileshare.store'), [ - 'name' => 'Lala', - 'type' => NextcloudConnection::class, - 'config' => [ - 'user' => 'badenpowell', - 'password' => 'uaLeitu3eecoweePhaeGei3Oa', - 'base_url' => env('TEST_NEXTCLOUD_DOMAIN'), - ] - ])->assertOk(); - - $connection = Fileshare::firstOrFail(); - $this->assertEquals('badenpowell', $connection->type->user); - $this->assertEquals('uaLeitu3eecoweePhaeGei3Oa', $connection->type->password); - $this->assertEquals(env('TEST_NEXTCLOUD_DOMAIN'), $connection->type->baseUrl); - $this->assertEquals('Lala', $connection->name); - } -} diff --git a/tests/Fileshare/FileshareUpdateActionTest.php b/tests/Fileshare/FileshareUpdateActionTest.php deleted file mode 100644 index eedd2a24..00000000 --- a/tests/Fileshare/FileshareUpdateActionTest.php +++ /dev/null @@ -1,56 +0,0 @@ -withoutExceptionHandling()->login()->loginNami()->withUser('badenpowell', 'secret'); - - $connection = Fileshare::factory() - ->type(OwncloudConnection::from(['user' => 'test', 'password' => 'test', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')])) - ->name('lokaler Server') - ->create(); - - $this->patch(route('fileshare.update', ['fileshare' => $connection]), [ - 'name' => 'Lala', - 'type' => OwncloudConnection::class, - 'config' => [ - 'user' => 'badenpowell', - 'password' => 'secret', - 'base_url' => env('TEST_OWNCLOUD_DOMAIN'), - ] - ])->assertOk(); - - $connection = Fileshare::firstOrFail(); - $this->assertEquals('badenpowell', $connection->type->user); - $this->assertEquals('secret', $connection->type->password); - $this->assertEquals(env('TEST_OWNCLOUD_DOMAIN'), $connection->type->baseUrl); - $this->assertEquals('Lala', $connection->name); - } - - public function testItChecksConnection(): void - { - $this->login()->loginNami()->withUser('test', 'test'); - - $connection = Fileshare::factory() - ->type(OwncloudConnection::from(['user' => 'test', 'password' => 'test', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')])) - ->name('lokaler Server') - ->create(); - - $this->patchJson(route('fileshare.update', ['fileshare' => $connection]), [ - 'name' => 'Lala', - 'type' => OwncloudConnection::class, - 'config' => [ - 'user' => 'badenpowell', - 'password' => 'secret', - 'base_url' => env('TEST_OWNCLOUD_DOMAIN'), - ] - ])->assertJsonValidationErrors(['type' => 'Verbindung fehlgeschlagen']); - } -} diff --git a/tests/FileshareTestCase.php b/tests/FileshareTestCase.php index aaeac0a8..2984e294 100644 --- a/tests/FileshareTestCase.php +++ b/tests/FileshareTestCase.php @@ -25,7 +25,7 @@ abstract class FileshareTestCase extends TestCase */ protected array $passwords = []; - public function setUp(): void + protected function setUp(): void { parent::setUp();