xeroizer 2.18.1 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +199 -223
  3. data/lib/xeroizer/connection.rb +49 -0
  4. data/lib/xeroizer/exceptions.rb +2 -0
  5. data/lib/xeroizer/generic_application.rb +12 -5
  6. data/lib/xeroizer/http.rb +5 -78
  7. data/lib/xeroizer/http_response.rb +157 -0
  8. data/lib/xeroizer/models/bank_account.rb +1 -0
  9. data/lib/xeroizer/models/bank_transaction.rb +1 -0
  10. data/lib/xeroizer/models/batch_payment.rb +27 -0
  11. data/lib/xeroizer/models/branding_theme.rb +49 -9
  12. data/lib/xeroizer/models/contact.rb +10 -4
  13. data/lib/xeroizer/models/contact_group.rb +45 -0
  14. data/lib/xeroizer/models/credit_note.rb +23 -22
  15. data/lib/xeroizer/models/from_bank_account.rb +1 -0
  16. data/lib/xeroizer/models/history_record.rb +72 -0
  17. data/lib/xeroizer/models/invoice.rb +14 -3
  18. data/lib/xeroizer/models/line_item.rb +17 -5
  19. data/lib/xeroizer/models/manual_journal.rb +2 -1
  20. data/lib/xeroizer/models/option.rb +1 -1
  21. data/lib/xeroizer/models/organisation.rb +2 -0
  22. data/lib/xeroizer/models/payment_service.rb +22 -0
  23. data/lib/xeroizer/models/payroll/address.rb +53 -0
  24. data/lib/xeroizer/models/payroll/bank_account.rb +18 -6
  25. data/lib/xeroizer/models/payroll/benefit_line.rb +26 -0
  26. data/lib/xeroizer/models/payroll/benefit_type.rb +45 -0
  27. data/lib/xeroizer/models/payroll/deduction_line.rb +32 -0
  28. data/lib/xeroizer/models/payroll/deduction_type.rb +49 -0
  29. data/lib/xeroizer/models/payroll/earnings_line.rb +39 -0
  30. data/lib/xeroizer/models/payroll/earnings_type.rb +53 -0
  31. data/lib/xeroizer/models/payroll/employee.rb +30 -8
  32. data/lib/xeroizer/models/payroll/leave_application.rb +27 -0
  33. data/lib/xeroizer/models/payroll/leave_line.rb +30 -0
  34. data/lib/xeroizer/models/payroll/leave_period.rb +15 -0
  35. data/lib/xeroizer/models/payroll/pay_items.rb +22 -0
  36. data/lib/xeroizer/models/payroll/pay_run.rb +33 -0
  37. data/lib/xeroizer/models/payroll/pay_schedule.rb +40 -0
  38. data/lib/xeroizer/models/payroll/pay_template.rb +24 -0
  39. data/lib/xeroizer/models/payroll/payment_method.rb +24 -0
  40. data/lib/xeroizer/models/payroll/paystub.rb +44 -0
  41. data/lib/xeroizer/models/payroll/reimbursement_line.rb +21 -0
  42. data/lib/xeroizer/models/payroll/reimbursement_type.rb +22 -0
  43. data/lib/xeroizer/models/payroll/salary_and_wage.rb +29 -0
  44. data/lib/xeroizer/models/payroll/super_line.rb +40 -0
  45. data/lib/xeroizer/models/payroll/tax_declaration.rb +50 -0
  46. data/lib/xeroizer/models/payroll/time_off_line.rb +20 -0
  47. data/lib/xeroizer/models/payroll/time_off_type.rb +32 -0
  48. data/lib/xeroizer/models/payroll/work_location.rb +25 -0
  49. data/lib/xeroizer/models/prepayment.rb +1 -0
  50. data/lib/xeroizer/models/purchase_order.rb +6 -6
  51. data/lib/xeroizer/models/quote.rb +76 -0
  52. data/lib/xeroizer/models/schedule.rb +1 -0
  53. data/lib/xeroizer/models/tax_component.rb +1 -0
  54. data/lib/xeroizer/models/to_bank_account.rb +1 -0
  55. data/lib/xeroizer/oauth.rb +12 -1
  56. data/lib/xeroizer/oauth2.rb +82 -0
  57. data/lib/xeroizer/oauth2_application.rb +49 -0
  58. data/lib/xeroizer/payroll_application.rb +8 -3
  59. data/lib/xeroizer/record/base.rb +1 -1
  60. data/lib/xeroizer/record/base_model.rb +1 -1
  61. data/lib/xeroizer/record/base_model_http_proxy.rb +4 -0
  62. data/lib/xeroizer/record/model_definition_helper.rb +1 -1
  63. data/lib/xeroizer/record/payroll_base.rb +4 -0
  64. data/lib/xeroizer/record/record_association_helper.rb +4 -4
  65. data/lib/xeroizer/record/validators/associated_validator.rb +1 -0
  66. data/lib/xeroizer/record/xml_helper.rb +18 -18
  67. data/lib/xeroizer/report/aged_receivables_by_contact.rb +1 -1
  68. data/lib/xeroizer/report/cell_xml_helper.rb +13 -13
  69. data/lib/xeroizer/response.rb +22 -17
  70. data/lib/xeroizer/version.rb +1 -1
  71. data/lib/xeroizer.rb +33 -4
  72. data/test/acceptance/about_creating_bank_transactions_test.rb +80 -82
  73. data/test/acceptance/about_creating_prepayment_test.rb +25 -30
  74. data/test/acceptance/about_fetching_bank_transactions_test.rb +12 -12
  75. data/test/acceptance/about_online_invoice_test.rb +6 -10
  76. data/test/acceptance/acceptance_test.rb +28 -26
  77. data/test/acceptance/bank_transfer_test.rb +12 -17
  78. data/test/acceptance/bulk_operations_test.rb +18 -16
  79. data/test/acceptance/connections_test.rb +11 -0
  80. data/test/stub_responses/bad_request.json +6 -0
  81. data/test/stub_responses/connections.json +16 -0
  82. data/test/stub_responses/expired_oauth2_token.json +6 -0
  83. data/test/stub_responses/generic_response_error.json +6 -0
  84. data/test/stub_responses/invalid_oauth2_request_token.json +6 -0
  85. data/test/stub_responses/invalid_tenant_header.json +6 -0
  86. data/test/stub_responses/object_not_found.json +6 -0
  87. data/test/stub_responses/organisations.xml +10 -0
  88. data/test/stub_responses/payment_service.xml +15 -0
  89. data/test/test_helper.rb +16 -11
  90. data/test/unit/generic_application_test.rb +21 -10
  91. data/test/unit/http_test.rb +284 -10
  92. data/test/unit/models/address_test.rb +2 -2
  93. data/test/unit/models/bank_transaction_model_parsing_test.rb +2 -2
  94. data/test/unit/models/bank_transaction_test.rb +1 -1
  95. data/test/unit/models/bank_transaction_validation_test.rb +1 -1
  96. data/test/unit/models/contact_test.rb +2 -2
  97. data/test/unit/models/credit_note_test.rb +8 -8
  98. data/test/unit/models/employee_test.rb +4 -4
  99. data/test/unit/models/invoice_test.rb +12 -12
  100. data/test/unit/models/journal_line_test.rb +6 -6
  101. data/test/unit/models/journal_test.rb +4 -4
  102. data/test/unit/models/line_item_sum_test.rb +1 -1
  103. data/test/unit/models/line_item_test.rb +26 -28
  104. data/test/unit/models/manual_journal_test.rb +3 -3
  105. data/test/unit/models/organisation_test.rb +16 -2
  106. data/test/unit/models/payment_service_test.rb +29 -0
  107. data/test/unit/models/phone_test.rb +7 -7
  108. data/test/unit/models/prepayment_test.rb +4 -4
  109. data/test/unit/models/repeating_invoice_test.rb +3 -3
  110. data/test/unit/models/tax_rate_test.rb +2 -2
  111. data/test/unit/oauth2_test.rb +171 -0
  112. data/test/unit/oauth_config_test.rb +1 -1
  113. data/test/unit/record/base_model_test.rb +13 -13
  114. data/test/unit/record/base_test.rb +15 -4
  115. data/test/unit/record/block_validator_test.rb +1 -1
  116. data/test/unit/record/connection_test.rb +60 -0
  117. data/test/unit/record/model_definition_test.rb +36 -36
  118. data/test/unit/record/parse_params_test.rb +2 -2
  119. data/test/unit/record/parse_where_hash_test.rb +13 -13
  120. data/test/unit/record/record_association_test.rb +14 -14
  121. data/test/unit/record/validators_test.rb +43 -43
  122. data/test/unit/record_definition_test.rb +7 -7
  123. data/test/unit/report_definition_test.rb +7 -7
  124. data/test/unit/report_test.rb +20 -20
  125. data/test/unit_test_helper.rb +16 -0
  126. metadata +100 -25
  127. data/lib/xeroizer/models/payroll/home_address.rb +0 -24
  128. data/lib/xeroizer/partner_application.rb +0 -51
  129. data/lib/xeroizer/private_application.rb +0 -25
  130. data/lib/xeroizer/public_application.rb +0 -21
  131. data/test/unit/http_tsl_12_upgrade_test.rb +0 -31
  132. data/test/unit/oauth_test.rb +0 -118
  133. data/test/unit/private_application_test.rb +0 -20
