Throw exception if client sends invalid request data

This commit is contained in:
Matthias Gutjahr 2021-10-25 14:02:18 +02:00 committed by thrillfall
parent 056b12a667
commit bac196e0b5
3 changed files with 7 additions and 12 deletions

View File

@ -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"],

View File

@ -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());

View File

@ -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);
}
}