xeroizer 2.17.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +246 -213
  3. data/lib/xeroizer/connection.rb +49 -0
  4. data/lib/xeroizer/exceptions.rb +4 -0
  5. data/lib/xeroizer/generic_application.rb +13 -5
  6. data/lib/xeroizer/http.rb +7 -80
  7. data/lib/xeroizer/http_response.rb +154 -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 +12 -6
  13. data/lib/xeroizer/models/contact_group.rb +45 -0
  14. data/lib/xeroizer/models/credit_note.rb +24 -22
  15. data/lib/xeroizer/models/currency.rb +14 -2
  16. data/lib/xeroizer/models/from_bank_account.rb +1 -0
  17. data/lib/xeroizer/models/history_record.rb +72 -0
  18. data/lib/xeroizer/models/invoice.rb +17 -3
  19. data/lib/xeroizer/models/item.rb +2 -1
  20. data/lib/xeroizer/models/item_purchase_details.rb +1 -1
  21. data/lib/xeroizer/models/line_item.rb +17 -5
  22. data/lib/xeroizer/models/manual_journal.rb +2 -1
  23. data/lib/xeroizer/models/online_invoice.rb +37 -0
  24. data/lib/xeroizer/models/option.rb +1 -1
  25. data/lib/xeroizer/models/organisation.rb +2 -0
  26. data/lib/xeroizer/models/payment_service.rb +22 -0
  27. data/lib/xeroizer/models/payroll/address.rb +53 -0
  28. data/lib/xeroizer/models/payroll/bank_account.rb +18 -6
  29. data/lib/xeroizer/models/payroll/benefit_line.rb +26 -0
  30. data/lib/xeroizer/models/payroll/benefit_type.rb +45 -0
  31. data/lib/xeroizer/models/payroll/deduction_line.rb +32 -0
  32. data/lib/xeroizer/models/payroll/deduction_type.rb +49 -0
  33. data/lib/xeroizer/models/payroll/earnings_line.rb +39 -0
  34. data/lib/xeroizer/models/payroll/earnings_type.rb +53 -0
  35. data/lib/xeroizer/models/payroll/employee.rb +30 -8
  36. data/lib/xeroizer/models/payroll/leave_application.rb +27 -0
  37. data/lib/xeroizer/models/payroll/leave_line.rb +30 -0
  38. data/lib/xeroizer/models/payroll/leave_period.rb +15 -0
  39. data/lib/xeroizer/models/payroll/pay_items.rb +22 -0
  40. data/lib/xeroizer/models/payroll/pay_run.rb +33 -0
  41. data/lib/xeroizer/models/payroll/pay_schedule.rb +40 -0
  42. data/lib/xeroizer/models/payroll/pay_template.rb +24 -0
  43. data/lib/xeroizer/models/payroll/payment_method.rb +24 -0
  44. data/lib/xeroizer/models/payroll/paystub.rb +44 -0
  45. data/lib/xeroizer/models/payroll/reimbursement_line.rb +21 -0
  46. data/lib/xeroizer/models/payroll/reimbursement_type.rb +22 -0
  47. data/lib/xeroizer/models/payroll/salary_and_wage.rb +29 -0
  48. data/lib/xeroizer/models/payroll/super_line.rb +40 -0
  49. data/lib/xeroizer/models/payroll/tax_declaration.rb +50 -0
  50. data/lib/xeroizer/models/payroll/time_off_line.rb +20 -0
  51. data/lib/xeroizer/models/payroll/time_off_type.rb +32 -0
  52. data/lib/xeroizer/models/payroll/work_location.rb +25 -0
  53. data/lib/xeroizer/models/prepayment.rb +1 -0
  54. data/lib/xeroizer/models/purchase_order.rb +6 -6
  55. data/lib/xeroizer/models/quote.rb +76 -0
  56. data/lib/xeroizer/models/schedule.rb +1 -0
  57. data/lib/xeroizer/models/tax_component.rb +1 -0
  58. data/lib/xeroizer/models/to_bank_account.rb +1 -0
  59. data/lib/xeroizer/oauth.rb +12 -1
  60. data/lib/xeroizer/oauth2.rb +82 -0
  61. data/lib/xeroizer/oauth2_application.rb +49 -0
  62. data/lib/xeroizer/payroll_application.rb +8 -3
  63. data/lib/xeroizer/record/base.rb +11 -2
  64. data/lib/xeroizer/record/base_model.rb +1 -1
  65. data/lib/xeroizer/record/base_model_http_proxy.rb +37 -17
  66. data/lib/xeroizer/record/model_definition_helper.rb +1 -1
  67. data/lib/xeroizer/record/payroll_base.rb +4 -0
  68. data/lib/xeroizer/record/record_association_helper.rb +4 -4
  69. data/lib/xeroizer/record/validators/associated_validator.rb +1 -0
  70. data/lib/xeroizer/record/xml_helper.rb +18 -18
  71. data/lib/xeroizer/report/aged_receivables_by_contact.rb +1 -1
  72. data/lib/xeroizer/report/cell_xml_helper.rb +13 -13
  73. data/lib/xeroizer/response.rb +22 -17
  74. data/lib/xeroizer/version.rb +1 -1
  75. data/lib/xeroizer.rb +34 -4
  76. data/test/acceptance/about_creating_bank_transactions_test.rb +89 -81
  77. data/test/acceptance/about_creating_prepayment_test.rb +25 -30
  78. data/test/acceptance/about_fetching_bank_transactions_test.rb +12 -12
  79. data/test/acceptance/about_online_invoice_test.rb +25 -0
  80. data/test/acceptance/acceptance_test.rb +28 -26
  81. data/test/acceptance/bank_transfer_test.rb +12 -17
  82. data/test/acceptance/bulk_operations_test.rb +18 -16
  83. data/test/acceptance/connections_test.rb +11 -0
  84. data/test/stub_responses/bad_request.json +6 -0
  85. data/test/stub_responses/connections.json +16 -0
  86. data/test/stub_responses/expired_oauth2_token.json +6 -0
  87. data/test/stub_responses/generic_response_error.json +6 -0
  88. data/test/stub_responses/invalid_oauth2_request_token.json +6 -0
  89. data/test/stub_responses/invalid_tenant_header.json +6 -0
  90. data/test/stub_responses/object_not_found.json +6 -0
  91. data/test/stub_responses/organisations.xml +10 -0
  92. data/test/stub_responses/payment_service.xml +15 -0
  93. data/test/test_helper.rb +17 -12
  94. data/test/unit/generic_application_test.rb +21 -10
  95. data/test/unit/http_test.rb +282 -10
  96. data/test/unit/models/address_test.rb +2 -2
  97. data/test/unit/models/bank_transaction_model_parsing_test.rb +2 -2
  98. data/test/unit/models/bank_transaction_test.rb +1 -1
  99. data/test/unit/models/bank_transaction_validation_test.rb +1 -1
  100. data/test/unit/models/contact_test.rb +20 -11
  101. data/test/unit/models/credit_note_test.rb +8 -8
  102. data/test/unit/models/employee_test.rb +4 -4
  103. data/test/unit/models/invoice_test.rb +12 -12
  104. data/test/unit/models/journal_line_test.rb +6 -6
  105. data/test/unit/models/journal_test.rb +4 -4
  106. data/test/unit/models/line_item_sum_test.rb +1 -1
  107. data/test/unit/models/line_item_test.rb +29 -37
  108. data/test/unit/models/manual_journal_test.rb +3 -3
  109. data/test/unit/models/organisation_test.rb +16 -2
  110. data/test/unit/models/payment_service_test.rb +29 -0
  111. data/test/unit/models/phone_test.rb +7 -7
  112. data/test/unit/models/prepayment_test.rb +4 -4
  113. data/test/unit/models/repeating_invoice_test.rb +3 -3
  114. data/test/unit/models/tax_rate_test.rb +2 -2
  115. data/test/unit/oauth2_test.rb +171 -0
  116. data/test/unit/oauth_config_test.rb +1 -1
  117. data/test/unit/record/base_model_test.rb +13 -13
  118. data/test/unit/record/base_test.rb +73 -4
  119. data/test/unit/record/block_validator_test.rb +1 -1
  120. data/test/unit/record/connection_test.rb +60 -0
  121. data/test/unit/record/model_definition_test.rb +36 -36
  122. data/test/unit/record/parse_params_test.rb +59 -0
  123. data/test/unit/record/parse_where_hash_test.rb +13 -13
  124. data/test/unit/record/record_association_test.rb +14 -14
  125. data/test/unit/record/validators_test.rb +43 -43
  126. data/test/unit/record_definition_test.rb +7 -7
  127. data/test/unit/report_definition_test.rb +7 -7
  128. data/test/unit/report_test.rb +20 -20
  129. data/test/unit_test_helper.rb +16 -0
  130. metadata +117 -27
  131. data/lib/xeroizer/models/payroll/home_address.rb +0 -24
  132. data/lib/xeroizer/partner_application.rb +0 -51
  133. data/lib/xeroizer/private_application.rb +0 -25
  134. data/lib/xeroizer/public_application.rb +0 -21
  135. data/test/unit/oauth_test.rb +0 -118
  136. data/test/unit/private_application_test.rb +0 -20
