2023-06-01 15:45:02 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Mailgateway;
|
|
|
|
|
|
|
|
use App\Mailgateway\Types\LocalType;
|
2023-06-07 22:52:02 +02:00
|
|
|
use App\Mailgateway\Types\MailmanType;
|
2023-06-01 15:45:02 +02:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2023-06-07 22:52:02 +02:00
|
|
|
use Phake;
|
2023-06-01 15:45:02 +02:00
|
|
|
use Tests\RequestFactories\MailgatewayRequestFactory;
|
2023-06-07 22:52:02 +02:00
|
|
|
use Tests\RequestFactories\MailmanTypeRequest;
|
2023-06-01 15:45:02 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class StoreTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->login()->loginNami();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItCanStoreALocalGateway(): void
|
|
|
|
{
|
2023-06-07 22:52:02 +02:00
|
|
|
$response = $this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->name('lala')->type(LocalType::class, [])->domain('example.com')->create());
|
2023-06-01 15:45:02 +02:00
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('mailgateways', [
|
|
|
|
'domain' => 'example.com',
|
|
|
|
'name' => 'lala',
|
2023-06-07 21:43:11 +02:00
|
|
|
'type' => json_encode([
|
|
|
|
'cls' => LocalType::class,
|
|
|
|
'params' => [],
|
|
|
|
]),
|
2023-06-01 15:45:02 +02:00
|
|
|
]);
|
|
|
|
}
|
2023-06-07 22:52:02 +02:00
|
|
|
|
|
|
|
public function testItCanStoreAMailmanGateway(): void
|
|
|
|
{
|
2023-07-19 10:44:17 +02:00
|
|
|
$typeParams = ['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret', 'owner' => 'owner@example.com'];
|
2023-06-07 22:52:02 +02:00
|
|
|
$this->stubIo(MailmanType::class, function ($mock) use ($typeParams) {
|
|
|
|
Phake::when($mock)->setParams($typeParams)->thenReturn($mock);
|
|
|
|
Phake::when($mock)->works()->thenReturn(true);
|
2023-07-19 10:44:17 +02:00
|
|
|
Phake::when($mock)->setOwner('owner@example.com')->thenReturn($mock);
|
2023-06-07 22:52:02 +02:00
|
|
|
});
|
|
|
|
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->type(MailmanType::class, MailmanTypeRequest::new()->create($typeParams))->create());
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('mailgateways', [
|
|
|
|
'type' => json_encode([
|
|
|
|
'cls' => MailmanType::class,
|
|
|
|
'params' => $typeParams,
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItThrowsErrorWhenMailmanConnectionFailed(): void
|
|
|
|
{
|
2023-07-19 10:44:17 +02:00
|
|
|
$typeParams = ['url' => 'https://example.com', 'user' => 'user', 'password' => 'secret', 'owner' => 'owner@example.com'];
|
2023-06-07 22:52:02 +02:00
|
|
|
$this->stubIo(MailmanType::class, function ($mock) use ($typeParams) {
|
|
|
|
Phake::when($mock)->setParams($typeParams)->thenReturn($mock);
|
|
|
|
Phake::when($mock)->works()->thenReturn(false);
|
2023-07-19 10:44:17 +02:00
|
|
|
Phake::when($mock)->setOwner('owner@example.com')->thenReturn($mock);
|
2023-06-07 22:52:02 +02:00
|
|
|
});
|
|
|
|
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->type(MailmanType::class, MailmanTypeRequest::new()->create($typeParams))->create())
|
|
|
|
->assertJsonValidationErrors('connection');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItValidatesCustomFields(): void
|
|
|
|
{
|
2023-07-19 10:44:17 +02:00
|
|
|
$typeParams = ['url' => 'https://example.com', 'user' => '', 'password' => 'secret', 'owner' => 'aaaa'];
|
2023-06-07 22:52:02 +02:00
|
|
|
$this->stubIo(MailmanType::class, function ($mock) use ($typeParams) {
|
|
|
|
Phake::when($mock)->setParams($typeParams)->thenReturn($mock);
|
|
|
|
Phake::when($mock)->works()->thenReturn(false);
|
|
|
|
});
|
|
|
|
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->type(MailmanType::class, MailmanTypeRequest::new()->create($typeParams))->create())
|
2023-07-19 10:44:17 +02:00
|
|
|
->assertJsonValidationErrors([
|
|
|
|
'type.params.user' => 'Benutzer ist erforderlich.',
|
|
|
|
'type.params.owner' => 'E-Mail-Adresse des Eigentümers muss eine gültige E-Mail-Adresse sein.',
|
|
|
|
]);
|
2023-06-07 22:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItValidatesType(): void
|
|
|
|
{
|
|
|
|
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->missingType()->create())
|
|
|
|
->assertJsonValidationErrors('type.cls');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItValidatesNameAndDomain(): void
|
|
|
|
{
|
|
|
|
$this->postJson('/api/mailgateway', MailgatewayRequestFactory::new()->withoutName()->withoutDomain()->create())
|
|
|
|
->assertJsonValidationErrors('domain')
|
|
|
|
->assertJsonValidationErrors('name');
|
|
|
|
}
|
2023-06-01 15:45:02 +02:00
|
|
|
}
|