def test_subscribe__sm_api_error__raises_SubscriptionManagerServiceError(): sm_client = SubscriptionManagerClient(mock.Mock()) sm_client.get_topics = mock.Mock(return_value=[Topic('topic1')]) sm_service = SubscriptionManagerService(sm_client) sm_client.post_subscription = mock.Mock(side_effect=APIError('server error', status_code=500)) with pytest.raises(SubscriptionManagerServiceError) as e: sm_service.subscribe('topic1')
def test_apierror_from_response(): detail = 'some_error' status_code = 400 response = Mock() response.json = Mock(return_value={'detail': detail}) response.status_code = status_code api_error = APIError.from_response(response) assert api_error.detail == detail assert api_error.status_code == status_code assert f"[{status_code}] - {detail}" == str(api_error)
def test_unsubscribe__sm_api_error__raises_SubscriptionManagerServiceError(): subscription = Subscription(id=1, queue=uuid.uuid4().hex) sm_client = SubscriptionManagerClient(mock.Mock()) sm_service = SubscriptionManagerService(sm_client) sm_service._get_subscription_by_queue = mock.Mock(return_value=subscription) sm_client.delete_subscription_by_id = mock.Mock(side_effect=APIError('server error', status_code=500)) with pytest.raises(SubscriptionManagerServiceError) as e: sm_service.unsubscribe(subscription.queue) assert f"Error while deleting subscription '{subscription.id}': [500] - server error" == str(e.value)
def test_api_error_from_response__empty_list_for_404_error_is_ignored(): status_code = 404 response = Mock() response.json = Mock(return_value=[]) response.status_code = status_code response.text = "" api_error = APIError.from_response(response) expected_detail = "" assert expected_detail == api_error.detail assert status_code == api_error.status_code assert f"[{status_code}] - {expected_detail}" == str(api_error)
def test_client__is_valid__is_false_if_credentials_are_incorrect(): broker_handler = Mock() sm_service = Mock() sm_service.sm_client = Mock() sm_service.client.ping_credentials = Mock( side_effect=APIError('detail', 401)) client = PubSubClient(broker_handler=broker_handler, sm_service=sm_service) assert client.is_valid() is False sm_service.client.ping_credentials.assert_called_once() # ping credentials should be called only the first time client.is_valid() sm_service.client.ping_credentials.assert_called_once()
def test_resume__sm_api_error__raises_SubscriptionManagerServiceError(): subscription = Subscription(id=1, queue=uuid.uuid4().hex, active=False) sm_client = SubscriptionManagerClient(mock.Mock()) sm_service = SubscriptionManagerService(sm_client) sm_service._get_subscription_by_queue = mock.Mock(return_value=subscription) sm_client.put_subscription = mock.Mock(side_effect=APIError('server error', status_code=500)) with pytest.raises(SubscriptionManagerServiceError) as e: sm_service.resume(subscription.queue) assert f"Error while updating subscription '{subscription.id}': [500] - server error" == str(e.value) called_subscription_id, called_subscription = sm_client.put_subscription.call_args[0] assert called_subscription_id == subscription.id assert called_subscription.active is True
def test_publisher__register_topic__sm_error__raises_ClientError(): broker_handler = mock.Mock() sm_service = mock.Mock() sm_service.create_topic = Mock( side_effect=APIError(status_code=500, detail="error")) def data_handler(context=None): return "data" topic = Topic(topic_name='topic', data_handler=data_handler) publisher = Publisher(broker_handler, sm_service) with pytest.raises(PubSubClientError) as e: publisher.register_topic(topic) assert f"Error while creating topic in SM: [500] - error" == str(e.value) assert topic not in publisher.topics_dict.values()
def test_publisher__register_topic__sm_error_409__logs_message(caplog): caplog.set_level(logging.DEBUG) broker_handler = mock.Mock() sm_service = mock.Mock() sm_service.create_topic = Mock( side_effect=APIError(status_code=409, detail="error")) def data_handler(context=None): return "data" topic = Topic(topic_name='topic', data_handler=data_handler) publisher = Publisher(broker_handler, sm_service) publisher.register_topic(topic) log_message = caplog.records[0] assert f"Topic with name {topic.name} already exists in SM" == log_message.message assert topic in publisher.topics_dict.values()
def delete_queue_binding(self, queue: str, topic: str, key: str) -> None: """ Deletes a queue binding :param queue: the name of the queue :param topic: the topic the queue is bound to :param key: the routing_key of the binding """ if topic == 'default': topic = 'amq.topic' bindings = self.get_queue_bindings(queue, topic=topic, key=key) if not bindings: raise APIError( f"No binding found between topic '{topic}' and queue '{queue}' with name '{key}'", 404) props = bindings[0]['properties_key'] url = self._get_delete_queue_binding_url(queue, topic, props) self.perform_request('DELETE', url)
def test_get_user__http_error_code__raises_api_error(error_code): response = Mock() response.status_code = error_code request_handler = Mock() request_handler.get = Mock(return_value=response) client = RabbitMQRestClient(request_handler=request_handler) with pytest.raises(APIError): client.get_user('name') @pytest.mark.parametrize('get_user_response, exists', [(RabbitMQUser('name'), True), (APIError('error', 400), False), (APIError('error', 401), False), (APIError('error', 403), False), (APIError('error', 404), False), (APIError('error', 500), False)]) def test_user_exists(get_user_response, exists): request_handler = Mock() client = RabbitMQRestClient(request_handler=request_handler) if isinstance(get_user_response, APIError): client.get_user = Mock(side_effect=get_user_response) else: client.get_user = Mock(return_value=get_user_response) assert exists == client.user_exists('name')
def _check_status_code(response): if response.status_code not in [200, 201, 204]: raise APIError.from_response(response)