@@ -1,11 +1,11 @@
1
- require "test_helper"
1
+ require 'unit_test_helper'
2
2
 
3
3
  class LineItemTest < Test::Unit::TestCase
4
4
  include TestHelper
5
5
  include Xeroizer::Record
6
6
 
7
7
  def setup
8
- @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET)
8
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET)
9
9
  end
10
10
 
11
11
  it "line_item tracking specified correctly" do
@@ -24,61 +24,53 @@ class LineItemTest < Test::Unit::TestCase
24
24
 
25
25
  line_item.quantity = 1
26
26
  line_item.unit_amount = BigDecimal("1337.00")
27
- line_item.tax_amount = BigDecimal("0.15")
28
27
 
29
- expected = BigDecimal((line_item.quantity * (line_item.unit_amount)).to_s).round(2)
30
-
31
- assert_equal expected.to_s, line_item.line_amount.to_s,
32
- "expected line_amount to equal unit_amount times quantity"
28
+ assert_equal "1337.0", line_item.line_amount.to_s,
29
+ "expected line_amount to equal unit_amount times quantity"
33
30
  end
34
31
 
35
32
  it "line_amount equals unit_amount times quantity minus the discount if there is a discount_rate" do
36
33
  line_item = LineItem.new(nil)
37
34
  line_item.quantity = 1
