2025-08-10 04:16:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Modules\Lemmy\Services;
|
|
|
|
|
|
|
|
|
|
use App\Enums\PlatformEnum;
|
2026-03-08 14:18:28 +01:00
|
|
|
use App\Modules\Lemmy\Services\LemmyApiService;
|
|
|
|
|
use Exception;
|
2026-02-25 23:22:05 +01:00
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
2025-08-10 04:16:53 +02:00
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class LemmyApiServiceTest extends TestCase
|
|
|
|
|
{
|
2026-02-25 23:22:05 +01:00
|
|
|
use RefreshDatabase;
|
2025-08-10 04:16:53 +02:00
|
|
|
|
|
|
|
|
public function test_constructor_sets_instance(): void
|
|
|
|
|
{
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
2025-08-11 18:26:00 +02:00
|
|
|
|
2025-08-10 04:16:53 +02:00
|
|
|
$reflection = new \ReflectionClass($service);
|
|
|
|
|
$property = $reflection->getProperty('instance');
|
|
|
|
|
$property->setAccessible(true);
|
2025-08-11 18:26:00 +02:00
|
|
|
|
2025-08-10 04:16:53 +02:00
|
|
|
$this->assertEquals('lemmy.world', $property->getValue($service));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_login_with_https_success(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'https://lemmy.world/api/v3/user/login' => Http::response(['jwt' => 'test-token'], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$token = $service->login('user', 'pass');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('test-token', $token);
|
|
|
|
|
|
|
|
|
|
Http::assertSent(function ($request) {
|
|
|
|
|
return $request->url() === 'https://lemmy.world/api/v3/user/login'
|
|
|
|
|
&& $request['username_or_email'] === 'user'
|
|
|
|
|
&& $request['password'] === 'pass';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_login_falls_back_to_http_on_https_failure(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
|
|
|
|
'https://lemmy.world/api/v3/user/login' => Http::response('', 500),
|
2026-03-08 14:18:28 +01:00
|
|
|
'http://lemmy.world/api/v3/user/login' => Http::response(['jwt' => 'http-token'], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$token = $service->login('user', 'pass');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('http-token', $token);
|
|
|
|
|
|
|
|
|
|
Http::assertSentCount(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_login_with_explicit_http_scheme(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'http://localhost/api/v3/user/login' => Http::response(['jwt' => 'local-token'], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('http://localhost');
|
|
|
|
|
$token = $service->login('user', 'pass');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('local-token', $token);
|
|
|
|
|
|
|
|
|
|
Http::assertSent(function ($request) {
|
|
|
|
|
return $request->url() === 'http://localhost/api/v3/user/login';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_login_with_explicit_https_scheme(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'https://secure.lemmy/api/v3/user/login' => Http::response(['jwt' => 'secure-token'], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('https://secure.lemmy');
|
|
|
|
|
$token = $service->login('user', 'pass');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('secure-token', $token);
|
|
|
|
|
|
|
|
|
|
Http::assertSent(function ($request) {
|
|
|
|
|
return $request->url() === 'https://secure.lemmy/api/v3/user/login';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_login_returns_null_on_unsuccessful_response(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response(['error' => 'Invalid credentials'], 401),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$token = $service->login('user', 'wrong');
|
|
|
|
|
|
|
|
|
|
$this->assertNull($token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_login_handles_rate_limit_error(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response('{"error":"rate_limit_error"}', 429),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
2025-08-11 18:26:00 +02:00
|
|
|
|
2026-02-25 23:22:05 +01:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
|
$this->expectExceptionMessage('Rate limited');
|
|
|
|
|
|
|
|
|
|
$service->login('user', 'pass');
|
2025-08-10 04:16:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_login_returns_null_when_jwt_missing_from_response(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response(['success' => true], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$token = $service->login('user', 'pass');
|
|
|
|
|
|
|
|
|
|
$this->assertNull($token);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 23:22:05 +01:00
|
|
|
public function test_login_handles_exception_and_throws(): void
|
2025-08-10 04:16:53 +02:00
|
|
|
{
|
|
|
|
|
Http::fake(function () {
|
|
|
|
|
throw new Exception('Network error');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
|
2026-02-25 23:22:05 +01:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
|
$this->expectExceptionMessage('Connection failed');
|
|
|
|
|
|
|
|
|
|
$service->login('user', 'pass');
|
2025-08-10 04:16:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_get_community_id_success(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*' => Http::response([
|
|
|
|
|
'community_view' => [
|
2026-03-08 14:18:28 +01:00
|
|
|
'community' => ['id' => 123],
|
|
|
|
|
],
|
|
|
|
|
], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$id = $service->getCommunityId('test-community', 'token');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(123, $id);
|
|
|
|
|
|
|
|
|
|
Http::assertSent(function ($request) {
|
|
|
|
|
return str_contains($request->url(), '/api/v3/community')
|
|
|
|
|
&& str_contains($request->url(), 'name=test-community')
|
|
|
|
|
&& $request->header('Authorization')[0] === 'Bearer token';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_get_community_id_throws_on_unsuccessful_response(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response('Not found', 404),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
|
|
|
|
|
$this->expectException(Exception::class);
|
|
|
|
|
$this->expectExceptionMessage('Failed to fetch community: 404');
|
|
|
|
|
|
|
|
|
|
$service->getCommunityId('missing', 'token');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_get_community_id_throws_when_community_not_in_response(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response(['success' => true], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
|
|
|
|
|
$this->expectException(Exception::class);
|
|
|
|
|
$this->expectExceptionMessage('Community not found');
|
|
|
|
|
|
|
|
|
|
$service->getCommunityId('test', 'token');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_sync_channel_posts_success(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*' => Http::response([
|
|
|
|
|
'posts' => [
|
|
|
|
|
[
|
|
|
|
|
'post' => [
|
|
|
|
|
'id' => 1,
|
|
|
|
|
'url' => 'https://example.com/1',
|
|
|
|
|
'name' => 'Post 1',
|
2026-03-08 14:18:28 +01:00
|
|
|
'published' => '2024-01-01T00:00:00Z',
|
|
|
|
|
],
|
2025-08-10 04:16:53 +02:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'post' => [
|
|
|
|
|
'id' => 2,
|
|
|
|
|
'url' => 'https://example.com/2',
|
|
|
|
|
'name' => 'Post 2',
|
2026-03-08 14:18:28 +01:00
|
|
|
'published' => '2024-01-02T00:00:00Z',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$service->syncChannelPosts('token', 42, 'test-community');
|
|
|
|
|
|
|
|
|
|
Http::assertSent(function ($request) {
|
|
|
|
|
return str_contains($request->url(), '/api/v3/post/list')
|
|
|
|
|
&& str_contains($request->url(), 'community_id=42')
|
|
|
|
|
&& str_contains($request->url(), 'limit=50')
|
|
|
|
|
&& str_contains($request->url(), 'sort=New');
|
|
|
|
|
});
|
2026-02-25 23:22:05 +01:00
|
|
|
|
|
|
|
|
// Verify posts were stored in the database
|
|
|
|
|
$this->assertDatabaseHas('platform_channel_posts', [
|
|
|
|
|
'platform' => PlatformEnum::LEMMY->value,
|
|
|
|
|
'channel_id' => '42',
|
|
|
|
|
'channel_name' => 'test-community',
|
|
|
|
|
'post_id' => '1',
|
|
|
|
|
'url' => 'https://example.com/1',
|
|
|
|
|
'title' => 'Post 1',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('platform_channel_posts', [
|
|
|
|
|
'platform' => PlatformEnum::LEMMY->value,
|
|
|
|
|
'channel_id' => '42',
|
|
|
|
|
'channel_name' => 'test-community',
|
|
|
|
|
'post_id' => '2',
|
|
|
|
|
'url' => 'https://example.com/2',
|
|
|
|
|
'title' => 'Post 2',
|
|
|
|
|
]);
|
2025-08-10 04:16:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_sync_channel_posts_handles_unsuccessful_response(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response('Error', 500),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$service->syncChannelPosts('token', 42, 'test-community');
|
|
|
|
|
|
|
|
|
|
Http::assertSentCount(1);
|
2026-02-25 23:22:05 +01:00
|
|
|
$this->assertDatabaseCount('platform_channel_posts', 0);
|
2025-08-10 04:16:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_sync_channel_posts_handles_exception(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake(function () {
|
|
|
|
|
throw new Exception('Network error');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$service->syncChannelPosts('token', 42, 'test-community');
|
2025-08-11 18:26:00 +02:00
|
|
|
|
2025-08-10 04:16:53 +02:00
|
|
|
// Assert that the method completes without throwing
|
|
|
|
|
$this->assertTrue(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_create_post_with_all_parameters(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response(['post_view' => ['post' => ['id' => 999]]], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$result = $service->createPost(
|
|
|
|
|
'token',
|
|
|
|
|
'Test Title',
|
|
|
|
|
'Test Body',
|
|
|
|
|
42,
|
|
|
|
|
'https://example.com',
|
|
|
|
|
'https://example.com/thumb.jpg',
|
|
|
|
|
5
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(['post_view' => ['post' => ['id' => 999]]], $result);
|
|
|
|
|
|
|
|
|
|
Http::assertSent(function ($request) {
|
|
|
|
|
$data = $request->data();
|
2026-03-08 14:18:28 +01:00
|
|
|
|
2025-08-10 04:16:53 +02:00
|
|
|
return $request->url() === 'https://lemmy.world/api/v3/post'
|
|
|
|
|
&& $data['name'] === 'Test Title'
|
|
|
|
|
&& $data['body'] === 'Test Body'
|
|
|
|
|
&& $data['community_id'] === 42
|
|
|
|
|
&& $data['url'] === 'https://example.com'
|
|
|
|
|
&& $data['custom_thumbnail'] === 'https://example.com/thumb.jpg'
|
|
|
|
|
&& $data['language_id'] === 5;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_create_post_with_minimal_parameters(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response(['post_view' => ['post' => ['id' => 888]]], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$result = $service->createPost(
|
|
|
|
|
'token',
|
|
|
|
|
'Title Only',
|
|
|
|
|
'Body Only',
|
|
|
|
|
42
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(['post_view' => ['post' => ['id' => 888]]], $result);
|
|
|
|
|
|
|
|
|
|
Http::assertSent(function ($request) {
|
|
|
|
|
$data = $request->data();
|
2026-03-08 14:18:28 +01:00
|
|
|
|
2025-08-10 04:16:53 +02:00
|
|
|
return $request->url() === 'https://lemmy.world/api/v3/post'
|
|
|
|
|
&& $data['name'] === 'Title Only'
|
|
|
|
|
&& $data['body'] === 'Body Only'
|
|
|
|
|
&& $data['community_id'] === 42
|
2026-03-08 14:18:28 +01:00
|
|
|
&& ! isset($data['url'])
|
|
|
|
|
&& ! isset($data['custom_thumbnail'])
|
|
|
|
|
&& ! isset($data['language_id']);
|
2025-08-10 04:16:53 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_create_post_throws_on_unsuccessful_response(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response('Forbidden', 403),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
|
|
|
|
|
$this->expectException(Exception::class);
|
|
|
|
|
$this->expectExceptionMessage('Failed to create post: 403');
|
|
|
|
|
|
|
|
|
|
$service->createPost('token', 'Title', 'Body', 42);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_get_languages_success(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
|
|
|
|
'*' => Http::response([
|
|
|
|
|
'all_languages' => [
|
|
|
|
|
['id' => 1, 'code' => 'en', 'name' => 'English'],
|
2026-03-08 14:18:28 +01:00
|
|
|
['id' => 2, 'code' => 'fr', 'name' => 'French'],
|
|
|
|
|
],
|
|
|
|
|
], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$languages = $service->getLanguages();
|
|
|
|
|
|
|
|
|
|
$this->assertCount(2, $languages);
|
|
|
|
|
$this->assertEquals('en', $languages[0]['code']);
|
|
|
|
|
$this->assertEquals('fr', $languages[1]['code']);
|
|
|
|
|
|
|
|
|
|
Http::assertSent(function ($request) {
|
|
|
|
|
return str_contains($request->url(), '/api/v3/site');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_get_languages_returns_empty_array_on_failure(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response('Error', 500),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$languages = $service->getLanguages();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals([], $languages);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_get_languages_handles_exception(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake(function () {
|
|
|
|
|
throw new Exception('Network error');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$languages = $service->getLanguages();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals([], $languages);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_get_languages_returns_empty_when_all_languages_missing(): void
|
|
|
|
|
{
|
|
|
|
|
Http::fake([
|
2026-03-08 14:18:28 +01:00
|
|
|
'*' => Http::response(['site_view' => []], 200),
|
2025-08-10 04:16:53 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new LemmyApiService('lemmy.world');
|
|
|
|
|
$languages = $service->getLanguages();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals([], $languages);
|
|
|
|
|
}
|
2025-08-11 18:26:00 +02:00
|
|
|
}
|