data/lib/xeroizer.rb CHANGED
@@ -5,6 +5,7 @@ require 'active_support/inflector'
5
5
  require "active_support/core_ext/array"
6
6
  require "active_support/core_ext/big_decimal/conversions"
7
7
  require 'oauth'
8
+ require 'oauth2'
8
9
  require 'oauth/signature/rsa/sha1'
9
10
  require 'nokogiri'
10
11
  require 'builder'
@@ -17,14 +18,17 @@ $: << File.expand_path(File.dirname(__FILE__))
17
18
  require 'class_level_inheritable_attributes'
18
19
  require 'xeroizer/exceptions'
19
20
  require 'xeroizer/oauth'
21
+ require 'xeroizer/oauth2'
20
22
  require 'xeroizer/http_encoding_helper'
21
23
  require 'xeroizer/http'
24
+ require 'xeroizer/connection'
22
25
 
23
26
  require 'xeroizer/record/base_model'
24
27
  require 'xeroizer/record/payroll_base_model'
25
28
  require 'xeroizer/record/base'
26
29
  require 'xeroizer/record/payroll_base'
27
30
  require 'xeroizer/configuration'
31
+ require 'xeroizer/http_response'
28
32
 
29
33
  # Include models
30
34
  require 'xeroizer/models/account'