38
35
  line_item.unit_amount = BigDecimal("1337.00")
39
- line_item.tax_amount = BigDecimal("0.15")
40
- line_item.discount_rate = BigDecimal("10.0")
36
+ line_item.discount_rate = BigDecimal("12.34")
41
37
 
42
- discount = (100 - line_item.discount_rate) / 100
43
- expected = BigDecimal(((line_item.quantity * line_item.unit_amount) * discount).to_s).round(2)
44
- assert_equal expected.to_s, line_item.line_amount.to_s,
45
- "expected line_amount to equal unit_amount times quantity minus the discount"
38
+ assert_equal "1172.01", line_item.line_amount.to_s,
39
+ "expected line_amount to equal unit_amount times quantity minus the discount"
46
40
  end
47
41
 
48
- it "line_amount is zero when quantity is nil or zero" do
42
+ it "line_amount equals unit_amount times quantity minus the discount if there is a discount_amount" do
49
43
  line_item = LineItem.new(nil)
44
+ line_item.quantity = 1
45
+ line_item.unit_amount = BigDecimal("1337.00")
46
+ line_item.discount_amount = BigDecimal("164.99")
50
47
 
51
- line_item.quantity = nil
52
- line_item.unit_amount = BigDecimal("1.00")
53
- line_item.tax_amount = BigDecimal("0.15")
54
-
55
- assert_equal "0.0", line_item.line_amount.to_s, "expected line amount zero when quantity is nil"
56
-
57
- line_item.quantity = 0
58
- assert_equal "0.0", line_item.line_amount.to_s, "expected line amount zero when quantity is zero"
48
+ assert_equal "1172.01", line_item.line_amount.to_s,
49
+ "expected line_amount to equal unit_amount times quantity minus the discount amount"
59
50
  end
