2022-10-18 15:21:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Mailman;
|
|
|
|
|
2022-10-20 11:11:52 +02:00
|
|
|
use App\Mailman\Data\MailingList;
|
2022-10-18 16:44:36 +02:00
|
|
|
use App\Mailman\Exceptions\MailmanServiceException;
|
2022-10-18 15:21:14 +02:00
|
|
|
use App\Mailman\Support\MailmanService;
|
2022-10-18 16:44:36 +02:00
|
|
|
use Generator;
|
2022-10-18 15:21:14 +02:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2022-10-20 11:11:52 +02:00
|
|
|
use Tests\RequestFactories\MailmanListRequestFactory;
|
2022-10-18 15:21:14 +02:00
|
|
|
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')]);
|
|
|
|
}
|
2022-10-18 16:44:36 +02:00
|
|
|
|
|
|
|
public function testItGetsMembersFromList(): void
|
|
|
|
{
|
|
|
|
Http::fake([
|
|
|
|
'http://mailman.test/api/lists/listid/roster/member?page=1&count=10' => Http::response(json_encode([
|
|
|
|
'entries' => [
|
2023-06-13 21:21:30 +02:00
|
|
|
['email' => 'test@example.com', 'self_link' => 'https://example.com/994'],
|
2022-10-18 16:44:36 +02:00
|
|
|
],
|
|
|
|
'total_size' => 2,
|
|
|
|
]), 200),
|
|
|
|
]);
|
|
|
|
|
2023-06-13 21:21:30 +02:00
|
|
|
$result = app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->members(MailingList::factory()->id('listid')->toData())->first();
|
2022-10-18 16:44:36 +02:00
|
|
|
|
2023-06-13 21:21:30 +02:00
|
|
|
$this->assertEquals(994, $result->memberId);
|
|
|
|
$this->assertEquals('test@example.com', $result->email);
|
2022-10-18 16:44:36 +02:00
|
|
|
Http::assertSentCount(1);
|
|
|
|
Http::assertSent(fn ($request) => 'GET' === $request->method() && 'http://mailman.test/api/lists/listid/roster/member?page=1&count=10' === $request->url() && $request->header('Authorization') === ['Basic '.base64_encode('user:secret')]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItThrowsExceptionWhenLoginFailed(): void
|
|
|
|
{
|
|
|
|
$this->expectException(MailmanServiceException::class);
|
|
|
|
Http::fake([
|
|
|
|
'http://mailman.test/api/lists/listid/roster/member?page=1&count=10' => Http::response('', 401),
|
|
|
|
]);
|
|
|
|
|
2023-06-13 21:21:30 +02:00
|
|
|
app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->members(MailingList::factory()->id('listid')->toData())->first();
|
2022-10-18 16:44:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:11:52 +02:00
|
|
|
public function testItCanGetLists(): void
|
|
|
|
{
|
|
|
|
Http::fake([
|
|
|
|
'http://mailman.test/api/lists?page=1&count=10' => Http::sequence()
|
|
|
|
->push(json_encode([
|
|
|
|
'entries' => [
|
|
|
|
MailmanListRequestFactory::new()->create(['display_name' => 'Eltern', 'fqdn_listname' => 'eltern@example.com']),
|
|
|
|
MailmanListRequestFactory::new()->create(['display_name' => 'Eltern2', 'fqdn_listname' => 'eltern2@example.com']),
|
|
|
|
],
|
|
|
|
'start' => 0,
|
|
|
|
'total_size' => 2,
|
|
|
|
]), 200),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$lists = app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->getLists()->all();
|
|
|
|
$this->assertCount(2, $lists);
|
|
|
|
$this->assertInstanceOf(MailingList::class, $lists[0]);
|
|
|
|
$this->assertEquals('Eltern', $lists[0]->displayName);
|
|
|
|
}
|
|
|
|
|
2022-10-18 16:44:36 +02:00
|
|
|
public function listDataProvider(): Generator
|
|
|
|
{
|
|
|
|
foreach (range(3, 40) as $i) {
|
|
|
|
yield [
|
|
|
|
collect(range(1, $i))
|
2023-06-13 21:21:30 +02:00
|
|
|
->map(fn ($num) => ['email' => 'test'.$num.'@example.com', 'self_link' => 'https://example.com/994'])
|
2022-10-18 16:44:36 +02:00
|
|
|
->toArray(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider listDataProvider
|
|
|
|
*/
|
|
|
|
public function testItReturnsMoreThanOneResult(array $totals): void
|
|
|
|
{
|
|
|
|
$totals = collect($totals);
|
|
|
|
foreach ($totals->chunk(10) as $n => $chunk) {
|
|
|
|
Http::fake([
|
|
|
|
'http://mailman.test/api/lists/listid/roster/member?page='.($n + 1).'&count=10' => Http::response(json_encode([
|
|
|
|
'entries' => $chunk,
|
|
|
|
'total_size' => $totals->count(),
|
|
|
|
]), 200),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-06-13 21:21:30 +02:00
|
|
|
$result = app(MailmanService::class)->setCredentials('http://mailman.test/api/', 'user', 'secret')->members(MailingList::factory()->id('listid')->toData());
|
2022-10-18 16:44:36 +02:00
|
|
|
|
|
|
|
$this->assertCount($totals->count(), $result->toArray());
|
|
|
|
Http::assertSentCount($totals->chunk(10)->count());
|
|
|
|
}
|
2022-10-18 15:21:14 +02:00
|
|
|
}
|