Skip to content

Commit dbf7181

Browse files
committed
File: add validation for path uniqueness
1 parent bfab1ec commit dbf7181

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

app/models/code_ocean/file.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class File < ApplicationRecord
6060
validates :weight, absence: true, unless: :teacher_defined_assessment?
6161
validates :file, presence: true if :context.is_a?(Submission)
6262
validates :context_type, inclusion: {in: ALLOWED_CONTEXT_TYPES}
63+
validates :path, uniqueness: {scope: %I[name file_type context_id context_type role]}
64+
6365
# xml_id_path must be unique within the scope of an exercise.
6466
# Initially, it may match the record’s id (set while exporting),
6567
# but it can later diverge as long as uniqueness is preserved.

spec/models/code_ocean/file_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,18 @@
137137
end
138138
end
139139
end
140+
141+
context 'when a file with same attributes (path, name file_type context_id context_type role) already exists' do
142+
before do
143+
create(:file, name: 'static', context: exercise)
144+
file.validate
145+
end
146+
147+
let(:exercise) { create(:dummy) }
148+
let(:file) { build(:file, name: 'static', context: exercise) }
149+
150+
it 'has an error for path' do
151+
expect(file.errors[:path]).to be_present
152+
end
153+
end
140154
end

spec/services/proforma_service/convert_task_to_exercise_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
ProformaXML::TaskFile.new(
347347
id: 'ms-file-2',
348348
content: 'content',
349-
filename: 'filename.txt',
349+
filename: 'filename2.txt',
350350
used_by_grader: 'used_by_grader',
351351
visible: 'yes',
352352
usage_by_lms: 'display',
@@ -456,7 +456,7 @@
456456
ProformaXML::TaskFile.new(
457457
id: 'test_file_id2',
458458
content: 'testfile-content',
459-
filename: 'testfile.txt',
459+
filename: 'testfile2.txt',
460460
used_by_grader: 'yes',
461461
visible: 'no',
462462
usage_by_lms: 'display',

0 commit comments

Comments
 (0)