Skip to content

Commit e2f84ae

Browse files
Added character limit for tag and exam template names to schema (#8272)
1 parent ec21b50 commit e2f84ae

8 files changed

Lines changed: 88 additions & 14 deletions

File tree

‎.active_record_doctor.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ActiveRecordDoctor.configure do
22
# Remove each override when the detector's existing findings are fixed.
33
detector :incorrect_dependent_option, enabled: false
4-
detector :incorrect_length_validation, enabled: false
4+
detector :incorrect_length_validation, enabled: true
55
detector :mismatched_foreign_key_type, enabled: false
66
detector :missing_foreign_keys, enabled: false
77
detector :missing_non_null_constraint, enabled: false

‎Changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- Added additional details to the account settings and renamed the Settings dropdown to Account (#8207)
3232

3333
### 🐛 Bug fixes
34+
- Fixed validations not matching schema length limit by adding a schema limit for exam template names and tag names (#8272)
3435
- Fixed denied JSON requests returning an HTML page in place of a JSON 403 response (#8265)
3536
- Fixed edited grades in the marks spreadsheet reverting to their previous value after being saved (#8260)
3637
- Fixed `extensions_controller.rb` to only create extensions for groupings within the current course (#8258)

‎app/models/exam_template.rb‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# crop_x :decimal(, )
1212
# crop_y :decimal(, )
1313
# filename :string not null
14-
# name :string not null
14+
# name :string(20) not null
1515
# num_pages :integer not null
1616
# created_at :datetime not null
1717
# updated_at :datetime not null
@@ -29,6 +29,9 @@
2929
require 'fileutils'
3030

3131
class ExamTemplate < ApplicationRecord
32+
# Constants
33+
NAME_MAX_LENGTH = 20
34+
3235
before_validation :set_defaults_for_name, :set_formats_for_name_and_filename
3336
before_save :undo_mark_for_destruction
3437
after_create_commit :create_base_path
@@ -37,7 +40,7 @@ class ExamTemplate < ApplicationRecord
3740
validates :filename, :num_pages, :name, presence: true
3841
validates :name,
3942
uniqueness: { scope: :assignment },
40-
length: { maximum: 20 }
43+
length: { maximum: NAME_MAX_LENGTH }
4144
validates :num_pages, numericality: { greater_than_or_equal_to: 0,
4245
only_integer: true }
4346
validates :automatic_parsing, inclusion: { in: [true, false] }

‎app/models/tag.rb‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
# id :integer not null, primary key
77
# description :string
8-
# name :string not null
8+
# name :string(30) not null
99
# created_at :datetime
1010
# updated_at :datetime
1111
# assessment_id :bigint
@@ -23,18 +23,22 @@
2323
#
2424
# rubocop:enable Layout/LineLength, Lint/RedundantCopDisableDirective
2525
class Tag < ApplicationRecord
26+
# Constants
27+
NUM_CSV_FIELDS = 3
28+
NAME_MAX_LENGTH = 30
29+
2630
has_and_belongs_to_many :groupings
2731
belongs_to :role
2832
belongs_to :assessment, optional: true
2933

3034
has_one :course, through: :role
3135
validate :courses_should_match
32-
validates :name, presence: true, length: { maximum: 30 }, uniqueness: { scope: :assessment_id, conditions: -> {
33-
where.not(assessment_id: nil)
34-
} }
35-
36-
# Constants
37-
NUM_CSV_FIELDS = 3
36+
validates :name,
37+
presence: true,
38+
length: { maximum: NAME_MAX_LENGTH },
39+
uniqueness: { scope: :assessment_id, conditions: -> {
40+
where.not(assessment_id: nil)
41+
} }
3842

3943
def ==(other)
4044
description == other.description &&
@@ -47,7 +51,7 @@ def self.from_csv(data, course, assignment_id)
4751
result = MarkusCsv.parse(data) do |row|
4852
raise CsvInvalidLineError if row.length < NUM_CSV_FIELDS
4953
name, description, role_id = row[0], row[1], instructors[row[2]]
50-
if name.nil? || name.strip.blank? || role_id.nil?
54+
if name.nil? || name.strip.blank? || role_id.nil? || name.length > NAME_MAX_LENGTH
5155
raise CsvInvalidLineError
5256
end
5357

@@ -74,7 +78,7 @@ def self.from_yml(data, course, assignment_id, allow_ta_upload: false)
7478
author = ta_author.id unless ta_author.nil? || !ta_author.grader_permission.manage_assessments
7579
end
7680
name, description, role_id = row[:name], row[:description], author
77-
if name.nil? || name.strip.blank? || role_id.nil?
81+
if name.nil? || name.strip.blank? || role_id.nil? || name.strip.length > NAME_MAX_LENGTH
7882
raise ArgumentError, I18n.t('tags.invalid_tag_data', item: row)
7983
end
8084

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class ChangeNameLimitInExamTemplatesAndTags < ActiveRecord::Migration[8.1]
2+
def up
3+
change_column :exam_templates, :name, :string, limit: 20, null: false
4+
change_column :tags, :name, :string, limit: 30, null: false
5+
end
6+
7+
def down
8+
change_column :exam_templates, :name, :string, limit: nil, null: false
9+
change_column :tags, :name, :string, limit: nil, null: false
10+
end
11+
end

‎db/structure.sql‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ CREATE TABLE public.exam_templates (
648648
num_pages integer NOT NULL,
649649
created_at timestamp without time zone NOT NULL,
650650
updated_at timestamp without time zone NOT NULL,
651-
name character varying NOT NULL,
651+
name character varying(20) NOT NULL,
652652
cover_fields character varying DEFAULT ''::character varying NOT NULL,
653653
automatic_parsing boolean DEFAULT false NOT NULL,
654654
crop_x numeric,
@@ -2061,7 +2061,7 @@ ALTER SEQUENCE public.submissions_id_seq OWNED BY public.submissions.id;
20612061

20622062
CREATE TABLE public.tags (
20632063
id integer NOT NULL,
2064-
name character varying NOT NULL,
2064+
name character varying(30) NOT NULL,
20652065
description character varying,
20662066
assessment_id bigint,
20672067
role_id bigint NOT NULL,
@@ -4824,6 +4824,7 @@ ALTER TABLE ONLY public.submission_files
48244824
SET search_path TO "$user", public;
48254825

48264826
INSERT INTO "schema_migrations" (version) VALUES
4827+
('20261110055739'),
48274828
('20261106010918'),
48284829
('20261030000000'),
48294830
('20260926233000'),

‎spec/models/exam_template_spec.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
describe ExamTemplate do
2+
subject { exam_template }
3+
24
let(:exam_template) { create(:exam_template_midterm) }
35
let(:group) { create(:group, course: exam_template.course) }
46
let(:grouping) { create(:grouping, group: group, assignment: exam_template.assignment) }
57

8+
it { is_expected.to validate_length_of(:name).is_at_most(ExamTemplate::NAME_MAX_LENGTH) }
9+
610
describe '#missing_pages' do
711
it 'returns all page numbers when no pages have been scanned' do
812
expect(exam_template.missing_pages(group)).to eq (1..6).to_a

‎spec/models/tag_spec.rb‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,59 @@
11
describe Tag do
22
subject { create(:tag, assessment: create(:assignment)) }
33

4+
let(:course) { create(:course) }
5+
let(:instructor) { create(:instructor, course: course) }
6+
let(:assignment) { create(:assignment, course: course) }
7+
let(:max_name) { 'a' * Tag::NAME_MAX_LENGTH }
8+
let(:too_long_name) { 'a' * (Tag::NAME_MAX_LENGTH + 1) }
9+
410
it { is_expected.to have_one(:course) }
511

612
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:assessment_id) }
713

14+
it { is_expected.to validate_length_of(:name).is_at_most(Tag::NAME_MAX_LENGTH) }
15+
816
it_behaves_like 'course associations'
17+
18+
describe '.from_csv' do
19+
let(:user_name) { instructor.user.user_name }
20+
21+
it 'accepts a tag whose name is exactly the maximum length' do
22+
data = CSV.generate_line([max_name, 'description', user_name])
23+
24+
result = nil
25+
expect { result = Tag.from_csv(data, course, assignment.id) }.to change { Tag.count }.by(1)
26+
expect(result[:invalid_lines]).to be_blank
27+
end
28+
29+
it 'rejects tags with names longer than the maximum length' do
30+
data = CSV.generate_line([too_long_name, 'description', user_name])
31+
32+
result = nil
33+
expect { result = Tag.from_csv(data, course, assignment.id) }.not_to(change { Tag.count })
34+
expect(result[:invalid_lines]).not_to be_empty
35+
end
36+
end
37+
38+
describe '.from_yml' do
39+
let(:user_name) { instructor.user.user_name }
40+
41+
def yml_row(name)
42+
{ name: name, description: 'description', user: user_name }
43+
end
44+
45+
it 'accepts a tag whose name is exactly the maximum length' do
46+
result = nil
47+
expect { result = Tag.from_yml([yml_row(max_name)], course, assignment.id) }
48+
.to change { Tag.count }.by(1)
49+
expect(result).not_to be_a(StandardError)
50+
end
51+
52+
it 'rejects tags with names longer than the maximum length' do
53+
result = nil
54+
expect { result = Tag.from_yml([yml_row(too_long_name)], course, assignment.id) }
55+
.not_to(change { Tag.count })
56+
expect(result).to be_a(ArgumentError)
57+
end
58+
end
959
end

0 commit comments

Comments
 (0)