mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-20 18:40:37 -05:00
Add controller and job tests
Integration tests for every controller: permissions, the estimate and invoice editors (drafts, save and send, conflicts, the linked-estimate and paid-invoice unlocks), payments, drafts, customers and share links, estimate/invoice PDFs for share-link customers, items, accounts, the business report, and QboController (OAuth state check, webhook signature, manual billing). Unit tests for the webhook job, the sync dispatcher and QboSyncJob. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9ed3ef23fb
commit
da82f7e12c
11 files changed
+1310
No files matched your search
@@ -0,0 +1,180 @@
|
||||
#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 CustomersControllerTest < Redmine::IntegrationTest
|
||||
include QboFakeServiceHelper
|
||||
fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :issues, :trackers,
|
||||
:projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :workflows, :journals,
|
||||
:journal_details
|
||||
|
||||
def setup
|
||||
# The search keeps its rows in memory, keyed by customer count and newest change (to the second); tests
|
||||
# roll back, so two tests can make the same key with different customers
|
||||
CustomerSearchService.instance_variable_set(:@candidates, nil)
|
||||
Customer.where(id: [9101, 9102]).delete_all
|
||||
@customer = local_customer(9101, 'Jane Customer')
|
||||
@customer.update_columns(phone_number: '4015550101')
|
||||
local_customer(9102, 'Bob Builder')
|
||||
Issue.find(1).update_column(:customer_id, 9101)
|
||||
Issue.find(8).update_column(:customer_id, 9101)
|
||||
CustomerToken.delete_all
|
||||
@fake = install_fake_quickbooks
|
||||
end
|
||||
|
||||
# Gives jsmith's Manager role the permissions and logs jsmith in (again, if someone was logged in)
|
||||
def grant(*permissions)
|
||||
permissions.each { |p| Role.find(1).add_permission!(p) }
|
||||
reset!
|
||||
log_user('jsmith', 'jsmith')
|
||||
end
|
||||
|
||||
def test_pages_need_view_customers
|
||||
log_user('jsmith', 'jsmith')
|
||||
Role.find(1).remove_permission!(:view_customers)
|
||||
get '/customers'
|
||||
assert_response :forbidden
|
||||
get '/customers/9101'
|
||||
assert_response :forbidden
|
||||
get '/customers/autocomplete', params: { q: 'jane' }
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_index_lists_and_sorts
|
||||
grant(:view_customers)
|
||||
get '/customers', params: { sort: 'name' }
|
||||
assert_response :success
|
||||
assert_select 'a[href=?]', '/customers/9101'
|
||||
assert_select 'a[href=?]', '/customers/9102'
|
||||
end
|
||||
|
||||
def test_a_single_search_match_opens_the_customer
|
||||
grant(:view_customers)
|
||||
get '/customers', params: { search: 'Jane Customer' }
|
||||
assert_redirected_to '/customers/9101'
|
||||
end
|
||||
|
||||
def test_show_lists_open_and_closed_issues
|
||||
grant(:view_customers)
|
||||
get '/customers/9101'
|
||||
assert_response :success
|
||||
assert_select 'a[href=?]', '/issues/1'
|
||||
assert_select 'a[href=?]', '/issues/8'
|
||||
get '/customers/424242'
|
||||
assert_response :not_found
|
||||
end
|
||||
|
||||
def test_autocomplete
|
||||
grant(:view_customers)
|
||||
get '/customers/autocomplete', params: { q: 'jane custmer' }
|
||||
assert_response :success
|
||||
assert_equal [9101], JSON.parse(response.body).map { |c| c['id'] }
|
||||
get '/customers/autocomplete', params: { phone: '0101' }
|
||||
assert_equal ['Jane Customer'], JSON.parse(response.body).map { |c| c['name'] }
|
||||
end
|
||||
|
||||
def test_create_needs_add_customers
|
||||
grant(:view_customers)
|
||||
Role.find(1).remove_permission!(:add_customers)
|
||||
get '/customers/new'
|
||||
assert_response :forbidden
|
||||
post '/customers', params: { customer: { name: 'New Person' } }
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_create_pushes_to_quickbooks
|
||||
grant(:view_customers, :add_customers)
|
||||
post '/customers', params: { customer: { name: 'new person', phone_number: '(401) 555-0123', email: 'new@example.com' } }
|
||||
customer = Customer.find_by(name: 'New Person')
|
||||
assert customer, 'customer created'
|
||||
assert_redirected_to "/customers/#{customer.id}"
|
||||
assert_equal '4015550123', customer.phone_number
|
||||
assert_equal 'new@example.com', @fake.created.last.email_address.address
|
||||
end
|
||||
|
||||
# The controller doesn't accept API keys (no accept_api_auth) and Redmine ignores the session on JSON
|
||||
# requests, so customers can't be created as JSON: the format.json branches of #create aren't reachable
|
||||
def test_json_create_is_refused
|
||||
Role.find(1).add_permission!(:view_customers)
|
||||
Role.find(1).add_permission!(:add_customers)
|
||||
with_settings rest_api_enabled: '1' do
|
||||
post '/customers.json', params: { customer: { name: 'Json Person' } },
|
||||
headers: { 'X-Redmine-API-Key' => User.find(2).api_key }
|
||||
end
|
||||
assert_includes [401, 403], response.status
|
||||
assert_not Customer.exists?(name: 'Json Person')
|
||||
end
|
||||
|
||||
def test_update_needs_edit_customers
|
||||
grant(:view_customers)
|
||||
Role.find(1).remove_permission!(:edit_customers)
|
||||
patch '/customers/9101', params: { customer: { name: 'Changed' } }
|
||||
assert_response :forbidden
|
||||
|
||||
grant(:edit_customers)
|
||||
get '/customers/9101/edit'
|
||||
assert_response :success
|
||||
patch '/customers/9101', params: { customer: { name: 'jane changed' } }
|
||||
assert_redirected_to '/customers/9101'
|
||||
assert_equal 'Jane Changed', @customer.reload.name
|
||||
assert_equal 'Jane Changed', @fake.updated.last.display_name
|
||||
end
|
||||
|
||||
def test_sync_is_admin_only
|
||||
grant(:view_customers)
|
||||
get '/customers/sync'
|
||||
assert_response :forbidden
|
||||
reset!
|
||||
log_user('admin', 'admin')
|
||||
Customer.expects(:sync)
|
||||
get '/customers/sync'
|
||||
assert_response :redirect
|
||||
end
|
||||
|
||||
# ---- share links ----
|
||||
|
||||
def test_share_needs_the_permission_and_a_visible_issue
|
||||
grant(:view_customers)
|
||||
Role.find(1).remove_permission!(:share_issues_with_customers)
|
||||
get '/customers/share/1'
|
||||
assert_response :forbidden
|
||||
|
||||
grant(:share_issues_with_customers)
|
||||
get '/customers/share/1'
|
||||
token = CustomerToken.find_by(issue_id: 1)
|
||||
assert_redirected_to "/customers/view/#{token.token}"
|
||||
Project.find(2).update_column(:is_public, false)
|
||||
Member.where(user_id: 2, project_id: 2).destroy_all
|
||||
hidden = Issue.find(4)
|
||||
assert_not hidden.visible?(User.find(2))
|
||||
get "/customers/share/#{hidden.id}"
|
||||
assert_response :not_found
|
||||
assert_not CustomerToken.exists?(issue_id: hidden.id)
|
||||
end
|
||||
|
||||
def test_share_link_shows_the_issue_to_anyone
|
||||
token = CustomerToken.create!(issue_id: 1, expires_at: 1.week.from_now)
|
||||
with_settings login_required: '1' do
|
||||
get "/customers/view/#{token.token}"
|
||||
end
|
||||
assert_response :success
|
||||
assert_select 'h3, h2, div.subject', text: /#{Regexp.escape(Issue.find(1).subject)}/
|
||||
end
|
||||
|
||||
def test_expired_or_unknown_share_links_are_refused
|
||||
expired = CustomerToken.create!(issue_id: 1, expires_at: 1.week.from_now)
|
||||
expired.update_column(:expires_at, 1.day.ago)
|
||||
get "/customers/view/#{expired.token}"
|
||||
assert_response :forbidden
|
||||
get '/customers/view/not-a-token'
|
||||
assert_response :forbidden
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,118 @@
|
||||
#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 and invoice PDFs: logged-in users, or a share-link customer for their own issue's documents only
|
||||
class DocumentPdfsTest < Redmine::IntegrationTest
|
||||
include QboFakeServiceHelper
|
||||
fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :issues, :trackers,
|
||||
:projects_trackers, :issue_statuses, :enabled_modules, :enumerations
|
||||
|
||||
def setup
|
||||
local_customer
|
||||
Estimate.where(id: [598, 599]).delete_all
|
||||
Invoice.where(id: [601, 602]).delete_all
|
||||
Estimate.create!(id: 598, doc_number: '1068', customer_id: 9101)
|
||||
Estimate.create!(id: 599, doc_number: '1069', customer_id: 9101)
|
||||
@invoice = Invoice.create!(id: 601, doc_number: '1234', customer_id: 9101, txn_date: Date.new(2026, 11, 12))
|
||||
Invoice.create!(id: 602, doc_number: '1235', customer_id: 9101, txn_date: Date.new(2026, 11, 12))
|
||||
issue = Issue.find(1)
|
||||
issue.update_column(:estimate_id, 598)
|
||||
issue.invoices = [@invoice]
|
||||
CustomerToken.delete_all
|
||||
@token = CustomerToken.create!(issue_id: 1, expires_at: 1.week.from_now)
|
||||
@fake = install_fake_quickbooks
|
||||
[qbo_estimate(id: 598, doc_number: '1068'), qbo_estimate(id: 599, doc_number: '1069'),
|
||||
qbo_invoice(id: 601, doc_number: '1234'), qbo_invoice(id: 602, doc_number: '1235')].each { |d| @fake.add(d) }
|
||||
@fake.pdf_body = '%PDF-1.4 test'
|
||||
end
|
||||
|
||||
def open_share_link
|
||||
get "/customers/view/#{@token.token}"
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_logged_in_user_gets_the_estimate_pdf
|
||||
log_user('jsmith', 'jsmith')
|
||||
get '/estimates/598'
|
||||
assert_response :success
|
||||
assert_equal 'application/pdf', response.media_type
|
||||
assert_match 'estimate 1068.pdf', response.headers['Content-Disposition']
|
||||
get '/estimates/doc', params: { search: '1069' }
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_estimate_missing_locally_is_synced_first
|
||||
log_user('jsmith', 'jsmith')
|
||||
Estimate.expects(:sync_by_id).with('777')
|
||||
get '/estimates/777'
|
||||
assert_response :redirect
|
||||
assert_equal I18n.t(:notice_estimate_not_found), flash[:error]
|
||||
end
|
||||
|
||||
def test_anonymous_visitors_must_log_in
|
||||
get '/estimates/598'
|
||||
assert_redirected_to %r{/login}
|
||||
get '/invoices/601'
|
||||
assert_redirected_to %r{/login}
|
||||
end
|
||||
|
||||
def test_share_link_customer_sees_only_their_estimate
|
||||
open_share_link
|
||||
get '/estimates/598'
|
||||
assert_response :success
|
||||
get '/estimates/doc', params: { search: '1068' }
|
||||
assert_response :success
|
||||
get '/estimates/599'
|
||||
assert_response :forbidden
|
||||
get '/estimates/doc', params: { search: '1069' }
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_share_link_customer_sees_only_their_invoices
|
||||
open_share_link
|
||||
get '/invoices/601'
|
||||
assert_response :success
|
||||
assert_match 'invoice 1234.pdf', response.headers['Content-Disposition']
|
||||
get '/invoices/602'
|
||||
assert_response :forbidden
|
||||
get '/invoices/601', params: { invoice_ids: %w[601 602] }
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_expired_share_link_stops_working
|
||||
open_share_link
|
||||
@token.update_column(:expires_at, 1.minute.ago)
|
||||
get '/invoices/601'
|
||||
assert_redirected_to %r{/login}
|
||||
end
|
||||
|
||||
def test_invoice_pdf_failure_redirects
|
||||
log_user('jsmith', 'jsmith')
|
||||
get '/invoices/999'
|
||||
assert_response :redirect
|
||||
assert_equal I18n.t(:notice_invoice_not_found), flash[:error]
|
||||
end
|
||||
|
||||
def test_syncs_are_admin_only
|
||||
log_user('jsmith', 'jsmith')
|
||||
get '/estimates/sync'
|
||||
assert_response :forbidden
|
||||
get '/invoices/sync'
|
||||
assert_response :forbidden
|
||||
reset!
|
||||
log_user('admin', 'admin')
|
||||
Estimate.expects(:sync)
|
||||
Invoice.expects(:sync)
|
||||
get '/estimates/sync'
|
||||
get '/invoices/sync'
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,154 @@
|
||||
#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__)
|
||||
|
||||
# Items, accounts and employee syncs (admin), and the business report
|
||||
class QboAdminPagesTest < Redmine::IntegrationTest
|
||||
include QboFakeServiceHelper
|
||||
fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :issues, :trackers,
|
||||
:projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :workflows
|
||||
|
||||
def setup
|
||||
Item.where(id: [9701, 9702]).delete_all
|
||||
Account.where(id: [9801, 9802]).delete_all
|
||||
@fake = install_fake_quickbooks
|
||||
end
|
||||
|
||||
def item(id, name, attrs = {})
|
||||
Item.new({ id: id, name: name, description: '', unit_price: 10, active: true, type: 'Service' }.merge(attrs))
|
||||
.tap { |i| i.skip_qbo_push = true }.tap(&:save!)
|
||||
end
|
||||
|
||||
def admin
|
||||
reset!
|
||||
log_user('admin', 'admin')
|
||||
end
|
||||
|
||||
def jsmith
|
||||
reset!
|
||||
log_user('jsmith', 'jsmith')
|
||||
end
|
||||
|
||||
def test_items_are_admin_only
|
||||
item(9701, 'Labor')
|
||||
jsmith
|
||||
%w[/items /items/new /items/9701 /items/9701/edit /items/sync /accounts /accounts/sync /employees/sync].each do |path|
|
||||
get path
|
||||
assert_response :forbidden, path
|
||||
end
|
||||
post '/items', params: { item: { name: 'Sneaky' } }
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_item_pages
|
||||
item(9701, 'Labor')
|
||||
admin
|
||||
get '/items'
|
||||
assert_response :success
|
||||
assert_select 'body', text: /Labor/
|
||||
get '/items/9701'
|
||||
assert_response :success
|
||||
get '/items/9701/edit'
|
||||
assert_response :success
|
||||
get '/items/424242'
|
||||
assert_response :not_found
|
||||
end
|
||||
|
||||
def test_create_item_in_quickbooks
|
||||
admin
|
||||
post '/items', params: { item: { name: 'Brake Pads', description: 'Front pads', unit_price: '45', type: 'NonInventory', taxable: '1' } }
|
||||
created = @fake.created.last
|
||||
assert_equal 'Brake Pads', created.name
|
||||
assert_redirected_to "/items/#{created.id}"
|
||||
assert Item.exists?(created.id.to_i)
|
||||
end
|
||||
|
||||
def test_duplicate_name_opens_the_existing_item
|
||||
admin
|
||||
@fake.define_singleton_method(:create) do |_doc|
|
||||
raise Quickbooks::IntuitRequestException, "Duplicate Name Exists Error:\nThe name supplied already exists. : Id=9702"
|
||||
end
|
||||
post '/items', params: { item: { name: 'Labor', description: '', unit_price: '1' } }
|
||||
assert_redirected_to '/items/9702'
|
||||
end
|
||||
|
||||
def test_update_and_destroy_item
|
||||
item(9701, 'Labor')
|
||||
admin
|
||||
patch '/items/9701', params: { item: { name: 'Shop Labor', unit_price: '125' } }
|
||||
assert_redirected_to '/items/9701'
|
||||
assert_equal 'Shop Labor', Item.find(9701).name
|
||||
assert_equal 'Shop Labor', @fake.updated.last.name
|
||||
|
||||
patch '/items/9701', params: { item: { unit_price: '-1' } }
|
||||
assert_response :unprocessable_entity
|
||||
|
||||
delete '/items/9701'
|
||||
assert_redirected_to '/items'
|
||||
assert_not Item.exists?(9701)
|
||||
assert_equal false, @fake.updated.last.active?
|
||||
end
|
||||
|
||||
def test_item_autocomplete_lists_sellable_items
|
||||
item(9701, 'Labor', description: 'Shop labor')
|
||||
item(9702, 'Labor old', active: false)
|
||||
admin
|
||||
get '/items/autocomplete', params: { q: 'lab' }
|
||||
assert_equal [9701], JSON.parse(response.body).map { |i| i['id'] }
|
||||
end
|
||||
|
||||
def test_accounts
|
||||
Account.create!(id: 9801, name: 'Sales', classification: 'Revenue')
|
||||
Account.create!(id: 9802, name: 'Rent', classification: 'Expense')
|
||||
admin
|
||||
get '/accounts'
|
||||
assert_response :success
|
||||
assert_select 'body', text: /Sales/
|
||||
patch '/accounts/set_default', params: { default_account_id: 9801 }
|
||||
assert_redirected_to '/accounts'
|
||||
assert Account.find(9801).default
|
||||
patch '/accounts/set_default', params: { default_account_id: 424_242 }
|
||||
assert_equal I18n.t(:error_default_account_not_selected), flash[:error]
|
||||
end
|
||||
|
||||
def test_admin_syncs
|
||||
admin
|
||||
Item.expects(:sync)
|
||||
Account.expects(:sync)
|
||||
Employee.expects(:sync)
|
||||
Payment.expects(:sync)
|
||||
%w[/items/sync /accounts/sync /employees/sync /qbo_payments/sync].each do |path|
|
||||
get path
|
||||
assert_response :redirect, path
|
||||
end
|
||||
end
|
||||
|
||||
# ---- business report ----
|
||||
|
||||
def test_report_needs_the_permission
|
||||
Role.find(1).remove_permission!(:view_business_reports)
|
||||
jsmith
|
||||
get '/qbo_reports'
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_report_remembers_projects
|
||||
Role.find(1).add_permission!(:view_business_reports)
|
||||
jsmith
|
||||
get '/qbo_reports', params: { period: 'last_month' }
|
||||
assert_response :success
|
||||
get '/qbo_reports', params: { project_ids: ['1', '999'], save_projects: '1' }
|
||||
assert_response :success
|
||||
assert_equal [1], User.find(2).pref[:qbo_report_project_ids]
|
||||
get '/qbo_reports'
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,182 @@
|
||||
#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__)
|
||||
|
||||
# Connecting to QuickBooks, manual syncs and billing, and Intuit's webhook
|
||||
class QboControllerTest < Redmine::IntegrationTest
|
||||
fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :issues, :trackers,
|
||||
:projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :workflows
|
||||
|
||||
SECRET = 'webhook-secret'.freeze
|
||||
|
||||
def setup
|
||||
Qbo.delete_all
|
||||
Customer.where(id: 9101).delete_all
|
||||
end
|
||||
|
||||
# Redmine keeps plugin settings with indifferent access (RedmineQbo.settings reads symbol keys)
|
||||
def settings(token)
|
||||
{ 'webhook_token' => token }.with_indifferent_access
|
||||
end
|
||||
|
||||
def signed(body, secret = SECRET)
|
||||
Base64.strict_encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret, body))
|
||||
end
|
||||
|
||||
def post_webhook(body, signature)
|
||||
post '/qbo/webhook', params: body, headers: { 'CONTENT_TYPE' => 'application/json', 'intuit-signature' => signature }
|
||||
end
|
||||
|
||||
# ---- webhook ----
|
||||
|
||||
def test_webhook_with_a_valid_signature_queues_the_job
|
||||
body = '{"eventNotifications":[]}'
|
||||
WebhookProcessJob.expects(:perform_later).with(body)
|
||||
with_settings plugin_redmine_qbo: settings(SECRET) do
|
||||
post_webhook(body, signed(body))
|
||||
end
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
def test_webhook_refuses_bad_or_missing_signatures
|
||||
body = '{"eventNotifications":[]}'
|
||||
WebhookProcessJob.expects(:perform_later).never
|
||||
with_settings plugin_redmine_qbo: settings(SECRET) do
|
||||
post_webhook(body, signed(body, 'wrong'))
|
||||
assert_response :unauthorized
|
||||
post_webhook(body, '')
|
||||
assert_response :unauthorized
|
||||
end
|
||||
with_settings plugin_redmine_qbo: settings('') do
|
||||
post_webhook(body, signed(body, ''))
|
||||
assert_response :unauthorized
|
||||
end
|
||||
end
|
||||
|
||||
def test_webhook_needs_no_login
|
||||
body = '{}'
|
||||
WebhookProcessJob.stubs(:perform_later)
|
||||
with_settings login_required: '1', plugin_redmine_qbo: settings(SECRET) do
|
||||
post_webhook(body, signed(body))
|
||||
end
|
||||
assert_response :ok
|
||||
end
|
||||
|
||||
# ---- admin pages ----
|
||||
|
||||
def test_connecting_and_syncing_are_admin_only
|
||||
log_user('jsmith', 'jsmith')
|
||||
get '/qbo/authenticate'
|
||||
assert_response :forbidden
|
||||
get '/qbo/sync'
|
||||
assert_response :forbidden
|
||||
get '/qbo/oauth_callback', params: { code: 'x', state: 'y', realmId: '1' }
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_authenticate_redirects_to_intuit_with_a_state
|
||||
log_user('admin', 'admin')
|
||||
QboOauthService.expects(:authorization_url).with { |args| args[:state].present? && args[:callback_url].end_with?('/qbo/oauth_callback') }
|
||||
.returns('https://appcenter.intuit.com/connect/oauth2?x=1')
|
||||
get '/qbo/authenticate'
|
||||
assert_redirected_to 'https://appcenter.intuit.com/connect/oauth2?x=1'
|
||||
end
|
||||
|
||||
def test_oauth_callback_checks_the_state
|
||||
log_user('admin', 'admin')
|
||||
QboOauthService.expects(:exchange!).never
|
||||
get '/qbo/oauth_callback', params: { code: 'abc', state: 'forged', realmId: '123' }
|
||||
assert_redirected_to '/settings/plugin/redmine_qbo'
|
||||
assert_equal I18n.t(:notice_qbo_oauth_state_mismatch), flash[:error]
|
||||
end
|
||||
|
||||
def test_oauth_callback_connects
|
||||
log_user('admin', 'admin')
|
||||
state = nil
|
||||
QboOauthService.stubs(:authorization_url).with { |args| state = args[:state] }.returns('https://intuit.example/')
|
||||
get '/qbo/authenticate'
|
||||
QboOauthService.expects(:exchange!).with(code: 'abc', callback_url: anything, realm_id: '123')
|
||||
get '/qbo/oauth_callback', params: { code: 'abc', state: state, realmId: '123' }
|
||||
assert_redirected_to '/qbo/sync'
|
||||
assert_equal I18n.t(:label_connected), flash[:notice]
|
||||
|
||||
# The state is used once
|
||||
get '/qbo/oauth_callback', params: { code: 'abc', state: state, realmId: '123' }
|
||||
assert_equal I18n.t(:notice_qbo_oauth_state_mismatch), flash[:error]
|
||||
end
|
||||
|
||||
def test_oauth_failure_is_reported
|
||||
log_user('admin', 'admin')
|
||||
state = nil
|
||||
QboOauthService.stubs(:authorization_url).with { |args| state = args[:state] }.returns('https://intuit.example/')
|
||||
get '/qbo/authenticate'
|
||||
QboOauthService.stubs(:exchange!).raises(OAuth2::Error.new(Struct.new(:parsed, :body, :status).new({}, 'bad', 400)))
|
||||
get '/qbo/oauth_callback', params: { code: 'abc', state: state, realmId: '123' }
|
||||
assert_redirected_to '/settings/plugin/redmine_qbo'
|
||||
assert_equal I18n.t(:label_error), flash[:error]
|
||||
end
|
||||
|
||||
def test_sync
|
||||
log_user('admin', 'admin')
|
||||
QboSyncDispatcher.expects(:sync!).with(full_sync: true)
|
||||
get '/qbo/sync', params: { full_sync: 'true' }
|
||||
assert_response :redirect
|
||||
assert_equal I18n.t(:label_syncing), flash[:notice]
|
||||
end
|
||||
|
||||
# ---- billing ----
|
||||
|
||||
def billable_issue
|
||||
customer = Customer.new(id: 9101, name: 'Jane Customer')
|
||||
customer.skip_qbo_push = true
|
||||
customer.save!
|
||||
User.find(3).update_column(:employee_id, 31)
|
||||
Issue.find(1).tap { |i| i.update_columns(customer_id: 9101, assigned_to_id: 3) }
|
||||
end
|
||||
|
||||
def test_bill_needs_the_permission
|
||||
billable_issue
|
||||
log_user('jsmith', 'jsmith')
|
||||
Role.find(1).remove_permission!(:bill_issues)
|
||||
post '/bill/1'
|
||||
assert_response :forbidden
|
||||
post '/bill/99999'
|
||||
assert_response :not_found
|
||||
end
|
||||
|
||||
def test_bill_queues_the_job
|
||||
issue = billable_issue
|
||||
Qbo.new(realm_id: '1').save!(validate: false)
|
||||
Role.find(1).add_permission!(:bill_issues)
|
||||
log_user('jsmith', 'jsmith')
|
||||
BillIssueTimeJob.expects(:perform_later).with(issue.id)
|
||||
post "/bill/#{issue.id}"
|
||||
assert_redirected_to "/issues/#{issue.id}"
|
||||
assert_match 'Jane Customer', flash[:notice]
|
||||
end
|
||||
|
||||
def test_bill_explains_what_is_missing
|
||||
Role.find(1).add_permission!(:bill_issues)
|
||||
log_user('jsmith', 'jsmith')
|
||||
BillIssueTimeJob.expects(:perform_later).never
|
||||
Issue.find(1).update_columns(customer_id: nil)
|
||||
post '/bill/1'
|
||||
assert_equal I18n.t(:notice_billing_error_no_customer), flash[:error]
|
||||
|
||||
billable_issue
|
||||
post '/bill/1'
|
||||
assert_equal I18n.t(:notice_billing_error_no_qbo), flash[:error]
|
||||
|
||||
User.find(3).update_column(:employee_id, nil)
|
||||
post '/bill/1'
|
||||
assert_equal I18n.t(:notice_billing_error_no_employee), flash[:error]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,250 @@
|
||||
#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 QboEstimatesControllerTest < Redmine::IntegrationTest
|
||||
include QboEditorTestHelper
|
||||
|
||||
def estimate_params(extra = {})
|
||||
{ customer_id: '9101', txn_date: '2026-11-20', email: 'jane@example.com', lines: line_params }.merge(extra)
|
||||
end
|
||||
|
||||
def test_editor_needs_write_estimates
|
||||
login_with(:view_customers)
|
||||
get '/qbo_estimates/new'
|
||||
assert_response :forbidden
|
||||
post '/qbo_estimates', params: { estimate: estimate_params }
|
||||
assert_response :forbidden
|
||||
assert_empty @fake.created
|
||||
end
|
||||
|
||||
def test_new_estimate
|
||||
login_with(:write_estimates)
|
||||
get '/qbo_estimates/new', params: { customer_id: 9101 }
|
||||
assert_response :success
|
||||
assert_select 'input[name=?][value=?]', 'estimate[customer_id]', '9101'
|
||||
end
|
||||
|
||||
def test_create
|
||||
login_with(:write_estimates)
|
||||
post '/qbo_estimates', params: { estimate: estimate_params }
|
||||
saved = @fake.created.first
|
||||
assert saved
|
||||
assert_redirected_to "/qbo_estimates/#{saved.id}/edit"
|
||||
assert_equal BigDecimal('240'), saved.line_items.first.amount
|
||||
end
|
||||
|
||||
def test_invalid_form_is_shown_again
|
||||
login_with(:write_estimates)
|
||||
post '/qbo_estimates', params: { estimate: estimate_params(customer_id: '', lines: {}) }
|
||||
assert_response :unprocessable_entity
|
||||
assert_select '#errorExplanation', text: /#{Regexp.escape(I18n.t(:error_estimate_needs_customer))}/
|
||||
assert_empty @fake.created
|
||||
end
|
||||
|
||||
def test_save_and_send_needs_send_estimates
|
||||
login_with(:write_estimates)
|
||||
post '/qbo_estimates', params: { estimate: estimate_params, save_and_send: '1' }
|
||||
assert_response :forbidden
|
||||
|
||||
login_with(:send_estimates)
|
||||
post '/qbo_estimates', params: { estimate: estimate_params, save_and_send: '1' }
|
||||
assert_equal [[@fake.created.first.id, 'jane@example.com']], @fake.sent
|
||||
end
|
||||
|
||||
def test_quickbooks_error_is_shown
|
||||
login_with(:write_estimates)
|
||||
@fake.define_singleton_method(:create) { |_doc| raise Quickbooks::IntuitRequestException, 'Business Validation Error' }
|
||||
post '/qbo_estimates', params: { estimate: estimate_params }
|
||||
assert_response :unprocessable_entity
|
||||
assert_select '#errorExplanation', text: /Business Validation Error/
|
||||
end
|
||||
|
||||
def test_edit_and_update
|
||||
login_with(:write_estimates)
|
||||
@fake.add(estimate_doc)
|
||||
get '/qbo_estimates/598/edit'
|
||||
assert_response :success
|
||||
assert_select 'fieldset.qbo-estimate-sheet[disabled]', 0
|
||||
|
||||
patch '/qbo_estimates/598', params: { estimate: estimate_params(sync_token: '3', lines: line_params(rate: '150')) }
|
||||
assert_redirected_to '/qbo_estimates/598/edit'
|
||||
assert_equal BigDecimal('300'), @fake.updated.first.line_items.first.amount
|
||||
end
|
||||
|
||||
def test_stale_update_is_a_conflict
|
||||
login_with(:write_estimates)
|
||||
@fake.add(estimate_doc)
|
||||
patch '/qbo_estimates/598', params: { estimate: estimate_params(sync_token: '1') }
|
||||
assert_response :unprocessable_entity
|
||||
assert_select '#errorExplanation', text: /#{Regexp.escape(I18n.t(:error_estimate_changed_in_quickbooks))}/
|
||||
end
|
||||
|
||||
def test_missing_estimate_redirects
|
||||
login_with(:write_estimates)
|
||||
get '/qbo_estimates/999/edit'
|
||||
assert_response :redirect
|
||||
assert_equal I18n.t(:notice_estimate_not_found), flash[:error]
|
||||
end
|
||||
|
||||
# ---- estimates linked to an issue ----
|
||||
|
||||
def test_linked_estimate_is_read_only_without_the_permission
|
||||
login_with(:write_estimates)
|
||||
@fake.add(estimate_doc)
|
||||
Issue.find(1).update_column(:estimate_id, 598)
|
||||
get '/qbo_estimates/598/edit'
|
||||
assert_select 'fieldset.qbo-estimate-sheet[disabled]'
|
||||
assert_select '#qbo-unlock-estimate', 0
|
||||
# Asking to unlock without the permission changes nothing
|
||||
get '/qbo_estimates/598/edit', params: { unlock: 1 }
|
||||
assert_select 'fieldset.qbo-estimate-sheet[disabled]'
|
||||
patch '/qbo_estimates/598', params: { estimate: estimate_params(sync_token: '3'), unlock: '1' }
|
||||
assert_response :unprocessable_entity
|
||||
assert_empty @fake.updated
|
||||
end
|
||||
|
||||
def test_unlock_a_linked_estimate
|
||||
login_with(:write_estimates, :edit_linked_estimates)
|
||||
@fake.add(estimate_doc)
|
||||
Issue.find(1).update_column(:estimate_id, 598)
|
||||
get '/qbo_estimates/598/edit'
|
||||
assert_select 'a#qbo-unlock-estimate[href=?][data-confirm=?]', '/qbo_estimates/598/edit?unlock=1',
|
||||
I18n.t(:text_unlock_estimate_confirmation)
|
||||
|
||||
get '/qbo_estimates/598/edit', params: { unlock: 1 }
|
||||
assert_select 'fieldset.qbo-estimate-sheet[disabled]', 0
|
||||
assert_select 'input[type=hidden][name=unlock][value="1"]'
|
||||
assert_select '.qbo-estimate-notice', text: /#{Regexp.escape(I18n.t(:text_estimate_unlocked))}/
|
||||
|
||||
patch '/qbo_estimates/598', params: { estimate: estimate_params(sync_token: '3', lines: line_params(rate: '150')), unlock: '1' }
|
||||
assert_redirected_to '/qbo_estimates/598/edit'
|
||||
assert_equal BigDecimal('300'), @fake.updated.first.line_items.first.amount
|
||||
assert_equal 598, Issue.find(1).estimate_id
|
||||
end
|
||||
|
||||
def test_billed_estimate_cannot_be_unlocked
|
||||
login_with(:write_estimates, :edit_linked_estimates)
|
||||
@fake.add(estimate_doc(status: 'Closed'))
|
||||
Issue.find(1).update_column(:estimate_id, 598)
|
||||
get '/qbo_estimates/598/edit', params: { unlock: 1 }
|
||||
assert_select 'fieldset.qbo-estimate-sheet[disabled]'
|
||||
patch '/qbo_estimates/598', params: { estimate: estimate_params(sync_token: '3'), unlock: '1' }
|
||||
assert_response :unprocessable_entity
|
||||
assert_empty @fake.updated
|
||||
end
|
||||
|
||||
# ---- duplicates, drafts, emails ----
|
||||
|
||||
def test_duplicate
|
||||
login_with(:write_estimates)
|
||||
@fake.add(estimate_doc)
|
||||
get '/qbo_estimates/new', params: { duplicate: 598 }
|
||||
assert_response :success
|
||||
assert_select '.flash.notice', text: /1068/
|
||||
end
|
||||
|
||||
def test_drafts
|
||||
login_with(:write_estimates)
|
||||
post '/qbo_estimates', params: { estimate: estimate_params(customer_id: '9101', lines: line_params), save_draft: '1' }
|
||||
draft = QboDraft.last
|
||||
assert_redirected_to "/qbo_estimates/new?draft_id=#{draft.id}"
|
||||
assert_equal [9101, 2], [draft.customer_id, draft.user_id]
|
||||
get "/qbo_estimates/new?draft_id=#{draft.id}"
|
||||
assert_response :success
|
||||
|
||||
# Saving to QuickBooks deletes the draft
|
||||
post '/qbo_estimates', params: { estimate: estimate_params, draft_id: draft.id }
|
||||
assert_not QboDraft.exists?(draft.id)
|
||||
|
||||
get '/qbo_estimates/new', params: { draft_id: draft.id }
|
||||
assert_redirected_to '/qbo_drafts'
|
||||
end
|
||||
|
||||
def test_send_email
|
||||
login_with(:write_estimates)
|
||||
@fake.add(estimate_doc)
|
||||
post '/qbo_estimates/598/send_email', params: { email: 'jane@example.com' }
|
||||
assert_response :forbidden
|
||||
login_with(:send_estimates)
|
||||
post '/qbo_estimates/598/send_email', params: { email: 'jane@example.com' }
|
||||
assert_redirected_to '/qbo_estimates/598/edit'
|
||||
assert_equal [%w[598 jane@example.com]], @fake.sent
|
||||
end
|
||||
|
||||
# ---- editor lookups ----
|
||||
|
||||
def test_customer_info
|
||||
login_with(:write_estimates)
|
||||
get '/qbo_estimates/customer_info', params: { customer_id: 9101 }
|
||||
assert_equal 'Jane Customer', JSON.parse(response.body)['name']
|
||||
get '/qbo_estimates/customer_info', params: { customer_id: 424_242 }
|
||||
assert_response :not_found
|
||||
get '/qbo_estimates/customer_info', params: { customer_id: 9101, document: 'invoice' }
|
||||
assert JSON.parse(response.body).key?('open_issues_html')
|
||||
end
|
||||
|
||||
def test_items_search
|
||||
Item.where(id: [9701, 9702]).delete_all
|
||||
[[9701, 'Labor', true], [9702, 'Lab coat', false]].each do |id, name, active|
|
||||
Item.new(id: id, name: name, description: '', unit_price: 120, active: active, type: 'Service')
|
||||
.tap { |i| i.skip_qbo_push = true }.save!
|
||||
end
|
||||
login_with(:write_invoices)
|
||||
get '/qbo_estimates/items', params: { q: 'lab' }
|
||||
assert_equal [9701], JSON.parse(response.body).map { |i| i['id'] }
|
||||
end
|
||||
|
||||
def test_refresh_company_is_admin_only
|
||||
login_with(:write_estimates)
|
||||
post '/qbo_estimates/refresh_company'
|
||||
assert_response :forbidden
|
||||
reset!
|
||||
log_user('admin', 'admin')
|
||||
RedmineQboBilling::Company.expects(:refresh!)
|
||||
post '/qbo_estimates/refresh_company'
|
||||
assert_response :redirect
|
||||
end
|
||||
|
||||
# ---- issues from an estimate ----
|
||||
|
||||
def test_issues_from_an_estimate
|
||||
Estimate.where(id: 598).delete_all
|
||||
Estimate.create!(id: 598, doc_number: '1068', customer_id: 9101)
|
||||
Estimate.any_instance.stubs(:line_items).returns([qbo_sales_line(2, 10, 'Labor', 2, 120, description: 'Install lift kit')])
|
||||
Setting.stubs(:plugin_redmine_qbo).returns('issue_default_project_id' => '1')
|
||||
@fake.add(estimate_doc)
|
||||
|
||||
login_with(:write_estimates)
|
||||
get '/qbo_estimates/598/issues_preview'
|
||||
assert_response :forbidden
|
||||
|
||||
login_with(:create_estimate_issues, :add_issues)
|
||||
get '/qbo_estimates/598/issues_preview'
|
||||
assert_response :success
|
||||
|
||||
post '/qbo_estimates/598/create_issues', params: { project_id: 1, lines: { '2' => { selected: '1', tracker_id: '1' } } }
|
||||
assert_redirected_to '/qbo_estimates/598/edit'
|
||||
issue = Issue.where(estimate_id: 598).last
|
||||
assert_equal 'Install Lift Kit', issue.subject
|
||||
# The work is going ahead: the estimate is accepted
|
||||
assert_equal 'Accepted', @fake.updated.last.txn_status
|
||||
end
|
||||
|
||||
def test_create_issues_with_nothing_selected
|
||||
Estimate.where(id: 598).delete_all
|
||||
Estimate.create!(id: 598, doc_number: '1068', customer_id: 9101)
|
||||
Estimate.any_instance.stubs(:line_items).returns([qbo_sales_line(2, 10, 'Labor', 2, 120, description: 'Install')])
|
||||
login_with(:create_estimate_issues, :add_issues)
|
||||
post '/qbo_estimates/598/create_issues', params: { project_id: 1, lines: {} }
|
||||
assert_response :unprocessable_entity
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,137 @@
|
||||
#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 QboInvoicesControllerTest < Redmine::IntegrationTest
|
||||
include QboEditorTestHelper
|
||||
|
||||
def invoice_params(extra = {})
|
||||
{ customer_id: '9101', txn_date: '2026-11-20', email: 'jane@example.com', lines: line_params }.merge(extra)
|
||||
end
|
||||
|
||||
def test_editor_needs_write_invoices
|
||||
login_with(:write_estimates)
|
||||
get '/qbo_invoices/new'
|
||||
assert_response :forbidden
|
||||
post '/qbo_invoices', params: { estimate: invoice_params }
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
def test_new_and_create
|
||||
login_with(:write_invoices)
|
||||
get '/qbo_invoices/new', params: { customer_id: 9101 }
|
||||
assert_response :success
|
||||
post '/qbo_invoices', params: { estimate: invoice_params }
|
||||
saved = @fake.created.first
|
||||
assert_redirected_to "/qbo_invoices/#{saved.id}/edit"
|
||||
assert_kind_of Quickbooks::Model::Invoice, saved
|
||||
end
|
||||
|
||||
def test_new_from_an_estimate
|
||||
login_with(:write_invoices)
|
||||
@fake.add(estimate_doc)
|
||||
get '/qbo_invoices/new', params: { estimate_id: 598 }
|
||||
assert_response :success
|
||||
assert_select 'input[name=?][value=?]', 'estimate[linked_estimate_ids][]', '598'
|
||||
|
||||
post '/qbo_invoices', params: { estimate: invoice_params(linked_estimate_ids: ['598']) }
|
||||
assert_equal 'Closed', @fake.records['598'].txn_status
|
||||
end
|
||||
|
||||
def test_new_from_a_billed_estimate_explains_why
|
||||
login_with(:write_invoices)
|
||||
@fake.add(estimate_doc(status: 'Closed'))
|
||||
get '/qbo_invoices/new', params: { estimate_id: 598 }
|
||||
assert_not_equal 500, response.status
|
||||
assert_equal I18n.t(:error_estimate_closed_billed, number: '1068', status: 'Closed'), flash[:error]
|
||||
end
|
||||
|
||||
def test_save_and_send_needs_send_invoices
|
||||
login_with(:write_invoices)
|
||||
post '/qbo_invoices', params: { estimate: invoice_params, save_and_send: '1' }
|
||||
assert_response :forbidden
|
||||
login_with(:send_invoices)
|
||||
post '/qbo_invoices', params: { estimate: invoice_params, save_and_send: '1' }
|
||||
assert_equal 1, @fake.sent.size
|
||||
end
|
||||
|
||||
def test_edit_and_update
|
||||
login_with(:write_invoices)
|
||||
@fake.add(invoice_doc)
|
||||
get '/qbo_invoices/601/edit'
|
||||
assert_response :success
|
||||
patch '/qbo_invoices/601', params: { estimate: invoice_params(sync_token: '3', lines: line_params(rate: '150')) }
|
||||
assert_redirected_to '/qbo_invoices/601/edit'
|
||||
assert_equal BigDecimal('300'), @fake.updated.first.line_items.first.amount
|
||||
end
|
||||
|
||||
def test_paid_invoice_is_locked
|
||||
login_with(:write_invoices)
|
||||
@fake.add(invoice_doc(balance: '0'))
|
||||
get '/qbo_invoices/601/edit'
|
||||
assert_select 'fieldset.qbo-estimate-sheet[disabled]'
|
||||
assert_select '#qbo-unlock-invoice', 0
|
||||
patch '/qbo_invoices/601', params: { estimate: invoice_params(sync_token: '3'), unlock: '1' }
|
||||
assert_response :unprocessable_entity
|
||||
assert_empty @fake.updated
|
||||
end
|
||||
|
||||
def test_unlock_a_paid_invoice
|
||||
login_with(:write_invoices, :edit_paid_invoices)
|
||||
@fake.add(invoice_doc(balance: '0'))
|
||||
get '/qbo_invoices/601/edit'
|
||||
assert_select 'a#qbo-unlock-invoice[href=?]', '/qbo_invoices/601/edit?unlock=1'
|
||||
get '/qbo_invoices/601/edit', params: { unlock: 1 }
|
||||
assert_select 'fieldset.qbo-estimate-sheet[disabled]', 0
|
||||
patch '/qbo_invoices/601', params: { estimate: invoice_params(sync_token: '3', balance: '0'), unlock: '1' }
|
||||
assert_redirected_to '/qbo_invoices/601/edit'
|
||||
end
|
||||
|
||||
def test_partly_paid_invoice_needs_its_own_permission
|
||||
login_with(:write_invoices, :edit_paid_invoices)
|
||||
@fake.add(invoice_doc(balance: '100'))
|
||||
get '/qbo_invoices/601/edit'
|
||||
assert_select '#qbo-unlock-invoice', 0
|
||||
login_with(:edit_partially_paid_invoices)
|
||||
get '/qbo_invoices/601/edit'
|
||||
assert_select '#qbo-unlock-invoice'
|
||||
end
|
||||
|
||||
def test_import_options
|
||||
login_with(:write_invoices)
|
||||
@fake.query_results = { 'FROM Estimate' => [estimate_doc] }
|
||||
get '/qbo_invoices/import_options', params: { customer_id: 9101 }
|
||||
assert_response :success
|
||||
data = JSON.parse(response.body)
|
||||
assert_equal ['598'], data['estimates'].map { |e| e['id'] }
|
||||
get '/qbo_invoices/import_options', params: { customer_id: 424_242 }
|
||||
assert_response :not_found
|
||||
end
|
||||
|
||||
def test_send_email
|
||||
login_with(:write_invoices, :send_invoices)
|
||||
@fake.add(invoice_doc)
|
||||
post '/qbo_invoices/601/send_email', params: { email: 'jane@example.com' }
|
||||
assert_redirected_to '/qbo_invoices/601/edit'
|
||||
assert_equal [%w[601 jane@example.com]], @fake.sent
|
||||
end
|
||||
|
||||
def test_drafts
|
||||
login_with(:write_invoices)
|
||||
post '/qbo_invoices', params: { estimate: invoice_params(sales_person: 'Ricky'), save_draft: '1' }
|
||||
draft = QboDraft.last
|
||||
assert draft.invoice?
|
||||
assert_equal 'Ricky', draft.fields['sales_person']
|
||||
assert_redirected_to "/qbo_invoices/new?draft_id=#{draft.id}"
|
||||
get "/qbo_invoices/new?draft_id=#{draft.id}"
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,129 @@
|
||||
#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 QboPaymentsAndDraftsControllerTest < Redmine::IntegrationTest
|
||||
include QboEditorTestHelper
|
||||
|
||||
def setup
|
||||
Invoice.where(id: 601).delete_all
|
||||
@fake.query_results = { 'FROM Invoice' => [invoice_doc(balance: '240')], 'PaymentMethod' => [
|
||||
Quickbooks::Model::PaymentMethod.new(id: 2, name: 'Check', active: true)
|
||||
] }
|
||||
end
|
||||
|
||||
def payment_params(extra = {})
|
||||
{ customer_id: '9101', txn_date: '2026-11-20', payment_method_id: '2', amount: '100' }.merge(extra)
|
||||
end
|
||||
|
||||
def saved_payment
|
||||
doc = Quickbooks::Model::Payment.new(customer_ref: qbo_ref(9101, 'Jane Customer'), txn_date: Date.new(2026, 11, 20),
|
||||
total: BigDecimal('100'), payment_method_ref: qbo_ref(2, 'Check'))
|
||||
doc.id = '801'
|
||||
doc.sync_token = 1
|
||||
doc.line_items = [Quickbooks::Model::Line.new.tap { |l| l.amount = BigDecimal('100'); l.invoice_id = '601' }]
|
||||
@fake.add(doc)
|
||||
end
|
||||
|
||||
def test_receiving_needs_receive_payments
|
||||
login_with(:view_payments)
|
||||
get '/qbo_payments/new'
|
||||
assert_response :forbidden
|
||||
post '/qbo_payments', params: { payment: payment_params }
|
||||
assert_response :forbidden
|
||||
get '/qbo_payments', params: { customer_id: 9101 }
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_receive_a_payment
|
||||
login_with(:receive_payments)
|
||||
get '/qbo_payments/new', params: { customer_id: 9101 }
|
||||
assert_response :success
|
||||
post '/qbo_payments', params: { payment: payment_params }
|
||||
payment = @fake.created.first
|
||||
assert_redirected_to "/qbo_payments/#{payment.id}"
|
||||
assert_equal BigDecimal('100'), payment.line_items.first.amount
|
||||
end
|
||||
|
||||
def test_invalid_payment
|
||||
login_with(:receive_payments)
|
||||
post '/qbo_payments', params: { payment: payment_params(amount: '0') }
|
||||
assert_response :unprocessable_entity
|
||||
assert_empty @fake.created
|
||||
end
|
||||
|
||||
def test_show_edit_update_pdf
|
||||
login_with(:receive_payments)
|
||||
saved_payment
|
||||
get '/qbo_payments/801'
|
||||
assert_response :success
|
||||
get '/qbo_payments/801/edit'
|
||||
assert_response :success
|
||||
patch '/qbo_payments/801', params: { payment: payment_params(sync_token: '1', invoice_ids: ['601'], memo: 'Edited') }
|
||||
assert_redirected_to '/qbo_payments/801'
|
||||
assert_equal 'Edited', @fake.updated.first.private_note
|
||||
@fake.pdf_body = '%PDF-1.4'
|
||||
get '/qbo_payments/801/pdf'
|
||||
assert_equal 'application/pdf', response.media_type
|
||||
end
|
||||
|
||||
def test_stale_payment_update
|
||||
login_with(:receive_payments)
|
||||
saved_payment
|
||||
patch '/qbo_payments/801', params: { payment: payment_params(sync_token: '0') }
|
||||
assert_response :unprocessable_entity
|
||||
end
|
||||
|
||||
def test_missing_payment
|
||||
login_with(:view_payments)
|
||||
get '/qbo_payments/999'
|
||||
assert_response :redirect
|
||||
assert_equal I18n.t(:notice_payment_not_found), flash[:error]
|
||||
end
|
||||
|
||||
def test_open_invoices
|
||||
login_with(:receive_payments)
|
||||
get '/qbo_payments/open_invoices', params: { customer_id: 9101 }
|
||||
assert_equal ['601'], JSON.parse(response.body).map { |i| i['id'] }
|
||||
get '/qbo_payments/open_invoices', params: { customer_id: 424_242 }
|
||||
assert_response :not_found
|
||||
end
|
||||
|
||||
def test_payment_sync_is_admin_only
|
||||
login_with(:receive_payments)
|
||||
get '/qbo_payments/sync'
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
# ---- drafts ----
|
||||
|
||||
def test_drafts_by_permission
|
||||
estimate = QboDraft.create!(document: 'estimate', fields: { 'customer_name' => 'Est Draft' })
|
||||
invoice = QboDraft.create!(document: 'invoice', fields: { 'customer_name' => 'Inv Draft' })
|
||||
login_with(:view_customers)
|
||||
get '/qbo_drafts'
|
||||
assert_response :forbidden
|
||||
|
||||
login_with(:write_estimates)
|
||||
get '/qbo_drafts'
|
||||
assert_response :success
|
||||
assert_select 'body', text: /Est Draft/
|
||||
assert_select 'body', text: /Inv Draft/, count: 0
|
||||
|
||||
# Not allowed to delete an invoice draft
|
||||
delete "/qbo_drafts/#{invoice.id}"
|
||||
assert QboDraft.exists?(invoice.id)
|
||||
assert_equal I18n.t(:error_qbo_draft_not_found), flash[:error]
|
||||
|
||||
delete "/qbo_drafts/#{estimate.id}"
|
||||
assert_not QboDraft.exists?(estimate.id)
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user