60
51
 
61
- it "is not possible to set unit_amount to zero" do
52
+ it "coerces decimals when calculating line amount" do
62
53
  line_item = LineItem.new(nil)
54
+ line_item.quantity = "1"
55
+ line_item.unit_amount = 50
56
+ assert_equal 50, line_item.line_amount,
57
+ "expected line amount to be calculated from coerced values"
58
+ end
63
59
 
64
- line_item.quantity = 1
65
- line_item.unit_amount = nil
66
- line_item.tax_amount = BigDecimal("0.15")
60
+ context "line_item validations" do
67
61
 
68
- assert_equal 0.0, line_item.unit_amount,
69
- "Expected setting unit_amount to nil to be ignored, i.e., it should remain zero"
70
- end
62
+ it "is an invalid line_item if there is no description" do
63
+ line_item = LineItem.new(nil)
71
64
 
72
- it "line_amount is zero when unit_amount is nil or zero" do
73
- line_item = LineItem.new(nil)
65
+ assert_equal(false, line_item.valid?)
66
+ end
74
67
 
75
- line_item.quantity = 1
76
- line_item.unit_amount = nil
77
- line_item.tax_amount = BigDecimal("0.15")
68
+ it "is a valid line_item with minimal attributes" do
69
+ line_item = LineItem.new(nil)
70
+ line_item.description = "Test Description"
78
71
 
79
- assert_equal "0.0", line_item.line_amount.to_s, "expected line amount zero when unit_amount is nil"
72
+ assert_equal(true, line_item.valid?)
73
+ end
80
74
 
81
- line_item.unit_amount = BigDecimal("0.00")
82
- assert_equal "0.0", line_item.line_amount.to_s, "expected line amount zero when unit_amount is zero"
83
75
  end
84
76
  end
@@ -1,10 +1,10 @@
1
- require 'test_helper'
1
+ require 'unit_test_helper'
2
2
 
3
3
  class ManualJournalTest < Test::Unit::TestCase
4
4
  include TestHelper
5
5
 
6
6
  def setup
7
- @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET)
7
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET)
8
8
  mock_api('ManualJournals')
9
9
  end
10
10
 
@@ -22,4 +22,4 @@ class ManualJournalTest < Test::Unit::TestCase
22
22
  end
23
23
  end
24
24
  end
25
- end
25
+ end
@@ -1,10 +1,10 @@
1
- require 'test_helper'
1
+ require 'unit_test_helper'
2
2
 
3
3
  class OrganisationTest < Test::Unit::TestCase
4
4
  include TestHelper
5
5
 
6
6
  def setup
7
- @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET)
7
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET)
8
8
  end
9
9
 
10
10
  context "sales_tax_basis_validations" do
@@ -35,4 +35,18 @@ class OrganisationTest < Test::Unit::TestCase
35
35
  end
36
36
  end
37
37
 
38
+ context "parse response" do
39
+ it "includes payment_terms" do
40
+ @instance = Xeroizer::Record::OrganisationModel.new(nil, "Organisation")
41
+ some_xml = get_record_xml("organisations")
42
+
43
+ result = @instance.parse_response(some_xml)
44
+ organisation = result.response_items.first
45
+
46
+ assert_equal(organisation.payment_terms.bills.day, "4")
47
+ assert_equal(organisation.payment_terms.bills.type, "OFFOLLOWINGMONTH")
48
+ assert_equal(organisation.payment_terms.sales.day, "2")
49
+ assert_equal(organisation.payment_terms.sales.type, "OFFOLLOWINGMONTH")
50
+ end
51
+ end
38
52
  end
@@ -0,0 +1,29 @@
1
+ require 'unit_test_helper'
2
+
3
+ class PaymentServiceTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def setup
7
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET)
8
+ end
9
+
10
+ context "response parsing" do
11
+ it "parses default attributes" do
12
+ @instance = Xeroizer::Record::PaymentServiceModel.new(nil, "PaymentService")
13
+
14
+ some_xml = get_record_xml("payment_service")
15
+
16
+ result = @instance.parse_response(some_xml)
17
+ payment_service = result.response_items.first
18
+
19
+ keys = [:payment_service_id,
20
+ :payment_service_name,
21
+ :payment_service_type,
22
+ :payment_service_url,
23
+ :pay_now_text
24
+ ]
25
+
26
+ assert_equal(payment_service.attributes.keys, keys)
27
+ end
28
+ end
29
+ end
@@ -1,23 +1,23 @@
1
- require 'test_helper'
1
+ require 'unit_test_helper'
2
2
 
