fedi-feed-router/backend/tests/Unit/Services/Auth/LemmyAuthServiceTest.php

162 lines
5.7 KiB
PHP
Raw Normal View History

2025-08-06 21:49:13 +02:00
<?php
namespace Tests\Unit\Services\Auth;
2025-08-15 16:39:18 +02:00
use Domains\Platform\Enums\PlatformEnum;
use Domains\Platform\Exceptions\PlatformAuthException;
use Domains\Platform\Models\PlatformAccount;
use Domains\Platform\Services\Auth\Authenticators\LemmyAuthService;
2025-08-06 21:49:13 +02:00
use Illuminate\Foundation\Testing\RefreshDatabase;
2025-08-10 04:16:53 +02:00
use Illuminate\Support\Facades\Http;
2025-08-06 21:49:13 +02:00
use Tests\TestCase;
class LemmyAuthServiceTest extends TestCase
{
use RefreshDatabase;
2025-08-10 15:20:28 +02:00
2025-08-10 04:16:53 +02:00
protected function setUp(): void
{
parent::setUp();
2025-08-06 21:49:13 +02:00
}
2025-08-10 15:20:28 +02:00
public function test_get_token_successfully_authenticates(): void
2025-08-06 21:49:13 +02:00
{
2025-08-10 15:20:28 +02:00
// Mock successful HTTP response for both HTTPS and HTTP (fallback)
Http::fake([
'https://lemmy.test/api/v3/user/login' => Http::response(['jwt' => 'jwt-123'], 200),
'http://lemmy.test/api/v3/user/login' => Http::response(['jwt' => 'jwt-123'], 200)
]);
2025-08-10 04:16:53 +02:00
2025-08-06 21:49:13 +02:00
$account = PlatformAccount::factory()->create([
'username' => 'testuser',
'password' => 'testpass',
'instance_url' => 'https://lemmy.test'
]);
2025-08-10 15:20:28 +02:00
$result = app(LemmyAuthService::class)->getToken($account);
2025-08-06 21:49:13 +02:00
2025-08-10 15:20:28 +02:00
$this->assertEquals('jwt-123', $result);
2025-08-06 21:49:13 +02:00
}
public function test_get_token_throws_exception_when_username_missing(): void
{
2025-08-10 15:20:28 +02:00
$account = $this->createMock(PlatformAccount::class);
$account->method('__get')->willReturnCallback(function ($key) {
return match ($key) {
'username' => null,
'password' => 'testpass',
'instance_url' => 'https://lemmy.test',
default => null,
};
});
2025-08-06 21:49:13 +02:00
$this->expectException(PlatformAuthException::class);
$this->expectExceptionMessage('Missing credentials for account: ');
2025-08-10 15:20:28 +02:00
app(LemmyAuthService::class)->getToken($account);
2025-08-06 21:49:13 +02:00
}
public function test_get_token_throws_exception_when_password_missing(): void
{
2025-08-10 15:20:28 +02:00
$account = $this->createMock(PlatformAccount::class);
$account->method('__get')->willReturnCallback(function ($key) {
return match ($key) {
'username' => 'testuser',
'password' => null,
'instance_url' => 'https://lemmy.test',
default => null,
};
});
2025-08-06 21:49:13 +02:00
$this->expectException(PlatformAuthException::class);
$this->expectExceptionMessage('Missing credentials for account: testuser');
2025-08-10 15:20:28 +02:00
app(LemmyAuthService::class)->getToken($account);
2025-08-06 21:49:13 +02:00
}
public function test_get_token_throws_exception_when_instance_url_missing(): void
{
2025-08-10 15:20:28 +02:00
$account = $this->createMock(PlatformAccount::class);
$account->method('__get')->willReturnCallback(function ($key) {
return match ($key) {
'username' => 'testuser',
'password' => 'testpass',
'instance_url' => null,
default => null,
};
});
2025-08-06 21:49:13 +02:00
$this->expectException(PlatformAuthException::class);
$this->expectExceptionMessage('Missing credentials for account: testuser');
2025-08-10 15:20:28 +02:00
app(LemmyAuthService::class)->getToken($account);
2025-08-06 21:49:13 +02:00
}
public function test_get_token_throws_exception_when_login_fails(): void
{
2025-08-10 04:16:53 +02:00
// Mock failed HTTP response for both HTTPS and HTTP
Http::fake([
'https://lemmy.test/api/v3/user/login' => Http::response(['error' => 'Invalid credentials'], 401),
'http://lemmy.test/api/v3/user/login' => Http::response(['error' => 'Invalid credentials'], 401)
]);
2025-08-10 15:20:28 +02:00
$account = $this->createMock(PlatformAccount::class);
$account->method('__get')->willReturnCallback(function ($key) {
return match ($key) {
'username' => 'failingUser',
'password' => 'badpass',
'instance_url' => 'https://lemmy.test',
default => null,
};
});
2025-08-10 01:26:56 +02:00
$this->expectException(PlatformAuthException::class);
$this->expectExceptionMessage('Login failed for account: failingUser');
2025-08-10 15:20:28 +02:00
app(LemmyAuthService::class)->getToken($account);
2025-08-06 21:49:13 +02:00
}
public function test_get_token_throws_exception_when_login_returns_false(): void
{
2025-08-10 04:16:53 +02:00
// Mock response with empty/missing JWT for both HTTPS and HTTP
Http::fake([
'https://lemmy.test/api/v3/user/login' => Http::response(['success' => false], 200),
'http://lemmy.test/api/v3/user/login' => Http::response(['success' => false], 200)
]);
2025-08-10 15:20:28 +02:00
$account = $this->createMock(PlatformAccount::class);
$account->method('__get')->willReturnCallback(function ($key) {
return match ($key) {
'username' => 'emptyUser',
'password' => 'pass',
'instance_url' => 'https://lemmy.test',
default => null,
};
});
2025-08-10 01:26:56 +02:00
$this->expectException(PlatformAuthException::class);
$this->expectExceptionMessage('Login failed for account: emptyUser');
2025-08-10 15:20:28 +02:00
app(LemmyAuthService::class)->getToken($account);
2025-08-06 21:49:13 +02:00
}
public function test_platform_auth_exception_contains_correct_platform(): void
{
2025-08-10 15:20:28 +02:00
$account = $this->createMock(PlatformAccount::class);
$account->method('__get')->willReturnCallback(function ($key) {
return match ($key) {
'username' => null,
'password' => 'testpass',
'instance_url' => 'https://lemmy.test',
default => null,
};
});
2025-08-06 21:49:13 +02:00
try {
2025-08-10 15:20:28 +02:00
app(LemmyAuthService::class)->getToken($account);
2025-08-06 21:49:13 +02:00
$this->fail('Expected PlatformAuthException to be thrown');
} catch (PlatformAuthException $e) {
$this->assertEquals(PlatformEnum::LEMMY, $e->getPlatform());
}
}
2025-08-10 15:20:28 +02:00
}