Fix and test ActiveStorage::IntegrityError on upload
This commit is contained in:
parent
33a5d74c1e
commit
fed300bfe1
2 changed files with 37 additions and 2 deletions
|
@ -5,11 +5,14 @@ module Mutations
|
||||||
def validate_blob(blob_id)
|
def validate_blob(blob_id)
|
||||||
begin
|
begin
|
||||||
blob = ActiveStorage::Blob.find_signed(blob_id)
|
blob = ActiveStorage::Blob.find_signed(blob_id)
|
||||||
blob.identify
|
# open downloads the file and checks its hash
|
||||||
|
blob.open { |f| }
|
||||||
true
|
true
|
||||||
rescue ActiveStorage::FileNotFoundError
|
rescue ActiveStorage::FileNotFoundError
|
||||||
return false, { errors: ['Le fichier n’a pas été correctement téléversé sur le serveur de stockage'] }
|
return false, { errors: ['Le fichier n’a pas été correctement téléversé sur le serveur de stockage'] }
|
||||||
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
||||||
|
return false, { errors: ['L’identifiant du fichier téléversé est invalide'] }
|
||||||
|
rescue ActiveStorage::IntegrityError
|
||||||
return false, { errors: ['Le hash du fichier téléversé est invalide'] }
|
return false, { errors: ['Le hash du fichier téléversé est invalide'] }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -627,7 +627,7 @@ describe API::V2::GraphqlController do
|
||||||
it "should fail" do
|
it "should fail" do
|
||||||
expect(gql_errors).to eq(nil)
|
expect(gql_errors).to eq(nil)
|
||||||
expect(gql_data).to eq(dossierEnvoyerMessage: {
|
expect(gql_data).to eq(dossierEnvoyerMessage: {
|
||||||
errors: [{ message: "Le hash du fichier téléversé est invalide" }],
|
errors: [{ message: "L’identifiant du fichier téléversé est invalide" }],
|
||||||
message: nil
|
message: nil
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
@ -898,6 +898,29 @@ describe API::V2::GraphqlController do
|
||||||
}"
|
}"
|
||||||
end
|
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
|
it "should initiate a direct upload" do
|
||||||
expect(gql_errors).to eq(nil)
|
expect(gql_errors).to eq(nil)
|
||||||
|
|
||||||
|
@ -907,6 +930,15 @@ describe API::V2::GraphqlController do
|
||||||
expect(data[:blobId]).not_to be_nil
|
expect(data[:blobId]).not_to be_nil
|
||||||
expect(data[:signedBlobId]).not_to be_nil
|
expect(data[:signedBlobId]).not_to be_nil
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe 'dossierChangerGroupeInstructeur' do
|
describe 'dossierChangerGroupeInstructeur' do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue