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

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

    client = SubscriptionManagerClient(request_handler=request_handler)

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