mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-20 18:40:37 -05:00
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:
1 parent
17f24f9641
commit
b829593bc2
14 files changed
+174
-99
No files matched your search
@@ -100,8 +100,7 @@ class CustomersControllerTest < Redmine::IntegrationTest
|
||||
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
|
||||
# Customers are created from the page only; JSON requests are refused
|
||||
def test_json_create_is_refused
|
||||
Role.find(1).add_permission!(:view_customers)
|
||||
Role.find(1).add_permission!(:add_customers)
|
||||
@@ -109,10 +108,25 @@ class CustomersControllerTest < Redmine::IntegrationTest
|
||||
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_includes [401, 403, 406], response.status
|
||||
assert_not Customer.exists?(name: 'Json Person')
|
||||
end
|
||||
|
||||
def test_invalid_customer_shows_the_form_again
|
||||
grant(:view_customers, :add_customers)
|
||||
post '/customers', params: { customer: { name: '' } }
|
||||
assert_response :unprocessable_entity
|
||||
assert_select '#errorExplanation'
|
||||
end
|
||||
|
||||
def test_quickbooks_error_on_create_is_shown
|
||||
grant(:view_customers, :add_customers)
|
||||
@fake.define_singleton_method(:create) { |_doc| raise Quickbooks::IntuitRequestException, 'Duplicate Name Exists' }
|
||||
post '/customers', params: { customer: { name: 'Jane Customer' } }
|
||||
assert_redirected_to '/customers/new'
|
||||
assert_match 'Duplicate Name Exists', flash[:error]
|
||||
end
|
||||
|
||||
def test_update_needs_edit_customers
|
||||
grant(:view_customers)
|
||||
Role.find(1).remove_permission!(:edit_customers)
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -190,8 +190,8 @@ class RedminePagesTest < Redmine::IntegrationTest
|
||||
# ---- issue PDF ----
|
||||
|
||||
def test_issue_pdf_includes_the_estimate
|
||||
# A plain one-page PDF, like QuickBooks' (Redmine's own PDFs have optional content, which the merge refuses)
|
||||
estimate_pdf = CombinePDF.new.tap { |p| p << CombinePDF.create_page }.to_pdf
|
||||
# Made by Redmine's own PDF writer, which adds optional content: merged like the issue's PDF
|
||||
estimate_pdf = Redmine::Export::PDF::ITCPDF.new('en').tap { |p| p.add_page; p.write(5, 'ESTIMATE') }.output
|
||||
PdfService.any_instance.expects(:fetch_pdf).with(doc_ids: [598]).returns([estimate_pdf, '1068'])
|
||||
login
|
||||
get '/issues/1.pdf'
|
||||
|
||||
Reference in new issue
Block a user