xero_gateway-float 2.0.18 → 2.1.1

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 (36) hide show
  1. data/Rakefile +7 -1
  2. data/examples/partner_app.rb +2 -2
  3. data/lib/xero_gateway/accounts_list.rb +0 -4
  4. data/lib/xero_gateway/bank_transaction.rb +0 -2
  5. data/lib/xero_gateway/contact.rb +6 -9
  6. data/lib/xero_gateway/credit_note.rb +1 -3
  7. data/lib/xero_gateway/error.rb +16 -0
  8. data/lib/xero_gateway/exceptions.rb +5 -0
  9. data/lib/xero_gateway/gateway.rb +184 -82
  10. data/lib/xero_gateway/http.rb +38 -32
  11. data/lib/xero_gateway/invoice.rb +17 -9
  12. data/lib/xero_gateway/journal_line.rb +102 -0
  13. data/lib/xero_gateway/line_item_calculations.rb +0 -4
  14. data/lib/xero_gateway/manual_journal.rb +163 -0
  15. data/lib/xero_gateway/oauth.rb +29 -23
  16. data/lib/xero_gateway/partner_app.rb +6 -1
  17. data/lib/xero_gateway/response.rb +2 -0
  18. data/lib/xero_gateway/tax_rate.rb +1 -1
  19. data/lib/xero_gateway.rb +3 -0
  20. data/test/integration/accounts_list_test.rb +4 -4
  21. data/test/integration/create_invoice_test.rb +6 -0
  22. data/test/integration/create_manual_journal_test.rb +35 -0
  23. data/test/integration/create_payments_test.rb +35 -0
  24. data/test/integration/get_invoice_test.rb +27 -12
  25. data/test/integration/get_manual_journal_test.rb +50 -0
  26. data/test/integration/get_manual_journals_test.rb +88 -0
  27. data/test/integration/update_manual_journal_test.rb +31 -0
  28. data/test/test_helper.rb +39 -1
  29. data/test/unit/bank_transaction_test.rb +1 -1
  30. data/test/unit/credit_note_test.rb +1 -1
  31. data/test/unit/gateway_test.rb +15 -15
  32. data/test/unit/invoice_test.rb +3 -2
  33. data/test/unit/manual_journal_test.rb +93 -0
  34. data/test/unit/payment_test.rb +34 -0
  35. data/xero_gateway.gemspec +2 -2
  36. metadata +11 -2
data/lib/xero_gateway.rb CHANGED
@@ -8,6 +8,7 @@ require "oauth"
8
8
  require 'oauth/signature/rsa/sha1'
9
9
  require "forwardable"
10
10
  require "active_support/all"
11
+ require "tempfile"
11
12
 
12
13
  require File.join(File.dirname(__FILE__), 'oauth', 'oauth_consumer')
13
14
 
@@ -27,6 +28,8 @@ require File.join(File.dirname(__FILE__), 'xero_gateway', 'payment')
27
28
  require File.join(File.dirname(__FILE__), 'xero_gateway', 'invoice')
28
29
  require File.join(File.dirname(__FILE__), 'xero_gateway', 'bank_transaction')
29
30
  require File.join(File.dirname(__FILE__), 'xero_gateway', 'credit_note')
31
+ require File.join(File.dirname(__FILE__), 'xero_gateway', 'journal_line')
32
+ require File.join(File.dirname(__FILE__), 'xero_gateway', 'manual_journal')
30
33
  require File.join(File.dirname(__FILE__), 'xero_gateway', 'address')
31
34
  require File.join(File.dirname(__FILE__), 'xero_gateway', 'phone')
32
35
  require File.join(File.dirname(__FILE__), 'xero_gateway', 'organisation')
@@ -21,10 +21,10 @@ class AccountsListTest < Test::Unit::TestCase
21
21
  def test_raise_error_on_not_loaded
22
22
  accounts_list = @gateway.get_accounts_list(false)
23
23
  assert_equal(false, accounts_list.loaded?)
