adrema/modules/Mailgateway/StoreTest.php

123 lines
4.0 KiB
PHP
Raw Normal View History

2024-10-20 21:19:07 +02:00
<?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)
2024-10-24 22:42:30 +02:00
->set('typeClass', LocalType::class)
2024-10-20 21:19:07 +02:00
->set('name', 'lala')
->set('domain', 'example.com')
->call('onSave')
2024-10-24 22:42:30 +02:00
->assertHasNoErrors()
2024-10-20 21:19:07 +02:00
->assertDispatched('closeModal')
2024-10-20 21:23:47 +02:00
->assertDispatched('refresh-page')
2024-10-20 21:19:07 +02:00
->assertDispatched('success');
2024-10-24 22:42:30 +02:00
test()->assertDatabaseHas('mailgateways', [
2024-10-20 21:19:07 +02:00
'domain' => 'example.com',
'name' => 'lala',
'type' => json_encode([
2024-10-24 22:42:30 +02:00
'type' => LocalType::class,
'data' => [],
2024-10-20 21:19:07 +02:00
]),
]);
});
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')
2024-10-24 22:42:30 +02:00
->set('typeClass', LocalType::class)
2024-10-20 21:19:07 +02:00
->setArray($attributes)
->call('onSave')
->assertHasErrors($errors)
->assertNotDispatched('closeModal')
2024-10-20 21:23:47 +02:00
->assertNotDispatched('refresh-page')
2024-10-20 21:19:07 +02:00
->assertNotDispatched('success');
})->with([
[['name' => ''], ['name' => 'required']],
[['domain' => ''], ['domain' => 'required']],
2024-10-24 22:42:30 +02:00
[['typeClass' => ''], ['typeClass' => 'required']],
2024-10-20 21:19:07 +02:00
]);
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')
2024-10-24 22:42:30 +02:00
->set('typeClass', MailmanType::class)
->set('type.url', 'exampl.com')
->set('type.user', '::user::')
->set('type.password', 'password')
2024-10-20 21:19:07 +02:00
->setArray($attributes)
->call('onSave')
->assertHasErrors($errors)
->assertNotDispatched('closeModal');
})->with([
2024-10-24 22:42:30 +02:00
[['type.url' => ''], ['type.url' => 'required']],
[['type.user' => ''], ['type.user' => 'required']],
[['type.password' => ''], ['type.password' => 'required']],
[['type.owner' => ''], ['type.owner' => 'required']],
[['type.owner' => 'aaa'], ['type.owner' => 'email']],
2024-10-20 21:19:07 +02:00
]);
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',
2024-10-24 22:42:30 +02:00
'typeClass' => MailmanType::class,
2024-10-20 21:19:07 +02:00
])
2024-10-24 22:42:30 +02:00
->setArray('type', $typeParams)
2024-10-20 21:19:07 +02:00
->call('onSave')
->assertDispatched('closeModal');
2024-10-24 22:42:30 +02:00
test()->assertDatabaseHas('mailgateways', [
2024-10-20 21:19:07 +02:00
'type' => json_encode([
2024-10-24 22:42:30 +02:00
'type' => MailmanType::class,
'data' => $typeParams,
2024-10-20 21:19:07 +02:00
]),
'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',
2024-10-24 22:42:30 +02:00
'typeClass' => MailmanType::class,
2024-10-20 21:19:07 +02:00
])
2024-10-24 22:42:30 +02:00
->setArray('type', $typeParams)
2024-10-20 21:19:07 +02:00
->call('onSave')
->assertHasErrors('connection')
->assertNotDispatched('closeModal');
2024-10-24 22:42:30 +02:00
test()->assertDatabaseCount('mailgateways', 0);
2024-10-20 21:19:07 +02:00
});