def test_get_subscriptions__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_subscriptions()
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__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_get_subscriptions__list_of_subscriptions_is_returned():
    subscription_dict_list, expected_subscription_list = make_subscription_list(
    )

    response = Mock()
    response.status_code = 200
    response.content = subscription_dict_list
    response.json = Mock(return_value=subscription_dict_list)

    request_handler = Mock()
    request_handler.get = Mock(return_value=response)

    client = SubscriptionManagerClient(request_handler=request_handler)

    subscription_list = client.get_subscriptions()

    assert expected_subscription_list == subscription_list

    called_url = request_handler.get.call_args[0][0]
    assert BASE_URL + 'subscriptions/' == called_url