24
- assert_raise(XeroGateway::AccountsList::AccountsListNotLoadedError) { accounts_list[200] }
25
- assert_raise(XeroGateway::AccountsList::AccountsListNotLoadedError) { accounts_list.find_by_code(200) }
26
- assert_raise(XeroGateway::AccountsList::AccountsListNotLoadedError) { accounts_list.find_all_by_type('EXPENSE') }
27
- assert_raise(XeroGateway::AccountsList::AccountsListNotLoadedError) { accounts_list.find_all_by_tax_type('OUTPUT') }
24
+ assert_raise(XeroGateway::AccountsListNotLoadedError) { accounts_list[200] }
25
+ assert_raise(XeroGateway::AccountsListNotLoadedError) { accounts_list.find_by_code(200) }
26
+ assert_raise(XeroGateway::AccountsListNotLoadedError) { accounts_list.find_all_by_type('EXPENSE') }
27
+ assert_raise(XeroGateway::AccountsListNotLoadedError) { accounts_list.find_all_by_tax_type('OUTPUT') }
28
28
  end
29
29
 
30
30
  # Test simple lookup by account code (from cache).
@@ -33,6 +33,12 @@ class CreateInvoiceTest < Test::Unit::TestCase
33
33
  example_invoice = dummy_invoice.dup
34
34
  assert_equal true, example_invoice.valid?, "invoice is invalid - errors:\n\t#{example_invoice.errors.map { | error | "#{error[0]} #{error[1]}"}.join("\n\t")}"
35
35
  end
36
+
37
+ def test_create_invoice_invalid_with_invalid_invoice_type
38
+ example_invoice = dummy_invoice.dup
39
+ example_invoice.invoice_type = "ABC"
40
+ assert_equal false, example_invoice.valid?
41
+ end
36
42
 
37
43
  private
38
44
 
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class CreateManualJournalTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def setup
7
+ @gateway = XeroGateway::Gateway.new(CONSUMER_KEY, CONSUMER_SECRET)
8
+
9
+ if STUB_XERO_CALLS
10
+ @gateway.xero_url = "DUMMY_URL"
11
+
12
+ @gateway.stubs(:http_put).with {|client, url, body, params| url =~ /ManualJournals$/ }.returns(get_file_as_string("create_manual_journal.xml"))
13
+ @gateway.stubs(:http_post).with {|client, url, body, params| url =~ /ManualJournals$/ }.returns(get_file_as_string("manual_journal.xml"))
14
+ end
15
+ end
16
+
17
+ def test_create_manual_journal
18
+ example_manual_journal = create_test_manual_journal.dup
19
+
20
+ result = @gateway.create_manual_journal(example_manual_journal)
21
+ assert_kind_of XeroGateway::Response, result
22
+ assert result.success?
23
+ assert !result.request_xml.nil?
24
+ assert !result.response_xml.nil?
25
+ assert !result.manual_journal.manual_journal_id.nil?
26
+ assert example_manual_journal.manual_journal_id =~ GUID_REGEX
27
+ end
28
+
29
+ def test_create_manual_journal_valid
30
+ example_manual_journal = create_test_manual_journal.dup
31
+ assert_equal true, example_manual_journal.valid?,
32
+ "manual_journal is invalid - errors:\n\t#{example_manual_journal.errors.map { | error | "#{error[0]} #{error[1]}"}.join("\n\t")}"
33
+ end
34
+
35
+ end
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class CreatePaymentsTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def setup
7
+ @gateway = XeroGateway::Gateway.new(CONSUMER_KEY, CONSUMER_SECRET)
8
+
9
+ if STUB_XERO_CALLS
10
+ @gateway.xero_url = "DUMMY_URL"
11
+
12
+ @gateway.stubs(:http_put).with {|client, url, body, params| url =~ /Invoices$/ }.returns(get_file_as_string("create_invoice.xml"))
13
+ @gateway.stubs(:http_put).with {|client, url, params| url =~ /Payments$/ }.returns(get_file_as_string("create_payments.xml"))
14
+ end
15
+ end
16
+
17
+ def test_create_payment
18
+ example_invoice = dummy_invoice.dup
19
+
20
+ result = @gateway.create_invoice(example_invoice)
21
+
22
+ payment = XeroGateway::Payment.new(
23
+ :invoice_id => result.invoice.invoice_id,
24
+ :amount => 500,
25
+ :reference => "Test Payment",
26
+ :date => Time.now,
27
+ :code => "601"
28
+ )
29
+
30
+ result = @gateway.create_payment(payment)
31
+
32
+ assert_kind_of XeroGateway::Response, result
33
+ assert result.success?
34
+ end
35
+ end
@@ -2,18 +2,25 @@ require File.dirname(__FILE__) + '/../test_helper'
2
2
 