@@ -33,6 +37,7 @@ require 'xeroizer/models/allocation'
33
37
  require 'xeroizer/models/branding_theme'
34
38
  require 'xeroizer/models/bank_transaction'
35
39
  require 'xeroizer/models/bank_account'
40
+ require 'xeroizer/models/batch_payment'
36
41
  require 'xeroizer/models/from_bank_account'
37
42
  require 'xeroizer/models/to_bank_account'
38
43
  require 'xeroizer/models/bank_transfer'
@@ -42,6 +47,7 @@ require 'xeroizer/models/credit_note'
42
47
  require 'xeroizer/models/currency'
43
48
  require 'xeroizer/models/employee'
44
49
  require 'xeroizer/models/expense_claim'
50
+ require 'xeroizer/models/history_record'
45
51
  require 'xeroizer/models/invoice'
46
52
  require 'xeroizer/models/invoice_reminder'
47
53
  require 'xeroizer/models/online_invoice'
@@ -56,10 +62,12 @@ require 'xeroizer/models/manual_journal_line'
56
62
  require 'xeroizer/models/option'
57
63
  require 'xeroizer/models/organisation'
58
64
  require 'xeroizer/models/payment'
65
+ require 'xeroizer/models/payment_service'
59
66
  require 'xeroizer/models/prepayment'
60
67
  require 'xeroizer/models/overpayment'
61
68
  require 'xeroizer/models/phone'
62
69
  require 'xeroizer/models/purchase_order'
70
+ require 'xeroizer/models/quote'
63
71
  require 'xeroizer/models/receipt'
64
72
  require 'xeroizer/models/repeating_invoice'
65
73
  require 'xeroizer/models/schedule'
@@ -72,16 +80,37 @@ require 'xeroizer/models/journal_line_tracking_category'
72
80
  require 'xeroizer/models/contact_sales_tracking_category'
73
81
  require 'xeroizer/models/contact_purchases_tracking_category'
74
82
 
75
- require 'xeroizer/models/payroll/home_address'
76
83
  require 'xeroizer/models/payroll/bank_account'
84
+ require 'xeroizer/models/payroll/benefit_line'
85
+ require 'xeroizer/models/payroll/benefit_type'
86
+ require 'xeroizer/models/payroll/deduction_line'
87
+ require 'xeroizer/models/payroll/deduction_type'
88
+ require 'xeroizer/models/payroll/earnings_line'
89
+ require 'xeroizer/models/payroll/earnings_type'
77
90
  require 'xeroizer/models/payroll/employee'
91
+ require 'xeroizer/models/payroll/address'
92
+ require 'xeroizer/models/payroll/leave_line'
93
+ require 'xeroizer/models/payroll/pay_items'
94
+ require 'xeroizer/models/payroll/pay_run'
95
+ require 'xeroizer/models/payroll/pay_template'
96
+ require 'xeroizer/models/payroll/payment_method'
97
+ require 'xeroizer/models/payroll/pay_schedule'
98
+ require 'xeroizer/models/payroll/paystub'
99
+ require 'xeroizer/models/payroll/reimbursement_line'
100
+ require 'xeroizer/models/payroll/reimbursement_type'
101
+ require 'xeroizer/models/payroll/salary_and_wage'
102
+ require 'xeroizer/models/payroll/super_line'
103
+ require 'xeroizer/models/payroll/tax_declaration'
104
+ require 'xeroizer/models/payroll/time_off_line'
105
+ require 'xeroizer/models/payroll/time_off_type'
106
+ require 'xeroizer/models/payroll/work_location'
107
+ require 'xeroizer/models/payroll/leave_application'
108
+ require 'xeroizer/models/payroll/leave_period'
78
109
 
79
110
  require 'xeroizer/report/factory'
80
111
 
81
112
  require 'xeroizer/response'
82
113
 
83
114
  require 'xeroizer/generic_application'
84
- require 'xeroizer/public_application'
85
- require 'xeroizer/private_application'
86
- require 'xeroizer/partner_application'
115
+ require 'xeroizer/oauth2_application'
87
116
  require 'xeroizer/payroll_application'
@@ -4,40 +4,67 @@ require "acceptance_test"
4
4
  class AboutCreatingBankTransactions < Test::Unit::TestCase
5
5
  include AcceptanceTest
6
6
 
