Project

General

Profile

Defect #44721 ยป 0001-Reject-a-tracker-that-is-not-allowed-instead-of-usin.patch

Jan from Planio www.plan.io, 2026-12-06 13:10

View differences:

app/models/issue.rb
alias :base_reload :reload
def reload(*)
@workflow_rule_by_attribute = nil
@invalid_tracker_id = nil
@assignable_versions = nil
@relations = nil
@spent_hours = nil
......
if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id')
if allowed_target_trackers(user).where(:id => t.to_i).exists?
self.tracker_id = t
@invalid_tracker_id = nil
elsif t.present? && t.to_s != tracker_id.to_s
# Remember the rejected tracker so that the validation can report it
# instead of silently using another tracker
@invalid_tracker_id = t
end
end
if project && tracker.nil?
......
end
# Checks that the issue can not be added/moved to a disabled tracker
if project && (tracker_id_changed? || project_id_changed?)
if project && @invalid_tracker_id.present?
errors.add :tracker_id, :inclusion
elsif project && (tracker_id_changed? || project_id_changed?)
if tracker && !project.trackers.include?(tracker)
errors.add :tracker_id, :inclusion
end
app/models/mail_handler.rb
issue.tracker_id ||= project.trackers.first.try(:id)
end
end
issue.safe_attributes = attributes
issue.safe_attributes = without_unallowed_tracker(issue, attributes)
issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
issue.subject = cleaned_up_subject
if issue.subject.blank?
......
# If the received email was a reply to a private note, make the added note private
issue.private_notes = true
end
issue.safe_attributes = issue_attributes_from_keywords(issue)
issue.safe_attributes = without_unallowed_tracker(issue, issue_attributes_from_keywords(issue))
issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
journal.notes = cleaned_up_text_body
......
target
end
# Removes a tracker that the sender cannot use from the given attributes.
# The sender of an email cannot correct the tracker and submit again like
# the user of the form or of the API. The issue therefore keeps its tracker
# or gets the default one, and the email is not rejected.
def without_unallowed_tracker(issue, attrs)
tracker_id = attrs['tracker_id']
if tracker_id && tracker_id != issue.tracker_id &&
!issue.allowed_target_trackers(user).exists?(tracker_id)
attrs = attrs.except('tracker_id')
end
attrs
end
# Returns a Hash of issue attributes extracted from keywords in the email body
def issue_attributes_from_keywords(issue)
attrs = {
test/functional/issues_controller_test.rb
assert_equal '', issue.custom_field_value(cf2)
end
def test_create_should_ignore_unallowed_trackers
def test_create_should_reject_unallowed_trackers
role = Role.find(1)
role.set_permission_trackers :add_issues, [3]
role.save!
@request.session[:user_id] = 2
issue = new_record(Issue) do
assert_no_difference 'Issue.count' do
post(
:create,
:params => {
......
}
}
)
assert_response :found
end
assert_equal 3, issue.tracker_id
assert_response :success
assert_select_error /Tracker is not included in the list/
end
def test_post_create_with_watchers
test/integration/api_test/issues_test.rb
assert json['errors'].include?("Subject cannot be blank")
end
test "POST /issues.json with a tracker_id that is disabled in the project should respond with 422" do
project = Project.find(1)
project.trackers.delete(Tracker.find(2))
assert_no_difference('Issue.count') do
post(
'/issues.json',
:params => {:issue => {:project_id => 1, :subject => 'API', :tracker_id => 2}},
:headers => credentials('jsmith'))
end
assert_response :unprocessable_content
json = ActiveSupport::JSON.decode(response.body)
assert_include 'Tracker is not included in the list', json['errors']
end
test "POST /issues.json with a tracker_id that the role cannot add should respond with 422" do
role = Role.find(1)
role.set_permission_trackers :add_issues, [3]
role.save!
assert_no_difference('Issue.count') do
post(
'/issues.json',
:params => {:issue => {:project_id => 1, :subject => 'API', :tracker_id => 1}},
:headers => credentials('jsmith'))
end
assert_response :unprocessable_content
json = ActiveSupport::JSON.decode(response.body)
assert_include 'Tracker is not included in the list', json['errors']
end
test "PUT /issues/:id.json with a tracker_id that is disabled in the project should respond with 422" do
project = Project.find(1)
project.trackers.delete(Tracker.find(2))
put(
'/issues/1.json',
:params => {:issue => {:tracker_id => 2}},
:headers => credentials('jsmith'))
assert_response :unprocessable_content
json = ActiveSupport::JSON.decode(response.body)
assert_include 'Tracker is not included in the list', json['errors']
assert_equal 1, Issue.find(1).tracker_id
end
test "POST /issues.json with invalid project_id should respond with 422" do
post(
'/issues.json',
......
assert_equal 'A new note', Journal.last.notes
end
test "PUT /issues/:id.json authenticated via OAuth should not change the tracker without add_issues scope" do
test "PUT /issues/:id.json authenticated via OAuth should reject a tracker change without add_issues scope" do
put(
'/issues/1.json',
:params => {:issue => {:tracker_id => 2}},
:headers => oauth_credentials('jsmith', 'view_issues edit_issues'))
assert_response :no_content
assert_response :unprocessable_content
json = ActiveSupport::JSON.decode(response.body)
assert_include 'Tracker is not included in the list', json['errors']
assert_equal 1, Issue.find(1).tracker_id
end
test/unit/issue_test.rb
assert_not_equal [], issue.errors[:tracker_id]
end
def test_safe_attributes_with_unallowed_tracker_should_add_an_error
project = Project.find(1)
project.trackers.delete(Tracker.find(2))
issue = Issue.new(:project => project, :author_id => 2, :subject => 'Unallowed tracker')
issue.send :safe_attributes=, {'tracker_id' => '2'}, User.find(2)
assert !issue.save
assert_equal ['is not included in the list'], issue.errors[:tracker_id]
end
def test_safe_attributes_without_tracker_should_use_the_first_allowed_tracker
project = Project.find(1)
project.trackers.delete(Tracker.find(1))
issue = Issue.new(:project => project, :author_id => 2, :subject => 'No tracker')
issue.send :safe_attributes=, {'priority_id' => '5'}, User.find(2)
assert_equal 2, issue.tracker_id
assert issue.save
end
def test_safe_attributes_with_allowed_tracker_after_unallowed_tracker_should_save
project = Project.find(1)
project.trackers.delete(Tracker.find(2))
issue = Issue.new(:project => project, :author_id => 2, :subject => 'Second try')
issue.send :safe_attributes=, {'tracker_id' => '2'}, User.find(2)
issue.send :safe_attributes=, {'tracker_id' => '3'}, User.find(2)
assert issue.save
assert_equal 3, issue.tracker_id
end
def test_category_based_assignment
issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
:status_id => 1, :priority => IssuePriority.first,
test/unit/mail_handler_test.rb
assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
end
def test_add_issue_with_unallowed_tracker_should_use_the_default_tracker
# This email contains: 'Tracker: Feature Request'
User.find_by_login('jsmith').roles_for_project(Project.find(2)).each do |role|
role.set_permission_trackers :add_issues, [3]
role.save!
end
issue =
submit_email(
'ticket_with_attributes.eml',
:allow_override => ['tracker']
)
assert issue.is_a?(Issue)
assert !issue.new_record?
assert_equal 'Support request', issue.reload.tracker.to_s
end
def test_add_issue_with_spaces_between_attribute_and_separator
issue =
submit_email(
......
assert !journal.notes.match(/^Start Date:/i)
end
def test_update_issue_with_unallowed_tracker_should_keep_the_tracker
User.find_by_login('jsmith').roles_for_project(Project.find(1)).each do |role|
role.set_permission_trackers :add_issues, [3]
role.save!
end
journal =
submit_email(
'ticket_reply_with_status.eml',
:allow_override => ['tracker', 'status']
) do |raw|
raw.sub!('Status: Resolved', "Status: Resolved\nTracker: Bug")
end
assert journal.is_a?(Journal)
issue = Issue.find(2)
assert_equal 'Feature request', issue.tracker.name
assert_equal IssueStatus.find_by_name("Resolved"), issue.status
end
def test_update_issue_with_private_keyword_no
Role.find(1).add_permission! :set_issues_private
Issue.find(2).update_columns(:is_private => true)
    (1-1/1)