adrema/modules/Mailgateway/StoreTest.php

123 lines
4.0 KiB
PHP

<?php
namespace Tests\Feature\Mailgateway;
use App\Mailgateway\Types\LocalType;
use App\Mailgateway\Types\MailmanType;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Livewire\Livewire;
use Modules\Mailgateway\Components\Form;
use Tests\RequestFactories\MailmanTypeRequest;
use Tests\TestCase;
uses(DatabaseTransactions::class);
uses(TestCase::class);
it('test it saves a mail gateway', function () {
test()->withoutExceptionHandling()->login()->loginNami();
Livewire::test(Form::class)
->set('name', 'lala')
->set('domain', 'example.com')
->set('type', new LocalType())
->call('onSave')
->assertHasNoErrors()
->assertDispatched('closeModal')
->assertDispatched('refresh-page')
->assertDispatched('success');
$this->assertDatabaseHas('mailgateways', [
'domain' => 'example.com',
'name' => 'lala',
'type' => json_encode([
'type' => LocalType::class,
'data' => [],
]),
]);
});
it('test it validates mail gateway', function (array $attributes, array $errors) {
test()->withoutExceptionHandling()->login()->loginNami();
Livewire::test(Form::class)
->set('name', 'lala')
->set('domain', 'example.com')
->set('type', new LocalType())
->setArray($attributes)
->call('onSave')
->assertHasErrors($errors)
->assertNotDispatched('closeModal')
->assertNotDispatched('refresh-page')
->assertNotDispatched('success');
})->with([
[['name' => ''], ['name' => 'required']],
[['domain' => ''], ['domain' => 'required']],
[['type' => ''], ['type' => 'required']],
]);
it('test it validates mailman type', function (array $attributes, array $errors) {
test()->withoutExceptionHandling()->login()->loginNami();
Livewire::test(Form::class)
->set('name', 'lala')
->set('domain', 'example.com')
->set('cls', MailmanType::class)
->set('params.url', 'exampl.com')
->set('params.user', '::user::')
->set('params.password', 'password')
->setArray($attributes)
->call('onSave')
->assertHasErrors($errors)
->assertNotDispatched('closeModal');
})->with([
[['params.url' => ''], ['params.url' => 'required']],
[['params.user' => ''], ['params.user' => 'required']],
[['params.password' => ''], ['params.password' => 'required']],
[['params.owner' => ''], ['params.owner' => 'required']],
[['params.owner' => 'aaa'], ['params.owner' => 'email']],
]);
it('test it stores mailman gateway', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$typeParams = MailmanTypeRequest::new()->succeeds()->create(['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret', 'owner' => 'owner@example.com']);
Livewire::test(Form::class)
->setArray([
'name' => 'lala',
'domain' => 'https://example.com',
'cls' => MailmanType::class,
'params' => $typeParams
])
->call('onSave')
->assertDispatched('closeModal');
$this->assertDatabaseHas('mailgateways', [
'type' => json_encode([
'cls' => MailmanType::class,
'params' => $typeParams,
]),
'name' => 'lala',
'domain' => 'https://example.com',
]);
});
it('test it checks mailman connection', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$typeParams = MailmanTypeRequest::new()->fails()->create(['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret', 'owner' => 'owner@example.com']);
Livewire::test(Form::class)
->setArray([
'name' => 'lala',
'domain' => 'https://example.com',
'cls' => MailmanType::class,
'params' => $typeParams
])
->call('onSave')
->assertHasErrors('connection')
->assertNotDispatched('closeModal');
$this->assertDatabaseCount('mailgateways', 0);
});