Skip to content

Commit dbcf10b

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

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
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

0 commit comments

Comments
 (0)