3
3
  class PhoneTest < Test::Unit::TestCase
4
4
  include TestHelper
5
-
5
+
6
6
  def setup
7
- @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET)
7
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET)
8
8
  @contact = @client.Contact.build
9
9
 
10
10
  end
11
-
11
+
12
12
  context "validators" do
13
-
13
+
14
14
  should "not allow long phone numbers" do
15
15
  @phone = @contact.add_phone(phone_number: "1234567890123456789012345678901234567890123456789012345678901234567890")
16
16
  assert_equal(false, @phone.valid?)
17
17
  blank_error = @phone.errors_for(:phone_number).first
18
18
  assert_not_nil(blank_error)
19
19
  assert_equal("must be shorter than 50 characters", blank_error)
20
-
20
+
21
21
  end
22
22
 
23
23
  should "allow phone numbers" do
@@ -27,5 +27,5 @@ class PhoneTest < Test::Unit::TestCase
27
27
  assert_equal(0, @phone.errors.size)
28
28
  end
29
29
  end
30
-
30
+
31
31
  end
@@ -1,14 +1,14 @@
1
- require 'test_helper'
1
+ require 'unit_test_helper'
2
2
 
3
3
  class PrepaymentTest < Test::Unit::TestCase
4
4
  include TestHelper
5
-
5
+
6
6
  def setup
7
- @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET)
7
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET)
8
8
  mock_api("Prepayments")
9
9
  @prepayment = @client.Prepayment.first
10
10
  end
11
-
11
+
12
12
  context "prepayment attributes" do
13
13
  should "large-scale testing from API XML" do
14
14
  prepayments = @client.Prepayment.all
@@ -1,10 +1,10 @@
1
- require 'test_helper'
1
+ require 'unit_test_helper'
2
2
 
3
3
  class RepeatingInvoiceTest < Test::Unit::TestCase
4
4
  include TestHelper
5
5
 
6
6
  def setup
7
- @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET)
7
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET)
8
8
  mock_api('RepeatingInvoices')
9
9
  end
10
10
 
@@ -18,7 +18,7 @@ class RepeatingInvoiceTest < Test::Unit::TestCase
18
18
  repeating_invoice = repeating_invoices.first
19
19
 
20
20
  assert_equal "PowerDirect", repeating_invoice.contact_name
21
- assert_equal BigDecimal.new(90), repeating_invoice.total
21
+ assert_equal BigDecimal(90), repeating_invoice.total
22
22
  assert_equal true, repeating_invoice.accounts_payable?
23
23
 
24
24
  schedule = repeating_invoice.schedule
@@ -1,11 +1,11 @@
1
- require 'test_helper'
1
+ require 'unit_test_helper'
2
2
  require 'mocha/test_unit'
3
3
 
4
4
  class TaxRateTest < Test::Unit::TestCase
5
5
  include TestHelper
6
6
 
7
7
  def setup
8
- @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET)
8
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET)
9
9
  end
10
10
 
11
11
  should "have a primary key value of :name" do
