def test_get_topics__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()
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_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_get_topics__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()

    assert expected_topic_list == topic_list

    called_url = request_handler.get.call_args[0][0]
    assert BASE_URL + 'topics/' == 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