adrema/tests/Feature/Mailman/SettingTest.php

151 lines
5.6 KiB
PHP
Raw Normal View History

2022-10-18 15:04:47 +02:00
<?php
namespace Tests\Feature\Mailman;
2022-10-20 12:01:57 +02:00
use App\Mailman\Data\MailingList;
2022-10-18 15:04:47 +02:00
use App\Mailman\MailmanSettings;
use App\Mailman\Support\MailmanService;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2022-10-20 12:01:57 +02:00
use Illuminate\Support\LazyCollection;
2022-10-18 15:04:47 +02:00
use Phake;
2022-10-20 12:01:57 +02:00
use Tests\RequestFactories\MailmanListRequestFactory;
2022-10-18 15:04:47 +02:00
use Tests\TestCase;
class SettingTest extends TestCase
{
use DatabaseTransactions;
public function testItGetsMailSettings(): void
{
$this->withoutExceptionHandling()->login()->loginNami();
2022-10-20 03:22:50 +02:00
$this->stubIo(MailmanService::class, function ($mock) {
2022-10-20 12:01:57 +02:00
Phake::when($mock)->fromSettings(Phake::anyParameters())->thenReturn($mock);
2022-10-20 03:22:50 +02:00
Phake::when($mock)->check()->thenReturn(true);
2022-10-20 12:01:57 +02:00
Phake::when($mock)->getLists()->thenReturn(LazyCollection::make(function () {
yield MailingList::from(MailmanListRequestFactory::new()->create(['list_id' => 'F', 'fqdn_listname' => 'admin@example.com']));
}));
2022-10-20 03:22:50 +02:00
});
2022-10-18 15:04:47 +02:00
MailmanSettings::fake([
'base_url' => 'http://mailman.test/api',
'username' => 'user',
'password' => 'secret',
2022-10-20 03:22:50 +02:00
'is_active' => true,
2022-10-18 15:04:47 +02:00
]);
$response = $this->get('/setting/mailman');
$response->assertOk();
$this->assertInertiaHas([
'base_url' => 'http://mailman.test/api',
'username' => 'user',
'password' => '',
2022-10-20 03:22:50 +02:00
'is_active' => true,
2022-10-18 15:04:47 +02:00
], $response, 'data');
2022-10-20 03:22:50 +02:00
$this->assertInertiaHas(true, $response, 'state');
2022-10-20 12:01:57 +02:00
$this->assertInertiaHas('admin@example.com', $response, 'lists.0.name');
$this->assertInertiaHas('F', $response, 'lists.0.id');
2022-10-20 03:22:50 +02:00
}
public function testItReturnsWrongStateWhenLoginFailed(): void
{
$this->withoutExceptionHandling()->login()->loginNami();
$this->stubIo(MailmanService::class, function ($mock) {
2022-10-20 12:01:57 +02:00
Phake::when($mock)->fromSettings(Phake::anyParameters())->thenReturn($mock);
2022-10-20 03:22:50 +02:00
Phake::when($mock)->check()->thenReturn(false);
});
MailmanSettings::fake([
'base_url' => 'http://mailman.test/api',
'username' => 'user',
'password' => 'secret',
'is_active' => true,
]);
$response = $this->get('/setting/mailman');
$response->assertOk();
$this->assertInertiaHas(false, $response, 'state');
}
public function testItDoesntReturnAnyStateWhenMailmanIsInactive(): void
{
$this->withoutExceptionHandling()->login()->loginNami();
2022-10-20 12:01:57 +02:00
$this->stubIo(MailmanService::class, fn ($mock) => $mock);
2022-10-20 03:22:50 +02:00
MailmanSettings::fake([
'base_url' => 'http://mailman.test/api',
'username' => 'user',
'password' => 'secret',
'is_active' => false,
]);
$response = $this->get('/setting/mailman');
$response->assertOk();
$this->assertInertiaHas(null, $response, 'state');
Phake::verifyNoInteraction(app(MailmanService::class));
2022-10-18 15:04:47 +02:00
}
2022-10-20 03:22:50 +02:00
public function testItSetsMailmanSettings(): void
2022-10-18 15:04:47 +02:00
{
$this->stubIo(MailmanService::class, function ($mock) {
Phake::when($mock)->setCredentials('http://mailman.test/api', 'user', 'secret')->thenReturn($mock);
Phake::when($mock)->check()->thenReturn(true);
});
$this->withoutExceptionHandling()->login()->loginNami();
$response = $this->from('/setting/mailman')->post('/setting/mailman', [
'base_url' => 'http://mailman.test/api',
'username' => 'user',
'password' => 'secret',
2022-10-20 03:22:50 +02:00
'is_active' => true,
2022-10-20 12:01:57 +02:00
'all_parents_list' => 'P',
'all_list' => 'X',
2022-10-18 15:04:47 +02:00
]);
$response->assertRedirect('/setting/mailman');
$settings = app(MailmanSettings::class);
$this->assertEquals('http://mailman.test/api', $settings->base_url);
$this->assertEquals('secret', $settings->password);
$this->assertEquals('user', $settings->username);
2022-10-20 12:01:57 +02:00
$this->assertEquals('X', $settings->all_list);
$this->assertEquals('P', $settings->all_parents_list);
2022-10-18 15:04:47 +02:00
Phake::verify(app(MailmanService::class))->setCredentials('http://mailman.test/api', 'user', 'secret');
Phake::verify(app(MailmanService::class))->check();
}
public function testItThrowsErrorWhenLoginFailed(): void
{
$this->stubIo(MailmanService::class, function ($mock) {
Phake::when($mock)->setCredentials('http://mailman.test/api', 'user', 'secret')->thenReturn($mock);
Phake::when($mock)->check()->thenReturn(false);
});
$this->login()->loginNami();
$response = $this->from('/setting/mailman')->post('/setting/mailman', [
'base_url' => 'http://mailman.test/api',
'username' => 'user',
'password' => 'secret',
2022-10-20 03:22:50 +02:00
'is_active' => true,
2022-10-18 15:04:47 +02:00
]);
$response->assertSessionHasErrors(['mailman' => 'Verbindung fehlgeschlagen.']);
Phake::verify(app(MailmanService::class))->setCredentials('http://mailman.test/api', 'user', 'secret');
Phake::verify(app(MailmanService::class))->check();
}
public function testItValidatesPassword(): void
{
$this->stubIo(MailmanService::class, fn ($mock) => $mock);
$this->login()->loginNami();
$response = $this->from('/setting/mailman')->post('/setting/mailman', [
'base_url' => 'http://mailman.test/api',
'username' => 'user',
'password' => '',
2022-10-20 03:22:50 +02:00
'is_active' => true,
]);
$response->assertSessionHasErrors(['password' => 'Passwort ist erforderlich.']);
Phake::verifyNoInteraction(app(MailmanService::class));
}
2022-10-18 15:04:47 +02:00
}