adrema/modules/Mailgateway/UpdateTest.php

112 lines
4.7 KiB
PHP

<?php
namespace Modules\Mailgateway;
use Modules\Mailgateway\Models\Mailgateway;
use Modules\Mailgateway\Types\LocalType;
use Modules\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();
$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']);
Livewire::test(Form::class, ['id' => $mailgateway->id])
->assertSet('model', fn ($m) => $m->is($mailgateway))
->assertSet('name', '::name::')
->assertSet('domain', 'example.com')
->assertSet(
'type',
fn ($type) => $type instanceof MailmanType
&& $type->url === 'https://mailman.example.com'
&& $type->user === 'user' &&
$type->password === 'password'
&& $type->owner === 'owner'
);
});
it('test it sets attributes for local', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$mailgateway = Mailgateway::factory()->create(['name' => '::name::', 'domain' => 'example.com']);
Livewire::test(Form::class, ['id' => $mailgateway->id])
->assertSet('name', '::name::')
->assertSet('domain', 'example.com')
->assertSet('type', fn ($type) => $type instanceof LocalType);
});
it('test it validates type', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$mailgateway = Mailgateway::factory()->create(['name' => '::name::', 'domain' => 'example.com']);
Livewire::test(Form::class, ['id' => $mailgateway->id])
->set('typeClass', '')
->assertHasErrors(['typeClass' => 'required']);
});
it('test it updates a mailman gateway without updating password', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$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']);
Livewire::test(Form::class, ['id' => $mailgateway->id])
->set('name', '::newname::')
->call('onSave')
->assertHasNoErrors()
->assertDispatched('closeModal')
->assertDispatched('refresh-page')
->assertDispatched('success');
$this->assertDatabaseCount('mailgateways', 1);
$this->assertDatabaseHas('mailgateways', [
'name' => '::newname::',
'type' => json_encode(['type' => MailmanType::class, 'data' => $typeParams]),
]);
});
it('test it updates a mailman gateway with password', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$typeParams = MailmanTypeRequest::new()->state(['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($typeParams->toData())->create();
Livewire::test(Form::class, ['id' => $mailgateway->id])
->set('type.user', 'newuser')
->call('onSave')
->assertHasNoErrors();
$this->assertDatabaseCount('mailgateways', 1);
$this->assertDatabaseHas('mailgateways', [
'type' => json_encode(['type' => MailmanType::class, 'data' => $newTypeParams]),
]);
});
it('test it checks mailgateway connection when updating', function () {
test()->withoutExceptionHandling()->login()->loginNami();
$typeParams = MailmanTypeRequest::new()->state(['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($typeParams->toData())->create();
Livewire::test(Form::class, ['id' => $mailgateway->id])
->set('type.user', 'newuser')
->call('onSave')
->assertHasErrors('connection');
});