def test_put_subscription__http_error_code__raises_api_error(error_code):
    response = Mock()
    response.status_code = error_code

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

    client = SubscriptionManagerClient(request_handler=request_handler)

    with pytest.raises(APIError):
        client.put_subscription(1, Mock())
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_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_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