3
3
  class GetInvoiceTest < Test::Unit::TestCase
4
4
  include TestHelper
5
-
5
+
6
+ INVOICE_GET_URL = /Invoices(\/[0-9a-z\-]+)?$/i
7
+
6
8
  def setup
7
9
  @gateway = XeroGateway::Gateway.new(CONSUMER_KEY, CONSUMER_SECRET)
8
-
10
+
9
11
  if STUB_XERO_CALLS
10
12
  @gateway.xero_url = "DUMMY_URL"
11
-
12
- @gateway.stubs(:http_get).with {|client, url, params| url =~ /Invoices(\/[0-9a-z\-]+)?$/i }.returns(get_file_as_string("invoice.xml"))
13
- @gateway.stubs(:http_put).with {|client, url, body, params| url =~ /Invoices$/ }.returns(get_file_as_string("create_invoice.xml"))
13
+
14
+ @gateway.stubs(:http_get).with do |client, url, params, headers|
15
+ url =~ INVOICE_GET_URL && headers["Accept"] == "application/pdf"
16
+ end.returns(get_file_as_string("get_invoice.pdf"))
17
+
18
+ @gateway.stubs(:http_get).with {|client, url, params, headers| url =~ INVOICE_GET_URL && headers["Accept"].blank? }.returns(get_file_as_string("invoice.xml"))
19
+ @gateway.stubs(:http_put).with {|client, url, body, params| url =~ /Invoices$/ }.returns(get_file_as_string("create_invoice.xml"))
20
+
14
21
  end
15
22
  end
16
-
23
+
17
24
  def test_get_invoice
18
25
  # Make sure there is an invoice in Xero to retrieve
19
26
  invoice = @gateway.create_invoice(dummy_invoice).invoice
@@ -21,28 +28,36 @@ class GetInvoiceTest < Test::Unit::TestCase
21
28
  result = @gateway.get_invoice(invoice.invoice_id)
22
29
  assert result.success?
23
30
  assert !result.request_params.nil?
24
- assert !result.response_xml.nil?
31
+ assert !result.response_xml.nil?
25
32
  assert_equal result.invoice.invoice_number, invoice.invoice_number
26
33
 
27
34
  result = @gateway.get_invoice(invoice.invoice_number)
28
35
  assert result.success?
29
36
  assert !result.request_params.nil?
30
- assert !result.response_xml.nil?
37
+ assert !result.response_xml.nil?
31
38
  assert_equal result.invoice.invoice_id, invoice.invoice_id
32
39
  end
33
-
40
+
34
41
  def test_line_items_downloaded_set_correctly
35
42
  # Make sure there is an invoice in Xero to retrieve.
36
43
  example_invoice = @gateway.create_invoice(dummy_invoice).invoice
37
-
44
+
38
45
  # No line items.
39
46
  response = @gateway.get_invoice(example_invoice.invoice_id)
40
47
  assert_equal(true, response.success?)
41
-
48
+
42
49
  invoice = response.invoice
43
50
  assert_kind_of(XeroGateway::LineItem, invoice.line_items.first)
44
51
  assert_kind_of(XeroGateway::Invoice, invoice)
45
52
  assert_equal(true, invoice.line_items_downloaded?)
46
53
  end
47
-
54
+
55
+ def test_get_invoice_pdf
56
+ # Make sure there is an invoice in Xero to retrieve
57
+ example_invoice = @gateway.create_invoice(dummy_invoice).invoice
58
+
59
+ pdf_tempfile = @gateway.get_invoice(example_invoice.invoice_id, :pdf)
60
+ assert_equal get_file_as_string("get_invoice.pdf"), File.open(pdf_tempfile.path).read
61
+ end
62
+
48
63
  end
@@ -0,0 +1,50 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class GetManualJournalTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def setup
7
+ @gateway = XeroGateway::Gateway.new(CONSUMER_KEY, CONSUMER_SECRET)
8
+
9
+ if STUB_XERO_CALLS
10
+ @gateway.xero_url = "DUMMY_URL"
11
+
12
+ @gateway.stubs(:http_get).with {|client, url, params| url =~ /ManualJournals(\/[0-9a-z\-]+)?$/i }.returns(get_file_as_string("manual_journal.xml"))
13
+ @gateway.stubs(:http_put).with {|client, url, body, params| url =~ /ManualJournals$/ }.returns(get_file_as_string("create_manual_journal.xml"))
14
+ end
15
+ end
16
+
17
+ def test_get_manual_journal
18
+ # Make sure there is a manual journal in Xero to retrieve
19
+ response = @gateway.create_manual_journal(create_test_manual_journal)
20
+ manual_journal = response.manual_journal
21
+
22
+ result = @gateway.get_manual_journal(manual_journal.manual_journal_id)
23
+ assert result.success?
24
+ assert !result.request_params.nil?
25
+ assert !result.response_xml.nil?
26
+ assert_equal result.manual_journal.manual_journal_id, manual_journal.manual_journal_id
27
+ assert_equal result.manual_journal.narration, manual_journal.narration
28
+
29
+ result = @gateway.get_manual_journal(manual_journal.manual_journal_id)
30
+ assert result.success?
31
+ assert !result.request_params.nil?
32
+ assert !result.response_xml.nil?
33
+ assert_equal result.manual_journal.manual_journal_id, manual_journal.manual_journal_id
34
+ end
35
+
36
+ def test_journal_lines_downloaded_set_correctly
37
+ # Make sure there is a manual journal in Xero to retrieve.
38
+ example_manual_journal = @gateway.create_manual_journal(create_test_manual_journal).manual_journal
39
+
40
+ # No line items.
41
+ response = @gateway.get_manual_journal(example_manual_journal.manual_journal_id)
42
+ assert response.success?
43
+
44
+ manual_journal = response.manual_journal
45
+ assert_kind_of(XeroGateway::JournalLine, manual_journal.journal_lines.first)
46
+ assert_kind_of(XeroGateway::ManualJournal, manual_journal)
47
+ assert manual_journal.journal_lines_downloaded?
48
+ end
49
+
50
+ end
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class GetManualJournalsTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ INVALID_MANUAL_JOURNAL_ID = "99999999-9999-9999-9999-999999999999" unless defined?(INVALID_MANUAL_JOURNAL_ID)
7
+
8
+ def setup
9
+ @gateway = XeroGateway::Gateway.new(CONSUMER_KEY, CONSUMER_SECRET)
10
+
11
+ if STUB_XERO_CALLS
12
+ @gateway.xero_url = "DUMMY_URL"
13
+
14
+ @gateway.stubs(:http_get).with {|client, url, params| url =~ /ManualJournals(\/[0-9a-z\-]+)?$/i }.returns(get_file_as_string("manual_journals.xml"))
15
+ @gateway.stubs(:http_put).with {|client, url, body, params| url =~ /ManualJournals$/ }.returns(get_file_as_string("create_manual_journal.xml"))
16
+
17
+ # Get a manual journal with an invalid ID.
18
+ @gateway.stubs(:http_get).with {|client, url, params| url =~ Regexp.new("ManualJournals/#{INVALID_MANUAL_JOURNAL_ID}") }.returns(get_file_as_string("manual_journal_not_found_error.xml"))
19
+ end
20
+ end
21
+
22
+ def test_get_manual_journals
23
+ # Make sure there is a manual journal in Xero to retrieve
24
+ manual_journal = @gateway.create_manual_journal(create_test_manual_journal).manual_journal
25
+
26
+ result = @gateway.get_manual_journals
27
+ assert result.success?
28
+ assert !result.request_params.nil?
29
+ assert !result.response_xml.nil?
30
+ assert result.manual_journals.collect {|i| i.narration}.include?(manual_journal.narration)
31
+ assert result.manual_journals.collect {|i| i.manual_journal_id}.include?(manual_journal.manual_journal_id)
32
+ end
33
+
34
+ def test_get_manual_journals_with_modified_since_date
35
+ # Create a test manual journal
36
+ @gateway.create_manual_journal(create_test_manual_journal)
37
+
38
+ # Check that it is returned
39
+ result = @gateway.get_manual_journals(:modified_since => Date.today - 1)
40
+ assert result.success?
41
+ assert !result.request_params.nil?
42
+ assert !result.response_xml.nil?
43
+ assert result.request_params.keys.include?(:ModifiedAfter) # make sure the flag was sent
44
+ end
45
+
46
+ def test_journal_lines_downloaded_set_correctly
47
+ # No line items.
48
+ response = @gateway.get_manual_journals
49
+ assert_equal(true, response.success?)
50
+
51
+ manual_journal = response.manual_journals.first
52
+ assert_kind_of(XeroGateway::ManualJournal, manual_journal)
53
+ assert !manual_journal.journal_lines_downloaded?
54
+ end
55
+
56
+ # Make sure that a reference to gateway is passed when the get_manual_journals response is parsed.
57
+ def test_get_manual_journals_gateway_reference
58
+ result = @gateway.get_manual_journals
59
+ assert(result.success?)
60
+ assert_not_equal(0, result.manual_journals.size)
61
+
62
+ result.manual_journals.each do |manual_journal|
63
+ assert(manual_journal.gateway === @gateway)
64
+ end
65
+ end
66
+
67
+ # Test to make sure that we correctly error when a manual journal doesn't have an ID.
68
+ # This should usually never be ecountered.
69
+ def test_to_ensure_that_a_manual_journal_with_invalid_id_errors
70
+ # Make sure there is a manual journal to retrieve, even though we will mangle it later.
71
+ manual_journal = @gateway.create_manual_journal(create_test_manual_journal).manual_journal
72
+
73
+ result = @gateway.get_manual_journals
74
+ assert_equal(true, result.success?)
75
+
76
+ manual_journal = result.manual_journals.first
77
+ assert !manual_journal.journal_lines_downloaded?
78
+
79
+ # Mangle invoice_id to invalid one.
80
+ manual_journal.manual_journal_id = INVALID_MANUAL_JOURNAL_ID
81
+
82
+ # Make sure we fail here.
83
+ journal_lines = nil
84
+ assert_raise(XeroGateway::ManualJournalNotFoundError) { journal_lines = manual_journal.journal_lines }
85
+ assert_nil(journal_lines)
86
+ end
87
+
88
+ end
@@ -0,0 +1,31 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class UpdateManualJournalTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def setup
7
+ @gateway = XeroGateway::Gateway.new(CONSUMER_KEY, CONSUMER_SECRET)
8
+
9
+ if STUB_XERO_CALLS
10
+ @gateway.xero_url = "DUMMY_URL"
11
+
12
+ @gateway.stubs(:http_put).with {|client, url, body, params| url =~ /ManualJournals$/ }.returns(get_file_as_string("create_manual_journal.xml"))
13
+ @gateway.stubs(:http_post).with {|client, url, body, params| url =~ /ManualJournals$/ }.returns(get_file_as_string("manual_journal.xml"))
14
+ end
15
+ end
16
+
17
+ def test_update_manual_journal
18
+ manual_journal = @gateway.create_manual_journal(create_test_manual_journal).manual_journal
19
+
20
+ today = Date.today
21
+ manual_journal.date = today
22
+
23
+ result = @gateway.update_manual_journal(manual_journal)
24
+
25
+ assert result.success?
26
+ assert !result.request_xml.nil?
27
+ assert !result.response_xml.nil?
28
+ assert_equal manual_journal.manual_journal_id, result.manual_journal.manual_journal_id
29
+ assert_equal today, result.manual_journal.date if !STUB_XERO_CALLS
30
+ end
31
+ end
data/test/test_helper.rb CHANGED
@@ -6,7 +6,7 @@ require 'shoulda'
6
6
 
7
7
  require 'libxml'
8
8
 
9
- require File.dirname(__FILE__) + '/../lib/xero_gateway.rb'
9
+ require File.dirname(__FILE__) + '/../lib/xero_gateway.rb' unless defined?(XeroGateway)
10
10
 
11
11
  module TestHelper
12
12
  # The integration tests can be run against the Xero test environment. You mush have a company set up in the test
@@ -176,4 +176,42 @@ module TestHelper
176
176
  contact
177
177
  end
178
178
 
179
+ def create_test_manual_journal(params={}, journal_line_params={})
180
+ params = {
181
+ :date => Date.today,
182
+ :narration => 'test narration',
183
+ :status => 'POSTED'
184
+ }.merge(params)
185
+ manual_journal = XeroGateway::ManualJournal.new(params)
186
+ add_test_journal_lines(manual_journal, journal_line_params)
187
+ end
188
+
189
+ def add_test_journal_lines(manual_journal, journal_line_params)
190
+ if journal_line_params
191
+ journal_line_params = [journal_line_params].flatten # always use an array, even if only a single hash passed in
192
+
193
+ # At least one line item, make first have some defaults.
194
+ journal_line_params << {} if journal_line_params.size == 0
195
+ journal_line_params[0] = {
196
+ :description => "FIRST LINE",
197
+ :account_code => "200",
198
+ :line_amount => BigDecimal.new("100"),
199
+ :tracking => XeroGateway::TrackingCategory.new(:name => "blah", :options => "hello")
200
+ }.merge(journal_line_params[0])
201
+ params_line_1 = journal_line_params[1] || {}
202
+ journal_line_params[1] = {
203
+ :description => "SECOND LINE",
204
+ :account_code => "200",
205
+ :line_amount => BigDecimal.new("-100"),
206
+ :tracking => XeroGateway::TrackingCategory.new(:name => "blah2", :options => "hello2")
207
+ }.merge(params_line_1)
208
+
209
+ # Create line_items from line_item_params
210
+ journal_line_params.each do |journal_line|
211
+ manual_journal.add_journal_line(journal_line)
212
+ end
213
+ end
214
+ manual_journal
215
+ end
216
+
179
217
  end
