From 3eb01141d2283bd3d136c6d6bf423e2982e0774d Mon Sep 17 00:00:00 2001 From: Marie Leuliette Date: Wed, 13 Nov 2024 13:47:53 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Am=C3=A9liore=20la=20vites?= =?UTF-8?q?se=20d'=C3=A9x=C3=A9cution=20du=20script=20d'attribution=20des?= =?UTF-8?q?=20assets=20aux=20questions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/google_drive_storage.rb | 28 ++++++++++---------- lib/tasks/questions.rake | 21 +++++++-------- spec/lib/google_drive_storage_spec.rb | 38 +++++++++++++-------------- spec/tasks/questions_spec.rb | 26 ++++++++---------- 4 files changed, 53 insertions(+), 60 deletions(-) diff --git a/lib/google_drive_storage.rb b/lib/google_drive_storage.rb index 17cea056f..43713d9a5 100644 --- a/lib/google_drive_storage.rb +++ b/lib/google_drive_storage.rb @@ -2,6 +2,9 @@ class GoogleDriveStorage class Error < StandardError; end + + attr_reader :session + GOOGLE_DRIVE_ACCOUNT_CONFIG = { 'type' => 'service_account', 'project_id' => ENV.fetch('GOOGLE_PROJECT_ID', nil), @@ -18,7 +21,11 @@ class Error < StandardError; end 'universe_domain' => 'googleapis.com' }.freeze - def self.recupere_session + def initialize + @session = recupere_session + end + + def recupere_session Tempfile.create(['google_drive_service_account', '.json']) do |file| file.write(GOOGLE_DRIVE_ACCOUNT_CONFIG.to_json) file.rewind @@ -26,21 +33,14 @@ def self.recupere_session end end - def self.existe_dossier?(dossier_id) - session = recupere_session - begin - @dossier = session.collection_by_id(dossier_id) - rescue Google::Apis::ClientError => e - raise Error, "Le dossier avec l'id '#{dossier_id}' n'est pas accessible : #{e.message}" - end + def existe_dossier?(dossier_id) + true if @session.collection_by_id(dossier_id) + rescue Google::Apis::ClientError => e + raise Error, "Le dossier avec l'id '#{dossier_id}' n'est pas accessible : #{e.message}" end - def self.recupere_fichier(dossier_id, nom_fichier) - unless existe_dossier?(dossier_id) - raise "Le dossier avec l'id '#{dossier_id}' n'est pas accessible" - end - - file = @dossier.files.find { |f| f.name == nom_fichier } + def recupere_fichier(dossier_id, nom_fichier) + file = @session.file_by_title(nom_fichier) raise Error, "Fichier '#{nom_fichier}' n'existe pas dans le dossier '#{dossier_id}'" unless file file diff --git a/lib/tasks/questions.rake b/lib/tasks/questions.rake index 53da7aaa4..328fe9066 100644 --- a/lib/tasks/questions.rake +++ b/lib/tasks/questions.rake @@ -46,13 +46,14 @@ namespace :questions do dossier_id = 'DOSSIER_ID' logger = RakeLogger.logger unless ENV.key?(dossier_id) - logger.error "La variable d'environnement #{dossier_id} est manquante" + logger.error "La variable d'environnement DOSSIER_ID est manquante" logger.info 'Usage : rake questions:attache_assets DOSSIER_ID=' next end + @drive = GoogleDriveStorage.new begin - GoogleDriveStorage.existe_dossier?(ENV.fetch(dossier_id, nil)) + @drive.existe_dossier?(ENV.fetch(dossier_id, nil)) rescue GoogleDriveStorage::Error => e logger.error e.message next @@ -60,22 +61,20 @@ namespace :questions do NOM_TECHNIQUES_QCM.each do |question_nom_technique, nom_illustration| question = Question.find_by(nom_technique: question_nom_technique) - if question.blank? + if question + recupere_fichier(dossier_id, question, "#{nom_illustration}.png") + recupere_fichier(dossier_id, question, "#{question.nom_technique}.mp3", + question.transcription_intitule) + attach_audio_choix(dossier_id, question) + else puts "Pas de question trouvée pour le nom_technique '#{question_nom_technique}'" end - next if question.blank? - - recupere_fichier(ENV.fetch(dossier_id), question, "#{nom_illustration}.png") - recupere_fichier(ENV.fetch(dossier_id), question, "#{question.nom_technique}.mp3", - question.transcription_intitule) - attach_audio_choix(ENV.fetch(dossier_id), question) end - puts 'Création des assets finie.' end end def recupere_fichier(dossier_id, question, fichier_path, model = nil) - file = GoogleDriveStorage.recupere_fichier(dossier_id, fichier_path) + file = @drive.recupere_fichier(dossier_id, fichier_path) if file file_content = file.download_to_string attach_file_to(model || question, file_content, fichier_path, question.nom_technique) diff --git a/spec/lib/google_drive_storage_spec.rb b/spec/lib/google_drive_storage_spec.rb index 87993e590..b2f8e6f68 100644 --- a/spec/lib/google_drive_storage_spec.rb +++ b/spec/lib/google_drive_storage_spec.rb @@ -5,59 +5,57 @@ describe GoogleDriveStorage do let(:mock_session) { instance_double(GoogleDrive::Session) } - let(:mock_folder) { instance_double(GoogleDrive::Collection) } let(:mock_file) { instance_double(GoogleDrive::File, name: 'programme_tele.png') } + let(:mock_folder) { instance_double(GoogleDrive::Collection) } let(:folder_id) { 'fake-folder-id' } let(:file_name) { 'programme_tele.png' } + let(:storage) { described_class.new } before do - allow(described_class).to receive(:recupere_session).and_return(mock_session) + allow(storage).to receive(:recupere_session).and_return(mock_session) end - describe '.recupere_session' do + describe '#recupere_session' do it "s'authentifie avec succès et retourne une session valide" do - expect(described_class.recupere_session).to eq(mock_session) + expect(storage.recupere_session).to eq(mock_session) end end - describe '.recupere_fichier' do - before do - allow(mock_session).to receive(:collection_by_id).with(folder_id).and_return(mock_folder) - end - - context 'quand le fichier existe dans le dossier' do + describe '#recupere_fichier' do + context 'quand le fichier existe' do before do - allow(mock_folder).to receive(:files).and_return([mock_file]) + allow(storage.session).to receive(:file_by_title).with(file_name).and_return(mock_file) end it 'retourne le fichier' do - file = described_class.recupere_fichier(folder_id, file_name) + file = storage.recupere_fichier(folder_id, file_name) expect(file).not_to be_nil expect(file.name).to eq(file_name) end end - context "quand le fichier n'existe pas dans le dossier" do + context "quand le fichier n'existe pas" do before do - allow(mock_folder).to receive(:files).and_return([]) + allow(storage.session).to receive(:file_by_title).with(file_name).and_return(nil) end it 'retourne une erreur' do - expect { described_class.recupere_fichier(folder_id, file_name) }.to raise_error( + expect { storage.recupere_fichier(folder_id, file_name) }.to raise_error( + GoogleDriveStorage::Error, "Fichier '#{file_name}' n'existe pas dans le dossier '#{folder_id}'" ) end end end - describe '.existe_dossier?' do + describe '#existe_dossier?' do context 'quand le dossier existe' do before do - allow(mock_session).to receive(:collection_by_id).with(folder_id).and_return(mock_folder) + allow(storage.session).to receive(:collection_by_id).with(folder_id).and_return(mock_folder) end it 'retourne true' do - expect(described_class).to be_existe_dossier(folder_id) + expect(storage.existe_dossier?(folder_id)).to be true end end @@ -65,11 +63,11 @@ let!(:error) { Google::Apis::ClientError.new("notFound: File not found: #{folder_id}") } before do - allow(mock_session).to receive(:collection_by_id).with(folder_id).and_raise(error) + allow(storage.session).to receive(:collection_by_id).with(folder_id).and_raise(error) end it 'renvoie une erreur' do - expect { described_class.existe_dossier?(folder_id) }.to raise_error( + expect { storage.existe_dossier?(folder_id) }.to raise_error( GoogleDriveStorage::Error, "Le dossier avec l'id '#{folder_id}' n'est pas accessible : " \ "notFound: File not found: #{folder_id}" diff --git a/spec/tasks/questions_spec.rb b/spec/tasks/questions_spec.rb index aeaa7e306..42bee602f 100644 --- a/spec/tasks/questions_spec.rb +++ b/spec/tasks/questions_spec.rb @@ -14,13 +14,13 @@ context 'appellé avec une dossier id inconnu' do let(:error_message) do - "Le dossier avec l'id 'inconnue' n'est pas accessible: " \ + "Le dossier avec l'id 'inconnue' n'est pas accessible : " \ 'notFound: File not found: inconnue.' end before do ENV['DOSSIER_ID'] = 'inconnue' - allow(GoogleDriveStorage).to receive(:existe_dossier?).with('inconnue').and_raise( + allow(GoogleDriveStorage.new).to receive(:existe_dossier?).with('inconnue').and_raise( GoogleDriveStorage::Error, error_message ) subject.reenable @@ -60,20 +60,16 @@ before do ENV['DOSSIER_ID'] = 'fake-dossier-id' - allow(GoogleDriveStorage).to receive(:existe_dossier?).with('fake-dossier-id') - .and_return(true) + drive = instance_double(GoogleDriveStorage) + allow(GoogleDriveStorage).to receive(:new).and_return(drive) + allow(drive).to receive(:existe_dossier?).with('fake-dossier-id').and_return(true) + + file_names = ['programme_tele.png', 'lodi_1.mp3', 'LOdi_couverture.mp3', 'LOdi_drap.mp3'] + file_names.each_with_index do |file_name, index| + allow(drive).to receive(:recupere_fichier).with('fake-dossier-id', + file_name).and_return(fake_files[index]) + end - allow(GoogleDriveStorage).to receive(:recupere_fichier).with('fake-dossier-id', - 'programme_tele.png') - .and_return(fake_files[0]) - allow(GoogleDriveStorage).to receive(:recupere_fichier).with('fake-dossier-id', 'lodi_1.mp3') - .and_return(fake_files[1]) - allow(GoogleDriveStorage).to receive(:recupere_fichier).with('fake-dossier-id', - 'LOdi_couverture.mp3') - .and_return(fake_files[2]) - allow(GoogleDriveStorage).to receive(:recupere_fichier).with('fake-dossier-id', - 'LOdi_drap.mp3') - .and_return(fake_files[3]) subject.reenable subject.invoke end