@@ -0,0 +1,171 @@
1
+ require 'unit_test_helper'
2
+
3
+ class OAuth2Test < UnitTestCase
4
+ include TestHelper
5
+
6
+ setup do
7
+ @client_id = 'client_id'
8
+ @client_secret = 'client_secret'
9
+ @access_token = 'access_token'
10
+ @site = 'https://example.com'
11
+ @content_type = 'application/xml'
12
+ @path = '/path'
13
+ @uri = "#{@site}#{@path}"
14
+ @response_body = '{"test": true}'
15
+ @status_code = 200
16
+ @additional_headers = {foo: 'bar'}
17
+ @request_body = "xml"
18
+ @request_headers = { 'Content-Type' => @content_type }
19
+ @tenant_id = "tenant"
20
+ end
21
+
22
+ def instance
23
+ Xeroizer::OAuth2.new(@client_id, @client_secret, {
24
+ site: @site
25
+ }).tap do |oauth2|
26
+ oauth2.authorize_from_access(@access_token)
27
+ end
28
+ end
29
+
30
+ context "get" do
31
+ setup do
32
+ stub_request(:get, @uri).
33
+ with(
34
+ headers: {
35
+ 'Authorization' => "Bearer #{@access_token}",
36
+ }
37
+ ).to_return(status: @status_code, body: @response_body, headers: {})
38
+ end
39
+
40
+ context "when tenant_id is not present" do
41
+ should "make a get request" do
42
+ result = instance.get(@path)
43
+ assert_equal(result.code, @status_code)
44
+ assert_equal(result.plain_body, @response_body)
45
+ assert_requested :get, @uri
46
+ assert_not_requested :get, @uri, headers: { "Xero-tenant-id" => @tenant_id }
47
+ end
48
+ end
49
+
50
+ context "when tenant_id is present" do
51
+ setup do
52
+ @client = instance
53
+ @client.tenant_id = @tenant_id
54
+ end
55
+
56
+ should "make a get request with the tenant_id" do
57
+ result = @client.get(@path)
58
+ assert_equal(result.code, @status_code)
59
+ assert_equal(result.plain_body, @response_body)
60
+ assert_requested :get, @uri, headers: { "Xero-tenant-id" => @tenant_id }
61
+ end
62
+ end
63
+ end
64
+
65
+ context "delete" do
66
+ setup do
67
+ stub_request(:delete, @uri).
68
+ with(
69
+ headers: {
70
+ 'Authorization' => "Bearer #{@access_token}",
71
+ }
72
+ ).to_return(status: @status_code, body: @response_body, headers: {})
73
+ end
74
+
75
+ context "when tenant_id is not present" do
76
+ should "make a delete request" do
77
+ result = instance.delete(@path)
78
+ assert_equal(result.code, @status_code)
79
+ assert_not_requested :delete, @uri, headers: { "Xero-tenant-id" => @tenant_id }
80
+ end
81
+ end
82
+
83
+
84
+ context "when tenant_id is present" do
85
+ setup do
86
+ @client = instance
87
+ @client.tenant_id = @tenant_id
88
+ end
89
+
90
+ should "make a delete request with the tenant_id" do
91
+ result = @client.delete(@path)
92
+ assert_equal(result.code, @status_code)
93
+ assert_equal(result.plain_body, @response_body)
94
+ assert_requested :delete, @uri, headers: { "Xero-tenant-id" => @tenant_id }
95
+ end
96
+ end
97
+ end
98
+
99
+ context "post" do
100
+ setup do
101
+ stub_request(:post, @uri).
102
+ with(
103
+ body: @request_body,
104
+ headers: {
105
+ 'Authorization' => "Bearer #{@access_token}",
106
+ 'Content-Type' => @content_type,
107
+ }).
108
+ to_return(status: @status_code, body: @response_body, headers: @request_headers)
109
+
110
+ end
111
+
112
+ context "when tenant_id is not present" do
113
+ should "make a post request" do
114
+ result = instance.post(@path, @request_body, @request_headers)
115
+ assert_equal(result.code, @status_code)
116
+ assert_equal(result.plain_body, @response_body)
117
+ assert_not_requested :post, @uri, headers: { "Xero-tenant-id" => @tenant_id }
118
+ end
119
+ end
120
+
121
+ context "when tenant_id is present" do
122
+ setup do
123
+ @client = instance
124
+ @client.tenant_id = @tenant_id
125
+ end
126
+
127
+ should "make a post request with the tenant_id" do
128
+ result = @client.post(@path, @request_body, @request_headers)
129
+ assert_equal(result.code, @status_code)
130
+ assert_equal(result.plain_body, @response_body)
131
+ assert_requested :post, @uri, headers: { "Xero-tenant-id" => @tenant_id }
132
+ end
133
+ end
134
+ end
135
+
136
+ context "put" do
137
+ setup do
138
+ stub_request(:put, @uri).
139
+ with(
140
+ body: @request_body,
141
+ headers: {
142
+ 'Authorization' => "Bearer #{@access_token}",
143
+ 'Content-Type' => @content_type,
144
+ }).
145
+ to_return(status: @status_code, body: @response_body, headers: @request_headers)
146
+ end
147
+
148
+ context "when tenant_id is not present" do
149
+ should "make a put request" do
150
+ result = instance.put(@path, @request_body, @request_headers)
151
+ assert_equal(result.code, @status_code)
152
+ assert_equal(result.plain_body, @response_body)
153
+ assert_not_requested :put, @uri, headers: { "Xero-tenant-id" => @tenant_id }
154
+ end
155
+ end
156
+
157
+ context "when tenant_id is present" do
158
+ setup do
159
+ @client = instance
160
+ @client.tenant_id = @tenant_id
161
+ end
162
+
163
+ should "make a put request" do
164
+ result = @client.put(@path, @request_body, @request_headers)
165
+ assert_equal(result.code, @status_code)
166
+ assert_equal(result.plain_body, @response_body)
167
+ assert_requested :put, @uri, headers: { "Xero-tenant-id" => @tenant_id }
168
+ end
169
+ end
170
+ end
171
+ end
@@ -1,4 +1,4 @@
1
- require "test_helper"
1
+ require 'unit_test_helper'
2
2
 
