From ae5653b31d4f8919f47197768c2efe681ce15155 Mon Sep 17 00:00:00 2001 From: Jordi Alhambra Date: Mon, 7 Jun 2021 13:47:49 +0100 Subject: [PATCH] Media package client error (#3983) * Add delete container and list tags endpoints to MediaStore * Black reformat * Fixed Lint problems * Check if dictionary was deleted effectively * lint fix * MediaPackageClientError * Lint Fix * Test unknown channel describe * Concatenation Fix * MediaPackage - fix error message Co-authored-by: av Co-authored-by: Bert Blommers --- moto/mediapackage/exceptions.py | 12 ++++++++++++ moto/mediapackage/models.py | 16 ++++++++++++---- tests/test_mediapackage/test_mediapackage.py | 16 +++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/moto/mediapackage/exceptions.py b/moto/mediapackage/exceptions.py index baffc4882..4aec6f98f 100644 --- a/moto/mediapackage/exceptions.py +++ b/moto/mediapackage/exceptions.py @@ -1 +1,13 @@ from __future__ import unicode_literals + +from moto.core.exceptions import JsonRESTError + + +class MediaPackageClientError(JsonRESTError): + code = 400 + + +# AWS service exceptions are caught with the underlying botocore exception, ClientError +class ClientError(MediaPackageClientError): + def __init__(self, error, message): + super(ClientError, self).__init__(error, message) diff --git a/moto/mediapackage/models.py b/moto/mediapackage/models.py index 207857fd1..519307c19 100644 --- a/moto/mediapackage/models.py +++ b/moto/mediapackage/models.py @@ -1,8 +1,12 @@ from __future__ import unicode_literals -from boto3 import Session -from moto.core import BaseBackend, BaseModel + from collections import OrderedDict +from boto3 import Session + +from moto.core import BaseBackend, BaseModel +from .exceptions import ClientError + class Channel(BaseModel): def __init__(self, *args, **kwargs): @@ -97,8 +101,12 @@ class MediaPackageBackend(BaseBackend): return response_channels def describe_channel(self, id): - channel = self._channels[id] - return channel.to_dict() + try: + channel = self._channels[id] + return channel.to_dict() + except KeyError: + error = "NotFoundException" + raise ClientError(error, "channel with id={} not found".format(id)) def delete_channel(self, id): channel = self._channels[id] diff --git a/tests/test_mediapackage/test_mediapackage.py b/tests/test_mediapackage/test_mediapackage.py index 9e883ddf3..574eb3bba 100644 --- a/tests/test_mediapackage/test_mediapackage.py +++ b/tests/test_mediapackage/test_mediapackage.py @@ -1,7 +1,10 @@ from __future__ import unicode_literals import boto3 +import pytest import sure # noqa +from botocore.exceptions import ClientError # Boto3 will always throw this exception + from moto import mock_mediapackage region = "eu-west-1" @@ -11,7 +14,7 @@ def _create_channel_config(**kwargs): id = kwargs.get("id", "channel-id") description = kwargs.get("description", "Awesome channel!") tags = kwargs.get("tags", {"Customer": "moto"}) - channel_config = dict(Description=description, Id=id, Tags=tags,) + channel_config = dict(Description=description, Id=id, Tags=tags) return channel_config @@ -81,6 +84,17 @@ def test_describe_channel_succeeds(): describe_response["Tags"]["Customer"].should.equal("moto") +@mock_mediapackage +def test_describe_unknown_channel_throws_error(): + client = boto3.client("mediapackage", region_name=region) + channel_id = "unknown-channel" + with pytest.raises(ClientError) as err: + client.describe_channel(Id=channel_id) + err = err.value.response["Error"] + err["Code"].should.equal("NotFoundException") + err["Message"].should.equal("channel with id={} not found".format(str(channel_id))) + + @mock_mediapackage def test_delete_channel_successfully_deletes(): client = boto3.client("mediapackage", region_name=region)