1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
import os
try:
import mock
except ImportError:
from unittest import mock
import responses
import requests
import digitalocean
from .BaseTest import BaseTest
class TestBaseAPI(BaseTest):
def setUp(self):
super(TestBaseAPI, self).setUp()
self.manager = digitalocean.Manager(token=self.token)
self.user_agent = "{0}/{1} {2}/{3}".format('python-digitalocean',
digitalocean.__version__,
requests.__name__,
requests.__version__)
@responses.activate
def test_user_agent(self):
data = self.load_from_file('account/account.json')
url = self.base_url + 'account/'
responses.add(responses.GET, url,
body=data,
status=200,
content_type='application/json')
self.manager.get_account()
self.assertEqual(responses.calls[0].request.headers['User-Agent'],
self.user_agent)
@responses.activate
def test_customize_session(self):
data = self.load_from_file('account/account.json')
url = self.base_url + 'account/'
responses.add(responses.GET, url,
body=data,
status=200,
content_type='application/json')
self.manager._session.proxies['https'] = 'https://127.0.0.1:3128'
self.manager.get_account()
def test_custom_endpoint(self):
custom_endpoint = 'http://example.com/'
with mock.patch.dict(os.environ,
{'DIGITALOCEAN_END_POINT': custom_endpoint},
clear=True):
base_api = digitalocean.baseapi.BaseAPI()
self.assertEqual(base_api.end_point, custom_endpoint)
def test_invalid_custom_endpoint(self):
custom_endpoint = 'not a valid endpoint'
with mock.patch.dict(os.environ,
{'DIGITALOCEAN_END_POINT': custom_endpoint},
clear=True):
self.assertRaises(digitalocean.EndPointError, digitalocean.baseapi.BaseAPI)
|