7
- let :client do
8
- Xeroizer::PrivateApplication.new(@consumer_key, @consumer_secret, @key_file)
7
+ def assert_exists(bank_transaction, client)
8
+ assert_not_nil bank_transaction.id,
9
+ "Cannot check for exitence unless the bank transaction has non-null identifier"
10
+ assert_not_nil client.BankTransaction.find bank_transaction.id
11
+ end
12
+
13
+ def any_line_items(account)
14
+ [{
15
+ :description => "Clingfilm bike shorts",
16
+ :quantity => 1,
17
+ :unit_amount => "17.00",
18
+ :account_code => account.code,
19
+ :tax_type => account.tax_type
20
+ }]
21
+ end
22
+
23
+ def get_inclusive_tax(amount, tax_rate)
24
+ inclusive_tax = amount * (1 - (100/(100 + tax_rate)))
25
+ BigDecimal(inclusive_tax.to_s).round(2)
26
+ end
27
+
28
+ def get_exclusive_tax(amount, tax_rate)
29
+ exclusive_tax = amount * (tax_rate/100)
30
+ BigDecimal(exclusive_tax.to_s).round(2)
31
+ end
32
+
33
+ def get_tax_rate(tax_type, client)
34
+ @all_tax_types ||= client.TaxRate.all
35
+ @all_tax_types.select{|tax_rate| tax_rate.tax_type == tax_type}.first
9
36
  end
10
37
 
11
- def setup
12
- super
13
- all_accounts = client.Account.all
38
+ setup do
39
+ @client = AcceptanceTestHelpers.oauth2_client
40
+ all_accounts = @client.Account.all
14
41
  @account = all_accounts.select{|acct| acct.status == "ACTIVE" && acct.type == "REVENUE"}.first
15
- @bank_account = all_accounts.select{|acct| acct.status == "ACTIVE" && acct.type == "BANK"}.first
42
+ @bank_account = all_accounts.select{|acct| acct.status == "ACTIVE" && acct.type == "BANK"}.first
16
43
  end
17
44
 
18
45
  can "create a new SPEND bank transaction" do