3
3
  class OAuthConfigTest < Test::Unit::TestCase
4
4
  include Xeroizer
@@ -1,50 +1,50 @@
1
- require 'test_helper'
1
+ require 'unit_test_helper'
2
2
 
3
3
  class RecordBaseModelTest < Test::Unit::TestCase
4
4
  include TestHelper
5
-
5
+
6
6
  class AppleModel < Xeroizer::Record::BaseModel
7
7
  set_api_controller_name 'AppleController'
8
8
  set_permissions :read, :write
9
9
  end
10
-
10
+
11
11
  class PearModel < Xeroizer::Record::BaseModel
12
12
  set_api_controller_name 'PearController'
13
13
  set_permissions :read
14
14
  end
15
-
15
+
16
16
  class OrangeModel < Xeroizer::Record::BaseModel
17
17
  set_permissions :read, :write, :update
18
18
  end
19
-
19
+
20
20
  def setup
21
- @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET)
21
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET)
22
22
  @apple_model = AppleModel.new(@client, 'Apple')
23
23
  @pear_model = PearModel.new(@client, 'Pear')
24
24
  @orange_model = OrangeModel.new(@client, 'Orange')
25
25
  end
26
-
26
+
27
27
  context "RecordModel.api_controller_name" do
28
-
28
+
29
29
  should "api_controller_name should default to pluralized model name" do
30
30
  assert_equal('Oranges', @orange_model.api_controller_name)
31
31
  end
32
-
32
+
33
33
  should "set_api_controller_name should set the base controller name used by Xero" do
34
34
  assert_equal('AppleController', @apple_model.api_controller_name)
35
35
  assert_equal('PearController', @pear_model.api_controller_name)
36
36
  end
37
-
37
+
38
38
  end
39
-
39
+
40
40
  context "model permssions" do
41
-
41
+
42
42
  should "save permissions correctly" do
43
43
  assert_equal(%w(read write), @apple_model.class.permissions.keys.map(&:to_s).sort)
44
44
  assert_equal(%w(read), @pear_model.class.permissions.keys.map(&:to_s).sort)
45
45
  assert_equal(%w(read update write), @orange_model.class.permissions.keys.map(&:to_s).sort)
46
46
  end
47
-
47
+
48
48
  end
49
49
 
50
50
  context "InvalidPermissionError" do
@@ -1,10 +1,11 @@
1
- require 'test_helper'
1
+ require 'unit_test_helper'
2
2
 
3
3
  class RecordBaseTest < Test::Unit::TestCase
4
4
  include TestHelper
5
5
 
6
6
  def setup
7
- @client = Xeroizer::PublicApplication.new(CONSUMER_KEY, CONSUMER_SECRET)
7
+ @client = Xeroizer::OAuth2Application.new(CLIENT_ID, CLIENT_SECRET, tenant_id: TENANT_ID)
8
+ @client.authorize_from_access(ACCESS_TOKEN)
8
9
  @contact = @client.Contact.build(:name => 'Test Contact Name ABC')
