adrema/modules/Mailgateway/UpdateTest.php

109 lines
4.7 KiB
PHP
Raw Normal View History

2024-10-20 21:19:07 +02:00
<?php
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 Livewire\Livewire;
use Modules\Mailgateway\Components\Form;
use Tests\RequestFactories\MailmanTypeRequest;
use Tests\TestCase;
uses(DatabaseTransactions::class);
uses(TestCase::class);
it('test it sets attributes for mailman', function () {
test()->withoutExceptionHandling()->login()->loginNami();
2024-10-23 21:02:42 +02:00
$typeParams = MailmanTypeRequest::new()->state(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner']);
$mailgateway = Mailgateway::factory()->type($typeParams->toData())->create(['name' => '::name::', 'domain' => 'example.com']);
2024-10-20 21:19:07 +02:00
2024-10-20 22:02:43 +02:00
Livewire::test(Form::class, ['id' => $mailgateway->id])
->assertSet('model', fn ($m) => $m->is($mailgateway))
2024-10-20 21:19:07 +02:00
->assertSet('name', '::name::')
->assertSet('domain', 'example.com')
2024-10-23 21:02:42 +02:00
->assertSet(
'type',
fn ($type) => $type instanceof MailmanType
&& $type->url === 'https://mailman.example.com'
&& $type->user === 'user' &&
$type->password === 'password'
&& $type->owner === 'owner'
);
2024-10-20 21:19:07 +02:00
});
it('test it sets attributes for local', function () {
test()->withoutExceptionHandling()->login()->loginNami();
2024-10-23 21:02:42 +02:00
$mailgateway = Mailgateway::factory()->create(['name' => '::name::', 'domain' => 'example.com']);
2024-10-20 21:19:07 +02:00
2024-10-20 22:02:43 +02:00
Livewire::test(Form::class, ['id' => $mailgateway->id])
2024-10-20 21:19:07 +02:00
->assertSet('name', '::name::')
->assertSet('domain', 'example.com')
2024-10-23 21:02:42 +02:00
->assertSet('type', fn ($type) => $type instanceof LocalType);
2024-10-20 21:19:07 +02:00
});
it('test it validates type', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$mailgateway = Mailgateway::factory()->type(LocalType::class, [])->create(['name' => '::name::', 'domain' => 'example.com']);
2024-10-20 22:02:43 +02:00
Livewire::test(Form::class, ['id' => $mailgateway->id])
2024-10-20 21:19:07 +02:00
->set('cls', '')
->assertHasErrors(['cls' => 'required']);
});
it('test it updates a mailman gateway without updating password', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$typeParams = MailmanTypeRequest::new()->succeeds()->create(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com']);
$mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create(['name' => '::name::', 'domain' => 'example.com']);
2024-10-20 22:02:43 +02:00
Livewire::test(Form::class, ['id' => $mailgateway->id])
2024-10-20 21:19:07 +02:00
->set('name', '::newname::')
->call('onSave')
2024-10-20 21:27:00 +02:00
->assertHasNoErrors()
->assertDispatched('closeModal')
->assertDispatched('refresh-page')
->assertDispatched('success');
2024-10-20 21:19:07 +02:00
$this->assertDatabaseCount('mailgateways', 1);
$this->assertDatabaseHas('mailgateways', [
'name' => '::newname::',
'type' => json_encode(['cls' => MailmanType::class, 'params' => $typeParams]),
]);
});
it('test it updates a mailman gateway with password', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$typeParams = MailmanTypeRequest::new()->create(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com']);
$newTypeParams = MailmanTypeRequest::new()->succeeds()->create(['url' => 'https://mailman.example.com', 'user' => 'newuser', 'password' => 'password', 'owner' => 'owner@example.com']);
$mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create();
2024-10-20 22:02:43 +02:00
Livewire::test(Form::class, ['id' => $mailgateway->id])
2024-10-20 21:19:07 +02:00
->set('params.user', 'newuser')
->call('onSave')
->assertHasNoErrors();
$this->assertDatabaseCount('mailgateways', 1);
$this->assertDatabaseHas('mailgateways', [
'type' => json_encode(['cls' => MailmanType::class, 'params' => $newTypeParams]),
]);
});
it('test it checks mailgateway connection when updating', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$typeParams = MailmanTypeRequest::new()->create(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com']);
MailmanTypeRequest::new()->fails()->create(['url' => 'https://mailman.example.com', 'user' => 'newuser', 'password' => 'password', 'owner' => 'owner@example.com']);
$mailgateway = Mailgateway::factory()->type(MailmanType::class, $typeParams)->create();
2024-10-20 22:02:43 +02:00
Livewire::test(Form::class, ['id' => $mailgateway->id])
2024-10-20 21:19:07 +02:00
->set('params.user', 'newuser')
->call('onSave')
->assertHasErrors('connection');
});