-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_iland_int.py
More file actions
executable file
·162 lines (129 loc) · 5.57 KB
/
test_iland_int.py
File metadata and controls
executable file
·162 lines (129 loc) · 5.57 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_iland_int
----------------------------------
Integration tests for the `iland` module.
"""
import time
import unittest
import iland
import iland.constant
from iland.exception import ApiException, UnauthorizedException
try:
# only PyCharm w/ Python3 fails here without dot
from apicreds import (CLIENT_ID,
CLIENT_SECRET,
USERNAME,
PASSWORD,
PROXIES)
except ImportError:
try:
from .apicreds import (CLIENT_ID,
CLIENT_SECRET,
USERNAME,
PASSWORD,
PROXIES)
except ImportError:
CLIENT_ID = None
CLIENT_SECRET = None
USERNAME = None
PASSWORD = None
PROXIES = {}
VDC_UUID = \
'res01.ilandcloud.com:urn:vcloud:vdc:a066325d-6be0-4733-8d9f-7687c36f4536'
@unittest.skipIf(not CLIENT_ID and not CLIENT_SECRET and not USERNAME and not
PASSWORD, "No credentials provided")
class TestIlandInt(unittest.TestCase):
def setUp(self):
self._api = iland.Api(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
username=USERNAME,
password=PASSWORD)
self._api._base_url = iland.constant.BASE_URL
def tearDown(self):
pass
def test_get(self):
user = self._api.get('/users/' + USERNAME)
self.assertEqual(USERNAME, user.get('name'))
self.assertTrue(len(user.keys()) > 5)
def test_post(self):
user_alert_emails = self._api.get('/users/' + USERNAME +
'/alert-emails')
self.assertTrue(len(user_alert_emails['emails']) >= 1)
self.assertEquals(user_alert_emails['username'], USERNAME)
old_user_alert_emails = user_alert_emails
user_alert_emails = {'emails': ['test@iland.com', 'test2@iland.com'], }
self._api.post('/users/' + USERNAME + '/actions/update-alert-emails',
user_alert_emails)
self.assertEqual(2, len(user_alert_emails['emails']))
self._api.post('/users/' + USERNAME + '/actions/update-alert-emails',
old_user_alert_emails)
self.assertEquals(len(user_alert_emails['emails']), 2)
def test_put_delete(self):
vdc_uuid = VDC_UUID
vdc_md = self._api.get('/vdcs/' + vdc_uuid + '/metadata')
self.assertEquals(len(vdc_md['data']), 0)
new_md = [{'type': 'string',
'value': 'B',
'access': 'READ_WRITE',
'key': 'AAA'}, ]
new_md.extend(vdc_md['data'])
self._api.put('/vdcs/' + vdc_uuid + '/metadata', form_data=new_md)
time.sleep(10)
updated_md = self._api.get('/vdcs/' + vdc_uuid + '/metadata')
self.assertEquals(len(updated_md['data']), 1)
self._api.delete('/vdcs/' + vdc_uuid + '/metadata/' + 'AAA')
time.sleep(10)
updated_md = self._api.get('/vdcs/' + vdc_uuid + '/metadata')
self.assertEquals(len(updated_md['data']), 0)
def test_get_with_host_header(self):
user = self._api.get('/users/' + USERNAME,
headers={'Host': 'api.ilandcloud.com'})
self.assertEqual(USERNAME, user.get('name'))
self.assertTrue(len(user.keys()) > 5)
def test_get_with_disallowed_header(self):
user = self._api.get('/users/' + USERNAME,
headers={'Accept': 'text/csv'})
self.assertEqual(USERNAME, user.get('name'))
self.assertTrue(len(user.keys()) > 5)
def test_refresh_token(self):
self._api.login()
self.assertIsNotNone(self._api.get_access_token())
expires_in = self._api._token_expiration_time
# force expires for tests.
self._api._token_expiration_time = 0
self._api.refresh_access_token()
new_expires_in = self._api._token_expiration_time
self.assertTrue(new_expires_in > expires_in)
def test_unauthorized_errors(self):
wrongCredsApi = iland.Api(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
username='PYTHON_SDK_TEST',
password='XXXX')
wrongCredsApi._base_url = iland.constant.BASE_URL
with self.assertRaises(UnauthorizedException):
wrongCredsApi.login()
with self.assertRaises(UnauthorizedException):
wrongCredsApi.get_access_token()
with self.assertRaises(UnauthorizedException):
wrongCredsApi.refresh_access_token()
def test_api_errors(self):
with self.assertRaises(ApiException):
self._api.get('/doesnotexist')
def test_api_exception_properties(self):
with self.assertRaises(ApiException) as e:
self._api.get('/doesnotexist')
api_exception = e.exception
self.assertEqual(api_exception.error,
'NotFoundError')
self.assertEqual(api_exception.message,
'The specified resource does not exist.')
self.assertIn('Could not find resource for full path',
api_exception.detail_message)
@unittest.skipIf(not PROXIES, "No proxies defined")
def test_get_with_proxy(self):
self._api._proxies = PROXIES
user = self._api.get('/users/' + USERNAME)
self.assertEqual(USERNAME, user.get('name'))
self.assertTrue(len(user.keys()) > 5)