Add mailman service

This commit is contained in:
Philipp Lang 2022-10-18 15:21:14 +02:00
parent 3976e8f502
commit 5cd3578b36
2 changed files with 58 additions and 0 deletions

View File

@ -2,13 +2,33 @@
namespace App\Mailman\Support;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
class MailmanService
{
private string $baseUrl;
private string $username;
private string $password;
public function setCredentials(string $baseUrl, string $username, string $password): self
{
$this->baseUrl = $baseUrl;
$this->username = $username;
$this->password = $password;
return $this;
}
public function check(): bool
{
$response = $this->http()->get('/system/versions');
return 200 === $response->status();
}
private function http(): PendingRequest
{
return Http::withBasicAuth($this->username, $this->password)->withOptions(['base_uri' => $this->baseUrl]);
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Mailman;
use App\Mailman\Support\MailmanService;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class ServiceTest extends TestCase
{
public function testItChecksForCredentials(): void
{
Http::fake([
'http://mailman.test/api/system/versions' => Http::response('', 200),
]);
$result = app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->check();
$this->assertTrue($result);
Http::assertSentCount(1);
Http::assertSent(fn ($request) => 'GET' === $request->method() && 'http://mailman.test/api/system/versions' === $request->url() && $request->header('Authorization') === ['Basic '.base64_encode('user:secret')]);
}
public function testItFailsWhenChckingCredentials(): void
{
Http::fake([
'http://mailman.test/api/system/versions' => Http::response('', 401),
]);
$result = app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->check();
$this->assertFalse($result);
Http::assertSentCount(1);
Http::assertSent(fn ($request) => 'GET' === $request->method() && 'http://mailman.test/api/system/versions' === $request->url() && $request->header('Authorization') === ['Basic '.base64_encode('user:secret')]);
}
}