Ejemplo n.º 1
0
def test_assert_requests_post_is_called_once():
    token = 'my-test-token-123'
    aut = Automator(token)
    stashed = aut.requests.post
    droplet_id = 212611563

    full_url = 'https://api.digitalocean.com/v2/droplets/%s/actions' % droplet_id

    data = {"type": "shutdown"}
    json_headers = {'Content-Type': 'application/json'}

    # mock response w/ success status
    responses.reset()
    shutdown_droplet_success_response(droplet_id)
    mocked_response = requests.post(full_url)

    stashed = aut._Automator__check_action_status
    stashed2 = aut.requests.post
    aut._Automator__check_action_status = MagicMock(return_value='completed')
    aut.requests.post = MagicMock(return_value=mocked_response)

    aut.turnoff_droplet(droplet_id)

    aut.requests.post.assert_called_once_with(full_url,
                                              json=data,
                                              headers=json_headers)

    aut._Automator__check_action_status.assert_called_once_with(
        mocked_response.json()['action']['id'])

    aut._Automator__check_action_status = stashed
    aut.requests.post = stashed2
def test_assert_request_post_is_called_with_args():
    floating_ip = '124.52.234.6'
    droplet_id = 42312552

    full_url = 'https://api.digitalocean.com/v2/floating_ips/%s/actions' % floating_ip
    data = {"type": "assign", "droplet_id": 8219222}
    json_headers = {'Content-Type': 'application/json'}

    token = 'my-test-token-123'
    aut = Automator(token)

    stashed = aut.requests.post
    stashed2 = aut._Automator__check_action_status

    # mock response w/ success status
    responses.reset()
    assign_floating_ip_success_response(floating_ip, droplet_id)
    mocked_response = requests.post(full_url)

    aut._Automator__check_action_status = MagicMock(return_value='completed')
    aut.requests.post = MagicMock(return_value=mocked_response)

    aut.assign_floating_ip_to_droplet('124.52.234.6', 8219222)

    aut.requests.post.assert_called_once_with(full_url,
                                              json=data,
                                              headers=json_headers)

    aut.requests.post = stashed
    aut._Automator__check_action_status = stashed2
Ejemplo n.º 3
0
def test_if_response_status_201_return_completed():
    token = 'my-test-token-123'
    aut = Automator(token)
    droplet_id = 212611563
    # response mock w/ 201 response
    shutdown_droplet_success_response(droplet_id)

    stashed = aut._Automator__check_action_status

    aut._Automator__check_action_status = MagicMock(return_value='completed')

    result = aut.turnoff_droplet(droplet_id)
    assert result == 'completed'
    aut._Automator__check_action_status = stashed
def test_if_response_status_201_return_completed():
    token = 'my-test-token-123'
    aut = Automator(token)
    floating_ip = '135.155.152.2'
    droplet_id = 42143152
    # response mock w/ 201 response
    assign_floating_ip_success_response(floating_ip, droplet_id)

    stashed = aut._Automator__check_action_status
    aut._Automator__check_action_status = MagicMock(return_value='completed')

    result = aut.assign_floating_ip_to_droplet(floating_ip, droplet_id)
    assert result == 'completed'
    aut._Automator__check_action_status = stashed
def test_assert_requests_post_is_called_once():
    token = 'my-test-token-123'
    aut = Automator(token)
    stashed = aut.requests.post
    floating_ip = '124.52.234.6'
    droplet_id = 42312552

    full_url = 'https://api.digitalocean.com/v2/floating_ips/%s/actions' % floating_ip

    # mock response w/ success status
    responses.reset()
    assign_floating_ip_success_response(floating_ip, droplet_id)
    mocked_response = requests.post(full_url)

    stashed = aut._Automator__check_action_status
    stashed2 = aut.requests.post
    aut._Automator__check_action_status = MagicMock(return_value='completed')
    aut.requests.post = MagicMock(return_value=mocked_response)
    aut.assign_floating_ip_to_droplet(floating_ip, droplet_id)

    aut.requests.post.assert_called_once()

    aut._Automator__check_action_status = stashed
    aut.requests.post = stashed2