Add type validation
This commit is contained in:
parent
36dcff9738
commit
4a452ef1fe
|
@ -3,6 +3,7 @@
|
|||
namespace App\Mailgateway\Actions;
|
||||
|
||||
use App\Mailgateway\Models\Mailgateway;
|
||||
use App\Mailgateway\Types\Type;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Lorisleiva\Actions\ActionRequest;
|
||||
|
@ -12,26 +13,62 @@ class StoreAction
|
|||
{
|
||||
use AsAction;
|
||||
|
||||
public function handle(array $input)
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
*/
|
||||
public function handle(array $input): void
|
||||
{
|
||||
if (!(new $input['type']['cls']($input['type']['params']))->works()) {
|
||||
throw ValidationException::withMessages(['erorr' => 'Verbindung fehlgeschlagen.']);
|
||||
if (!app($input['type']['cls'])->setParams($input['type']['params'])->works()) {
|
||||
throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']);
|
||||
}
|
||||
|
||||
Mailgateway::create($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|max:255',
|
||||
'domain' => 'required|string|max:255',
|
||||
'type.cls' => ['required', 'string', 'max:255', Rule::in(app('mail-gateways'))],
|
||||
...$this->typeValidation(),
|
||||
'type.params' => 'present|array',
|
||||
...collect(request()->input('type.cls')::rules('storeValidator'))->mapWithKeys(fn ($rules, $key) => ["type.params.{$key}" => $rules]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getValidationAttributes(): array
|
||||
{
|
||||
return [
|
||||
'type.cls' => 'Typ',
|
||||
'name' => 'Beschreibung',
|
||||
'domain' => 'Domain',
|
||||
...collect(request()->input('type.cls')::fieldNames())->mapWithKeys(fn ($attribute, $key) => ["type.params.{$key}" => $attribute]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function typeValidation(): array
|
||||
{
|
||||
return [
|
||||
'type.cls' => ['required', 'string', 'max:255', Rule::in(app('mail-gateways'))],
|
||||
];
|
||||
}
|
||||
|
||||
public function prepareForValidation(ActionRequest $request): void
|
||||
{
|
||||
if (!is_subclass_of(request()->input('type.cls'), Type::class)) {
|
||||
throw ValidationException::withMessages(['type.cls' => 'Typ ist nicht valide.']);
|
||||
}
|
||||
}
|
||||
|
||||
public function asController(ActionRequest $request): void
|
||||
{
|
||||
$this->handle($request->validated());
|
||||
|
|
|
@ -40,7 +40,7 @@ class MailgatewayResource extends JsonResource
|
|||
'types' => app('mail-gateways')->map(fn ($gateway) => [
|
||||
'id' => $gateway,
|
||||
'name' => $gateway::name(),
|
||||
'fields' => $gateway::fields(),
|
||||
'fields' => $gateway::presentFields('storeValidator'),
|
||||
'defaults' => (object) $gateway::defaults(),
|
||||
])->prepend([
|
||||
'id' => null,
|
||||
|
|
|
@ -18,4 +18,9 @@ class LocalType extends Type
|
|||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function setParams(array $params): static
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,11 +10,13 @@ class MailmanType extends Type
|
|||
public string $user;
|
||||
public string $password;
|
||||
|
||||
public function __construct($params)
|
||||
public function setParams(array $params): static
|
||||
{
|
||||
$this->url = data_get($params, 'url');
|
||||
$this->user = data_get($params, 'user');
|
||||
$this->password = data_get($params, 'password');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function name(): string
|
||||
|
@ -27,6 +29,9 @@ class MailmanType extends Type
|
|||
return app(MailmanService::class)->setCredentials($this->url, $this->user, $this->password)->check();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function fields(): array
|
||||
{
|
||||
return [
|
||||
|
|
|
@ -6,10 +6,18 @@ abstract class Type
|
|||
{
|
||||
abstract public static function name(): string;
|
||||
|
||||
/**
|
||||
* @return array<int, MailgatewayCustomField>
|
||||
*/
|
||||
abstract public static function fields(): array;
|
||||
|
||||
abstract public function works(): bool;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $params
|
||||
*/
|
||||
abstract public function setParams(array $params): static;
|
||||
|
||||
public static function defaults(): array
|
||||
{
|
||||
return collect(static::fields())->mapWithKeys(fn ($field) => [
|
||||
|
@ -17,6 +25,14 @@ abstract class Type
|
|||
])->toArray();
|
||||
}
|
||||
|
||||
public static function presentFields(string $validator): array
|
||||
{
|
||||
return array_map(fn ($field) => [
|
||||
...$field,
|
||||
'is_required' => str_contains($field[$validator], 'required'),
|
||||
], static::fields());
|
||||
}
|
||||
|
||||
public static function rules(string $validator): array
|
||||
{
|
||||
return collect(static::fields())->mapWithKeys(fn ($field) => [
|
||||
|
@ -31,4 +47,12 @@ abstract class Type
|
|||
'params' => get_object_vars($this),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function fieldNames(): array
|
||||
{
|
||||
return collect(static::fields())->mapWithKeys(fn ($field) => [$field['name'] => $field['label']])->toArray();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ parameters:
|
|||
ContributionMemberData: 'array<string, mixed>'
|
||||
ContributionRequestArray: 'array{dateFrom: string, dateUntil: string, zipLocation: string, country: int, eventName: string, members: array<int, int>}'
|
||||
ContributionApiRequestArray: 'array{dateFrom: string, dateUntil: string, zipLocation: string, country: int, eventName: string, member_data: array<int, ContributionMemberData>}'
|
||||
MailgatewayCustomField: 'array{name: string, label: string, type: string, storeValidator: string, updateValidator: string, default: string}'
|
||||
|
||||
ignoreErrors:
|
||||
-
|
||||
|
|
|
@ -33,6 +33,9 @@
|
|||
}
|
||||
&.btn-danger {
|
||||
@apply bg-red-400 text-red-100 hover:bg-red-300;
|
||||
&:not(.disabled):hover {
|
||||
@apply bg-red-500 text-red-100;
|
||||
}
|
||||
}
|
||||
|
||||
&.label {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<div :class="{hidden: !isLoading, flex: isLoading}" class="absolute items-center top-0 h-full left-0 ml-2">
|
||||
<ui-spinner class="border-primary-400 w-6 h-6 group-hover:border-primary-200"></ui-spinner>
|
||||
</div>
|
||||
Weiter
|
||||
<slot></slot>
|
||||
</button>
|
||||
</template>
|
||||
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
<page-toolbar-button @click.prevent="model = {...data.meta.default}" color="primary" icon="plus">Neue Verbindung</page-toolbar-button>
|
||||
</template>
|
||||
<ui-popup heading="Neue Verbindung" v-if="model !== null && !model.id" @close="model = null">
|
||||
<div>
|
||||
<div class="grid grid-cols-2 gap-3 mt-6">
|
||||
<form @submit.prevent="submit">
|
||||
<section class="grid grid-cols-2 gap-3 mt-6">
|
||||
<f-text v-model="model.name" name="name" id="name" label="Bezeichnung" required></f-text>
|
||||
<f-text v-model="model.domain" name="domain" id="domain" label="Domain" required></f-text>
|
||||
<f-select
|
||||
|
@ -21,16 +21,25 @@
|
|||
id="type"
|
||||
:options="data.meta.types"
|
||||
:placeholder="''"
|
||||
required
|
||||
></f-select>
|
||||
<template v-for="(field, index) in getType(model.type.cls).fields">
|
||||
<f-text :key="index" v-if="field.type === 'text'" :label="field.label" :name="field.name" :id="field.name" v-model="model.type.params[field.name]"></f-text>
|
||||
<f-text
|
||||
:key="index"
|
||||
v-if="field.type === 'text'"
|
||||
:label="field.label"
|
||||
:name="field.name"
|
||||
:id="field.name"
|
||||
v-model="model.type.params[field.name]"
|
||||
:required="field.is_required"
|
||||
></f-text>
|
||||
</template>
|
||||
</div>
|
||||
<div class="flex mt-4 space-x-2">
|
||||
<a href="#" @click.prevent="submit" class="text-center btn btn-danger">Speichern</a>
|
||||
<a href="#" @click.prevent="model = null" class="text-center btn btn-primary">Abbrechen</a>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<section class="flex mt-4 space-x-2">
|
||||
<ui-button type="submit" class="btn-danger">Speichern</ui-button>
|
||||
<ui-button @click.prevent="model = null" class="btn-primary">Abbrechen</ui-button>
|
||||
</section>
|
||||
</form>
|
||||
</ui-popup>
|
||||
<setting-layout>
|
||||
<div class="w-full h-full pb-6">
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Tests\Feature\Mailgateway;
|
|||
|
||||
use App\Mailgateway\Models\Mailgateway;
|
||||
use App\Mailgateway\Types\LocalType;
|
||||
use App\Mailgateway\Types\MailmanType;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\TestCase;
|
||||
|
||||
|
@ -53,6 +54,15 @@ class IndexTest extends TestCase
|
|||
'id' => LocalType::class,
|
||||
'name' => 'Lokal',
|
||||
], $response, 'data.meta.types.1');
|
||||
$this->assertInertiaHas([
|
||||
'id' => MailmanType::class,
|
||||
'fields' => [
|
||||
[
|
||||
'name' => 'url',
|
||||
'is_required' => true,
|
||||
],
|
||||
],
|
||||
], $response, 'data.meta.types.2');
|
||||
$this->assertInertiaHas([
|
||||
'domain' => '',
|
||||
'name' => '',
|
||||
|
|
|
@ -3,8 +3,11 @@
|
|||
namespace Tests\Feature\Mailgateway;
|
||||
|
||||
use App\Mailgateway\Types\LocalType;
|
||||
use App\Mailgateway\Types\MailmanType;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Phake;
|
||||
use Tests\RequestFactories\MailgatewayRequestFactory;
|
||||
use Tests\RequestFactories\MailmanTypeRequest;
|
||||
use Tests\TestCase;
|
||||
|
||||
class StoreTest extends TestCase
|
||||
|
@ -20,7 +23,7 @@ class StoreTest extends TestCase
|
|||
|
||||
public function testItCanStoreALocalGateway(): void
|
||||
{
|
||||
$response = $this->post('/api/mailgateway', MailgatewayRequestFactory::new()->name('lala')->type(LocalType::class, [])->domain('example.com')->create());
|
||||
$response = $this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->name('lala')->type(LocalType::class, [])->domain('example.com')->create());
|
||||
|
||||
$response->assertOk();
|
||||
|
||||
|
@ -33,4 +36,56 @@ class StoreTest extends TestCase
|
|||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItCanStoreAMailmanGateway(): void
|
||||
{
|
||||
$typeParams = ['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret'];
|
||||
$this->stubIo(MailmanType::class, function ($mock) use ($typeParams) {
|
||||
Phake::when($mock)->setParams($typeParams)->thenReturn($mock);
|
||||
Phake::when($mock)->works()->thenReturn(true);
|
||||
});
|
||||
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->type(MailmanType::class, MailmanTypeRequest::new()->create($typeParams))->create());
|
||||
|
||||
$this->assertDatabaseHas('mailgateways', [
|
||||
'type' => json_encode([
|
||||
'cls' => MailmanType::class,
|
||||
'params' => $typeParams,
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItThrowsErrorWhenMailmanConnectionFailed(): void
|
||||
{
|
||||
$typeParams = ['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret'];
|
||||
$this->stubIo(MailmanType::class, function ($mock) use ($typeParams) {
|
||||
Phake::when($mock)->setParams($typeParams)->thenReturn($mock);
|
||||
Phake::when($mock)->works()->thenReturn(false);
|
||||
});
|
||||
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->type(MailmanType::class, MailmanTypeRequest::new()->create($typeParams))->create())
|
||||
->assertJsonValidationErrors('connection');
|
||||
}
|
||||
|
||||
public function testItValidatesCustomFields(): void
|
||||
{
|
||||
$typeParams = ['url' => 'https://example.com', 'user' => '', 'password' => 'secret'];
|
||||
$this->stubIo(MailmanType::class, function ($mock) use ($typeParams) {
|
||||
Phake::when($mock)->setParams($typeParams)->thenReturn($mock);
|
||||
Phake::when($mock)->works()->thenReturn(false);
|
||||
});
|
||||
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->type(MailmanType::class, MailmanTypeRequest::new()->create($typeParams))->create())
|
||||
->assertJsonValidationErrors(['type.params.user' => 'Benutzer ist erforderlich.']);
|
||||
}
|
||||
|
||||
public function testItValidatesType(): void
|
||||
{
|
||||
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->missingType()->create())
|
||||
->assertJsonValidationErrors('type.cls');
|
||||
}
|
||||
|
||||
public function testItValidatesNameAndDomain(): void
|
||||
{
|
||||
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->withoutName()->withoutDomain()->create())
|
||||
->assertJsonValidationErrors('domain')
|
||||
->assertJsonValidationErrors('name');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Tests\RequestFactories;
|
||||
|
||||
use App\Mailgateway\Types\Type;
|
||||
use Worksome\RequestFactories\RequestFactory;
|
||||
|
||||
class MailgatewayRequestFactory extends RequestFactory
|
||||
|
@ -39,4 +40,22 @@ class MailgatewayRequestFactory extends RequestFactory
|
|||
'params' => $params,
|
||||
]]);
|
||||
}
|
||||
|
||||
public function missingType(): self
|
||||
{
|
||||
return $this->state(['type' => [
|
||||
'cls' => null,
|
||||
'params' => [],
|
||||
]]);
|
||||
}
|
||||
|
||||
public function withoutName(): self
|
||||
{
|
||||
return $this->state(['name' => '']);
|
||||
}
|
||||
|
||||
public function withoutDomain(): self
|
||||
{
|
||||
return $this->state(['domain' => '']);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue