Fix and test ActiveStorage::IntegrityError on upload

This commit is contained in:
Paul Chavard 2020-12-10 21:55:45 +01:00 committed by simon lehericey
parent 33a5d74c1e
commit fed300bfe1
2 changed files with 37 additions and 2 deletions

View file

@ -5,11 +5,14 @@ module Mutations
def validate_blob(blob_id)
begin
blob = ActiveStorage::Blob.find_signed(blob_id)
blob.identify
# open downloads the file and checks its hash
blob.open { |f| }
true
rescue ActiveStorage::FileNotFoundError
return false, { errors: ['Le fichier na pas été correctement téléversé sur le serveur de stockage'] }
rescue ActiveSupport::MessageVerifier::InvalidSignature
return false, { errors: ['Lidentifiant du fichier téléversé est invalide'] }
rescue ActiveStorage::IntegrityError
return false, { errors: ['Le hash du fichier téléversé est invalide'] }
end
end

View file

@ -627,7 +627,7 @@ describe API::V2::GraphqlController do
it "should fail" do
expect(gql_errors).to eq(nil)
expect(gql_data).to eq(dossierEnvoyerMessage: {
errors: [{ message: "Le hash du fichier téléversé est invalide" }],
errors: [{ message: "Lidentifiant du fichier téléversé est invalide" }],
message: nil
})
end
@ -898,6 +898,29 @@ describe API::V2::GraphqlController do
}"
end
let(:attach_query) do
"mutation {
dossierEnvoyerMessage(input: {
dossierId: \"#{dossier.to_typed_id}\",
instructeurId: \"#{instructeur.to_typed_id}\",
body: \"Hello world\",
attachment: \"#{direct_upload_blob_id}\"
}) {
message {
body
}
errors {
message
}
}
}"
end
let(:attach_query_exec) { post :execute, params: { query: attach_query } }
let(:attach_query_body) { JSON.parse(attach_query_exec.body, symbolize_names: true) }
let(:attach_query_data) { attach_query_body[:data] }
let(:direct_upload_data) { gql_data[:createDirectUpload][:directUpload] }
let(:direct_upload_blob_id) { direct_upload_data[:signedBlobId] }
it "should initiate a direct upload" do
expect(gql_errors).to eq(nil)
@ -907,6 +930,15 @@ describe API::V2::GraphqlController do
expect(data[:blobId]).not_to be_nil
expect(data[:signedBlobId]).not_to be_nil
end
it "wrong hash error" do
blob = ActiveStorage::Blob.find direct_upload_data[:blobId]
blob.service.upload blob.key, StringIO.new('toto')
expect(attach_query_data).to eq(dossierEnvoyerMessage: {
errors: [{ message: "Le hash du fichier téléversé est invalide" }],
message: nil
})
end
end
describe 'dossierChangerGroupeInstructeur' do