def test_get_topics(sm_topics): sm_client = SubscriptionManagerClient(mock.Mock()) sm_client.get_topics = mock.Mock(return_value=sm_topics) sm_service = SubscriptionManagerService(sm_client) assert sm_topics == sm_service.get_topics()
def test_subscribe__topic_name_is_not_registered_in_sm__raises_SubscriptionManagerServiceError(): sm_client = SubscriptionManagerClient(mock.Mock()) sm_client.get_topics = mock.Mock(return_value=[Topic('topic1')]) sm_service = SubscriptionManagerService(sm_client) with pytest.raises(SubscriptionManagerServiceError) as e: sm_service.subscribe('topic2') assert 'topic2 is not registered in Subscription Manager' == str(e.value)
def test__get_subscription_by_queue__no_subscriptions_found__raises_SubscriptionManagerServiceError(): sm_client = SubscriptionManagerClient(mock.Mock()) sm_service = SubscriptionManagerService(sm_client) sm_client.get_subscriptions = mock.Mock(return_value=[]) with pytest.raises(SubscriptionManagerServiceError) as e: sm_service._get_subscription_by_queue('queue') assert "No subscription found for queue 'queue'" == str(e.value)
def test_delete_topic_by_id__http_error_code__raises_api_error(error_code): response = Mock() response.status_code = error_code request_handler = Mock() request_handler.delete = Mock(return_value=response) client = SubscriptionManagerClient(request_handler=request_handler) with pytest.raises(APIError): client.delete_topic_by_id(1)
def test_get_topics_own__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 = SubscriptionManagerClient(request_handler=request_handler) with pytest.raises(APIError): client.get_topics_own()
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_create_topic(): sm_client = SubscriptionManagerClient(mock.Mock()) sm_client.post_topic = mock.Mock() sm_service = SubscriptionManagerService(sm_client) sm_service.create_topic('topic_name') called_topic = sm_client.post_topic.call_args[0][0] assert called_topic.name == 'topic_name'
def test__get_subscription_by_name__subscription_is_found_and_returned(): subscription = Subscription(queue='queue') sm_client = SubscriptionManagerClient(mock.Mock()) sm_service = SubscriptionManagerService(sm_client) sm_client.get_subscriptions = mock.Mock(return_value=[subscription]) db_subscription = sm_service._get_subscription_by_queue(subscription.queue) assert db_subscription == subscription
def test_delete_topic(): sm_client = SubscriptionManagerClient(mock.Mock()) mock_delete_topic_by_id = mock.Mock() sm_client.delete_topic_by_id = mock_delete_topic_by_id sm_service = SubscriptionManagerService(sm_client) topic = Topic(name='topic', id=1) sm_service.delete_topic(topic) mock_delete_topic_by_id.assert_called_once_with(topic_id=topic.id)
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_delete_subscription_by_id(): response = Mock() response.status_code = 204 response.content = {} response.json = Mock(return_value={}) request_handler = Mock() request_handler.delete = Mock(return_value=response) client = SubscriptionManagerClient(request_handler=request_handler) subscription = client.delete_subscription_by_id(1) called_url = request_handler.delete.call_args[0][0] assert BASE_URL + 'subscriptions/1' == called_url
def test_resume__no_errors(): 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() sm_service.resume(subscription.queue) 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_unsubscribe__no_errors(): 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() sm_service.unsubscribe(subscription.queue) called_subscription_id = sm_client.delete_subscription_by_id.call_args[0][0] assert subscription.id == called_subscription_id
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 _get_sm_client(): return SubscriptionManagerClient.create( host=current_app.config['SUBSCRIPTION-MANAGER-API']['host'], https=current_app.config['SUBSCRIPTION-MANAGER-API']['https'], timeout=current_app.config['SUBSCRIPTION-MANAGER-API']['timeout'], verify=current_app.config['SUBSCRIPTION-MANAGER-API']['verify'], username=current_app.config['SUBSCRIPTION-MANAGER-API']['username'], password=current_app.config['SUBSCRIPTION-MANAGER-API']['password'])
def test_post_topic__topic_object_is_returned(): topic_dict, expected_topic = make_topic() response = Mock() response.status_code = 201 response.content = topic_dict response.json = Mock(return_value=topic_dict) request_handler = Mock() request_handler.post = Mock(return_value=response) client = SubscriptionManagerClient(request_handler=request_handler) topic = client.post_topic(Mock()) assert expected_topic == topic called_url = request_handler.post.call_args[0][0] assert BASE_URL + 'topics/' == called_url
def test_get_subscription_by_id__list_of_subscriptions_is_returned(): subscription_dict, expected_subscription = make_subscription() response = Mock() response.status_code = 200 response.content = subscription_dict response.json = Mock(return_value=subscription_dict) request_handler = Mock() request_handler.get = Mock(return_value=response) client = SubscriptionManagerClient(request_handler=request_handler) subscription = client.get_subscription_by_id(1) assert expected_subscription == subscription called_url = request_handler.get.call_args[0][0] assert BASE_URL + 'subscriptions/1' == called_url
def test_put_subscription__subscription_object_is_returned(): subscription_dict, expected_subscription = make_subscription() response = Mock() response.status_code = 200 response.content = subscription_dict response.json = Mock(return_value=subscription_dict) request_handler = Mock() request_handler.put = Mock(return_value=response) client = SubscriptionManagerClient(request_handler=request_handler) subscription = client.put_subscription(1, {'active': False}) assert expected_subscription == subscription called_url = request_handler.put.call_args[0][0] assert BASE_URL + 'subscriptions/1' == called_url
def test_get_topics_own__list_of_topics_is_returned(): topic_dict_list, expected_topic_list = make_topic_list() response = Mock() response.status_code = 200 response.content = topic_dict_list response.json = Mock(return_value=topic_dict_list) request_handler = Mock() request_handler.get = Mock(return_value=response) client = SubscriptionManagerClient(request_handler=request_handler) topic_list = client.get_topics_own() assert expected_topic_list == topic_list called_url = request_handler.get.call_args[0][0] assert BASE_URL + 'topics/own' == called_url
def test_subscribe__no_errors__returns_subscription_queue(): topic = Topic(name='topics', id=1) sm_client = SubscriptionManagerClient(mock.Mock()) sm_client.get_topics = mock.Mock(return_value=[topic]) sm_service = SubscriptionManagerService(sm_client) subscription = Subscription(queue=uuid.uuid4().hex, topic_id=topic.id) sm_client.post_subscription = mock.Mock(return_value=subscription) queue = sm_service.subscribe('topics') assert subscription.queue == queue called_subscription = sm_client.post_subscription.call_args[0][0] assert topic.id == called_subscription.topic_id
def _create_sm_client(config: ConfigDict, username: str, password: str) -> SubscriptionManagerClient: """ Create a SubscriptionManagerClient but first make a check whether the given credentials a valid. :param config: :param username: :param password: :return: """ sm_client: SubscriptionManagerClient = SubscriptionManagerClient.create( host=config['host'], https=config['https'], timeout=config['timeout'], verify=config['verify'], username=username, password=password ) return sm_client