From 5cd3578b36a4c3f699a8e940bb3583b6dd2dd397 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Tue, 18 Oct 2022 15:21:14 +0200 Subject: [PATCH] Add mailman service --- app/Mailman/Support/MailmanService.php | 20 ++++++++++++++ tests/Unit/Mailman/ServiceTest.php | 38 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/Unit/Mailman/ServiceTest.php diff --git a/app/Mailman/Support/MailmanService.php b/app/Mailman/Support/MailmanService.php index d08a8a66..13df0634 100644 --- a/app/Mailman/Support/MailmanService.php +++ b/app/Mailman/Support/MailmanService.php @@ -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]); } } diff --git a/tests/Unit/Mailman/ServiceTest.php b/tests/Unit/Mailman/ServiceTest.php new file mode 100644 index 00000000..b1291c27 --- /dev/null +++ b/tests/Unit/Mailman/ServiceTest.php @@ -0,0 +1,38 @@ + 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')]); + } +}