From 8609a648fd326eeaecbc0c7546f7c86653c53346 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 27 Jun 2024 00:00:29 +0200 Subject: [PATCH] Add fileshare index --- app/Fileshare/Actions/IndexAction.php | 23 ++++++++++ .../ConnectionTypes/ConnectionType.php | 10 ++++ .../ConnectionTypes/OwncloudConnection.php | 32 +++++++++++++ app/Fileshare/FileshareSettings.php | 30 ++++++++++++ app/Fileshare/Models/FileshareConnection.php | 16 +++++++ .../Resources/FileshareConnectionResource.php | 22 +++++++++ app/Setting/SettingServiceProvider.php | 2 + .../Models/FileshareConnectionFactory.php | 43 +++++++++++++++++ ...159_create_fileshare_connections_table.php | 33 +++++++++++++ tests/Fileshare/ConnectionIndexActionTest.php | 23 ++++++++++ tests/Fileshare/docker-compose.yml | 46 +++++++++++++++++++ tests/FileshareTestCase.php | 39 ++++++++++++++++ 12 files changed, 319 insertions(+) create mode 100644 app/Fileshare/Actions/IndexAction.php create mode 100644 app/Fileshare/ConnectionTypes/ConnectionType.php create mode 100644 app/Fileshare/ConnectionTypes/OwncloudConnection.php create mode 100644 app/Fileshare/FileshareSettings.php create mode 100644 app/Fileshare/Models/FileshareConnection.php create mode 100644 app/Fileshare/Resources/FileshareConnectionResource.php create mode 100644 database/factories/Fileshare/Models/FileshareConnectionFactory.php create mode 100644 database/migrations/2024_06_26_224159_create_fileshare_connections_table.php create mode 100644 tests/Fileshare/ConnectionIndexActionTest.php create mode 100644 tests/Fileshare/docker-compose.yml create mode 100644 tests/FileshareTestCase.php diff --git a/app/Fileshare/Actions/IndexAction.php b/app/Fileshare/Actions/IndexAction.php new file mode 100644 index 00000000..18eb56b5 --- /dev/null +++ b/app/Fileshare/Actions/IndexAction.php @@ -0,0 +1,23 @@ +put('menu', 'setting'); + session()->put('title', 'Datei-Verbindungen'); + + return Inertia::render('fileshare/Index', [ + 'data' => FileshareConnectionResource::collection(FileshareConnection::paginate(15)), + ]); + } +} diff --git a/app/Fileshare/ConnectionTypes/ConnectionType.php b/app/Fileshare/ConnectionTypes/ConnectionType.php new file mode 100644 index 00000000..26f85af3 --- /dev/null +++ b/app/Fileshare/ConnectionTypes/ConnectionType.php @@ -0,0 +1,10 @@ +withBasicAuth($this->user, $this->password)->acceptJson()->get($this->baseUrl . '/ocs/v1.php/cloud/capabilities?format=json'); + return $response->ok(); + } catch (ConnectionException $e) { + return false; + } + } +} diff --git a/app/Fileshare/FileshareSettings.php b/app/Fileshare/FileshareSettings.php new file mode 100644 index 00000000..9b094cbe --- /dev/null +++ b/app/Fileshare/FileshareSettings.php @@ -0,0 +1,30 @@ + ConnectionType::class, + ]; +} diff --git a/app/Fileshare/Resources/FileshareConnectionResource.php b/app/Fileshare/Resources/FileshareConnectionResource.php new file mode 100644 index 00000000..239ce241 --- /dev/null +++ b/app/Fileshare/Resources/FileshareConnectionResource.php @@ -0,0 +1,22 @@ + + */ + public function toArray($request) + { + return [ + 'name' => $this->name, + 'is_active' => $this->type->check(), + ]; + } +} diff --git a/app/Setting/SettingServiceProvider.php b/app/Setting/SettingServiceProvider.php index bb56e90b..d6f7ed12 100644 --- a/app/Setting/SettingServiceProvider.php +++ b/app/Setting/SettingServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Setting; +use App\Fileshare\FileshareSettings; use App\Form\FormSettings; use App\Invoice\InvoiceSettings; use App\Mailgateway\MailgatewaySettings; @@ -32,5 +33,6 @@ class SettingServiceProvider extends ServiceProvider app(SettingFactory::class)->register(MailgatewaySettings::class); app(SettingFactory::class)->register(NamiSettings::class); app(SettingFactory::class)->register(FormSettings::class); + app(SettingFactory::class)->register(FileshareSettings::class); } } diff --git a/database/factories/Fileshare/Models/FileshareConnectionFactory.php b/database/factories/Fileshare/Models/FileshareConnectionFactory.php new file mode 100644 index 00000000..15b53628 --- /dev/null +++ b/database/factories/Fileshare/Models/FileshareConnectionFactory.php @@ -0,0 +1,43 @@ + + */ +class FileshareConnectionFactory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = FileshareConnection::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'type' => '{}', + 'name' => '', + ]; + } + + public function type(ConnectionType $type): self + { + return $this->state(['type' => $type]); + } + + public function name(string $name): self + { + return $this->state(['name' => $name]); + } +} diff --git a/database/migrations/2024_06_26_224159_create_fileshare_connections_table.php b/database/migrations/2024_06_26_224159_create_fileshare_connections_table.php new file mode 100644 index 00000000..bc2f1e5d --- /dev/null +++ b/database/migrations/2024_06_26_224159_create_fileshare_connections_table.php @@ -0,0 +1,33 @@ +id(); + $table->string('name'); + $table->json('type'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('fileshare_connections'); + } +}; diff --git a/tests/Fileshare/ConnectionIndexActionTest.php b/tests/Fileshare/ConnectionIndexActionTest.php new file mode 100644 index 00000000..21327dc6 --- /dev/null +++ b/tests/Fileshare/ConnectionIndexActionTest.php @@ -0,0 +1,23 @@ +withoutExceptionHandling()->login()->loginNami()->withOwncloudUser('badenpowell', 'secret'); + FileshareConnection::factory() + ->type(OwncloudConnection::from(['user' => 'badenpowell', 'password' => 'secret', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')])) + ->name('lokaler Server') + ->create(); + + $this->get('/setting/fileshare') + ->assertInertiaPath('data.data.0.name', 'lokaler Server') + ->assertInertiaPath('data.data.0.is_active', true); + } +} diff --git a/tests/Fileshare/docker-compose.yml b/tests/Fileshare/docker-compose.yml new file mode 100644 index 00000000..88109d03 --- /dev/null +++ b/tests/Fileshare/docker-compose.yml @@ -0,0 +1,46 @@ +version: '3' + +services: + owncloud: + image: owncloud/server:10.10.0 + ports: + - 5566:8080 + depends_on: + - mariadb + environment: + - OWNCLOUD_DOMAIN=http://localhost:5566 + - OWNCLOUD_TRUSTED_DOMAINS= + - OWNCLOUD_DB_TYPE=mysql + - OWNCLOUD_DB_NAME=owncloud + - OWNCLOUD_DB_USERNAME=owncloud + - OWNCLOUD_DB_PASSWORD=owncloud + - OWNCLOUD_DB_HOST=mariadb + - OWNCLOUD_ADMIN_USERNAME=admin + - OWNCLOUD_ADMIN_PASSWORD=admin + - OWNCLOUD_MYSQL_UTF8MB4=true + - OWNCLOUD_REDIS_ENABLED=false + - OWNCLOUD_REDIS_HOST=false + healthcheck: + test: ['CMD', '/usr/bin/healthcheck'] + interval: 30s + timeout: 10s + retries: 5 + volumes: + - ./oc_tmp/files:/mnt/data + + mariadb: + image: mariadb:10.11 + environment: + - MYSQL_ROOT_PASSWORD=owncloud + - MYSQL_USER=owncloud + - MYSQL_PASSWORD=owncloud + - MYSQL_DATABASE=owncloud + - MARIADB_AUTO_UPGRADE=1 + command: ['--max-allowed-packet=128M', '--innodb-log-file-size=64M'] + healthcheck: + test: ['CMD', 'mysqladmin', 'ping', '-u', 'root', '--password=owncloud'] + interval: 10s + timeout: 5s + retries: 5 + volumes: + - ./oc_tmp/db:/var/lib/mysql diff --git a/tests/FileshareTestCase.php b/tests/FileshareTestCase.php new file mode 100644 index 00000000..3dd39ce2 --- /dev/null +++ b/tests/FileshareTestCase.php @@ -0,0 +1,39 @@ +http()->get('/ocs/v1.php/cloud/users?format=json')->json('ocs.data.users') as $user) { + if ($user === $this->adminUser) { + continue; + } + $this->http()->delete('/ocs/v1.php/cloud/users/' . $user); + } + } + + public function withOwncloudUser(string $username, string $password): self + { + $this->http()->asForm()->post('/ocs/v1.php/cloud/users?format=json', ['password' => $password, 'userid' => $username]); + + return $this; + } + + private function http(): PendingRequest + { + return Http::withOptions(['base_uri' => env('TEST_OWNCLOUD_DOMAIN')])->withBasicAuth($this->adminUser, $this->adminPassword)->acceptJson(); + } +}