Skip to content

Commit

Permalink
Merge pull request #1511 from xmera-circle/bug-fix/ActiveRecord-Value…
Browse files Browse the repository at this point in the history
…TooLong

Adds further validations
  • Loading branch information
picman authored Feb 22, 2024
2 parents d4f4d4a + 63257b5 commit b0ae0f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/dmsf_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DmsfFile < ApplicationRecord
scope :deleted, -> { where(deleted: STATUS_DELETED) }

validates :name, dmsf_file_name: true
validates :name, length: { maximum: 255 }
validates :name,
uniqueness: {
scope: %i[dmsf_folder_id project_id deleted],
Expand Down
3 changes: 3 additions & 0 deletions app/models/dmsf_file_revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ class DmsfFileRevision < ApplicationRecord
)

validates :title, presence: true
validates :title, length: { maximum: 255 }
validates :major_version, presence: true
validates :name, dmsf_file_name: true
validates :name, length: { maximum: 255 }
validates :disk_filename, length: { maximum: 255 }
validates :description, length: { maximum: 1.kilobyte }
validates :size, dmsf_max_file_size: true

Expand Down
25 changes: 25 additions & 0 deletions test/unit/dmsf_file_revision_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ def setup
@wf1 = DmsfWorkflow.find 1
end

def test_file_title_length_validation
file = DmsfFileRevision.new(title: Array.new(256).map { 'a' }.join,
name: 'Test Revision',
major_version: 1)
assert file.invalid?
assert_equal ['Title is too long (maximum is 255 characters)'], file.errors.full_messages
end

def test_file_name_length_validation
file = DmsfFileRevision.new(name: Array.new(256).map { 'a' }.join,
title: 'Test Revision',
major_version: 1)
assert file.invalid?
assert_equal ['Name is too long (maximum is 255 characters)'], file.errors.full_messages
end

def test_file_disk_filename_length_validation
file = DmsfFileRevision.new(disk_filename: Array.new(256).map { 'a' }.join,
title: 'Test Revision',
name: 'Test Revision',
major_version: 1)
assert file.invalid?
assert_equal ['Disk filename is too long (maximum is 255 characters)'], file.errors.full_messages
end

def test_delete_restore
@revision5.delete commit: false
assert @revision5.deleted?, "File revision #{@revision5.name} hasn't been deleted"
Expand Down
6 changes: 6 additions & 0 deletions test/unit/dmsf_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def setup
@wf2 = DmsfWorkflow.find 2
end

def test_file_name_length_validation
file = DmsfFile.new(name: Array.new(256).map { 'a' }.join)
assert file.invalid?
assert_equal ['Name is too long (maximum is 255 characters)'], file.errors.full_messages
end

def test_project_file_count_differs_from_project_visibility_count
assert_not_same @project1.dmsf_files.all.size, @project1.dmsf_files.visible.all.size
end
Expand Down

0 comments on commit b0ae0f5

Please sign in to comment.