forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_test.py
More file actions
412 lines (352 loc) · 14 KB
/
account_test.py
File metadata and controls
412 lines (352 loc) · 14 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from collections.abc import Iterable
from copy import deepcopy
from datetime import datetime
from test.unit.base import ClientBaseCase
from linode_api4 import AccountSettingsInterfacesForNewLinodes
from linode_api4.objects import (
Account,
AccountAvailability,
AccountBetaProgram,
AccountSettings,
Database,
Domain,
Event,
Firewall,
Image,
Instance,
Invoice,
Login,
LongviewClient,
NodeBalancer,
OAuthClient,
PaymentMethod,
ServiceTransfer,
StackScript,
User,
UserGrants,
Volume,
get_obj_grants,
)
from linode_api4.objects.account import ChildAccount
from linode_api4.objects.vpc import VPC
class InvoiceTest(ClientBaseCase):
"""
Tests methods of the Invoice
"""
def test_get_invoice(self):
"""
Tests that an invoice is loaded correctly by ID
"""
invoice = Invoice(self.client, 123456)
self.assertEqual(invoice._populated, False)
self.assertEqual(invoice.label, "Invoice #123456")
self.assertEqual(invoice._populated, True)
self.assertEqual(invoice.date, datetime(2015, 1, 1, 5, 1, 2))
self.assertEqual(invoice.total, 9.51)
def test_get_invoice_items(self):
"""
Tests that you can get items for an invoice
"""
invoice = Invoice(self.client, 123456)
items = invoice.items
self.assertEqual(len(items), 1)
item = items[0]
self.assertEqual(item.label, "Linode 2048 - Example")
self.assertEqual(item.type, "hourly")
self.assertEqual(item.amount, 9.51)
self.assertEqual(item.quantity, 317)
self.assertEqual(item.unit_price, "0.03")
self.assertEqual(
item.from_date,
datetime(year=2014, month=12, day=19, hour=0, minute=27, second=2),
)
self.assertEqual(
item.to_date,
datetime(year=2015, month=1, day=1, hour=4, minute=59, second=59),
)
def test_get_account(self):
"""
Tests that an account is loaded correctly by email
"""
account = Account(self.client, "support@linode.com", {})
self.assertEqual(account.email, "support@linode.com")
self.assertEqual(account.state, "PA")
self.assertEqual(account.city, "Philadelphia")
self.assertEqual(account.phone, "123-456-7890")
self.assertEqual(account.tax_id, "")
self.assertEqual(account.balance, 0)
self.assertEqual(account.company, "Linode")
self.assertEqual(account.address_1, "3rd & Arch St")
self.assertEqual(account.address_2, "")
self.assertEqual(account.zip, "19106")
self.assertEqual(account.first_name, "Test")
self.assertEqual(account.last_name, "Guy")
self.assertEqual(account.country, "US")
self.assertIsNotNone(account.capabilities)
self.assertIsNotNone(account.active_promotions)
self.assertEqual(account.balance_uninvoiced, 145)
self.assertEqual(account.billing_source, "akamai")
self.assertEqual(account.euuid, "E1AF5EEC-526F-487D-B317EBEB34C87D71")
self.assertIn("Linode Interfaces", account.capabilities)
def test_get_login(self):
"""
Tests that a login is loaded correctly by ID
"""
login = Login(self.client, 123)
self.assertEqual(login.id, 123)
self.assertEqual(login.ip, "192.0.2.0")
self.assertEqual(login.restricted, True)
self.assertEqual(login.status, "successful")
self.assertEqual(login.username, "test-user")
def test_get_account_settings(self):
"""
Tests that account settings are loaded correctly
"""
settings = AccountSettings(self.client, False, {})
self.assertEqual(settings.longview_subscription.id, "longview-100")
self.assertEqual(settings.managed, False)
self.assertEqual(settings.network_helper, False)
self.assertEqual(settings.object_storage, "active")
self.assertEqual(settings.backups_enabled, True)
self.assertEqual(
settings.interfaces_for_new_linodes,
AccountSettingsInterfacesForNewLinodes.linode_default_but_legacy_config_allowed,
)
def test_post_account_settings(self):
"""
Tests that account settings can be updated successfully
"""
settings = self.client.account.settings()
settings.network_helper = True
settings.backups_enabled = False
settings.interfaces_for_new_linodes = (
AccountSettingsInterfacesForNewLinodes.linode_only
)
with self.mock_put("/account/settings") as m:
settings.save()
assert m.call_data == {
"network_helper": True,
"backups_enabled": False,
"interfaces_for_new_linodes": AccountSettingsInterfacesForNewLinodes.linode_only,
}
def test_get_event(self):
"""
Tests that an event is loaded correctly by ID
"""
event = Event(self.client, 123, {})
self.assertEqual(event.action, "ticket_create")
self.assertEqual(event.created, datetime(2018, 1, 1, 0, 1, 1))
self.assertEqual(event.duration, 300.56)
self.assertIsNotNone(event.entity)
self.assertEqual(event.id, 123)
self.assertEqual(event.message, "None")
self.assertIsNone(event.percent_complete)
self.assertIsNone(event.rate)
self.assertTrue(event.read)
self.assertIsNotNone(event.secondary_entity)
self.assertTrue(event.seen)
self.assertIsNone(event.status)
self.assertIsNone(event.time_remaining)
self.assertEqual(event.username, "exampleUser")
def test_get_invoice(self):
"""
Tests that an invoice is loaded correctly by ID
"""
invoice = Invoice(self.client, 123, {})
self.assertEqual(invoice.date, datetime(2018, 1, 1, 0, 1, 1))
self.assertEqual(invoice.id, 123)
self.assertEqual(invoice.label, "Invoice")
self.assertEqual(invoice.subtotal, 120.25)
self.assertEqual(invoice.tax, 12.25)
self.assertEqual(invoice.total, 132.5)
self.assertIsNotNone(invoice.tax_summary)
def test_get_oauth_client(self):
"""
Tests that an oauth client is loaded correctly by ID
"""
client = OAuthClient(self.client, "2737bf16b39ab5d7b4a1", {})
self.assertEqual(client.id, "2737bf16b39ab5d7b4a1")
self.assertEqual(client.label, "Test_Client_1")
self.assertFalse(client.public)
self.assertEqual(
client.redirect_uri, "https://example.org/oauth/callback"
)
self.assertEqual(client.secret, "<REDACTED>")
self.assertEqual(client.status, "active")
self.assertEqual(
client.thumbnail_url,
"https://api.linode.com/v4/account/clients/2737bf16b39ab5d7b4a1/thumbnail",
)
def test_get_user(self):
"""
Tests that a user is loaded correctly by username
"""
user = User(self.client, "test-user", {})
self.assertEqual(user.username, "test-user")
self.assertEqual(user.email, "test-user@linode.com")
self.assertTrue(user.restricted)
self.assertTrue(user.tfa_enabled)
self.assertIsNotNone(user.ssh_keys)
def test_get_service_transfer(self):
"""
Tests that a service transfer is loaded correctly by token
"""
serviceTransfer = ServiceTransfer(self.client, "12345")
self.assertEqual(serviceTransfer.token, "12345")
self.assertTrue(serviceTransfer.is_sender)
self.assertEqual(serviceTransfer.status, "pending")
def test_get_payment_method(self):
"""
Tests that a payment method is loaded correctly by ID
"""
paymentMethod = PaymentMethod(self.client, 123)
self.assertEqual(paymentMethod.id, 123)
self.assertTrue(paymentMethod.is_default)
self.assertEqual(paymentMethod.type, "credit_card")
def test_payment_method_make_default(self):
"""
Tests that making a payment method default creates the correct api request.
"""
paymentMethod = PaymentMethod(self.client, 123)
with self.mock_post({}) as m:
paymentMethod.payment_method_make_default()
self.assertEqual(
m.call_url, "/account/payment-methods/123/make-default"
)
def test_service_transfer_accept(self):
"""
Tests that accepting a service transfer creates the correct api request.
"""
serviceTransfer = ServiceTransfer(self.client, "12345")
with self.mock_post({}) as m:
serviceTransfer.service_transfer_accept()
self.assertEqual(
m.call_url, "/account/service-transfers/12345/accept"
)
class AccountBetaProgramTest(ClientBaseCase):
"""
Tests methods of the AccountBetaProgram
"""
def test_account_beta_program_api_get(self):
beta_id = "cool"
account_beta_url = "/account/betas/{}".format(beta_id)
with self.mock_get(account_beta_url) as m:
beta = AccountBetaProgram(self.client, beta_id)
self.assertEqual(beta.id, beta_id)
self.assertEqual(beta.enrolled, datetime(2018, 1, 2, 3, 4, 5))
self.assertEqual(beta.started, datetime(2018, 1, 2, 3, 4, 5))
self.assertEqual(beta.ended, datetime(2018, 1, 2, 3, 4, 5))
self.assertEqual(m.call_url, account_beta_url)
class AccountAvailabilityTest(ClientBaseCase):
"""
Test methods of the AccountAvailability
"""
def test_account_availability_api_list(self):
with self.mock_get("/account/availability") as m:
availabilities = self.client.account.availabilities()
for avail in availabilities:
assert avail.region is not None
assert len(avail.unavailable) == 0
assert len(avail.available) > 0
self.assertEqual(m.call_url, "/account/availability")
def test_account_availability_api_get(self):
region_id = "us-east"
account_availability_url = "/account/availability/{}".format(region_id)
with self.mock_get(account_availability_url) as m:
availability = AccountAvailability(self.client, region_id)
self.assertEqual(availability.region, region_id)
self.assertEqual(availability.unavailable, [])
self.assertEqual(availability.available, ["Linodes", "Kubernetes"])
self.assertEqual(m.call_url, account_availability_url)
class ChildAccountTest(ClientBaseCase):
"""
Test methods of the ChildAccount
"""
def test_child_account_api_list(self):
result = self.client.account.child_accounts()
self.assertEqual(len(result), 1)
self.assertEqual(result[0].euuid, "E1AF5EEC-526F-487D-B317EBEB34C87D71")
def test_child_account_create_token(self):
child_account = self.client.load(ChildAccount, 123456)
with self.mock_post("/account/child-accounts/123456/token") as m:
token = child_account.create_token()
self.assertEqual(token.token, "abcdefghijklmnop")
self.assertEqual(m.call_data, {})
def test_get_user_grant():
"""
Tests that a user grant is loaded correctly
"""
grants = get_obj_grants()
assert grants.count(("linode", Instance)) > 0
assert grants.count(("domain", Domain)) > 0
assert grants.count(("stackscript", StackScript)) > 0
assert grants.count(("nodebalancer", NodeBalancer)) > 0
assert grants.count(("volume", Volume)) > 0
assert grants.count(("image", Image)) > 0
assert grants.count(("longview", LongviewClient)) > 0
assert grants.count(("database", Database)) > 0
assert grants.count(("firewall", Firewall)) > 0
assert grants.count(("vpc", VPC)) > 0
def test_user_grants_serialization():
"""
Tests that user grants from JSON is serialized correctly
"""
user_grants_json = {
"database": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"domain": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"firewall": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"global": {
"account_access": "read_only",
"add_databases": True,
"add_domains": True,
"add_firewalls": True,
"add_images": True,
"add_linodes": True,
"add_longview": True,
"add_nodebalancers": True,
"add_placement_groups": True,
"add_stackscripts": True,
"add_volumes": True,
"add_vpcs": True,
"cancel_account": False,
"child_account_access": True,
"longview_subscription": True,
},
"image": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"linode": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"longview": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"nodebalancer": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"stackscript": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"volume": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"vpc": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
}
expected_serialized_grants = deepcopy(user_grants_json)
for grants in expected_serialized_grants.values():
if isinstance(grants, Iterable):
for grant in grants:
if isinstance(grant, dict) and "label" in grant:
del grant["label"]
assert (
UserGrants(None, None, user_grants_json)._serialize()
== expected_serialized_grants
)