Limit estimate and invoice PDFs by permission; clean up

Any logged-in user could open any estimate or invoice PDF by id. Now:
- estimates: View customers or Write estimates, or an estimate attached
  to an issue the user can see (the issue page links to it); users
  without the permission don't trigger a sync of uncached estimates
- invoices: View customers, Write invoices, View/Receive payments or
  Issue refunds, or invoices attached to issues the user can see
- share-link customers are unchanged (their own issue's documents)
- the sidebar's estimate search only shows to those who can use it

The issue PDF merges an estimate PDF with optional content, as it
already did for the issue's own PDF, instead of failing.

Removed dead code: the JSON branches of CustomersController#create
(Redmine refuses JSON there) and the scaffold views for
qbo#authenticate, #oauth_callback and #webhook, which those actions
never render. An invalid customer now shows the form with a 422.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-20 17:36:20 -05:00
1 parent 17f24f9641
commit b829593bc2
14 files changed
+174 -99

No files matched your search

+84 -14
View File
@@ -10,7 +10,8 @@
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
# Estimate and invoice PDFs: users with a billing permission open any of them; other logged-in users only the
# documents attached to issues they can see; share-link customers only their own issue's documents
class DocumentPdfsTest < Redmine::IntegrationTest
include QboFakeServiceHelper
fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :issues, :trackers,
@@ -32,7 +33,15 @@ class DocumentPdfsTest < Redmine::IntegrationTest
@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'
@fake.pdf_body = CombinePDF.new.tap { |pdf| pdf << CombinePDF.create_page }.to_pdf
Role.find(1).remove_permission!(:view_customers, :write_estimates, :write_invoices, :view_payments,
:receive_payments, :issue_refunds)
end
def login(*permissions)
permissions.each { |p| Role.find(1).add_permission!(p) }
reset!
log_user('jsmith', 'jsmith')
end
def open_share_link
@@ -40,24 +49,85 @@ class DocumentPdfsTest < Redmine::IntegrationTest
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
def test_billing_users_open_any_estimate
%i[view_customers write_estimates].each do |permission|
Role.find(1).remove_permission!(:view_customers, :write_estimates)
login(permission)
get '/estimates/599'
assert_response :success, permission
assert_equal 'application/pdf', response.media_type
assert_match 'estimate 1069.pdf', response.headers['Content-Disposition']
get '/estimates/doc', params: { search: '1069' }
assert_response :success, permission
end
end
def test_estimate_missing_locally_is_synced_first
log_user('jsmith', 'jsmith')
def test_other_users_open_only_estimates_on_issues_they_can_see
login
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_estimate_on_a_hidden_issue_is_refused
Issue.find(1).update_column(:estimate_id, nil)
Issue.find(4).update_column(:estimate_id, 598)
Project.find(2).update_column(:is_public, false)
Member.where(user_id: 2, project_id: 2).destroy_all
login
get '/estimates/598'
assert_response :forbidden
end
def test_only_billing_users_sync_missing_estimates
login
Estimate.expects(:sync_by_id).never
get '/estimates/777'
assert_response :forbidden
login(:view_customers)
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_billing_users_open_any_invoice
%i[view_customers write_invoices view_payments receive_payments issue_refunds].each do |permission|
Role.find(1).remove_permission!(:view_customers, :write_invoices, :view_payments, :receive_payments, :issue_refunds)
login(permission)
get '/invoices/602'
assert_response :success, permission
get '/invoices/601', params: { invoice_ids: %w[601 602] }
assert_response :success, permission
end
end
def test_other_users_open_only_invoices_on_issues_they_can_see
login
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_estimate_search_shows_only_to_billing_users
login
get '/projects/ecookbook/issues'
assert_select '#estimate-search-form', 0
login(:write_estimates)
get '/projects/ecookbook/issues'
assert_select '#estimate-search-form'
end
def test_anonymous_visitors_must_log_in
get '/estimates/598'
assert_redirected_to %r{/login}
@@ -96,14 +166,14 @@ class DocumentPdfsTest < Redmine::IntegrationTest
end
def test_invoice_pdf_failure_redirects
log_user('jsmith', 'jsmith')
login(:write_invoices)
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')
login
get '/estimates/sync'
assert_response :forbidden
get '/invoices/sync'