diff --git a/.gitignore b/.gitignore index 1d2e9ec5..c723cdc5 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ Homestead.json .phpunit.result.cache /storage/temp/ /storage/debugbar/ +/tests/Fileshare/oc_tmp/* # User data files /data/ diff --git a/app/Fileshare/Actions/FileshareStoreAction.php b/app/Fileshare/Actions/FileshareStoreAction.php new file mode 100644 index 00000000..d041ffd6 --- /dev/null +++ b/app/Fileshare/Actions/FileshareStoreAction.php @@ -0,0 +1,37 @@ + 'required|string|max:255', + 'type' => 'required|string|max:255|exclude', + 'config' => 'array|exclude', + ]; + } + + public function asController(ActionRequest $request) + { + + $type = $request->input('type')::from($request->input('config')); + + if (!$type->check()) { + throw ValidationException::withMessages(['type' => 'Verbindung fehlgeschlagen']); + } + + FileshareConnection::create([ + ...$request->validated(), + 'type' => $type, + ]); + } +} diff --git a/app/Fileshare/Models/FileshareConnection.php b/app/Fileshare/Models/FileshareConnection.php index 06526e92..aafa0a7b 100644 --- a/app/Fileshare/Models/FileshareConnection.php +++ b/app/Fileshare/Models/FileshareConnection.php @@ -10,6 +10,8 @@ class FileshareConnection extends Model { use HasFactory; + public $guarded = []; + public $casts = [ 'type' => ConnectionType::class, ]; diff --git a/phpunit.xml b/phpunit.xml index f02d82b0..234aac07 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,6 +10,9 @@ ./tests/Feature + + ./tests/Fileshare + ./packages/laravel-nami/tests/Unit diff --git a/routes/web.php b/routes/web.php index 69bcb924..71b8b98e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -19,6 +19,7 @@ use App\Invoice\Actions\InvoiceStoreAction; use App\Course\Actions\CourseUpdateAction; use App\Dashboard\Actions\IndexAction as DashboardIndexAction; use App\Efz\ShowEfzDocumentAction; +use App\Fileshare\Actions\FileshareStoreAction; use App\Form\Actions\ExportAction as ActionsExportAction; use App\Form\Actions\FormDestroyAction; use App\Form\Actions\FormIndexAction; @@ -166,4 +167,7 @@ Route::group(['middleware' => 'auth:web'], function (): void { Route::get('/form/{form}/participants/{parent?}', ParticipantIndexAction::class)->name('form.participant.index'); Route::post('/form/{form}/is-dirty', IsDirtyAction::class)->name('form.is-dirty'); Route::delete('/participant/{participant}', ParticipantDestroyAction::class)->name('participant.destroy'); + + // ------------------------------------ fileshare ----------------------------------- + Route::post('/fileshare', FileshareStoreAction::class)->name('fileshare.store'); }); diff --git a/tests/Fileshare/ConnectionStoreActionTest.php b/tests/Fileshare/ConnectionStoreActionTest.php new file mode 100644 index 00000000..55212a7b --- /dev/null +++ b/tests/Fileshare/ConnectionStoreActionTest.php @@ -0,0 +1,61 @@ +withoutExceptionHandling()->login()->loginNami()->withOwncloudUser('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 = FileshareConnection::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.']); + } +}