adrema/modules/Mailgateway/UpdateTest.php

112 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-24 22:42:30 +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-24 22:42:30 +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-24 22:42:30 +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-24 22:42:30 +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();
2024-10-24 22:42:30 +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-24 22:42:30 +02:00
->set('typeClass', '')
->assertHasErrors(['typeClass' => 'required']);
2024-10-20 21:19:07 +02:00
});
it('test it updates a mailman gateway without updating password', function () {
test()->withoutExceptionHandling()->login()->loginNami();
2024-10-24 22:42:30 +02:00
$typeParams = MailmanTypeRequest::new()
->succeeds()
->state(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com'])
->toData();
$mailgateway = Mailgateway::factory()->type($typeParams)->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
->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::',
2024-10-24 22:42:30 +02:00
'type' => json_encode(['type' => MailmanType::class, 'data' => $typeParams]),
2024-10-20 21:19:07 +02:00
]);
});
it('test it updates a mailman gateway with password', function () {
test()->withoutExceptionHandling()->login()->loginNami();
2024-10-24 22:42:30 +02:00
$typeParams = MailmanTypeRequest::new()->state(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com']);
2024-10-20 21:19:07 +02:00
$newTypeParams = MailmanTypeRequest::new()->succeeds()->create(['url' => 'https://mailman.example.com', 'user' => 'newuser', 'password' => 'password', 'owner' => 'owner@example.com']);
2024-10-24 22:42:30 +02:00
$mailgateway = Mailgateway::factory()->type($typeParams->toData())->create();
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-24 22:42:30 +02:00
->set('type.user', 'newuser')
2024-10-20 21:19:07 +02:00
->call('onSave')
->assertHasNoErrors();
$this->assertDatabaseCount('mailgateways', 1);
$this->assertDatabaseHas('mailgateways', [
2024-10-24 22:42:30 +02:00
'type' => json_encode(['type' => MailmanType::class, 'data' => $newTypeParams]),
2024-10-20 21:19:07 +02:00
]);
});
it('test it checks mailgateway connection when updating', function () {
test()->withoutExceptionHandling()->login()->loginNami();
2024-10-24 22:42:30 +02:00
$typeParams = MailmanTypeRequest::new()->state(['url' => 'https://mailman.example.com', 'user' => 'user', 'password' => 'password', 'owner' => 'owner@example.com']);
2024-10-20 21:19:07 +02:00
MailmanTypeRequest::new()->fails()->create(['url' => 'https://mailman.example.com', 'user' => 'newuser', 'password' => 'password', 'owner' => 'owner@example.com']);
2024-10-24 22:42:30 +02:00
$mailgateway = Mailgateway::factory()->type($typeParams->toData())->create();
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-24 22:42:30 +02:00
->set('type.user', 'newuser')
2024-10-20 21:19:07 +02:00
->call('onSave')
->assertHasErrors('connection');
});