9
10
  end
10
11
 
@@ -26,14 +27,14 @@ class RecordBaseTest < Test::Unit::TestCase
26
27
  context "new_record? states" do
27
28
 
28
29
  should "new_record? should be false when loading data" do
29
- Xeroizer::OAuth.any_instance.stubs(:get).returns(stub(:plain_body => get_record_xml(:contact), :code => '200'))
30
+ Xeroizer::OAuth2.any_instance.stubs(:get).returns(stub(:plain_body => get_record_xml(:contact), :code => '200'))
30
31
  contact = @client.Contact.find('TESTID')
31
32
  assert_kind_of(Xeroizer::Record::Contact, contact)
32
33
  assert_equal(false, contact.new_record?)
33
34
  end
34
35
 
35
36
  should "new_record? should be false after successfully creating a record" do
36
- Xeroizer::OAuth.any_instance.stubs(:put).returns(stub(:plain_body => get_record_xml(:contact), :code => '200'))
37
+ Xeroizer::OAuth2.any_instance.stubs(:put).returns(stub(:plain_body => get_record_xml(:contact), :code => '200'))
37
38
  assert_equal(true, @contact.new_record?)
38
39
  assert_nil(@contact.contact_id)
39
40
  assert_equal(true, @contact.save, "Error saving contact: #{@contact.errors.inspect}")
@@ -103,4 +104,72 @@ class RecordBaseTest < Test::Unit::TestCase
103
104
  an_example_instance.save
104
105
  end
105
106
  end
107
+
108
+ context 'build' do
109
+
110
+ should "raise an undefined method error with useful message" do
111
+ assert_raise_message("undefined method `this_method_does_not_exist=' for #<Xeroizer::Record::Contact >") do
112
+ @client.Contact.build(:this_method_does_not_exist => true)
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ context 'saving' do
119
+ context 'invalid record' do
120
+ setup do
121
+ @contact.stubs(:valid?).returns(false)
122
+ end
123
+
124
+ must 'raise an exception saving with #save!' do
125
+ assert_raise(Xeroizer::RecordInvalid) do
126
+ @contact.save!
127
+ end
128
+ end
129
+
130
+ must 'return false saving with #save' do
131
+ assert_equal(false, @contact.save)
132
+ end
133
+ end
134
+
135
+ context 'api error received' do
136
+ setup do
137
+ response = get_file_as_string('api_exception.xml')
138
+ doc = Nokogiri::XML(response)
139
+ exception = Xeroizer::ApiException.new(doc.root.xpath("Type").text,
140
+ doc.root.xpath("Message").text,
141
+ response,
142
+ doc,
143
+ '<FakeRequest />')
144
+
145
+ @contact.stubs(:valid?).returns(true)
146
+ @contact.stubs(:create).raises(exception)
147
+ @contact.stubs(:update).raises(exception)
148
+ end
149
+
150
+ must 'raise an exception creating records with #save!' do
151
+ @contact.stubs(:new_record?).returns(true)
152
+ assert_raise(Xeroizer::ApiException) do
153
+ @contact.save!
154
+ end
155
+ end
156
+
157
+ must 'raise an exception updating records with #save!' do
158
+ @contact.stubs(:new_record?).returns(false)
159
+ assert_raise(Xeroizer::ApiException) do
160
+ @contact.save!
161
+ end
162
+ end
163
+
164
+ must 'return false creating records with #save' do
165
+ @contact.stubs(:new_record?).returns(true)
166
+ assert_equal(false, @contact.save)
167
+ end
168
+
169
+ must 'return false updating records with #save' do
170
+ @contact.stubs(:new_record?).returns(false)
171
+ assert_equal(false, @contact.save)
172
+ end
173
+ end
174
+ end
106
175
  end
@@ -1,4 +1,4 @@
1
- require "test_helper"
1
+ require 'unit_test_helper'
2
2
 
3
3
  class BlockValidatorTest < Test::Unit::TestCase
4
4
  def setup