From bac196e0b54355f3afee0ef60a14e569c9fa8204 Mon Sep 17 00:00:00 2001 From: Matthias Gutjahr Date: Mon, 25 Oct 2021 14:02:18 +0200 Subject: [PATCH] Throw exception if client sends invalid request data --- lib/Core/EpisodeAction/EpisodeActionReader.php | 5 ++++- .../Controller/EpisodeActionControllerTest.php | 9 --------- .../Unit/Core/EpisodeAction/EpisodeActionReaderTest.php | 5 +++-- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/Core/EpisodeAction/EpisodeActionReader.php b/lib/Core/EpisodeAction/EpisodeActionReader.php index 4a2efef..6ef5182 100644 --- a/lib/Core/EpisodeAction/EpisodeActionReader.php +++ b/lib/Core/EpisodeAction/EpisodeActionReader.php @@ -3,19 +3,22 @@ declare(strict_types=1); namespace OCA\GPodderSync\Core\EpisodeAction; +use InvalidArgumentException; + class EpisodeActionReader { private array $requiredProperties = ['podcast', 'episode', 'action', 'timestamp']; /** * @param array $episodeActionsArray [] * @return EpisodeAction[] + * @throws InvalidArgumentException */ public function fromArray(array $episodeActionsArray): array { $episodeActions = []; foreach ($episodeActionsArray as $episodeAction) { if ($this->hasRequiredProperties($episodeAction) === false) { - continue; + throw new InvalidArgumentException(sprintf('Client sent incomplete or invalid data: %s', json_encode($episodeAction, JSON_THROW_ON_ERROR))); } $episodeActions[] = new EpisodeAction( $episodeAction["podcast"], diff --git a/tests/Integration/Controller/EpisodeActionControllerTest.php b/tests/Integration/Controller/EpisodeActionControllerTest.php index da7fb02..9cb6070 100644 --- a/tests/Integration/Controller/EpisodeActionControllerTest.php +++ b/tests/Integration/Controller/EpisodeActionControllerTest.php @@ -100,15 +100,6 @@ class EpisodeActionControllerTest extends TestCase "started": 15, "position": 120, "total": 500 - }, - { - "podcast": "https://example.org/podcast.php", - "episode": "https://example.com/files/foo-bar-123.mp3", - "guid": "foo-bar-123", - "timestamp": "2009-12-12T09:05:21", - "started": -1, - "position": -1, - "total": -1 } ]', true, 512, JSON_THROW_ON_ERROR); $request = new Request([], new SecureRandom(), $this->getMockBuilder(IConfig::class)->getMock()); diff --git a/tests/Unit/Core/EpisodeAction/EpisodeActionReaderTest.php b/tests/Unit/Core/EpisodeAction/EpisodeActionReaderTest.php index 0774c6d..d1b54df 100644 --- a/tests/Unit/Core/EpisodeAction/EpisodeActionReaderTest.php +++ b/tests/Unit/Core/EpisodeAction/EpisodeActionReaderTest.php @@ -57,11 +57,12 @@ class EpisodeActionReaderTest extends TestCase { } public function testCreateWithFaultyData(): void { - $episodeActions = (new EpisodeActionReader())->fromArray([ + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Client sent incomplete or invalid data: {"podcast":"https:\/\/example.org\/feed.xml","action":"download","timestamp":"2021-10-03T12:03:17"}'); + (new EpisodeActionReader())->fromArray([ ["podcast" => "https://example.org/feed.xml", "action" => "download", "timestamp" => "2021-10-03T12:03:17"], ["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode2.mp3", "guid" => "episode2", "action" => "download", "timestamp" => "2021-10-03T12:03:17"], ]); - $this->assertCount(1, $episodeActions); } }