19
- new_transaction = client.BankTransaction.build(
20
- :type => "SPEND",
21
- :contact => { :name => "Jazz Kang" },
22
- :line_items => any_line_items(@account),
23
- :bank_account => { :account_id => @bank_account.account_id }
46
+ new_transaction = @client.BankTransaction.build(
47
+ :type => "SPEND",
48
+ :contact => { :name => "Jazz Kang" },
49
+ :line_items => any_line_items(@account),
50
+ :bank_account => { :account_id => @bank_account.account_id }
24
51
  )
25
52
 
26
53
  assert new_transaction.save, "Save failed with the following errors: #{new_transaction.errors.inspect}"
27
- assert_exists new_transaction
54
+ assert_exists(new_transaction, @client)
28
55
  end
29
56
 
30
57
  can "update a SPEND bank transaction, for example by setting its status" do
31
- new_transaction = client.BankTransaction.build(
32
- :type => "SPEND",
33
- :contact => { :name => "Jazz Kang" },
34
- :line_items => any_line_items(@account),
35
- :bank_account => { :account_id => @bank_account.account_id }
58
+ new_transaction = @client.BankTransaction.build(
59
+ :type => "SPEND",
60
+ :contact => { :name => "Jazz Kang" },
61
+ :line_items => any_line_items(@account),
62
+ :bank_account => { :account_id => @bank_account.account_id }
36
63
  )
37
64
 
38
65
  assert new_transaction.save, "Save failed with the following errors: #{new_transaction.errors.inspect}"
39
66
 
40
- assert_exists new_transaction
67
+ assert_exists new_transaction, @client
41
68
 
42
69
  the_new_type = "RECEIVE"
43
70
 
@@ -49,110 +76,81 @@ class AboutCreatingBankTransactions < Test::Unit::TestCase
49
76
 
50
77
  assert_equal expected_id, new_transaction.id, "Expected the id to be the same because it has been updated"
51
78
 
52
- refreshed_bank_transaction = client.BankTransaction.find expected_id
79
+ refreshed_bank_transaction = @client.BankTransaction.find expected_id
53
80
 
54
81
  assert_equal the_new_type, refreshed_bank_transaction.type,
55
- "Expected the bank transaction to've had its type updated"
82
+ "Expected the bank transaction to've had its type updated"
56
83
  end
57
84
 
58
85
  can "update a bank transaction by adding line items provided you calculate the tax_amount correctly" do
59
- new_transaction = client.BankTransaction.build(
60
- :type => "SPEND",
61
- :contact => { :name => "Jazz Kang" },
62
- :line_items => any_line_items(@account),
63
- :bank_account => { :account_id => @bank_account.account_id },
64
- :line_amount_types => "Exclusive"
86
+ new_transaction = @client.BankTransaction.build(
87
+ :type => "SPEND",
88
+ :contact => { :name => "Jazz Kang" },
89
+ :line_items => any_line_items(@account),
90
+ :bank_account => { :account_id => @bank_account.account_id },
91
+ :line_amount_types => "Exclusive"
65
92
  )
66
93
 
67
94
  assert new_transaction.save, "Save failed with the following errors: #{new_transaction.errors.inspect}"
68
- assert_exists new_transaction
95
+ assert_exists new_transaction, @client
69
96
 
70
97
  expected_id = new_transaction.id
71
98
 
72
- tax_rate = get_tax_rate(@account.tax_type).effective_rate
99
+ tax_rate = get_tax_rate(@account.tax_type, @client).effective_rate
73
100
 
74
101
  unit_price = BigDecimal("1337.00")
75
102
 
76
103
  the_new_line_items = [
77
- {
78
- :description => "Burrito skin",
79
- :quantity => 1,
80
- :unit_amount => unit_price,
81
- :account_code => @account.code,
82
- :tax_type => @account.tax_type,
83
- :tax_amount => get_exclusive_tax(unit_price, tax_rate)
84
- }
104
+ {
105
+ :description => "Burrito skin",
106
+ :quantity => 1,
107
+ :unit_amount => unit_price,
108
+ :account_code => @account.code,
109
+ :tax_type => @account.tax_type,
110
+ :tax_amount => get_exclusive_tax(unit_price, tax_rate)
111
+ }
85
112
  ]
86
113
 
87
114
  new_transaction.line_items = the_new_line_items
88
115
 
89
116
  assert new_transaction.save, "Update failed with the following errors: #{new_transaction.errors.inspect}"
90
117
 
91
- refreshed_bank_transaction = client.BankTransaction.find expected_id
118
+ refreshed_bank_transaction = @client.BankTransaction.find expected_id
92
119
 
93
120
  assert_equal expected_id, new_transaction.id,
94
- "Expected the id to be the same because it has been updated"
121
+ "Expected the id to be the same because it has been updated"
95
122
 
96
123
  assert_equal 1, refreshed_bank_transaction.line_items.size,
97
- "Expected the bank transaction to've had its line items updated to just one"
124
+ "Expected the bank transaction to've had its line items updated to just one"
98
125
 
99
126
  the_first_line_item = refreshed_bank_transaction.line_items.first
100
127
 
101
128
  assert_equal "Burrito skin", the_first_line_item.description,
102
- "Expected the bank transaction to've had its line items updated, " +
103
- "but the first one's description does not match: #{the_first_line_item.inspect}"
104
- end
105
-
106
- def get_inclusive_tax(amount, tax_rate)
107
- inclusive_tax = amount * (1 - (100/(100 + tax_rate)))
108
- BigDecimal(inclusive_tax.to_s).round(2)
109
- end
110
-
111
- def get_exclusive_tax(amount, tax_rate)
112
- exclusive_tax = amount * (tax_rate/100)
113
- BigDecimal(exclusive_tax.to_s).round(2)
114
- end
115
-
116
- def get_tax_rate tax_type
117
- @all_tax_types ||= client.TaxRate.all
118
- @all_tax_types.select{|tax_rate| tax_rate.tax_type == tax_type}.first
129
+ "Expected the bank transaction to've had its line items updated, " +
130
+ "but the first one's description does not match: #{the_first_line_item.inspect}"
119
131
  end
120
132
 
121
133
  can "create a new RECEIVE bank transaction" do
122
- new_transaction = client.BankTransaction.build(
123
- :type => "RECEIVE",
124
- :contact => { :name => "Jazz Kang" },
125
- :line_items => any_line_items(@account),
126
- :bank_account => { :account_id => @bank_account.account_id }
134
+ new_transaction = @client.BankTransaction.build(
135
+ :type => "RECEIVE",
136
+ :contact => { :name => "Jazz Kang" },
137
+ :line_items => any_line_items(@account),
138
+ :bank_account => { :account_id => @bank_account.account_id }
127
139
  )
128
140
 
129
141
  assert new_transaction.save, "Save failed with the following errors: #{new_transaction.errors.inspect}"
130
- assert_exists new_transaction
142
+ assert_exists new_transaction, @client
131
143
  end
132
144
 
133
145
  it "treats line item unit_amounts as tax EXCLUSIVE"
134
146
  must "not set the tax_amount manually on line items"
135
147
 
136
- def assert_exists(bank_transaction)
137
- assert_not_nil bank_transaction.id,
138
- "Cannot check for exitence unless the bank transaction has non-null identifier"
139
- assert_not_nil client.BankTransaction.find bank_transaction.id
140
- end
141
148
 
142
- def any_line_items(account)
143
- [{
144
- :description => "Clingfilm bike shorts",
145
- :quantity => 1,
146
- :unit_amount => "17.00",
147
- :account_code => account.code,
148
- :tax_type => account.tax_type
149
- }]
150
- end
151
149
 
152
150
  it "fails with ApiException when you try and create a new bank account with missing account type with save! method" do
153
- new_account = client.Account.build(
154
- :name => "Example bank account",
155
- :code => "ACC-001"
151
+ new_account = @client.Account.build(
152
+ :name => "Example bank account",
153
+ :code => "ACC-001"
156
154
  )
157
155
 
158
156
  assert_raise Xeroizer::ApiException do
@@ -161,9 +159,9 @@ class AboutCreatingBankTransactions < Test::Unit::TestCase
161
159
  end
162
160
 
163
161
  it "returns false when you try and create a new bank account with a missing account type with save method" do
164
- new_account = client.Account.build(
165
- :name => "Example bank account",
166
- :code => "ACC-001"
162
+ new_account = @client.Account.build(
163
+ :name => "Example bank account",
164
+ :code => "ACC-001"
167
165
  )
168
166
 
169
167
  assert new_account.save == false, "Account save method expected to return false"
@@ -4,43 +4,38 @@ require "acceptance_test"
4
4
  class AboutCreatingPrepayment < Test::Unit::TestCase
5
5
  include AcceptanceTest
6
6
 
7
- let :client do
8
- Xeroizer::PrivateApplication.new(@consumer_key, @consumer_secret, @key_file)
7
+ def any_line_items(account)
8
+ [{
9
+ :description => "Clingfilm bike shorts",
10
+ :quantity => 1,
11
+ :unit_amount => "200.00",
12
+ :account_code => account.code,
13
+ :tax_type => account.tax_type
14
+ }]
15
+ end
16
+
17
+ def assert_exists(prepayment, client)
18
+ assert_not_nil prepayment.id,
19
+ "Cannot check for exitence unless the prepayment (bank transaction) has non-null identifier"
20
+ assert_not_nil client.BankTransaction.find prepayment.id
9
21
  end
10
22
 
11
- def setup
12
- super
13
- all_accounts = client.Account.all
23
+ setup do
24
+ @client = AcceptanceTestHelpers.oauth2_client
25
+ all_accounts = @client.Account.all
14
26
  @account = all_accounts.select{|acct| acct.status == "ACTIVE" && acct.type == "REVENUE"}.first
15
- @bank_account = all_accounts.select{|acct| acct.status == "ACTIVE" && acct.type == "BANK"}.first
27
+ @bank_account = all_accounts.select{|acct| acct.status == "ACTIVE" && acct.type == "BANK"}.first
16
28
  end
17
29
 
18
30
  can "create a new PrePayment bank transaction" do
19
- new_transaction = client.BankTransaction.build(
20
- :type => "RECEIVE-PREPAYMENT",
21
- :contact => { :name => "Jazz Kang" },
22
- :line_items => any_line_items(@account),
23
- :bank_account => { :account_id => @bank_account.account_id }
31
+ new_transaction = @client.BankTransaction.build(
32
+ :type => "RECEIVE-PREPAYMENT",
33
+ :contact => { :name => "Jazz Kang" },
34
+ :line_items => any_line_items(@account),
35
+ :bank_account => { :account_id => @bank_account.account_id }
24
36
  )
25
37
 
26
38
  assert new_transaction.save, "Save failed with the following errors: #{new_transaction.errors.inspect}"
27
- assert_exists new_transaction
39
+ assert_exists new_transaction, @client
28
40
  end
29
-
30
- def any_line_items(account)
31
- [{
32
- :description => "Clingfilm bike shorts",
33
- :quantity => 1,
34
- :unit_amount => "200.00",
35
- :account_code => account.code,
36
- :tax_type => account.tax_type
37
- }]
38
- end
39
-
40
- def assert_exists(prepayment)
41
- assert_not_nil prepayment.id,
42
- "Cannot check for exitence unless the prepayment (bank transaction) has non-null identifier"
43
- assert_not_nil client.BankTransaction.find prepayment.id
44
- end
45
-
46
- end
41
+ end
@@ -5,40 +5,40 @@ require "bank_transaction_reference_data"
5
5
  class AboutFetchingBankTransactions < Test::Unit::TestCase
6
6
  include AcceptanceTest
7
7
 
8
- def client
9
- @client ||= Xeroizer::PrivateApplication.new(@consumer_key, @consumer_secret, @key_file)
8
+ setup do
9
+ @client = AcceptanceTestHelpers.oauth2_client
10
10
  end
11
11
 
12
12
  context "when requesting a single bank transaction with a reference" do
13
13
  setup do
14
- @a_new_bank_transaction = BankTransactionReferenceData.new(client).bank_transaction
14
+ @a_new_bank_transaction = BankTransactionReferenceData.new(@client).bank_transaction
15
15
  end
16
16
 
17
17
  it "has the extended set of attributes" do
18
18
  keys = [:line_amount_types, :contact, :date, :status, :line_items,
19
19
  :updated_date_utc, :currency_code, :bank_transaction_id,
20
20
  :bank_account, :type, :reference, :is_reconciled, :currency_rate]
21
- assert_equal(@a_new_bank_transaction.attributes.keys, keys)
21
+ assert_equal(keys, @a_new_bank_transaction.attributes.keys)
22
22
  end
23
23
 
24
24
  it "returns full line item details" do
25
- single_bank_transaction = client.BankTransaction.find @a_new_bank_transaction.id
25
+ single_bank_transaction = @client.BankTransaction.find @a_new_bank_transaction.id
26
26
 
27
27
  assert_not_empty single_bank_transaction.line_items,
28
- "expected the bank transaction's line items to have been included"
28
+ "expected the bank transaction's line items to have been included"
29
29
  end
30
30
  end
31
31
 
32
32
  context "when requesting all bank transactions (i.e., without filter)" do
33
33
  setup do
34
- @the_first_bank_transaction = client.BankTransaction.all.first
34
+ @the_first_bank_transaction = @client.BankTransaction.all.detect { |trans| trans.attributes.keys.include?(:reference) }
35
35
  end
36
36
 
37
37
  it "has the limited set of attributes" do
38
- keys = [:line_amount_types, :contact, :date, :status, :updated_date_utc,
39
- :currency_code, :bank_transaction_id, :bank_account, :type, :reference,
40
- :is_reconciled]
41
- assert_equal(@the_first_bank_transaction.attributes.keys, keys)
38
+ keys = [:line_amount_types, :contact, :date, :status, :updated_date_utc,
39
+ :currency_code, :bank_transaction_id, :bank_account, :type, :reference,
40
+ :is_reconciled, :has_attachments]
41
+ assert_equal(keys, @the_first_bank_transaction.attributes.keys)
42
42
  end
43
43
 
44
44
  it "returns contact" do
@@ -49,5 +49,5 @@ class AboutFetchingBankTransactions < Test::Unit::TestCase
49
49
  assert_not_nil @the_first_bank_transaction.bank_account
50
50
  end
51
51
  end
52
- end
53
52
 
53
+ end
@@ -4,17 +4,13 @@ require "acceptance_test"
4
4
  class AboutGetOnlineInvoiceUrl < Test::Unit::TestCase
5
5
  include AcceptanceTest
6
6
 
7
- let :client do
8
- Xeroizer::PrivateApplication.new(@consumer_key, @consumer_secret, @key_file)
7
+ setup do
8
+ @client = AcceptanceTestHelpers.oauth2_client
9
+ @invoice = @client.Invoice.all(:where => 'Type=="ACCREC"').first
10
+ @invoice_acc_pay = @client.Invoice.all(:where => 'Type=="ACCPAY"').first
9
11
  end
10
12
 
11
- def setup
12
- super
13
- @invoice = client.Invoice.all(:where => 'Type=="ACCREC"').first
14
- @invoice_acc_pay = client.Invoice.all(:where => 'Type=="ACCPAY"').first
15
- end
16
-
17
- can "Request OnlineInvoice of an AccRec invoice" do
13
+ can "Request OnlineInvoice of an AccRec invoice" do
18
14
  @online_invoice = @invoice.online_invoice
19
15
 
20
16
  assert @online_invoice.online_invoice_url, "online_invoice_url not found"
@@ -26,4 +22,4 @@ class AboutGetOnlineInvoiceUrl < Test::Unit::TestCase
26
22
  @online_invoice_acc_pay = @invoice_acc_pay.online_invoice
27
23
  end
28
24
  end
29
- end
25
+ end
@@ -1,7 +1,35 @@
1
+ module AcceptanceTestHelpers
2
+ def self.oauth2_client
3
+ config = self.load_oauth2_config_from_env
4
+ Xeroizer::OAuth2Application.new(
5
+ config.client_id,
6
+ config.client_secret,
7
+ access_token: config.access_token,
8
+ tenant_id: config.tenant_id
9
+ )
10
+ end
11
+
12
+ def self.load_oauth2_config_from_env
13
+ raise "No XERO_CLIENT_ID environment variable specified." unless ENV["XERO_CLIENT_ID"]
14
+ raise "No XERO_CLIENT_SECRET environment variable specified." unless ENV["XERO_CLIENT_SECRET"]
15
+ raise "No XERO_ACCESS_TOKEN environment variable specified." unless ENV["XERO_ACCESS_TOKEN"]
16
+ raise "No XERO_TENANT_ID environment variable specified." unless ENV["XERO_TENANT_ID"]
17
+
18
+ OpenStruct.new(client_id: ENV["XERO_CLIENT_ID"],
19
+ client_secret: ENV["XERO_CLIENT_SECRET"],
20
+ access_token: ENV["XERO_ACCESS_TOKEN"],
21
+ tenant_id: ENV["XERO_TENANT_ID"])
22
+ end
23
+ end
24
+
1
25
  module AcceptanceTest
2
26
  class << self
3
27
  def included(klass)
4
28
  klass.class_eval do
29
+ def self.it_works_using_oauth2(&block)
30
+ instance_exec(AcceptanceTestHelpers.oauth2_client, 'oauth2', &block)
31
+ end
32
+
5
33
  def self.log_to_console
6
34
  Xeroizer::Logging.const_set :Log, Xeroizer::Logging::StdOutLog
7
35
  end
@@ -24,30 +52,4 @@ module AcceptanceTest
24
52
  end
25
53
  end
26
54
  end
27
-
28
- def setup
29
- config = load_config_from_file || load_config_from_env
30
-
31
- @key_file = config.key_file
32
- @consumer_key = config.consumer_key
33
- @consumer_secret = config.consumer_secret
34
- end
35
-
36
- private
37
-
38
- def load_config_from_file
39
- the_file_name = File.join(File.dirname(__FILE__), '..', '..', '.oauth')
40
-
41
- return nil unless File.exist? the_file_name
42
-
43
- Xeroizer::OAuthConfig.load IO.read the_file_name
44
- end
45
-
46
- def load_config_from_env
47
- assert_not_nil ENV["CONSUMER_KEY"], "No CONSUMER_KEY environment variable specified."
48
- assert_not_nil ENV["CONSUMER_SECRET"], "No CONSUMER_SECRET environment variable specified."
49
- assert_not_nil ENV["PRIVATE_KEY_PATH"], "No PRIVATE_KEY_PATH environment variable specified."
50
- assert File.exist?(ENV["PRIVATE_KEY_PATH"]), "The file <#{ENV["PRIVATE_KEY_PATH"]}> does not exist."
51
- Xeroizer::OAuthCredentials.new ENV["CONSUMER_KEY"], ENV["CONSUMER_SECRET"], ENV["PRIVATE_KEY_PATH"]
52
- end
53
55
  end
@@ -4,23 +4,18 @@ require "acceptance_test"
4
4
  class BankTransfer < Test::Unit::TestCase
5
5
  include AcceptanceTest
6
6
 
7
- let :client do
8
- Xeroizer::PrivateApplication.new(@consumer_key, @consumer_secret, @key_file)
9
- end
10
-
11
- def setup
12
- super
13
- all_accounts = client.Account.all
14
- @from_bank_account = all_accounts.select { |acct| acct.status == "ACTIVE" && acct.type == "BANK" }.first
15
- @to_bank_account = all_accounts.select { |acct| acct.status == "ACTIVE" && acct.type == "BANK" }.last
16
- end
7
+ it_works_using_oauth2 do |client, client_type|
8
+ can "create a bank for #{client_type}" do
9
+ all_accounts = client.Account.all
10
+ @from_bank_account = all_accounts.select { |acct| acct.status == "ACTIVE" && acct.type == "BANK" }.first
11
+ @to_bank_account = all_accounts.select { |acct| acct.status == "ACTIVE" && acct.type == "BANK" }.last
17
12
 
18
- can "create a bank transfer" do
19
- new_transfer = client.BankTransfer.build(
20
- :amount => 300,
21
- :from_bank_account => { :account_id => @from_bank_account.account_id },
22
- :to_bank_account => { :account_id => @to_bank_account.account_id }
23
- )
24
- assert new_transfer.save, "Save failed with the following errors: #{new_transfer.errors.inspect}"
13
+ new_transfer = client.BankTransfer.build(
14
+ :amount => 300,
15
+ :from_bank_account => { :account_id => @from_bank_account.account_id },
16
+ :to_bank_account => { :account_id => @to_bank_account.account_id }
17
+ )
18
+ assert new_transfer.save, "Save failed with the following errors: #{new_transfer.errors.inspect}"
19
+ end
25
20
  end
26
21
  end
@@ -1,5 +1,8 @@
1
1
  require "test_helper"
2
2
  require "acceptance_test"
3
+ require "shoulda/matchers"
4
+
5
+ include Shoulda::Matchers
3
6
 
4
7
  class BulkOperationsTest < Test::Unit::TestCase
5
8
  include AcceptanceTest
@@ -8,18 +11,17 @@ class BulkOperationsTest < Test::Unit::TestCase
8
11
  "test-person-#{rand 1000000000}"
9
12
  end
10
13
 
11
- def setup
12
- super
13
- @client = Xeroizer::PrivateApplication.new(@consumer_key, @consumer_secret, @key_file)
14
+ setup do
15
+ @client = AcceptanceTestHelpers.oauth2_client
14
16
  end
15
17
 
16
- can "create multiple invoices at once" do
18
+ should "create multiple invoices at once" do
17
19
  c1, c2 = nil, nil
18
20
  assert_true(
19
- @client.Contact.batch_save do
20
- c1 = @client.Contact.build(name: random_name)
21
- c2 = @client.Contact.build(name: random_name)
22
- end
21
+ @client.Contact.batch_save do
22
+ c1 = @client.Contact.build(name: random_name)
23
+ c2 = @client.Contact.build(name: random_name)
24
+ end
23
25
  )
24
26
  [c1, c2].each {|c| assert_false c.new_record? }
25
27
  end
@@ -27,11 +29,11 @@ class BulkOperationsTest < Test::Unit::TestCase
27
29
  can "create and update new records in bulk" do
28
30
  c1, c2 = nil, nil
29
31
  assert_true(
30
- @client.Contact.batch_save do
31
- c1 = @client.Contact.create(name: random_name)
32
- c1.email_address = "foo@bar.com"
33
- c2 = @client.Contact.build(name: random_name)
34
- end
32
+ @client.Contact.batch_save do
33
+ c1 = @client.Contact.create(name: random_name)
34
+ c1.email_address = "foo@bar.com"
35
+ c2 = @client.Contact.build(name: random_name)
36
+ end
35
37
  )
36
38
  [c1, c2].each {|c| assert_false c.new_record? }
37
39
  c1.download_complete_record!
@@ -40,9 +42,9 @@ class BulkOperationsTest < Test::Unit::TestCase
40
42
 
41
43
  can "return false from #batch_save if validation fails" do
42
44
  assert_false(
43
- @client.Contact.batch_save do
44
- @client.Contact.build(email_address: "guy-with-no-name@example.com")
45
- end
45
+ @client.Contact.batch_save do
46
+ @client.Contact.build(email_address: "guy-with-no-name@example.com")
47
+ end
46
48
  )
47
49
  end
48
50
  end