mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-20 18:40:37 -05:00
Add tests for forms, billing helpers and models
Unit tests for EstimateForm, PaymentForm, RefundForm, InvoiceRefundForm, InvoiceStatus, LaborItems/ManHours, IssueValues, TitleCase, the RedmineQboBilling permission helpers and field hooks, and the models: Customer, Estimate, Invoice, Payment (with their activity events), Item, Account, QboDraft, QboRefund, QboSyncStatus and QboBaseModel's push and sync behaviour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
48f92f241f
commit
00400c21dc
13 files changed
+1147
No files matched your search
@@ -0,0 +1,101 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
# PaymentForm, RefundForm and InvoiceRefundForm
|
||||
class BillingFormsTest < ActiveSupport::TestCase
|
||||
def payment(attrs = {})
|
||||
RedmineQboBilling::PaymentForm.from_params({ 'customer_id' => '9101', 'txn_date' => '2026-11-20',
|
||||
'payment_method_id' => '2', 'amount' => '$1,000' }.merge(attrs))
|
||||
end
|
||||
|
||||
def test_payment_form_from_params
|
||||
form = payment('invoice_ids' => ['7', '', '7', '8'], 'sync_token' => '3', 'deposit' => '1')
|
||||
assert_equal %w[7 8], form.invoice_ids
|
||||
assert_equal '3', form.sync_token
|
||||
assert_equal BigDecimal('1000'), form.amount_value
|
||||
assert_equal Date.new(2026, 11, 20), form.txn_date_value
|
||||
assert form.deposit?
|
||||
assert_not form.persisted?
|
||||
assert RedmineQboBilling::PaymentForm.from_params({}, id: '12').persisted?
|
||||
assert_equal [], RedmineQboBilling::PaymentForm.new.invoice_ids
|
||||
end
|
||||
|
||||
def test_payment_form_validation
|
||||
assert payment.valid?
|
||||
form = RedmineQboBilling::PaymentForm.from_params({ 'amount' => '0', 'txn_date' => 'x' })
|
||||
assert_not form.valid?
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_needs_customer)
|
||||
assert_includes form.errors[:base], I18n.t(:error_payment_needs_method)
|
||||
assert_includes form.errors[:base], I18n.t(:error_payment_amount)
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_field_invalid, field: I18n.t(:label_payment_date))
|
||||
assert_not payment('amount' => 'abc').valid?
|
||||
assert_not payment('amount' => '-5').valid?
|
||||
end
|
||||
|
||||
def refund(attrs = {})
|
||||
RedmineQboBilling::RefundForm.from_params({ 'customer_id' => '9101', 'txn_date' => '2026-11-20',
|
||||
'method' => 'card', 'reason' => 'Canceled order' }.merge(attrs))
|
||||
end
|
||||
|
||||
def test_refund_form
|
||||
form = refund('parts_amount' => '307.99', 'close_estimate' => '1', 'cancel_issues' => '0')
|
||||
assert form.valid?, form.errors.full_messages.inspect
|
||||
assert_equal BigDecimal('307.99'), form.parts_value
|
||||
assert form.close_estimate?
|
||||
assert_not form.cancel_issues?
|
||||
assert_not form.email_customer?
|
||||
assert_not form.check?
|
||||
assert_equal BigDecimal('0'), refund.parts_value
|
||||
end
|
||||
|
||||
def test_refund_form_validation
|
||||
form = RedmineQboBilling::RefundForm.from_params('method' => 'check', 'parts_amount' => '-1')
|
||||
assert_not form.valid?
|
||||
%i[error_estimate_needs_customer error_refund_check_number error_refund_reason error_refund_parts_amount].each do |key|
|
||||
assert_includes form.errors[:base], I18n.t(key)
|
||||
end
|
||||
assert_includes refund('method' => 'cash').tap(&:valid?).errors[:base], I18n.t(:error_refund_method)
|
||||
assert_includes refund('parts_amount' => 'lots').tap(&:valid?).errors[:base], I18n.t(:error_refund_parts_amount)
|
||||
assert refund('method' => 'check', 'check_number' => '1042').valid?
|
||||
end
|
||||
|
||||
def invoice_refund(attrs = {})
|
||||
RedmineQboBilling::InvoiceRefundForm.from_params(
|
||||
{ 'customer_id' => '9101', 'invoice_id' => '601', 'txn_date' => '2026-11-20', 'method' => 'card',
|
||||
'reason' => 'Returned part', 'lines' => { '3' => { 'refund' => '1', 'quantity' => '2', 'amount' => '$1,060.00' },
|
||||
'4' => { 'refund' => '0', 'quantity' => '', 'amount' => '' } } }.merge(attrs)
|
||||
)
|
||||
end
|
||||
|
||||
def test_invoice_refund_form_lines
|
||||
form = invoice_refund('charge_fee' => '1')
|
||||
assert form.valid?, form.errors.full_messages.inspect
|
||||
assert_equal ['3'], form.chosen.keys
|
||||
assert_equal BigDecimal('2'), form.line(3).quantity_value
|
||||
assert_equal BigDecimal('1060'), form.line('3').amount_value
|
||||
assert_nil form.line('4').quantity_value
|
||||
assert_nil form.line('4').amount_value
|
||||
assert_nil form.line('9')
|
||||
assert form.charge_fee?
|
||||
assert_not form.note_issues?
|
||||
assert_includes RedmineQboBilling::InvoiceRefundForm.permitted, { lines: {} }
|
||||
end
|
||||
|
||||
def test_invoice_refund_form_validation
|
||||
form = RedmineQboBilling::InvoiceRefundForm.from_params('method' => 'check')
|
||||
assert_not form.valid?
|
||||
%i[error_estimate_needs_customer error_refund_no_invoice error_refund_check_number error_refund_reason
|
||||
error_refund_no_lines].each { |key| assert_includes form.errors[:base], I18n.t(key) }
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_field_invalid, field: I18n.t(:label_refund_date))
|
||||
assert_equal({}, form.lines)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,86 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
class CustomerTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
Customer.where(id: [9201, 9202, 9203]).delete_all
|
||||
end
|
||||
|
||||
def customer(id, name, phone: nil, mobile: nil)
|
||||
record = Customer.new(id: id, name: name, phone_number: phone, mobile_phone_number: mobile)
|
||||
record.skip_qbo_push = true
|
||||
record.save!
|
||||
record
|
||||
end
|
||||
|
||||
def test_name_is_title_cased_locally_and_in_quickbooks
|
||||
record = Customer.new(id: 9201, name: ' jane DOE llc ')
|
||||
assert_equal 'Jane DOE Llc', record.name
|
||||
assert_equal 'Jane DOE Llc', record.details.display_name
|
||||
end
|
||||
|
||||
def test_phone_numbers_keep_digits_only
|
||||
record = customer(9201, 'Jane', phone: '(401) 555-0101', mobile: '+1 401.555.0102')
|
||||
assert_equal '4015550101', record.phone_number
|
||||
assert_equal '14015550102', record.mobile_phone_number
|
||||
end
|
||||
|
||||
def test_to_s_adds_the_last_four_digits
|
||||
assert_equal 'Jane - 0101', customer(9201, 'Jane', phone: '4015550101').to_s
|
||||
assert_equal 'Bob', customer(9202, 'Bob').to_s
|
||||
end
|
||||
|
||||
def test_needs_a_name_and_a_unique_id
|
||||
assert_not Customer.new(id: 9201, name: '').tap { |c| c.skip_qbo_push = true }.valid?
|
||||
customer(9201, 'Jane')
|
||||
duplicate = Customer.new(id: 9201, name: 'Other')
|
||||
duplicate.skip_qbo_push = true
|
||||
assert_not duplicate.valid?
|
||||
end
|
||||
|
||||
def test_search_by_name_or_phone
|
||||
customer(9201, 'Jane Customer', phone: '4015550101')
|
||||
customer(9202, 'Bob Builder', mobile: '4015550199')
|
||||
customer(9203, '100% Jeep')
|
||||
assert_equal [9201], Customer.search('jane').pluck(:id)
|
||||
assert_equal [9202], Customer.search('0199').pluck(:id)
|
||||
# LIKE wildcards are matched literally
|
||||
assert_equal [9203], Customer.search('100%').pluck(:id)
|
||||
end
|
||||
|
||||
def test_redmine_search_needs_every_token
|
||||
customer(9201, 'Jane Customer', phone: '4015550101')
|
||||
customer(9202, 'Jane Other')
|
||||
assert_equal({ 9201 => 9201 }, Customer.search_result_ranks_and_ids(%w[jane 0101], User.find(1)))
|
||||
assert_equal({}, Customer.search_result_ranks_and_ids([], User.find(1)))
|
||||
end
|
||||
|
||||
def test_quickbooks_details_setters
|
||||
record = Customer.new(id: 9201, name: 'Jane')
|
||||
record.primary_phone = '401-555-0101'
|
||||
record.mobile_phone = '401-555-0102'
|
||||
record.email = 'jane@example.com'
|
||||
record.notes = 'Prefers texts'
|
||||
assert_equal '401-555-0101', record.primary_phone
|
||||
assert_equal '401-555-0102', record.mobile_phone
|
||||
assert_equal 'jane@example.com', record.email
|
||||
assert_equal 'Prefers texts', record.details.notes
|
||||
assert_nil record.project
|
||||
end
|
||||
|
||||
def test_saving_pushes_to_quickbooks_unless_skipped
|
||||
CustomerService.any_instance.expects(:push).once.returns(true)
|
||||
Customer.create!(id: 9201, name: 'Jane')
|
||||
CustomerService.any_instance.expects(:push).never
|
||||
customer(9202, 'Bob')
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,216 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
class EstimateFormTest < ActiveSupport::TestCase
|
||||
Form = RedmineQboBilling::EstimateForm
|
||||
Line = Form::Line
|
||||
|
||||
def line(attrs = {})
|
||||
Line.from_params(attrs)
|
||||
end
|
||||
|
||||
def valid_form(attrs = {})
|
||||
Form.new({ customer_id: '9101', txn_date: '2026-11-20',
|
||||
lines: [line(item_id: '1', item_name: 'Labor', quantity: '2', rate: '120')] }.merge(attrs))
|
||||
end
|
||||
|
||||
# Line
|
||||
|
||||
def test_line_from_params_cleans_up_the_row
|
||||
row = line('id' => '', 'kind' => 'other', 'item_id' => '5', 'item_name' => ' Labor ', 'description' => ' Brakes ',
|
||||
'quantity' => ' 2 ', 'rate' => '$1,200.50', 'taxable' => '1', 'time_activity_id' => '')
|
||||
assert_nil row.id
|
||||
assert_nil row.kind
|
||||
assert_equal 'Labor', row.item_name
|
||||
assert_equal 'Brakes', row.description
|
||||
assert_equal BigDecimal('1200.50'), row.rate_value
|
||||
assert row.taxable?
|
||||
assert_nil row.time_activity_id
|
||||
assert_not line('taxable' => '0').taxable?
|
||||
end
|
||||
|
||||
def test_line_amount_defaults_quantity_to_one_and_rounds
|
||||
assert_equal BigDecimal('45.50'), line(rate: '45.50').amount
|
||||
assert_equal BigDecimal('3.33'), line(quantity: '0.333', rate: '10').amount
|
||||
assert_equal BigDecimal('0'), line(description: 'note').amount
|
||||
assert_equal BigDecimal('0'), line(kind: 'subtotal', rate: '10').amount
|
||||
end
|
||||
|
||||
def test_line_kinds
|
||||
assert line(kind: 'subtotal').subtotal?
|
||||
assert line(item_id: '1').item?
|
||||
assert_not line(kind: 'subtotal', item_id: '1').item?
|
||||
assert line(description: 'Notes').description_only?
|
||||
assert_not line(description: 'Notes', rate: '5').description_only?
|
||||
assert line.blank?
|
||||
assert_not line(kind: 'subtotal').blank?
|
||||
assert line(item_name: 'Typed only').unmatched_item?
|
||||
assert_not line(item_id: '1', item_name: 'Labor').unmatched_item?
|
||||
end
|
||||
|
||||
def test_line_number_invalid
|
||||
assert line(quantity: 'two').number_invalid?
|
||||
assert line(rate: 'abc').number_invalid?
|
||||
assert_not line(quantity: '2', rate: '1,000').number_invalid?
|
||||
assert_not line(kind: 'subtotal', rate: 'abc').number_invalid?
|
||||
end
|
||||
|
||||
# Parsing helpers
|
||||
|
||||
def test_decimal
|
||||
assert_equal BigDecimal('1234.5'), Form.decimal('$1,234.50')
|
||||
assert_nil Form.decimal('')
|
||||
assert_nil Form.decimal('abc')
|
||||
end
|
||||
|
||||
def test_date
|
||||
assert_equal Date.new(2026, 11, 20), Form.date('2026-11-20')
|
||||
assert_equal Date.new(2026, 1, 2), Form.date(Date.new(2026, 1, 2))
|
||||
assert_nil Form.date('')
|
||||
assert_nil Form.date('11/20/2026')
|
||||
end
|
||||
|
||||
def test_custom_field_hash_keeps_numeric_ids_only
|
||||
field = Struct.new(:id).new(3)
|
||||
assert_equal({ '2' => 'VIN1', '3' => 'x' }, Form.custom_field_hash(2 => ' VIN1 ', 'name' => 'no', field => 'x'))
|
||||
assert_equal({}, Form.custom_field_hash(nil))
|
||||
form = Form.new(custom_fields: { 2 => 'VIN1' })
|
||||
assert_equal 'VIN1', form.custom_field(2)
|
||||
assert_equal 'VIN1', form.custom_field(Struct.new(:id).new('2'))
|
||||
assert_equal '', form.custom_field(9)
|
||||
end
|
||||
|
||||
def test_from_params
|
||||
form = Form.from_params({ 'customer_id' => ' 9101 ', 'txn_date' => '2026-11-20', 'sync_token' => '',
|
||||
'total_tax' => '4.20', 'linked_estimate_ids' => ['5', '', '5', '6'],
|
||||
'custom_fields' => { '2' => 'VIN' },
|
||||
'lines' => { '0' => { 'item_id' => '1', 'rate' => '10' }, '1' => { 'description' => 'Note' } } },
|
||||
id: '42', document: 'invoice')
|
||||
assert_equal '42', form.id
|
||||
assert form.persisted?
|
||||
assert form.invoice?
|
||||
assert_nil form.sync_token
|
||||
assert_equal '9101', form.customer_id
|
||||
assert_equal '4.20', form.total_tax
|
||||
assert_equal %w[5 6], form.linked_estimate_ids
|
||||
assert_equal({ '2' => 'VIN' }, form.custom_fields)
|
||||
assert_equal 2, form.lines.size
|
||||
assert_equal 'Note', form.lines.last.description
|
||||
end
|
||||
|
||||
def test_defaults
|
||||
form = Form.new
|
||||
assert_equal 'estimate', form.document
|
||||
assert_not form.invoice?
|
||||
assert_not form.persisted?
|
||||
assert_equal [], form.lines
|
||||
assert_equal [], form.linked_estimate_ids
|
||||
end
|
||||
|
||||
# Totals and discounts
|
||||
|
||||
def test_subtotal_skips_blank_rows
|
||||
form = Form.new(lines: [line(quantity: '2', rate: '10', item_id: '1'), line, line(rate: '5.25', item_id: '2')])
|
||||
assert_equal 2, form.filled_lines.size
|
||||
assert_equal BigDecimal('25.25'), form.subtotal
|
||||
end
|
||||
|
||||
def test_percent_discount
|
||||
form = valid_form(discount_value: '10%')
|
||||
assert form.percent_discount?
|
||||
assert form.discount?
|
||||
assert_equal BigDecimal('24'), form.discount_amount
|
||||
end
|
||||
|
||||
def test_amount_discount_and_unknown_type
|
||||
assert_equal BigDecimal('15.56'), valid_form(discount_type: 'amount', discount_value: '15.555').discount_amount
|
||||
assert_equal 'percent', valid_form(discount_type: 'bogus').discount_type
|
||||
assert_not valid_form(discount_value: '0').discount?
|
||||
assert_equal BigDecimal('0'), valid_form.discount_amount
|
||||
end
|
||||
|
||||
def test_fully_paid_and_sent
|
||||
assert Form.new(balance: '0').fully_paid?
|
||||
assert Form.new(balance: '-1').fully_paid?
|
||||
assert_not Form.new(balance: '10').fully_paid?
|
||||
assert_not Form.new.fully_paid?
|
||||
assert Form.new(email_status: 'EmailSent').sent?
|
||||
assert_not Form.new(email_status: 'NeedToSend').sent?
|
||||
end
|
||||
|
||||
# Validation
|
||||
|
||||
def test_valid_form
|
||||
assert valid_form.valid?, valid_form.errors.full_messages.inspect
|
||||
end
|
||||
|
||||
def test_needs_customer_line_and_date
|
||||
form = Form.new(txn_date: '')
|
||||
assert_not form.valid?
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_needs_customer)
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_needs_a_line)
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_field_invalid, field: I18n.t(:label_estimate_date))
|
||||
end
|
||||
|
||||
def test_invoice_date_label
|
||||
form = valid_form(txn_date: 'nope', document: 'invoice')
|
||||
assert_not form.valid?
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_field_invalid, field: I18n.t(:label_invoice_date))
|
||||
end
|
||||
|
||||
def test_bad_expiration_and_due_dates
|
||||
form = valid_form(expiration_date: 'soon', due_date: '13/45/2026')
|
||||
assert_not form.valid?
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_field_invalid, field: I18n.t(:label_expiration_date))
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_field_invalid, field: I18n.t(:label_due_date))
|
||||
end
|
||||
|
||||
def test_line_errors_are_numbered_by_filled_row
|
||||
form = valid_form(lines: [line, line(item_name: 'Ghost'), line(item_id: '1', rate: 'x')])
|
||||
assert_not form.valid?
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_line_unmatched_item, line: 1, name: 'Ghost')
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_line_number, line: 2)
|
||||
end
|
||||
|
||||
# A row with an unreadable price and no product or description: kept, so it needs a description
|
||||
def test_description_line_needs_text
|
||||
form = valid_form
|
||||
form.lines << line(rate: 'abc')
|
||||
assert_not form.valid?
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_line_description, line: 2)
|
||||
end
|
||||
|
||||
def test_email_addresses
|
||||
assert valid_form(email: 'a@example.com, b@example.com').valid?
|
||||
form = valid_form(email: 'a@example.com, not-an-address')
|
||||
assert_not form.valid?
|
||||
assert_includes form.errors[:base], I18n.t(:error_estimate_field_invalid, field: I18n.t(:label_customer_email))
|
||||
end
|
||||
|
||||
def test_discount_validation
|
||||
invalid = I18n.t(:error_estimate_field_invalid, field: I18n.t(:label_discount_from_quickbooks))
|
||||
assert_includes valid_form(discount_value: '101').tap(&:valid?).errors[:base], invalid
|
||||
assert_includes valid_form(discount_value: '-5').tap(&:valid?).errors[:base], invalid
|
||||
assert_includes valid_form(discount_value: 'ten').tap(&:valid?).errors[:base], invalid
|
||||
assert_includes valid_form(discount_type: 'amount', discount_value: '241').tap(&:valid?).errors[:base],
|
||||
I18n.t(:error_discount_too_large)
|
||||
assert valid_form(discount_type: 'amount', discount_value: '240').valid?
|
||||
assert valid_form(discount_value: '100').valid?
|
||||
end
|
||||
|
||||
def test_plugin_errors_block_the_save
|
||||
RedmineQboBilling::IssueValues.stubs(:plugin_errors).returns(['One vehicle per invoice'])
|
||||
form = valid_form
|
||||
assert_not form.valid?
|
||||
assert_includes form.errors[:base], 'One vehicle per invoice'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,63 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
class InvoiceStatusTest < ActiveSupport::TestCase
|
||||
Status = RedmineQboBilling::InvoiceStatus
|
||||
TODAY = Date.new(2026, 11, 20)
|
||||
|
||||
def status(total, balance, due: nil)
|
||||
Status.status(total: total, balance: balance, due_date: due, today: TODAY)
|
||||
end
|
||||
|
||||
def test_statuses
|
||||
assert_nil status(100, nil)
|
||||
assert_nil status(100, ' ')
|
||||
assert_equal :voided, status(0, 0)
|
||||
assert_equal :paid, status(100, 0)
|
||||
assert_equal :paid, status(100, -5)
|
||||
assert_equal :overdue, status(100, 40, due: TODAY - 1)
|
||||
assert_equal :partial, status(100, 40, due: TODAY)
|
||||
assert_equal :open, status(100, 100)
|
||||
assert_equal :open, status('100.00', '100')
|
||||
end
|
||||
|
||||
def test_bad_numbers_count_as_zero
|
||||
assert_equal :voided, status('junk', '0')
|
||||
assert_equal BigDecimal('0'), Status.to_d('abc')
|
||||
assert_equal BigDecimal('0'), Status.to_d(nil)
|
||||
end
|
||||
|
||||
def test_labels
|
||||
paid_on = Date.new(2026, 11, 3)
|
||||
assert_equal '', Status.label(nil)
|
||||
assert_equal I18n.t(:label_invoice_status_open), Status.label(:open)
|
||||
assert_equal I18n.t(:label_invoice_status_paid_on, date: I18n.l(paid_on)), Status.label(:paid, paid_on: paid_on)
|
||||
assert_equal I18n.t(:label_invoice_status_paid), Status.label(:paid)
|
||||
end
|
||||
|
||||
def test_short_label_counts_days_to_pay
|
||||
assert_equal I18n.t(:label_invoice_status_paid_days, days: 3),
|
||||
Status.short_label(:paid, paid_on: Date.new(2026, 11, 4), invoice_date: Date.new(2026, 11, 1))
|
||||
# Paid the same day (or before the invoice): just "Paid"
|
||||
assert_equal I18n.t(:label_invoice_status_paid),
|
||||
Status.short_label(:paid, paid_on: Date.new(2026, 11, 1), invoice_date: Date.new(2026, 11, 1))
|
||||
assert_equal I18n.t(:label_invoice_status_open), Status.short_label(:open, paid_on: Date.new(2026, 11, 4), invoice_date: Date.new(2026, 11, 1))
|
||||
end
|
||||
|
||||
def test_title_and_days
|
||||
assert_nil Status.title(:open, paid_on: TODAY)
|
||||
assert_nil Status.title(:paid)
|
||||
assert_equal I18n.t(:text_invoice_paid_in_full, date: I18n.l(TODAY)), Status.title(:paid, paid_on: TODAY)
|
||||
assert_nil Status.days_to_pay(nil, TODAY)
|
||||
assert_equal(-2, Status.days_to_pay(TODAY, TODAY - 2))
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,66 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
class IssueValuesTest < ActiveSupport::TestCase
|
||||
fixtures :projects, :users, :issues, :trackers, :issue_statuses, :enumerations
|
||||
|
||||
Values = RedmineQboBilling::IssueValues
|
||||
Form = RedmineQboBilling::EstimateForm
|
||||
|
||||
def form(lines, attrs = {})
|
||||
Form.new({ lines: lines.map { |l| Form::Line.new(l) } }.merge(attrs))
|
||||
end
|
||||
|
||||
def test_issue_reference_reads_the_first_line
|
||||
assert_equal '116', Values.issue_reference("Install #116: lift kit\nmore #200: text")
|
||||
assert_nil Values.issue_reference('no reference')
|
||||
assert_nil Values.issue_reference(nil)
|
||||
end
|
||||
|
||||
def test_issue_ids_from_time_lines_added_from_notes_and_linked_estimates
|
||||
Issue.find(3).update_column(:estimate_id, 777)
|
||||
doc = form([{ description: 'Install #1: lift', time_activity_id: '55', rate: '1' },
|
||||
{ description: 'Added from: Repair #2: brakes' },
|
||||
{ description: 'Mentions #5: but not imported', rate: '1' },
|
||||
{ description: 'Subtotal #6: x', kind: 'subtotal', time_activity_id: '9' }],
|
||||
document: 'invoice', linked_estimate_ids: ['777'])
|
||||
assert_equal %w[1 2 3], Values.issue_ids(doc).sort
|
||||
assert_equal [1, 2, 3], Values.issues(doc).map(&:id).sort
|
||||
end
|
||||
|
||||
def test_saved_estimate_counts_its_own_issues
|
||||
Issue.find(4).update_column(:estimate_id, 888)
|
||||
assert_equal ['4'], Values.issue_ids(form([], id: '888'))
|
||||
assert_equal [], Values.issue_ids(form([]))
|
||||
assert_equal [], Values.issues(form([]))
|
||||
end
|
||||
|
||||
def test_values_merge_plugins_and_drop_blanks
|
||||
Redmine::Hook.stubs(:call_hook).with(:qbo_billing_issue_values, anything)
|
||||
.returns([{ 2 => { 1 => 'VIN1', 2 => ' ' } }, 'junk', { '2' => { 3 => 'VIN3' }, '4' => 'not a hash' }])
|
||||
issues = Issue.where(id: [1, 2, 3]).to_a
|
||||
assert_equal({ '2' => { '1' => 'VIN1', '3' => 'VIN3' } }, Values.values_for(issues))
|
||||
assert_equal({ '1' => { '2' => 'VIN1' }, '3' => { '2' => 'VIN3' } }, Values.by_issue(issues))
|
||||
assert_equal({}, Values.values_for([]))
|
||||
end
|
||||
|
||||
def test_plugin_errors
|
||||
Redmine::Hook.stubs(:call_hook).with(:qbo_billing_validate, has_entries(document: 'invoice'))
|
||||
.returns([['Two vehicles', nil], '', 'Other'])
|
||||
assert_equal ['Two vehicles', 'Other'], Values.plugin_errors(form([], document: 'invoice'))
|
||||
end
|
||||
|
||||
def test_a_failing_plugin_does_not_stop_the_save
|
||||
Redmine::Hook.stubs(:call_hook).raises(StandardError, 'boom')
|
||||
assert_equal [], Values.plugin_errors(form([]))
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,81 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
# LaborItems and ManHours
|
||||
class ManHoursTest < ActiveSupport::TestCase
|
||||
def ref(id, name)
|
||||
Quickbooks::Model::BaseReference.new(id.to_s, name: name)
|
||||
end
|
||||
|
||||
def sales_line(item_id, name, quantity)
|
||||
line = Quickbooks::Model::InvoiceLineItem.new
|
||||
line.sales_item! do |d|
|
||||
d.item_ref = ref(item_id, name)
|
||||
d.quantity = BigDecimal(quantity.to_s)
|
||||
end
|
||||
line
|
||||
end
|
||||
|
||||
def invoice(*lines)
|
||||
Quickbooks::Model::Invoice.new.tap { |i| i.line_items = lines }
|
||||
end
|
||||
|
||||
def test_ids_from_settings
|
||||
assert_equal %w[1 2], RedmineQboBilling::LaborItems.ids('labor_item_ids' => [1, '2', '', '1'])
|
||||
assert_equal [], RedmineQboBilling::LaborItems.ids({})
|
||||
end
|
||||
|
||||
def test_match_by_id_or_name
|
||||
labor = RedmineQboBilling::LaborItems
|
||||
assert labor.match?(ref(5, 'Anything'), ids: ['5'])
|
||||
assert_not labor.match?(ref(6, 'Labor'), ids: ['5'])
|
||||
assert labor.match?(ref(6, 'Labor'), ids: [])
|
||||
assert labor.match?(ref(6, 'Services:labor'), ids: [])
|
||||
assert_not labor.match?(ref(6, 'Laborious part'), ids: [])
|
||||
assert_not labor.match?(nil, ids: [])
|
||||
assert_not labor.name_matches?(' ', ['Labor'])
|
||||
assert labor.name_matches?('Shop Labor', ['shop labor'])
|
||||
end
|
||||
|
||||
def test_item_ids_and_names_fall_back_to_matching_names
|
||||
Setting.stubs(:plugin_redmine_qbo).returns({})
|
||||
Item.stubs(:sellable).returns(stub(pluck: [[11, 'Labor'], [12, 'Brake pads'], [13, 'Shop:Labor']]))
|
||||
assert_equal %w[11 13], RedmineQboBilling::LaborItems.item_ids
|
||||
assert_equal ['Labor'], RedmineQboBilling::LaborItems.names
|
||||
end
|
||||
|
||||
def test_chosen_item_ids_and_names
|
||||
Setting.stubs(:plugin_redmine_qbo).returns('labor_item_ids' => ['21'])
|
||||
assert_equal ['21'], RedmineQboBilling::LaborItems.item_ids
|
||||
Item.stubs(:where).with(id: ['21']).returns(stub(pluck: []))
|
||||
assert_equal ['21'], RedmineQboBilling::LaborItems.names
|
||||
end
|
||||
|
||||
def test_hours_by_name
|
||||
Setting.stubs(:plugin_redmine_qbo).returns({})
|
||||
doc = invoice(sales_line(1, 'Labor', 2.5), sales_line(2, 'Brake pads', 4), sales_line(1, 'Labor', '1.25'))
|
||||
assert_equal 3.75, RedmineQboBilling::ManHours.hours(doc)
|
||||
end
|
||||
|
||||
def test_hours_by_id_include_bundles
|
||||
bundle = Quickbooks::Model::InvoiceLineItem.new
|
||||
bundle.group_line_detail!
|
||||
bundle.group_line_detail.line_items = [sales_line(9, 'Install', 3), sales_line(2, 'Part', 1)]
|
||||
doc = invoice(bundle, sales_line(9, 'Install', 1))
|
||||
assert_equal 4.0, RedmineQboBilling::ManHours.hours(doc, item_ids: ['9'])
|
||||
end
|
||||
|
||||
def test_hours_of_nothing
|
||||
assert_equal 0.0, RedmineQboBilling::ManHours.hours(nil)
|
||||
assert_equal 0.0, RedmineQboBilling::ManHours.hours(invoice, item_ids: ['9'])
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,112 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
# Estimate, Invoice and Payment: the local QuickBooks caches, and their events on the activity page (QboActivity)
|
||||
class QboDocumentsTest < ActiveSupport::TestCase
|
||||
fixtures :users
|
||||
|
||||
CUSTOMER_ID = 9301
|
||||
|
||||
def setup
|
||||
Estimate.where(id: [9401, 9402]).delete_all
|
||||
Invoice.where(id: [9501, 9502, 9503]).delete_all
|
||||
Payment.where(id: [9601]).delete_all
|
||||
Customer.where(id: CUSTOMER_ID).delete_all
|
||||
customer = Customer.new(id: CUSTOMER_ID, name: 'Jane Customer')
|
||||
customer.skip_qbo_push = true
|
||||
customer.save!
|
||||
end
|
||||
|
||||
def estimate(id, attrs = {})
|
||||
Estimate.create!({ id: id, doc_number: "E#{id}", customer_id: CUSTOMER_ID, txn_date: Date.new(2026, 11, 10),
|
||||
total_amount: 1250, txn_status: 'Pending' }.merge(attrs))
|
||||
end
|
||||
|
||||
def invoice(id, attrs = {})
|
||||
Invoice.create!({ id: id, doc_number: id.to_s, customer_id: CUSTOMER_ID, txn_date: Date.new(2026, 11, 10),
|
||||
total_amount: 1250, balance: 1250, due_date: Date.new(2026, 12, 10) }.merge(attrs))
|
||||
end
|
||||
|
||||
def user(*permissions)
|
||||
user = User.find(2)
|
||||
user.stubs(:allowed_to?).returns(false)
|
||||
permissions.each { |p| user.stubs(:allowed_to?).with(p, nil, global: true).returns(true) }
|
||||
user
|
||||
end
|
||||
|
||||
def test_estimate
|
||||
record = estimate(9401)
|
||||
assert_equal 'E9401', record.to_s
|
||||
assert_equal 'E9401', record.name
|
||||
assert_not record.billed?
|
||||
RedmineQboBilling::EstimateWriter::BILLED_STATUSES.each do |status|
|
||||
assert estimate(9402, txn_status: status).billed?, status
|
||||
Estimate.where(id: 9402).delete_all
|
||||
end
|
||||
assert_not Estimate.new(id: 9402).valid?
|
||||
end
|
||||
|
||||
def test_invoice_status_and_activity_description
|
||||
today = Date.new(2026, 11, 20)
|
||||
assert_equal :open, invoice(9501).status(today: today)
|
||||
partial = invoice(9502, balance: 400)
|
||||
assert_equal :partial, partial.status(today: today)
|
||||
User.current.stubs(:today).returns(today)
|
||||
assert_equal "$1,250.00 · #{I18n.t(:label_invoice_status_partial)} · #{I18n.t(:text_activity_invoice_owed, amount: '$400.00')}",
|
||||
partial.activity_description
|
||||
assert_equal "$1,250.00 · #{I18n.t(:label_invoice_status_open)}", Invoice.find(9501).activity_description
|
||||
unknown = invoice(9503, balance: nil)
|
||||
assert_nil unknown.status
|
||||
assert_equal '$1,250.00', unknown.activity_description
|
||||
assert_equal '9503', unknown.to_s
|
||||
end
|
||||
|
||||
def test_payment_activity_description
|
||||
payment = Payment.create!(id: 9601, customer_id: CUSTOMER_ID, txn_date: Date.new(2026, 11, 2),
|
||||
total_amount: 500, unapplied_amount: 0)
|
||||
assert_equal '', payment.activity_description
|
||||
payment.unapplied_amount = 150
|
||||
assert_equal I18n.t(:text_activity_payment_unapplied, amount: '$150.00'), payment.activity_description
|
||||
assert_equal '2026-11-02 500.0', payment.to_s
|
||||
end
|
||||
|
||||
def test_activity_needs_a_permission_and_no_project
|
||||
estimate(9401)
|
||||
assert_equal [], Estimate.find_events('qbo_estimates', user, nil, nil, {})
|
||||
viewer = user(:view_customers)
|
||||
assert_equal [9401], Estimate.find_events('qbo_estimates', viewer, nil, nil, {}).map(&:id)
|
||||
assert_equal [], Estimate.find_events('qbo_estimates', viewer, nil, nil, project: Project.new)
|
||||
assert_equal [], Estimate.find_events('qbo_estimates', viewer, nil, nil, author: viewer)
|
||||
assert_equal({ 'qbo_estimates' => { permission: :view_customers } }, Estimate.activity_provider_options)
|
||||
assert Payment.qbo_activity_visible_to?(user(:receive_payments))
|
||||
end
|
||||
|
||||
def test_activity_by_quickbooks_date
|
||||
estimate(9401, txn_date: Date.new(2026, 11, 10))
|
||||
estimate(9402, txn_date: Date.new(2026, 11, 12))
|
||||
viewer = user(:write_estimates)
|
||||
found = Estimate.find_events('qbo_estimates', viewer, Date.new(2026, 11, 11), Date.new(2026, 11, 13), {})
|
||||
assert_equal [9402], found.map(&:id)
|
||||
assert_equal [9402], Estimate.find_events('qbo_estimates', viewer, nil, nil, limit: 1).map(&:id)
|
||||
end
|
||||
|
||||
def test_activity_datetime
|
||||
record = estimate(9401, txn_date: Date.new(2026, 11, 10))
|
||||
assert_equal Time.local(2026, 11, 10, 12), record.activity_datetime
|
||||
record.txn_date = record.created_at.getlocal.to_date
|
||||
assert_equal record.created_at, record.activity_datetime
|
||||
assert_equal 'Jane Customer', record.activity_customer
|
||||
assert_nil record.project
|
||||
assert_nil record.activity_money(nil)
|
||||
assert_equal I18n.t(:label_none), Estimate.new.activity_customer
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,67 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
class QboDraftTest < ActiveSupport::TestCase
|
||||
fixtures :users
|
||||
|
||||
def setup
|
||||
QboDraft.delete_all
|
||||
end
|
||||
|
||||
def draft(document, fields = {})
|
||||
QboDraft.create!(document: document, fields: fields)
|
||||
end
|
||||
|
||||
def test_document_must_be_known
|
||||
assert_not QboDraft.new(document: 'payment').valid?
|
||||
assert QboDraft.new(document: 'invoice').valid?
|
||||
end
|
||||
|
||||
def test_fields_round_trip_and_bad_data
|
||||
record = draft('estimate', 'customer_name' => 'Jane', 'lines' => { '0' => { 'rate' => '10', 'item_id' => '1' } })
|
||||
assert_equal 'Jane', record.reload.fields['customer_name']
|
||||
record.data = 'not json'
|
||||
assert_equal({}, record.fields)
|
||||
record.data = '[1, 2]'
|
||||
assert_equal({}, record.fields)
|
||||
record.data = nil
|
||||
assert_equal({}, record.fields)
|
||||
end
|
||||
|
||||
def test_form_subtotal_and_line_count
|
||||
record = draft('invoice', 'lines' => { '0' => { 'item_id' => '1', 'quantity' => '2', 'rate' => '10' },
|
||||
'1' => { 'kind' => 'subtotal' }, '2' => { 'description' => 'Note' },
|
||||
'3' => {} })
|
||||
assert record.invoice?
|
||||
assert record.form.invoice?
|
||||
assert_equal BigDecimal('20'), record.subtotal
|
||||
assert_equal 2, record.line_count
|
||||
end
|
||||
|
||||
def test_customer_name_falls_back_to_the_typed_name
|
||||
assert_equal 'Typed Name', draft('estimate', 'customer_name' => 'Typed Name').customer_name
|
||||
assert_nil draft('estimate').customer_name
|
||||
end
|
||||
|
||||
def test_visible_by_permission
|
||||
estimate = draft('estimate')
|
||||
invoice = draft('invoice')
|
||||
user = User.find(2)
|
||||
user.stubs(:allowed_to?).returns(false)
|
||||
assert_empty QboDraft.visible(user)
|
||||
user.stubs(:allowed_to?).with(:write_estimates, nil, global: true).returns(true)
|
||||
assert_equal [estimate], QboDraft.visible(user).to_a
|
||||
user.stubs(:allowed_to?).with(:write_invoices, nil, global: true).returns(true)
|
||||
assert_equal [estimate, invoice], QboDraft.visible(user).order(:id).to_a
|
||||
assert_equal [invoice], QboDraft.for_document(:invoice).to_a
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,122 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
# Item, Account and what every QuickBooks-backed model shares (QboBaseModel)
|
||||
class QboItemsAndAccountsTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
Item.where(id: [9701, 9702, 9703, 9704]).delete_all
|
||||
Account.where(id: [9801, 9802]).delete_all
|
||||
end
|
||||
|
||||
def item(id, attrs = {})
|
||||
record = Item.new({ id: id, name: "Item #{id}", description: '', unit_price: 10, active: true, type: 'Service' }.merge(attrs))
|
||||
record.skip_qbo_push = true
|
||||
record.save!
|
||||
record
|
||||
end
|
||||
|
||||
def account(id, attrs = {})
|
||||
Account.create!({ id: id, name: "Account #{id}" }.merge(attrs))
|
||||
end
|
||||
|
||||
def test_item_setters_write_through_to_quickbooks
|
||||
income = account(9801, name: 'Sales')
|
||||
record = Item.new(id: 9701, name: 'Labor', description: 'Shop labor', sku: 'LAB', unit_price: 120, type: 'Service',
|
||||
account_id: income.id)
|
||||
assert_equal 'Labor', record.details.name
|
||||
assert_equal 'Shop labor', record.details.description
|
||||
assert_equal 'LAB', record.details.sku
|
||||
assert_equal BigDecimal('120'), record.details.unit_price
|
||||
assert_equal 'Service', record.details.type
|
||||
assert_equal '9801', record.details.income_account_ref.value.to_s
|
||||
assert record.taxable
|
||||
end
|
||||
|
||||
def test_unknown_account_leaves_the_quickbooks_ref_alone
|
||||
record = Item.new(id: 9701, name: 'Labor', account_id: 12_345)
|
||||
assert_nil record.details.income_account_ref
|
||||
assert_equal 12_345, record.account_id
|
||||
end
|
||||
|
||||
def test_item_validation
|
||||
assert_not Item.new(id: 9701, name: '').tap { |i| i.skip_qbo_push = true }.valid?
|
||||
assert_not Item.new(id: 9701, name: 'Part', unit_price: -1).tap { |i| i.skip_qbo_push = true }.valid?
|
||||
end
|
||||
|
||||
def test_sellable
|
||||
item(9701)
|
||||
item(9702, type: 'Category')
|
||||
item(9703, active: false)
|
||||
item(9704, type: nil)
|
||||
assert_equal [9701, 9704], Item.sellable.where(id: [9701, 9702, 9703, 9704]).order(:id).pluck(:id)
|
||||
end
|
||||
|
||||
def test_destroying_an_item_makes_it_inactive_in_quickbooks
|
||||
record = item(9701)
|
||||
record.expects(:push_to_qbo).once
|
||||
record.destroy
|
||||
assert_equal false, record.details.active?
|
||||
end
|
||||
|
||||
def test_one_default_account
|
||||
first = account(9801, default: true)
|
||||
second = account(9802, default: true)
|
||||
assert_not first.reload.default
|
||||
assert_equal second, Account.get_default
|
||||
ref = second.ref
|
||||
assert_equal 9802, ref.value
|
||||
assert_equal 'Account 9802', ref.name
|
||||
assert_equal 'Account 9802', second.to_s
|
||||
end
|
||||
|
||||
def test_push_toggle
|
||||
assert Item.qbo_push_enabled
|
||||
assert_not Estimate.qbo_push_enabled
|
||||
record = Item.new(id: 9701, name: 'Part')
|
||||
assert record.push_to_qbo?
|
||||
record.skip_qbo_push = true
|
||||
assert_not record.push_to_qbo?
|
||||
assert_not Estimate.new.push_to_qbo?
|
||||
end
|
||||
|
||||
def test_method_missing_reaches_the_quickbooks_entity
|
||||
record = Item.new(id: 9701, name: 'Part')
|
||||
assert record.respond_to?(:purchase_desc)
|
||||
record.purchase_desc = 'Bought in bulk'
|
||||
assert_equal 'Bought in bulk', record.purchase_desc
|
||||
assert_raises(NoMethodError) { record.not_a_quickbooks_field }
|
||||
end
|
||||
|
||||
def test_sync_queues_jobs
|
||||
QboSyncStatus.delete_all
|
||||
QboSyncJob.expects(:perform_later).with(entity: 'Item', full_sync: true)
|
||||
Item.sync
|
||||
assert_equal 'queued', QboSyncStatus.find_by(entity: 'Item').state
|
||||
QboSyncJob.expects(:perform_later).with(entity: 'Item', id: 5)
|
||||
Item.sync_by_id(5)
|
||||
QboSyncJob.expects(:perform_later).with(entity: 'Estimate', doc_number: '1068')
|
||||
Estimate.sync_by_doc_number('1068')
|
||||
end
|
||||
|
||||
def test_last_sync
|
||||
Employee.delete_all
|
||||
assert_equal I18n.t(:label_qbo_never_synced), Employee.last_sync
|
||||
end
|
||||
|
||||
def test_estimate_line_issue_link
|
||||
QboEstimateLineIssue.delete_all
|
||||
link = QboEstimateLineIssue.create!(estimate_id: 1, line_id: '3', issue_id: 1)
|
||||
assert link.created_on
|
||||
assert_not QboEstimateLineIssue.new(estimate_id: 1, line_id: '3', issue_id: 2).valid?
|
||||
assert_not QboEstimateLineIssue.new(line_id: '4').valid?
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,87 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
class QboRefundTest < ActiveSupport::TestCase
|
||||
def refund(attrs = {})
|
||||
QboRefund.new({ customer_id: 9101, method: 'card', status: 'done', kind: 'credit' }.merge(attrs))
|
||||
end
|
||||
|
||||
def test_validations
|
||||
assert refund.valid?
|
||||
assert_not refund(method: 'cash').valid?
|
||||
assert_not refund(status: 'maybe').valid?
|
||||
assert_not refund(kind: 'other').valid?
|
||||
assert_not refund(customer_id: nil).valid?
|
||||
end
|
||||
|
||||
def test_flags_and_method_name
|
||||
record = refund(method: 'check', kind: 'invoice', status: 'incomplete')
|
||||
assert record.check?
|
||||
assert record.invoice?
|
||||
assert_not record.done?
|
||||
assert_equal I18n.t(:label_refund_method_check), record.method_name
|
||||
end
|
||||
|
||||
def test_issue_id_lists
|
||||
record = refund
|
||||
record.canceled_issue_ids = ['3', 3, 4]
|
||||
record.noted_issue_ids = [7]
|
||||
assert_equal [3, 4], record.canceled_issue_ids
|
||||
assert_equal [7], record.noted_issue_ids
|
||||
record[:canceled_issue_ids] = 'garbage'
|
||||
record[:noted_issue_ids] = nil
|
||||
assert_equal [], record.canceled_issue_ids
|
||||
assert_equal [], record.noted_issue_ids
|
||||
end
|
||||
|
||||
def test_refunded_lines_round_trip
|
||||
record = refund
|
||||
record.refunded_lines = [QboRefund::RefundedLine.new(key: 3, description: 'Brake pads', quantity: 2,
|
||||
amount: BigDecimal('60'), parts: true),
|
||||
QboRefund::RefundedLine.new(key: 'L1', description: 'Labor', quantity: nil,
|
||||
amount: 120, parts: nil)]
|
||||
lines = record.refunded_lines
|
||||
assert_equal %w[3 L1], lines.map(&:key)
|
||||
assert_equal '2', lines.first.quantity
|
||||
assert_equal BigDecimal('60'), lines.first.amount
|
||||
assert lines.first.parts
|
||||
assert_not lines.last.parts
|
||||
assert_nil lines.last.quantity
|
||||
record[:refunded_lines] = '{bad'
|
||||
assert_equal [], record.refunded_lines
|
||||
end
|
||||
|
||||
def test_document_label
|
||||
assert_equal 'E1068', refund(estimate_number: '1068').document_label
|
||||
assert_nil refund.document_label
|
||||
assert_equal I18n.t(:label_refund_invoice_number, number: '1234'),
|
||||
refund(kind: 'invoice', invoice_number: '1234').document_label
|
||||
end
|
||||
|
||||
def test_purchase_url
|
||||
RedmineQbo.stubs(:sandbox_mode?).returns(false)
|
||||
assert_nil refund.purchase_url
|
||||
assert_equal 'https://app.qbo.intuit.com/app/expense?txnId=5', refund(purchase_id: '5').purchase_url
|
||||
assert_equal 'https://app.qbo.intuit.com/app/check?txnId=5', refund(purchase_id: '5', method: 'check').purchase_url
|
||||
assert_equal 'https://app.qbo.intuit.com/app/refundreceipt?txnId=5', refund(purchase_id: '5', kind: 'invoice').purchase_url
|
||||
RedmineQbo.stubs(:sandbox_mode?).returns(true)
|
||||
assert_equal 'https://sandbox.qbo.intuit.com/app/expense?txnId=5', refund(purchase_id: '5').purchase_url
|
||||
end
|
||||
|
||||
def test_of_invoice_needs_a_receipt
|
||||
QboRefund.delete_all
|
||||
with_receipt = refund(kind: 'invoice', invoice_id: 601, purchase_id: '900').tap { |r| r.save!(validate: false) }
|
||||
refund(kind: 'invoice', invoice_id: 601, purchase_id: '').save!(validate: false)
|
||||
refund(kind: 'invoice', invoice_id: 602, purchase_id: '901').save!(validate: false)
|
||||
assert_equal [with_receipt.id], QboRefund.of_invoice(601).pluck(:id)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,54 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
class QboSyncStatusTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
QboSyncStatus.delete_all
|
||||
end
|
||||
|
||||
def test_queued_running_progress_finished
|
||||
QboSyncStatus.queued('Invoice')
|
||||
status = QboSyncStatus.find_by(entity: 'Invoice')
|
||||
assert_equal 'queued', status.state
|
||||
assert_not status.running?
|
||||
|
||||
QboSyncStatus.running('Invoice')
|
||||
QboSyncStatus.progress('Invoice', 25)
|
||||
QboSyncStatus.progress(:Invoice, '5')
|
||||
status.reload
|
||||
assert status.running?
|
||||
assert_equal 30, status.processed
|
||||
assert status.started_at
|
||||
|
||||
# Asking again while it runs doesn't put it back in the queue
|
||||
QboSyncStatus.queued('Invoice')
|
||||
assert status.reload.running?
|
||||
|
||||
QboSyncStatus.finished('Invoice')
|
||||
assert_not QboSyncStatus.exists?(entity: 'Invoice')
|
||||
end
|
||||
|
||||
def test_by_entity_ignores_stale_rows
|
||||
QboSyncStatus.queued('Invoice')
|
||||
QboSyncStatus.queued('Estimate')
|
||||
QboSyncStatus.create!(entity: 'Payment', state: 'running', updated_at: 7.hours.ago)
|
||||
assert_equal %w[Estimate Invoice], QboSyncStatus.by_entity.keys.sort
|
||||
assert_equal ['Invoice'], QboSyncStatus.by_entity([:Invoice]).keys
|
||||
end
|
||||
|
||||
def test_errors_are_logged_not_raised
|
||||
QboSyncStatus.stubs(:where).raises(StandardError, 'db gone')
|
||||
assert_nothing_raised { QboSyncStatus.progress('Invoice', 1) }
|
||||
assert_nothing_raised { QboSyncStatus.finished('Invoice') }
|
||||
assert_equal({}, QboSyncStatus.by_entity)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,71 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
# RedmineQboBilling's permission helpers and plugin hooks for the editor's custom fields
|
||||
class RedmineQboBillingTest < ActiveSupport::TestCase
|
||||
Billing = RedmineQboBilling
|
||||
|
||||
def user(*permissions)
|
||||
user = User.new
|
||||
user.stubs(:allowed_to?).returns(false)
|
||||
permissions.each { |p| user.stubs(:allowed_to?).with(p, nil, global: true).returns(true) }
|
||||
user
|
||||
end
|
||||
|
||||
def test_simple_permissions
|
||||
{ writer?: :write_estimates, sender?: :send_estimates, payment_receiver?: :receive_payments,
|
||||
refunder?: :issue_refunds, invoice_writer?: :write_invoices, invoice_sender?: :send_invoices }.each do |helper, permission|
|
||||
assert Billing.public_send(helper, user(permission)), helper
|
||||
assert_not Billing.public_send(helper, user), helper
|
||||
end
|
||||
end
|
||||
|
||||
def test_payment_viewer
|
||||
assert Billing.payment_viewer?(user(:view_payments))
|
||||
assert Billing.payment_viewer?(user(:receive_payments))
|
||||
assert_not Billing.payment_viewer?(user)
|
||||
end
|
||||
|
||||
def test_paid_invoice_editor
|
||||
assert Billing.paid_invoice_editor?(true, user(:write_invoices, :edit_paid_invoices))
|
||||
assert_not Billing.paid_invoice_editor?(false, user(:write_invoices, :edit_paid_invoices))
|
||||
assert Billing.paid_invoice_editor?(false, user(:write_invoices, :edit_partially_paid_invoices))
|
||||
assert_not Billing.paid_invoice_editor?(true, user(:write_invoices, :edit_partially_paid_invoices))
|
||||
assert_not Billing.paid_invoice_editor?(true, user(:edit_paid_invoices))
|
||||
end
|
||||
|
||||
def test_field_options_merge_and_dedupe
|
||||
field = Struct.new(:id).new(2)
|
||||
Redmine::Hook.stubs(:call_hook).with(:qbo_billing_field_options, anything).returns([
|
||||
{ field => [{ value: 'vin1', label: 'Jeep' }, { 'value' => ' ' }] },
|
||||
{ '2' => [{ value: 'VIN1' }, { value: 'VIN2' }] },
|
||||
'not a hash'
|
||||
])
|
||||
assert_equal({ '2' => [{ value: 'vin1', label: 'Jeep' }, { value: 'VIN2', label: 'VIN2' }] },
|
||||
Billing.field_options(Customer.new))
|
||||
assert_equal({}, Billing.field_options(nil))
|
||||
end
|
||||
|
||||
def test_field_options_survive_a_failing_plugin
|
||||
Redmine::Hook.stubs(:call_hook).raises(StandardError)
|
||||
assert_equal({}, Billing.field_options(Customer.new))
|
||||
assert_equal({}, Billing.field_defaults(Customer.new))
|
||||
end
|
||||
|
||||
def test_field_defaults_first_value_wins
|
||||
Redmine::Hook.stubs(:call_hook).with(:qbo_billing_field_defaults, has_entries(issues: [])).returns([
|
||||
{ 2 => '', 3 => 'first' }, { '2' => 'VIN', '3' => 'second' }, nil
|
||||
])
|
||||
assert_equal({ '3' => 'first', '2' => 'VIN' }, Billing.field_defaults(Customer.new))
|
||||
assert_equal({}, Billing.field_defaults(nil))
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
class TitleCaseTest < ActiveSupport::TestCase
|
||||
def test_capitalizes_first_letters_only
|
||||
assert_equal 'Led Light Bar For JL', RedmineQbo::TitleCase.call('led light bar for JL')
|
||||
assert_equal 'LED 4x4 (Front)', RedmineQbo::TitleCase.call("LED 4x4\t(front)")
|
||||
assert_equal 'Éclair', RedmineQbo::TitleCase.call('éclair')
|
||||
assert_equal '', RedmineQbo::TitleCase.call(' ')
|
||||
assert_nil RedmineQbo::TitleCase.call(nil)
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user