check for ifc file conformity

pull/7876/head
ulferts 5 years ago
parent 690dc1a36e
commit d9cef43d82
No known key found for this signature in database
GPG Key ID: A205708DE1284017
  1. 15
      modules/ifc_models/app/contracts/ifc_models/base_contract.rb
  2. 3
      modules/ifc_models/config/locales/en.yml
  3. 28
      modules/ifc_models/spec/contracts/ifc_models/shared_contract_examples.rb

@ -44,6 +44,7 @@ module IFCModels
user_allowed_to_manage
user_is_uploader
ifc_attachment_existent
ifc_attachment_is_ifc
uploader_is_ifc_attachment_author
super
@ -65,6 +66,20 @@ module IFCModels
errors.add :base, :ifc_attachment_missing unless model.ifc_attachment
end
def ifc_attachment_is_ifc
return unless model.ifc_attachment&.new_record?
firstline = File.open(model.ifc_attachment.file.file.path, &:readline)
begin
unless firstline.match?(/^ISO-10303-21;/)
errors.add :base, :invalid_ifc_file
end
rescue ArgumentError
errors.add :base, :invalid_ifc_file
end
end
def uploader_is_ifc_attachment_author
errors.add :uploader_id, :invalid if model.ifc_attachment && model.uploader != model.ifc_attachment.author
end

@ -41,4 +41,5 @@ en:
ifc_models/ifc_model:
attributes:
base:
ifc_attachment_missing: "No ifc file attached"
ifc_attachment_missing: "No ifc file attached."
invalid_ifc_file: "The provided file is not a valid IFC file."

@ -106,6 +106,34 @@ shared_examples_for 'ifc model contract' do
end
end
context 'if the new ifc file is no valid ifc file' do
let(:ifc_file) { FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true }
let(:ifc_attachment) do
User.execute_as current_user do
ifc_model.attach_files('first' => { 'file' => ifc_file, 'description' => 'ifc' })
ifc_model.attachments.last
end
end
it 'is invalid' do
expect_valid(false, base: %i(invalid_ifc_file))
end
end
context 'if the new ifc file is a valid ifc file' do
let(:ifc_file) do
FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true, content: "ISO-10303-21;"
end
let(:ifc_attachment) do
User.execute_as current_user do
ifc_model.attach_files('first' => { 'file' => ifc_file, 'description' => 'ifc' })
ifc_model.attachments.last
end
end
it_behaves_like 'is valid'
end
context 'if user is not allowed to manage ifc models' do
let(:permissions) { [] }

Loading…
Cancel
Save