-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtest_accounting_api.py
More file actions
689 lines (652 loc) · 25.1 KB
/
Copy pathtest_accounting_api.py
File metadata and controls
689 lines (652 loc) · 25.1 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# -*- coding: utf-8 -*-
import datetime
import filecmp
from decimal import Decimal
from pathlib import Path
import pytest
from dateutil import tz
from xero_python.accounting import (
AccountingApi,
models,
Account,
Accounts,
AccountType,
CurrencyCode,
LineAmountTypes,
Invoices,
Invoice,
Contact,
Payment,
HistoryRecords,
RequestEmpty,
HistoryRecord,
Address,
ContactGroup,
ContactPerson,
Phone,
LineItem,
Attachments,
Attachment,
)
from xero_python.api_client import ApiClient
from xero_python.assets import Pagination
from xero_python.rest import RESTClientObject
@pytest.fixture
def accounting_api(api_client):
return AccountingApi(api_client=api_client)
@pytest.fixture
def sandbox_accounting_api(api_client):
return AccountingApi(
api_client=api_client,
base_url="http://127.0.0.1:4010",
)
@pytest.fixture
def invoice_pdf():
return Path(__file__).resolve().parent.joinpath("fixtures/inv-0032.pdf")
def test_accounting_api_instance_configuration(accounting_api):
"""
Test AccountingApi() configured correctly
"""
assert isinstance(accounting_api, AccountingApi)
assert isinstance(accounting_api.api_client, ApiClient)
assert isinstance(accounting_api.api_client.rest_client, RESTClientObject)
@pytest.mark.vcr()
def test_get_organisations(accounting_api: AccountingApi, xero_tenant_id):
"""
Test AccountingApi.get_organisations()
"""
# Given correct api client and xero tenant id
# When fetching organisations
response = accounting_api.get_organisations(xero_tenant_id=xero_tenant_id)
# Then expect correct response with organisations tenant has access to
assert isinstance(response, models.Organisations)
organisations = response.organisations
assert isinstance(organisations, list)
assert len(organisations) == 1
org = organisations[0]
assert isinstance(org, models.Organisation)
assert org.organisation_id
assert org.is_demo_company
@pytest.mark.vcr()
def test_invoice_attachment_upload_and_download(
accounting_api, xero_tenant_id, invoice_pdf
):
"""This is a full integration test, tested steps are:
1. get first page of invoices
2. choose first invoice
3. upload test pdf file as invoice attachment
4. confirm the invoice has new attachment
5. download uploaded pdf file
"""
# 1. get first page of invoices
invoiceResponse = accounting_api.get_invoices(xero_tenant_id, page=1)
assert isinstance(invoiceResponse, Invoices)
assert len(invoiceResponse.invoices)
# 2. choose first invoice
invoice = invoiceResponse.invoices[0]
assert isinstance(invoice, Invoice)
# 3. upload test pdf file as invoice attachment
include_online = True
with invoice_pdf.open("rb") as pdf:
response = accounting_api.create_invoice_attachment_by_file_name(
xero_tenant_id,
invoice.invoice_id,
file_name=invoice_pdf.name,
include_online=include_online,
body=pdf.read(),
)
assert isinstance(response, Attachments)
assert len(response.attachments) == 1
attachment = response.attachments[0]
assert isinstance(attachment, Attachment)
assert attachment.file_name == invoice_pdf.name
assert attachment.include_online == include_online
assert attachment.content_length == invoice_pdf.stat().st_size
# 4. confirm the invoice has new attachment
attachments = accounting_api.get_invoice_attachments(
xero_tenant_id, invoice.invoice_id
)
assert isinstance(attachments, Attachments)
assert len(attachments.attachments)
for invoice_attachment in attachments.attachments:
if invoice_attachment.attachment_id == attachment.attachment_id:
assert invoice_attachment == attachment
break
else:
raise AssertionError("uploaded invoice attachment not found")
# 5. download uploaded pdf file
temp_pdf_path = accounting_api.get_invoice_attachment_by_id(
xero_tenant_id,
invoice.invoice_id,
attachment.attachment_id,
content_type=attachment.mime_type,
)
assert isinstance(temp_pdf_path, str)
assert temp_pdf_path
temp_pdf = Path(temp_pdf_path)
assert temp_pdf.exists()
assert filecmp.cmp(str(invoice_pdf), temp_pdf_path, shallow=False)
# test cleanup
temp_pdf.unlink()
@pytest.mark.sandbox
def test_get_accounts(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test data
expected = Accounts(
accounts=[
Account(
account_id="ebd06280-af70-4bed-97c6-7451a454ad85",
bank_account_number="0209087654321050",
bank_account_type="BANK",
code="091",
currency_code=CurrencyCode.NZD,
enable_payments_to_account=False,
has_attachments=False,
name="Business Savings Account",
tax_type="NONE",
type=AccountType.BANK,
),
Account(
account_id="7d05a53d-613d-4eb2-a2fc-dcb6adb80b80",
code="200",
description="Income from any normal business activity",
enable_payments_to_account=False,
has_attachments=False,
name="Sales",
tax_type="OUTPUT2",
type=AccountType.REVENUE,
),
]
)
# When getting all accounts
result = sandbox_accounting_api.get_accounts(xero_tenant_id)
# Then expect all accounts to be received
assert result == expected
@pytest.mark.sandbox
def test_get_account(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test data
account_id = "99ce6032-0678-4aa0-8148-240c75fee33a"
expected = Accounts(
accounts=[
Account(
_class="EXPENSE",
account_id=account_id,
code="123456",
description="Hello World",
enable_payments_to_account=False,
has_attachments=False,
name="FooBar",
reporting_code="EXP",
reporting_code_name="Expense",
show_in_expense_claims=False,
status="ACTIVE",
tax_type="INPUT",
type=AccountType.EXPENSE,
updated_date_utc=datetime.datetime(
2019, 2, 22, 1, 2, 39, 120000, tzinfo=tz.UTC
),
)
]
)
# When getting account
result = sandbox_accounting_api.get_account(xero_tenant_id, account_id)
# Then expect one account to be received
assert result == expected
@pytest.mark.sandbox
def test_create_account(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test account data
expected = Account(
_class="EXPENSE",
account_id="66b262e2-561e-423e-8937-47d558f13442",
code="123456",
description="Hello World",
enable_payments_to_account=False,
has_attachments=False,
name="Foobar",
reporting_code="EXP",
reporting_code_name="Expense",
show_in_expense_claims=False,
status="ACTIVE",
tax_type="INPUT",
type=AccountType.EXPENSE,
updated_date_utc=datetime.datetime(
2019, 2, 21, 23, 59, 9, 320000, tzinfo=tz.UTC
),
)
account = Account(
code=expected.code,
name=expected.name,
type=expected.type,
bank_account_number=None, # required but not set in sandbox version
)
# When creating account
result = sandbox_accounting_api.create_account(xero_tenant_id, account)
# Then expect new account to be created
assert isinstance(result, Accounts)
assert len(result.accounts) == 1
result_account = result.accounts[0]
assert result_account == expected
@pytest.mark.sandbox
def test_update_account(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test data
account_id = "99ce6032-0678-4aa0-8148-240c75fee33a"
expected = accounts = Accounts(
accounts=[
Account(
_class="EXPENSE",
account_id=account_id,
code="654321",
description="Good Bye World",
enable_payments_to_account=False,
has_attachments=False,
name="BarFoo",
reporting_code="EXP",
reporting_code_name="Expense",
show_in_expense_claims=False,
status="ACTIVE",
tax_type="INPUT",
type=AccountType.EXPENSE,
updated_date_utc=datetime.datetime(
2019, 2, 22, 0, 29, 49, 333000, tzinfo=tz.UTC
),
)
]
)
# When updating account
result = sandbox_accounting_api.update_account(xero_tenant_id, account_id, accounts)
# Then expect the account to be updated
assert result == expected
@pytest.mark.sandbox
def test_delete_account(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test data
account_id = "7f3c0bec-f3e7-4073-b4d6-cc56dd027ef1"
expected = Accounts(
accounts=[
Account(
_class="EXPENSE",
account_id=account_id,
code="123456",
description="Hello World",
enable_payments_to_account=False,
has_attachments=False,
name="FooBar",
reporting_code="EXP",
reporting_code_name="Expense",
show_in_expense_claims=False,
status="DELETED",
tax_type="INPUT",
type=AccountType.EXPENSE,
updated_date_utc=datetime.datetime(
2019, 2, 22, 1, 16, 57, 210000, tzinfo=tz.UTC
),
)
]
)
# When deleting account
result = sandbox_accounting_api.delete_account(xero_tenant_id, account_id)
# Then expect the account to be deleted
assert result == expected
@pytest.mark.sandbox
def test_get_account_attachments(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test data
account_id = "7f3c0bec-f3e7-4073-b4d6-cc56dd027ef1"
expected = Attachments(
attachments=[
Attachment(
attachment_id="52a643be-cd5c-489f-9778-53a9fd337756",
content_length=Decimal("2878711"),
file_name="sample5.jpg",
include_online=None,
mime_type="image/jpg",
url="https://api.xero.com/api.xro/2.0/Accounts/"
"da962997-a8bd-4dff-9616-01cdc199283f/Attachments/sample5.jpg",
)
]
)
# When getting account attachments
result = sandbox_accounting_api.get_account_attachments(xero_tenant_id, account_id)
# Then expect the account attachments to be received
assert result == expected
@pytest.mark.sandbox
def test_get_invoices(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test invoices data
# When getting all invoices
result: Invoices = sandbox_accounting_api.get_invoices(xero_tenant_id)
# Then expect correct invoices received
expected = Invoices (
invoices=[
Invoice(
amount_credited = Decimal(0),
amount_due = Decimal(0),
amount_paid= Decimal(0),
contact=Contact(
addresses=[],
contact_groups=[],
contact_id="a3675fc4-f8dd-4f03-ba5b-f1870566bcd7",
contact_persons=[],
has_attachments=False,
has_validation_errors=False,
name="Barney Rubble-83203",
phones=[],
),
credit_notes=[],
currency_code=CurrencyCode.NZD,
currency_rate=Decimal(1),
date=datetime.date(2018, 10, 20),
due_date=datetime.date(2018, 12, 30),
has_attachments=False,
has_errors=False,
invoice_id="d4956132-ed94-4dd7-9eaa-aa22dfdf06f2",
invoice_number="INV-0001",
is_discounted=False,
line_amount_types=LineAmountTypes.EXCLUSIVE,
line_items=[],
overpayments=[],
payments=[],
prepayments=[],
reference="Red Fish, Blue Fish",
repeating_invoice_id="428c0d75-909f-4b04-8403-a48dc27283b0",
sent_to_contact=True,
status="VOIDED",
sub_total=Decimal(40),
total=Decimal(40),
total_tax=Decimal(0),
type="ACCREC",
updated_date_utc=datetime.datetime(2018, 11, 2, 16, 31, 30, 160000, tzinfo=tz.UTC),
updated_date_utc_string="2018-11-02T16:31:30Z",
),
Invoice(
amount_credited=Decimal(0),
amount_due=Decimal(0),
amount_paid=Decimal(46),
contact=Contact(
addresses=[],
contact_groups=[],
contact_id="a3675fc4-f8dd-4f03-ba5b-f1870566bcd7",
contact_persons=[],
has_attachments=False,
has_validation_errors=False,
name="Barney Rubble-83203",
phones=[],
),
credit_notes=[],
currency_code=CurrencyCode.NZD,
currency_rate=Decimal(1),
date=datetime.date(2018, 10, 20),
due_date=datetime.date(2018, 12, 30),
fully_paid_on_date=datetime.date(2018, 11, 29),
has_attachments=False,
has_errors=False,
invoice_id="046d8a6d-1ae1-4b4d-9340-5601bdf41b87",
invoice_number="INV-0002",
is_discounted=False,
line_amount_types=LineAmountTypes.EXCLUSIVE,
line_items=[],
overpayments=[],
payments=[
Payment(
amount=Decimal(46),
currency_rate=Decimal(1),
date=datetime.date(2018, 11, 29),
has_account=False,
has_validation_errors=False,
payment_id="99ea7f6b-c513-4066-bc27-b7c65dcd76c2",
)
],
prepayments=[],
reference="Red Fish, Blue Fish",
#repeating_invoice_id="428c0d75-909f-4b04-8403-a48dc27283b0",
sent_to_contact=True,
status="PAID",
sub_total=Decimal(40),
total=Decimal(46),
total_tax=Decimal(6),
type="ACCREC",
updated_date_utc=datetime.datetime(2018, 11, 2, 16, 36, 32, 690000, tzinfo=tz.UTC),
updated_date_utc_string="2018-11-02T16:36:32Z",
),
Invoice(
amount_credited=Decimal(0),
amount_due=Decimal(115),
amount_paid=Decimal(0),
contact=Contact(
addresses=[],
contact_groups=[],
contact_id="a3675fc4-f8dd-4f03-ba5b-f1870566bcd7",
contact_persons=[],
has_attachments=False,
has_validation_errors=False,
name="Barney Rubble-83203",
phones=[],
),
credit_notes=[],
currency_code=CurrencyCode.NZD,
currency_rate=Decimal(1),
date=datetime.date(2018, 11, 2),
due_date=datetime.date(2018, 11, 7),
has_attachments=False,
has_errors=False,
invoice_number="INV-0003",
invoice_id="7ef31b20-de17-4312-8382-412f869b1510",
is_discounted=False,
line_amount_types=LineAmountTypes.EXCLUSIVE,
line_items=[],
overpayments=[],
payments=[],
prepayments=[],
reference="",
status="AUTHORISED",
sub_total=Decimal(100),
total=Decimal(115),
total_tax=Decimal(15),
type="ACCREC",
updated_date_utc=datetime.datetime(2018, 11, 2, 16, 37, 28, 927000, tzinfo=tz.UTC),
updated_date_utc_string="2018-11-02T16:37:28Z"
)
],
pagination= Pagination(
item_count= 3,
page = 1,
page_count= 1,
page_size= 100
)
)
assert str(result) == str(expected)
@pytest.mark.sandbox
def test_get_invoice_history(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test invoice data
invoice_id = "d4956132-ed94-4dd7-9eaa-aa22dfdf06f2"
# When getting invoice history
result = sandbox_accounting_api.get_invoice_history(xero_tenant_id, invoice_id)
# Then expect invoice history to be received
expected = HistoryRecords(
[
HistoryRecord(
changes="Attached a file",
date_utc= datetime.datetime(2018, 11, 8, 15, 1, 21, 470000, tzinfo=tz.UTC),
details= "Attached the file sample2.jpg through the Xero API using Xero API Partner",
user="System Generated",
),
HistoryRecord(
changes="Credit Applied",
date_utc=datetime.datetime(2016, 10, 17, 20, 46, 1, 173000, tzinfo=tz.UTC),
details="Bank transfer from Business Wells Fargo to My Savings on November 12, 2016 for 20.00.",
user="System Generated",
)
]
)
assert result == expected
@pytest.mark.sandbox
def test_update_or_create_invoices(
sandbox_accounting_api: AccountingApi, xero_tenant_id
):
# Given sandbox API, tenant id, and hardcoded test invoices data
expected = invoices = Invoices(
invoices=[
Invoice(
amount_due=Decimal("40.00"),
amount_paid=Decimal("0.00"),
contact=Contact(
addresses=[
Address(
address_type="STREET",
attention_to="",
city="",
country="",
postal_code="",
region="",
),
Address(
address_type="POBOX",
attention_to="",
city="Anytown",
country="USA",
postal_code="10101",
region="NY",
),
],
bank_account_details="",
contact_groups=[
ContactGroup(
contact_group_id="17b44ed7-4389-4162-91cb-3dd5766e4e22",
contacts=[],
name="Oasis",
status="ACTIVE",
)
],
contact_id="430fa14a-f945-44d3-9f97-5df5e28441b8",
contact_persons=[
ContactPerson(
email_address="debbie@rockstar.com",
first_name="Debbie",
include_in_emails=False,
last_name="Gwyther",
)
],
contact_status="ACTIVE",
email_address="liam@rockstar.com",
first_name="Liam",
has_attachments=False,
has_validation_errors=False,
is_customer=True,
is_supplier=True,
last_name="Gallagher",
name="Liam Gallagher",
phones=[
Phone(
phone_area_code="212",
phone_country_code="",
phone_number="222-2222",
phone_type="DEFAULT",
),
Phone(
phone_area_code="",
phone_country_code="",
phone_number="",
phone_type="DDI",
),
Phone(
phone_area_code="212",
phone_country_code="",
phone_number="333-2233",
phone_type="FAX",
),
Phone(
phone_area_code="212",
phone_country_code="",
phone_number="444-3433",
phone_type="MOBILE",
),
],
purchases_tracking_categories=[],
sales_tracking_categories=[],
updated_date_utc=datetime.datetime(
2019, 3, 5, 0, 54, 41, 53000, tzinfo=tz.UTC
),
),
currency_code=CurrencyCode.NZD,
currency_rate=Decimal("1.000000"),
date=datetime.date(2019, 3, 11),
due_date=datetime.date(2018, 12, 10),
has_attachments=False,
has_errors=False,
invoice_id="ed255415-e141-4150-aab7-89c3bbbb851c",
invoice_number="INV-0007",
is_discounted=False,
line_amount_types=LineAmountTypes.EXCLUSIVE,
line_items=[
LineItem(
account_code="200",
description="Acme Tires",
line_amount=Decimal("40.00"),
line_item_id="5f7a612b-fdcc-4d33-90fa-a9f6bc6db32f",
quantity=Decimal("2.0000"),
tax_amount=Decimal("0.00"),
tax_type="NONE",
tracking=[],
unit_amount=Decimal("20.00"),
)
],
overpayments=[],
prepayments=[],
reference="Website Design",
sent_to_contact=False,
status="AUTHORISED",
status_attribute_string="OK",
sub_total=Decimal("40.00"),
total=Decimal("40.00"),
total_tax=Decimal("0.00"),
type="ACCREC",
updated_date_utc=datetime.datetime(
2019, 3, 11, 17, 58, 46, 117000, tzinfo=tz.UTC
),
updated_date_utc_string="2019-03-11T17:58:46Z",
)
]
)
# When updating or creating invoices
result = sandbox_accounting_api.update_or_create_invoices(xero_tenant_id, invoices)
# Then expect invoices to be created or updated
assert result == expected
@pytest.mark.sandbox
def test_create_invoice_history(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test data
invoice_id = "d4956132-ed94-4dd7-9eaa-aa22dfdf06f2"
history_records = HistoryRecords(
[
HistoryRecord(
details="Received through the Xero API from ABC Org",
changes="Approved",
user="System Generated",
date_utc=datetime.datetime(2018, 2, 28, 21, 2, 11, tzinfo=tz.UTC),
),
HistoryRecord(
details="INV-0041 to ABC Furniture for 100.00.",
changes="Edited",
user="Mac Haag",
date_utc=datetime.datetime(2018, 2, 28, 21, 1, 29, tzinfo=tz.UTC),
),
]
)
# When creating invoice history
result = sandbox_accounting_api.create_invoice_history(
xero_tenant_id, invoice_id, history_records
)
# Then expect created invoice history records
expected = HistoryRecords(
[HistoryRecord(
date_utc=datetime.datetime(2019, 2, 23, 5, 23, 20, 362000, tzinfo=tz.UTC),
details="Hello World"
)]
)
assert result == expected
@pytest.mark.sandbox
def test_email_invoice(sandbox_accounting_api: AccountingApi, xero_tenant_id):
# Given sandbox API, tenant id, and hardcoded test data
invoice_id = "d4956132-ed94-4dd7-9eaa-aa22dfdf06f2"
request_empty = RequestEmpty()
# When email invoice
result = sandbox_accounting_api.email_invoice(
xero_tenant_id, invoice_id, request_empty
)
# Then expect email invoiced
assert result is None, "email_invoice doesn't return any data"