def test_get_all_droplets_call_requests_get_once(self):
     aut = Automator('whatever-test-token')
     stashed = aut.requests.get
     aut.requests.get = MagicMock()
     aut.get_all_droplets()
     aut.requests.get.assert_called_once()
     aut.requests.get = stashed
 def test_get_all_droplets_call_requests_w_args(self):
     aut = Automator('whatever-test-token')
     stashed = aut.requests.get
     aut.requests.get = MagicMock()
     aut.get_all_droplets()
     aut.requests.get.assert_called_once_with(
         'https://api.digitalocean.com/v2/droplets')
     aut.requests.get = stashed
    def test_request_get_should_be_called_3_times(self):
        responses.reset()
        aut = Automator('whatever-test-token')

        # mocks
        list_droplets_response()
        stashed = aut.requests.get
        aut.requests.get = MagicMock()
        aut.requests.get.side_effect = self.get_responses()

        aut.get_all_droplets()

        assert aut.requests.get.call_count == 3
        aut.requests.get = stashed
 def test_get_all_droplets_should_return_list(self):
     aut = Automator('whatever-test-token')
     list_droplets_response()
     stashed = aut.requests.get
     aut.requests.get = MagicMock()
     aut.requests.get.side_effect = self.get_responses()
     assert type(aut.get_all_droplets()) is list
     aut.requests.get = stashed
 def test_get_all_droplets_item_should_contain_keys(self):
     some_expected_keys = ['id', 'name', 'region']
     aut = Automator('whatever-test-token')
     list_droplets_response()
     stashed = aut.requests.get
     aut.requests.get = MagicMock()
     aut.requests.get.side_effect = self.get_responses()
     assert set(some_expected_keys) <= set(aut.get_all_droplets()[0])
     aut.requests.get = stashed
    def test_sum_all_droplets_should_be_the_same_as_meta(self):
        # test failing cauz of responses bug: https://github.com/getsentry/responses/issues/352
        # responses.reset()
        # aut = Automator('whatever-test-token')
        # meta_total = get_json_from_file(
        #     'list_droplets_page_1.json')['meta']['total']
        # list_droplets_response()
        # assert len(aut.get_all_droplets()) == meta_total

        # rewriting temporary:
        aut = Automator('whatever-test-token')
        meta_total = get_json_from_file(
            'list_droplets_page_1.json')['meta']['total']

        # mocks
        list_droplets_response()
        stashed = aut.requests.get
        aut.requests.get = MagicMock()
        aut.requests.get.side_effect = self.get_responses()

        assert len(aut.get_all_droplets()) == meta_total

        aut.requests.get = stashed
import ipdb
from pydoautomator import Automator
import os

digital_ocean_token = os.getenv("DO_TOKEN")

# requires ENV
name = os.getenv('DROPLET_HOST')
floating = os.getenv('FLOATING_IP')
snapshot = os.getenv('SNAPSHOT_ID')


aut = Automator(digital_ocean_token)

droplets = aut.get_all_droplets()

print('trying to turn off droplet named %s' % name )
for droplet in droplets:
    if droplet['name'] == name:
        droplet_id = droplet['id']
        print('turning off droplet id %s' % droplet_id)
        aut.turnoff_droplet(droplet_id)
        print('destroying droplet id %s' % droplet_id)
        aut.destroy_droplet(droplet_id)