Add fileshare settings

This commit is contained in:
philipp lang 2024-12-22 18:32:05 +01:00
parent 291e69e68c
commit ca9bf70c9a
11 changed files with 426 additions and 187 deletions

View File

@ -185,6 +185,7 @@ return [
Modules\Nami\NamiServiceProvider::class,
Modules\Auth\AuthServiceProvider::class,
Modules\Form\FormServiceProvider::class,
Modules\Fileshare\FileshareServiceProvider::class,
],
/*

View File

@ -0,0 +1,112 @@
<?php
namespace Modules\Fileshare\Components;
use App\Fileshare\ConnectionTypes\ConnectionType;
use App\Fileshare\Models\Fileshare;
use Modules\Mailgateway\Models\Mailgateway;
use Modules\Mailgateway\Types\Type;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\On;
use Livewire\Attributes\Validate;
use Livewire\Component;
class Form extends Component
{
public string $name = '';
public array $type = [];
#[Validate('required|string|max:255|exclude')]
public ?string $typeClass = null;
public array $types;
public ?Fileshare $model = null;
public function rules()
{
return [
'name' => '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'
<div>
<form class="grid grid-cols-2 gap-3">
<x-form::text name="name" wire:model="name" label="Bezeichnung" required />
<x-form::select name="typeClass" wire:model.live="typeClass" label="Typ" :options="$types" required />
@foreach($this->fields() as $index => $field)
<x-form::text
wire:key="$index"
wire:model="type.{{$field['key']}}"
:label="$field['label']"
:type="$field['type']"
:name="'type.'.$field['key']"
required
></x-form::text>
@endforeach
</form>
</div>
HTML;
}
}

View File

@ -0,0 +1,155 @@
<?php
namespace Modules\Fileshare\Components;
use App\Fileshare\ConnectionTypes\NextcloudConnection;
use App\Fileshare\ConnectionTypes\OwncloudConnection;
use App\Fileshare\Models\Fileshare;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Tests\FileshareTestCase;
uses(FileshareTestCase::class);
uses(DatabaseTransactions::class);
it('it displays values', function () {
test()->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']);
});

View File

@ -0,0 +1,20 @@
<?php
namespace Modules\Fileshare\Components;
use App\Fileshare\FileshareSettings;
use App\Fileshare\Models\Fileshare;
use Livewire\Component;
class SettingView extends Component
{
public $settingClass = FileshareSettings::class;
public function render()
{
return view('fileshare::setting-view', [
'data' => Fileshare::get(),
]);
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace Modules\Fileshare\Components;
use App\Fileshare\ConnectionTypes\OwncloudConnection;
use App\Fileshare\Models\Fileshare;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Modules\Fileshare\Components\SettingView;
use Tests\FileshareTestCase;
use Tests\TestCase;
uses(FileshareTestCase::class);
uses(DatabaseTransactions::class);
it('it renders page', function () {
test()->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');
});

View File

@ -0,0 +1,36 @@
<x-page::setting-layout :active="$settingClass">
<x-slot:toolbar>
<x-ui::badge wire:click.prevent="$dispatch('openModal', {component: 'modules.fileshare.components.form', props: {}, title: 'Neue Verbindung'})" icon="plus">Neue Verbindung</x-ui::badge>
</x-slot:toolbar>
<div>
<x-ui::table>
<thead>
<th>Bezeichnung</th>
<th>Typ</th>
<th>Prüfung</th>
<th>Aktion</th>
</thead>
@foreach ($data as $index => $share)
<tr wire:key="$index">
<td>{{ $share->name }}</td>
<td>{{ $share->type::title() }}</td>
<td>
<x-ui::boolean-display :value="$share->type->check()"
hint="Verbindungsstatus"
right="Verbindung erfolgreich"
wrong="Verbindung fehlgeschlagen"
></x-ui::boolean-display>
</td>
<td>
<x-ui::action wire:click="$dispatch('openModal', {
component: 'modules.fileshare.components.form',
props: {id: '{{$share->id}}'},
title: 'Verbindung {{$share->name}} bearbeiten'}
)" icon="pencil" variant="warning">Bearbeiten</x-ui::action>
</td>
</tr>
@endforeach
</x-ui::table>
</div>
</x-page::setting-layout>

View File

@ -0,0 +1,38 @@
<?php
namespace Modules\Fileshare;
use App\Fileshare\FileshareSettings;
use App\Setting\SettingFactory;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Modules\Fileshare\Components\SettingView;
class FileshareServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
app(SettingFactory::class)->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');
}
}

View File

@ -1,47 +0,0 @@
<?php
namespace Tests\Fileshare;
use App\Fileshare\ConnectionTypes\NextcloudConnection;
use App\Fileshare\ConnectionTypes\OwncloudConnection;
use App\Fileshare\Models\Fileshare;
use Tests\FileshareTestCase;
class FileshareIndexActionTest extends FileshareTestCase
{
public function testItListsOwncloudConnectionsThatAreActive(): void
{
$this->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');
}
}

View File

@ -1,83 +0,0 @@
<?php
namespace Tests\Fileshare;
use App\Fileshare\ConnectionTypes\NextcloudConnection;
use App\Fileshare\ConnectionTypes\OwncloudConnection;
use App\Fileshare\Models\Fileshare;
use Tests\FileshareTestCase;
class FileshareStoreActionTest extends FileshareTestCase
{
public function testItStoresAConnection(): void
{
$this->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);
}
}

View File

@ -1,56 +0,0 @@
<?php
namespace Tests\Fileshare;
use App\Fileshare\ConnectionTypes\OwncloudConnection;
use App\Fileshare\Models\Fileshare;
use Tests\FileshareTestCase;
class FileshareUpdateActionTest extends FileshareTestCase
{
public function testItStoresAConnection(): void
{
$this->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']);
}
}

View File

@ -25,7 +25,7 @@ abstract class FileshareTestCase extends TestCase
*/
protected array $passwords = [];
public function setUp(): void
protected function setUp(): void
{
parent::setUp();