@@ -65,7 +65,7 @@ class BankTransactionTest < Test::Unit::TestCase
65
65
 
66
66
  # Test that pushing anything else into add_line_item fails.
67
67
  ["invalid", 100, nil, []].each do | invalid_object |
68
- assert_raise(XeroGateway::Invoice::InvalidLineItemError) { @bank_transaction.add_line_item(invalid_object) }
68
+ assert_raise(XeroGateway::InvalidLineItemError) { @bank_transaction.add_line_item(invalid_object) }
69
69
  assert_equal(2, @bank_transaction.line_items.size)
70
70
  end
71
71
  end
@@ -215,7 +215,7 @@ class CreditNoteTest < Test::Unit::TestCase
215
215
 
216
216
  # Test that pushing anything else into add_line_item fails.
217
217
  ["invalid", 100, nil, []].each do | invalid_object |
218
- assert_raise(XeroGateway::CreditNote::InvalidLineItemError) { credit_note.add_line_item(invalid_object) }
218
+ assert_raise(XeroGateway::InvalidLineItemError) { credit_note.add_line_item(invalid_object) }
219
219
  assert_equal(2, credit_note.line_items.size)
220
220
  end
221
221
  end
@@ -6,28 +6,28 @@ class GatewayTest < Test::Unit::TestCase
6
6
  def setup
7
7
  @gateway = XeroGateway::Gateway.new(CONSUMER_KEY, CONSUMER_SECRET)
8
8
  end
9
-
9
+
10
10
  context "with error handling" do
11
-
11
+
12
12
  should "handle token expired" do
13
13
  XeroGateway::OAuth.any_instance.stubs(:get).returns(stub(:plain_body => get_file_as_string("token_expired"), :code => "401"))
14
-
14
+
15
15
  assert_raises XeroGateway::OAuth::TokenExpired do
16
16
  @gateway.get_accounts
17
17
  end
18
18
  end
19
-
19
+
20
20
  should "handle invalid request tokens" do
21
21
  XeroGateway::OAuth.any_instance.stubs(:get).returns(stub(:plain_body => get_file_as_string("invalid_request_token"), :code => "401"))
22
-
22
+
23
23
  assert_raises XeroGateway::OAuth::TokenInvalid do
24
24
  @gateway.get_accounts
25
25
  end
26
26
  end
27
-
27
+
28
28
  should "handle invalid consumer key" do
29
29
  XeroGateway::OAuth.any_instance.stubs(:get).returns(stub(:plain_body => get_file_as_string("invalid_consumer_key"), :code => "401"))
30
-
30
+
31
31
  assert_raises XeroGateway::OAuth::TokenInvalid do
32
32
  @gateway.get_accounts
33
33
  end
@@ -48,10 +48,10 @@ class GatewayTest < Test::Unit::TestCase
48
48
  @gateway.get_accounts
49
49
  end
50
50
  end
51
-
51
+
52
52
  should "handle ApiExceptions" do
53
53
  XeroGateway::OAuth.any_instance.stubs(:put).returns(stub(:plain_body => get_file_as_string("api_exception.xml"), :code => "400"))
54
-
54
+
55
55
  assert_raises XeroGateway::ApiException do
