Skip to content

Commit 1d9bbd5

Browse files
authored
OTWO-7640 Ignore-files ui silently discards input exceeding 1000 chars (#1896)
* OTWO-7640 Ignore-files ui silently discards input exceeding 1000 chars * Rubocop and code review fix * Testcase coverage * solved allowed files update issue
1 parent aacc818 commit 1d9bbd5

5 files changed

Lines changed: 81 additions & 4 deletions

File tree

app/controllers/enlistments_controller.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ def edit_allowed_files
4747
end
4848

4949
def update
50-
@enlistment.update(enlistment_params)
51-
@enlistment.project.schedule_delayed_analysis(3.minutes)
52-
redirect_to project_enlistments_path(@project), flash: { success: t('.success') }
50+
if @enlistment.update(enlistment_params)
51+
@enlistment.project.schedule_delayed_analysis(3.minutes)
52+
redirect_to project_enlistments_path(@project), flash: { success: t('.success') }
53+
else
54+
template = params[:enlistment].key?(:allowed_fyles) ? :edit_allowed_files : :edit
55+
render template, status: :unprocessable_entity
56+
end
5357
end
5458

5559
def destroy

app/models/enlistment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Enlistment < ApplicationRecord
1919
acts_as_editable editable_attributes: [:ignore]
2020
acts_as_protected parent: :project
2121

22-
validates :ignore, length: { maximum: 1000 }, allow_nil: true
22+
validates :ignore, length: { maximum: 1000, message: :too_long_ignore }, allow_nil: true
2323
validates :allowed_fyles, length: { maximum: 1000 }, allow_nil: true
2424
validate :validate_code_location, if: -> { @nested_code_location }
2525

config/locales/enlistments.en.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
en:
2+
activerecord:
3+
errors:
4+
models:
5+
enlistment:
6+
attributes:
7+
ignore:
8+
too_long_ignore: 'cannot exceed 1000 characters'
29
enlistments:
310
index:
411
title: 'The %{name} Open Source Project on Open Hub : Code Locations Page'

test/controllers/commits_controller_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,38 @@ class CommitsControllerTest < ActionController::TestCase
198198
end
199199
end
200200

201+
describe 'individual_named_commits' do
202+
it 'should set commits to empty array when commit_contributor is nil' do
203+
Project.any_instance.stubs(:commit_contributors).returns(stub(find_by: nil))
204+
get :index, params: { project_id: @project.id, contributor_id: 0 }
205+
_(assigns(:commits)).must_equal []
206+
end
207+
end
208+
209+
describe 'events and event_details with no analysis' do
210+
it 'should return not_found xml when project has no analysis for events' do
211+
Project.any_instance.stubs(:best_analysis).returns(NilAnalysis.new)
212+
get :events, params: { project_id: @project.id, id: @commit1.id, contributor_id: @name1.id }, format: :xml
213+
assert_response :not_found
214+
end
215+
216+
it 'should render 404 when contributor fact not found for event_details' do
217+
ContributorFact.stubs(:find_by).returns(nil)
218+
Airbrake.stubs(:notify)
219+
get :event_details, params: { contributor_id: 0, id: @commit1.id,
220+
project_id: @project.id, time: "commit_#{@commit1.time.to_i}" }
221+
assert_response :not_found
222+
end
223+
end
224+
225+
describe 'redirect_to_message_if_oversized_project' do
226+
it 'should redirect to root when project is oversized' do
227+
CommitsController.any_instance.stubs(:oversized_project?).returns(true)
228+
get :index, params: { project_id: @project.id }
229+
assert_redirected_to root_path
230+
end
231+
end
232+
201233
private
202234

203235
def create_commits

test/models/enlistment_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,40 @@ class EnlistmentTest < ActiveSupport::TestCase
8989
end
9090
_(KnowledgeBaseStatus.find_by(project_id: enlistment.project_id).in_sync).must_equal false
9191
end
92+
93+
describe 'ignore field validation' do
94+
it 'should allow nil value' do
95+
enlistment = create(:enlistment, ignore: nil)
96+
_(enlistment.valid?).must_equal true
97+
end
98+
99+
it 'should allow empty string' do
100+
enlistment = create(:enlistment, ignore: '')
101+
_(enlistment.valid?).must_equal true
102+
end
103+
104+
it 'should allow strings under 1000 characters' do
105+
enlistment = create(:enlistment, ignore: 'a' * 500)
106+
_(enlistment.valid?).must_equal true
107+
end
108+
109+
it 'should allow strings exactly 1000 characters' do
110+
enlistment = create(:enlistment, ignore: 'a' * 1000)
111+
_(enlistment.valid?).must_equal true
112+
end
113+
114+
it 'should reject strings exceeding 1000 characters with appropriate error messages' do
115+
enlistment = build(:enlistment, ignore: 'a' * 1001)
116+
_(enlistment.valid?).must_equal false
117+
_(enlistment.errors[:ignore]).must_include 'cannot exceed 1000 characters'
118+
_(enlistment.errors.full_messages).must_include 'Ignore cannot exceed 1000 characters'
119+
end
120+
121+
it 'should reject very long strings' do
122+
enlistment = build(:enlistment, ignore: 'a' * 5000)
123+
_(enlistment.valid?).must_equal false
124+
end
125+
end
92126
end
93127

94128
# TODO: Replace this once we remove code_locations table dependency from enlistments_controller.

0 commit comments

Comments
 (0)