Fix encryption issue in tests

This commit is contained in:
myrmidex 2025-08-04 21:58:51 +02:00
parent 727a562b0d
commit 40a0c9d4e6
3 changed files with 64 additions and 28 deletions

View file

@ -40,10 +40,6 @@ public function boot(): void
\App\Listeners\PublishArticle::class,
);
Event::listen(
\App\Events\ExceptionLogged::class,
\App\Listeners\LogExceptionToDatabase::class,
);
app()->make(ExceptionHandler::class)
->reportable(function (Throwable $e) {

View file

@ -52,41 +52,72 @@ public function test_article_discovery_for_feed_job_processes_feed(): void
$job = new ArticleDiscoveryForFeedJob($feed);
// Mock the external dependency behavior
$this->app->bind(\App\Services\Article\ArticleFetcher::class, function () {
$mock = \Mockery::mock(\App\Services\Article\ArticleFetcher::class);
$mock->shouldReceive('fetchArticles')->andReturn([
['title' => 'Test Article', 'url' => 'https://example.com/article1'],
['title' => 'Another Article', 'url' => 'https://example.com/article2']
]);
return $mock;
});
// Mock the ArticleFetcher to return created articles
$mockFetcher = \Mockery::mock('alias:' . \App\Services\Article\ArticleFetcher::class);
$article1 = Article::factory()->create(['url' => 'https://example.com/article1', 'feed_id' => $feed->id]);
$article2 = Article::factory()->create(['url' => 'https://example.com/article2', 'feed_id' => $feed->id]);
$mockFetcher->shouldReceive('getArticlesFromFeed')
->with($feed)
->andReturn(collect([$article1, $article2]));
$job->handle();
// Should create articles and fire events
// Should have articles in database
$this->assertCount(2, Article::all());
Event::assertDispatched(NewArticleFetched::class, 2);
// Note: Events are not fired by ArticleDiscoveryForFeedJob directly
// They would be fired by the Article model when created
}
public function test_sync_channel_posts_job_processes_successfully(): void
{
$channel = PlatformChannel::factory()->create();
// Verify encryption is properly configured
$key = config('app.key');
$cipher = config('app.cipher');
$this->assertNotNull($key, 'APP_KEY should be set');
// The supported method expects the raw key, not the base64: prefixed version
$rawKey = base64_decode(substr($key, 7)); // Remove 'base64:' prefix and decode
$this->assertTrue(app('encrypter')->supported($rawKey, $cipher), 'Encryption should be supported');
$channel = PlatformChannel::factory()->create([
'channel_id' => '' // Empty string to trigger getCommunityId call
]);
// Create platform account with proper factory
$account = \App\Models\PlatformAccount::factory()->create([
'is_active' => true,
'username' => 'testuser',
'platform' => 'lemmy',
'instance_url' => 'https://lemmy.example.com'
]);
// Attach the account to the channel with active status
$channel->platformAccounts()->attach($account->id, [
'is_active' => true,
'priority' => 1
]);
$job = new SyncChannelPostsJob($channel);
// Mock the external dependency
$this->app->bind(\App\Modules\Lemmy\Services\LemmyApiService::class, function () {
$mock = \Mockery::mock(\App\Modules\Lemmy\Services\LemmyApiService::class);
$mock->shouldReceive('getChannelPosts')->andReturn([]);
return $mock;
});
// Mock the LemmyApiService class
$mockApi = \Mockery::mock('overload:' . \App\Modules\Lemmy\Services\LemmyApiService::class);
$mockApi->shouldReceive('login')
->with('testuser', 'test-password') // From factory default
->once()
->andReturn('fake-jwt-token');
$mockApi->shouldReceive('getCommunityId')
->once()
->andReturn(123);
$mockApi->shouldReceive('syncChannelPosts')
->once()
->andReturn(true);
$result = $job->handle();
$job->handle();
$this->assertTrue($result);
$this->assertTrue(true); // If we get here without exception, test passes
}
public function test_publish_to_lemmy_job_has_correct_configuration(): void
{
$article = Article::factory()->create();
@ -178,6 +209,14 @@ public function test_validate_article_listener_processes_new_article(): void
'validated_at' => null
]);
// Mock ArticleFetcher to return valid article data
$mockFetcher = \Mockery::mock('alias:' . \App\Services\Article\ArticleFetcher::class);
$mockFetcher->shouldReceive('fetchArticleData')
->with($article)
->andReturn([
'full_article' => 'Test article content'
]);
$listener = new ValidateArticleListener();
$event = new NewArticleFetched($article);
@ -232,7 +271,8 @@ public function test_log_exception_to_database_listener_creates_log(): void
]);
$listener = new LogExceptionToDatabase();
$event = new ExceptionLogged($log);
$exception = new \Exception('Test exception message');
$event = new ExceptionOccurred($exception, \App\LogLevelEnum::ERROR, 'Test exception message');
$listener->handle($event);
@ -243,7 +283,7 @@ public function test_log_exception_to_database_listener_creates_log(): void
$savedLog = Log::where('message', 'Test exception message')->first();
$this->assertNotNull($savedLog);
$this->assertEquals('error', $savedLog->level);
$this->assertEquals(\App\LogLevelEnum::ERROR, $savedLog->level);
}
public function test_event_listener_registration_works(): void
@ -258,7 +298,7 @@ public function test_event_listener_registration_works(): void
$listeners = Event::getListeners(ArticleReadyToPublish::class);
$this->assertNotEmpty($listeners);
$listeners = Event::getListeners(ExceptionLogged::class);
$listeners = Event::getListeners(ExceptionOccurred::class);
$this->assertNotEmpty($listeners);
}

View file

@ -9,7 +9,7 @@ services:
environment:
- APP_ENV=local
- APP_DEBUG=true
- APP_KEY=base64:YOUR_APP_KEY_HERE
- APP_KEY=base64:5VABFQKtzx6flRFn7rQUQYI/G8xLnkUSYPVaYz2s/4M=
- DB_CONNECTION=mysql
- DB_HOST=db
- DB_PORT=3306