56
56
  @gateway.create_invoice(XeroGateway::Invoice.new)
57
57
  end
@@ -86,17 +86,17 @@ class GatewayTest < Test::Unit::TestCase
86
86
 
87
87
  assert_raises XeroGateway::UnparseableResponse do
88
88
  @gateway.create_invoice(XeroGateway::Invoice.new)
89
- end
89
+ end
90
90
  end
91
-
91
+
92
92
  end
93
-
93
+
94
94
  def test_unknown_error_handling
95
95
  if STUB_XERO_CALLS
96
96
  @gateway.xero_url = "DUMMY_URL"
97
- @gateway.stubs(:http_get).with {|client, url, params| url =~ /Invoices\/AN_INVALID_ID$/ }.returns(get_file_as_string("unknown_error.xml"))
97
+ @gateway.stubs(:http_get).with {|client, url, params| url =~ /Invoices\/AN_INVALID_ID$/ }.returns(get_file_as_string("unknown_error.xml"))
98
98
  end
99
-
99
+
100
100
  result = @gateway.get_invoice("AN_INVALID_ID")
101
101
  assert !result.success?
102
102
  assert_equal 1, result.errors.size
@@ -109,7 +109,7 @@ class GatewayTest < Test::Unit::TestCase
109
109
  @gateway.xero_url = "DUMMY_URL"
110
110
  @gateway.stubs(:http_get).with {|client, url, params| url =~ /Invoices\/UNKNOWN_INVOICE_NO$/ }.returns(get_file_as_string("invoice_not_found_error.xml"))
111
111
  end
112
-
112
+
113
113
  result = @gateway.get_invoice("UNKNOWN_INVOICE_NO")
114
114
  assert !result.success?
115
115
  assert_equal 1, result.errors.size
@@ -229,7 +229,7 @@ class InvoiceTest < Test::Unit::TestCase
229
229
 
230
230
  # Test that pushing anything else into add_line_item fails.
231
231
  ["invalid", 100, nil, []].each do | invalid_object |
232
- assert_raise(XeroGateway::Invoice::InvalidLineItemError) { invoice.add_line_item(invalid_object) }
232
+ assert_raise(XeroGateway::InvalidLineItemError) { invoice.add_line_item(invalid_object) }
233
233
  assert_equal(2, invoice.line_items.size)
234
234
  end
235
235
  end
@@ -240,8 +240,9 @@ class InvoiceTest < Test::Unit::TestCase
240
240
  end
241
241
 
242
242
  def test_optional_params
243
- invoice = create_test_invoice(:url => 'http://example.com')
243
+ invoice = create_test_invoice(:url => 'http://example.com', :branding_theme_id => 'a94a78db-5cc6-4e26-a52b-045237e56e6e')
244
244
  assert_equal 'http://example.com', invoice.url
245
+ assert_equal 'a94a78db-5cc6-4e26-a52b-045237e56e6e', invoice.branding_theme_id
245
246
  end
246
247
 
247
248
  private
@@ -0,0 +1,93 @@
1
+ require File.join(File.dirname(__FILE__), '../test_helper.rb')
2
+
3
+ class ManualJournalTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ context "creating test manual journals" do
7
+ should "work" do
8
+ manual_journal = create_test_manual_journal
9
+
10
+ # test transaction defaults
11
+ assert_equal 'POSTED', manual_journal.status
12
+ assert_kind_of Date, manual_journal.date
13
+ assert_equal 'test narration', manual_journal.narration
14
+
15
+ # Test the journal_line defaults.
16
+ journal_line = manual_journal.journal_lines.first
17
+ assert_equal('FIRST LINE', journal_line.description)
18
+ assert_equal('200', journal_line.account_code)
19
+ assert_equal(BigDecimal.new('100'), journal_line.line_amount)
20
+ end
21
+
22
+ should "allow overriding transaction defaults" do
23
+ assert_equal 'DRAFT', create_test_manual_journal(:status => 'DRAFT').status
24
+ end
25
+ end
26
+
27
+ context "adding journal lines" do
28
+ setup do
29
+ @manual_journal = create_test_manual_journal
30
+ end
31
+
32
+ should "work" do
33
+ assert_equal 2, @manual_journal.journal_lines.size
34
+ assert @manual_journal.valid?
35
+
36
+ journal_line_params = {:description => "Test Item 1", :line_amount => 100, :account_code => '200'}
37
+
38
+ # Test adding line item by hash
39
+ journal_line = @manual_journal.add_journal_line(journal_line_params)
40
+ assert_kind_of(XeroGateway::JournalLine, journal_line)
41
+ assert_equal(journal_line_params[:description], journal_line.description)
42
+ assert_equal(journal_line_params[:line_amount], journal_line.line_amount)
43
+ assert_equal(3, @manual_journal.journal_lines.size)
44
+
45
+ # Test adding line item by XeroGateway::JournalLine
46
+ journal_line = @manual_journal.add_journal_line(journal_line_params)
47
+ assert_kind_of(XeroGateway::JournalLine, journal_line)
48
+ assert_equal(journal_line_params[:description], journal_line.description)
49
+ assert_equal(journal_line_params[:line_amount], journal_line.line_amount)
50
+ assert_equal(4, @manual_journal.journal_lines.size)
51
+
52
+ # Test that having only 1 journal line fails.
53
+ @manual_journal.journal_lines = []
54
+ @manual_journal.add_journal_line(journal_line_params)
55
+ assert !@manual_journal.valid?
56
+ end
57
+ end
58
+
59
+
60
+ context "building and parsing XML" do
61
+ should "work vice versa" do
62
+ manual_journal = create_test_manual_journal
63
+ manual_journal_as_xml = manual_journal.to_xml
64
+ manual_journal_element = REXML::XPath.first(REXML::Document.new(manual_journal_as_xml), "/ManualJournal")
65
+
66
+ # checking for mandatory fields
67
+ assert_xml_field manual_journal_element, 'Date'
68
+ assert_xml_field manual_journal_element, 'Narration', :value => 'test narration'
69
+ assert_xml_field manual_journal_element, 'Status', :value => 'POSTED'
70
+
71
+ parsed_manual_journal = XeroGateway::ManualJournal.from_xml(manual_journal_element)
72
+ assert_equal(manual_journal, parsed_manual_journal)
73
+ end
74
+
75
+ should "work for optional params" do
76
+ manual_journal = create_test_manual_journal(:url => 'http://example.com?with=params&and=more')
77
+ manual_journal_element = REXML::XPath.first(REXML::Document.new(manual_journal.to_xml), "/ManualJournal")
78
+
79
+ assert_xml_field manual_journal_element, 'Url', :value => 'http://example.com\?with=params&amp;and=more'
80
+
81
+ parsed_manual_journal = XeroGateway::ManualJournal.from_xml(manual_journal_element)
82
+ assert_equal 'http://example.com?with=params&and=more', parsed_manual_journal.url
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def assert_xml_field(xml, field_name, options={})
89
+ assert_match /#{field_name}/, xml.to_s, "Didn't find the field #{field_name} in the XML document!"
90
+ assert_match /#{field_name}.*#{options[:value]}.*#{field_name}/, xml.to_s, "The field #{field_name} was expected to be '#{options[:value]}'!" if options[:value]
91
+ end
92
+
93
+ end
@@ -0,0 +1,34 @@
1
+ require File.join(File.dirname(__FILE__), '../test_helper.rb')
2
+
3
+ class PaymentTest < Test::Unit::TestCase
4
+
5
+ # Tests that a payment can be converted into XML that Xero can understand, and then converted back to a payment
6
+ def test_build_and_parse_xml
7
+ payment = create_test_payment
8
+
9
+ # Generate the XML message
10
+ payment_as_xml = payment.to_xml
11
+
12
+ # Parse the XML message and retrieve the account element
13
+ payment_element = REXML::XPath.first(REXML::Document.new(payment_as_xml), "/Payment")
14
+
15
+ # Build a new account from the XML
16
+ result_payment = XeroGateway::Payment.from_xml(payment_element)
17
+
18
+ # Check the details
19
+ assert_equal payment, result_payment
20
+ end
21
+
22
+
23
+ private
24
+
25
+ def create_test_payment
26
+ XeroGateway::Payment.new.tap do |payment|
27
+ payment.invoice_id = "a99a9aaa-9999-99a9-9aa9-aaaaaa9a9999"
28
+ payment.amount = 100.0
29
+ payment.date = Time.now.beginning_of_day
30
+ payment.reference = "Invoice Payment"
31
+ payment.code = 200
32
+ end
33
+ end
34
+ end